PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 3.7.1
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v3.7.1
3.7.3 3.7.2 3.7.1 3.7.0 3.6.0 3.6.1 3.5.2 trunk 2.0 2.1 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 All 74 releases
stockpack / assets / js / stockpack-images.js

stockpack-images.js in StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more 3.7.1, at assets/js/stockpack-images.js

250 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import StockpackImage from './stockpack-image';
2 import StockpackDialog from './stockpack-dialog';
3
4 const StockpackImages = wp.media.View.extend( {
5 tagName: 'ul',
6 className: 'attachments',
7
8 attributes: {
9 tabIndex: -1,
10 },
11 dialog: null,
12
13 initialize() {
14 this.el.id = _.uniqueId( '__stockpack-images-view-' );
15
16 _.defaults( this.options, {
17 sortable: false,
18 refreshSensitivity: wp.media.isTouchDevice ? 300 : 200,
19 refreshThreshold: 1.5,
20 resize: true,
21 idealColumnWidth:jQuery( window ).width() < 640 ? 135 : 150,
22 provider: '',
23 } );
24
25 this._viewsByCid = {};
26 this.$window = jQuery( window );
27 this.resizeEvent = 'resize.media-modal-columns';
28 this.fixScroll = _.debounce( this.maybeFixScroll.bind( this ), 700 );
29
30 this.collection.on( 'add', function( attachment ) {
31 this.views.add( this.createAttachmentView( attachment ), {
32 at: this.collection.indexOf( attachment ),
33 } );
34 this.fixScroll();
35 }, this );
36
37 this.collection.on( 'remove', function( attachment ) {
38 const view = this._viewsByCid[ attachment.cid ];
39 delete this._viewsByCid[ attachment.cid ];
40
41 if ( view ) {
42 view.remove();
43 }
44 }, this );
45
46 this.collection.on( 'reset', this.render, this );
47 this.collection.on( 'query_error', _.debounce( this.queryError, 150 ), this );
48 // Throttle the scroll handler and bind this.
49 this.scroll = _.chain( this.scroll ).bind( this ).throttle( this.options.refreshSensitivity ).value();
50
51 this.options.scrollElement = this.options.scrollElement || this.el;
52 jQuery( this.options.scrollElement ).on( 'scroll', this.scroll );
53
54 _.bindAll( this, 'setColumns' );
55
56 if ( this.options.resize ) {
57 this.on( 'ready', this.bindEvents );
58 this.controller.on( 'open', this.setColumns );
59
60 // Call this.setColumns() after this view has been rendered in the DOM so
61 // attachments get proper width applied.
62 _.defer( this.setColumns, this );
63 }
64
65 this.prepareDebounced = _.debounce( this.prepare.bind( this ), 700 );
66 },
67
68 /**
69 * @param {StockpackImage} attachment
70 * @return {wp.media.View} description
71 */
72 createAttachmentView( attachment ) {
73 const view = new StockpackImage( {
74 model: attachment,
75 collection: this.collection,
76 selection: this.options.selection,
77 controller: this.controller,
78 provider: this.options.provider,
79 } );
80
81 return this._viewsByCid[ attachment.cid ] = view;
82 },
83
84 bindEvents() {
85 this.$window.off( this.resizeEvent ).on( this.resizeEvent, _.debounce( this.setColumns, 50 ) );
86 },
87
88 prepare() {
89 // Create all of the Attachment views, and replace
90 // the list in a single DOM operation.
91 if ( this.collection.length ) {
92 const view = this.collection.map( this.createAttachmentView, this );
93 this.views.set( view );
94 // If there are no elements, clear the views and load some.
95 } else {
96 this.views.unset();
97 this.collection.more();
98 }
99 },
100
101 maybeFixScroll() {
102 if ( this.el.scrollHeight === this.el.clientHeight ) {
103 this.scroll();
104 }
105 },
106
107 scroll() {
108 let el = this.options.scrollElement,
109 scrollTop = el.scrollTop;
110
111 // The scroll event occurs on the document, but the element
112 // that should be checked is the document body.
113 if ( el === document ) {
114 el = document.body;
115 scrollTop = jQuery( document ).scrollTop();
116 }
117
118 if ( ! jQuery( el ).is( ':visible' ) || ! this.collection.hasMore() ) {
119 return;
120 }
121
122 const toolbar = this.views.parent.toolbar;
123
124 // Show the spinner only if we are close to the bottom.
125 if ( el.scrollHeight - ( scrollTop + el.clientHeight ) < el.clientHeight / 3 ) {
126 toolbar.get( 'spinner' ).show();
127 }
128
129 if ( el.scrollHeight < scrollTop + ( el.clientHeight * this.options.refreshThreshold ) ) {
130 this.collection.more().done( function() {
131 // view.scroll();
132 toolbar.get( 'spinner' ).hide();
133 } );
134 }
135 },
136
137 setColumns() {
138 const prev = this.columns,
139 width = this.$el.width();
140
141 if ( width ) {
142 this.columns = Math.min( Math.round( width / this.options.idealColumnWidth ), 12 ) || 1;
143
144 if ( ! prev || prev !== this.columns ) {
145 this.$el.closest( '.media-frame-content' ).attr( 'data-columns', this.columns );
146 }
147 }
148 },
149
150 show() {
151 this.$el.removeClass( 'hidden' );
152 },
153 hide() {
154 this.$el.addClass( 'hidden' );
155 },
156
157 queryError( error ) {
158 const exception = Array.isArray( error ) ? error[ 0 ] : error;
159
160 if ( ! exception ) {
161 return;
162 }
163
164 switch ( exception.code ) {
165 case 'premium_limit_reached' :
166 this.premiumLimit( exception );
167 break;
168 case 'anonymous_limit_reached' :
169 this.anonymousLimit( exception );
170 break;
171 case 'free_limit_reached' :
172 this.freeLimit( exception );
173 break;
174
175 default:
176 // do nothing
177 break;
178 }
179 },
180
181 freeLimit( exception ) {
182 if ( jQuery( '#free-limit' ).length > 0 ) {
183 return;
184 }
185 this.dialog = new StockpackDialog( {
186 ...wp.media.view.l10n.stockpack.limit.free,
187 ...{
188 id: 'free-limit',
189 error: exception.message,
190 controller: this.controller,
191 },
192 },
193 );
194
195 this.dialog.open();
196 },
197
198 anonymousLimit( exception ) {
199 if ( jQuery( '#anonymous-limit' ).length > 0 ) {
200 return;
201 }
202
203 this.dialog = new StockpackDialog( {
204 ...wp.media.view.l10n.stockpack.limit.anonymous,
205 ...{
206 id: 'anonymous-limit',
207 error: exception.message,
208 controller: this.controller,
209 },
210 },
211 );
212
213 this.dialog.open();
214 },
215
216 premiumLimit( exception ) {
217 if ( jQuery( '#premium-limit' ).length > 0 ) {
218 return;
219 }
220 this.dialog = new StockpackDialog( {
221 ...wp.media.view.l10n.stockpack.limit.premium,
222 ...{
223 id: 'premium-limit',
224 error: exception.message,
225 controller: this.controller,
226 },
227 },
228 );
229 this.dialog.buttons( [
230 {
231 text: wp.media.view.l10n.stockpack.limit.cancel,
232 click: () => {
233 this.dialog.close();
234 },
235 },
236 {
237 text: wp.media.view.l10n.stockpack.limit.contact,
238 click: () => {
239 window.open( wp.media.view.settings.stockpack.contact_url, '_blank' );
240 },
241 class: 'button-primary',
242 },
243 ] );
244 this.dialog.open();
245 },
246
247 } );
248
249 export default StockpackImages;
250