PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 2.4
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v2.4
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 2.6.1 All 73 releases
stockpack / assets / js / stockpack-images.js

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

246 lines 5.8 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 = error[ 0 ];
159
160 switch ( exception.code ) {
161 case 'premium_limit_reached' :
162 this.premiumLimit( exception );
163 break;
164 case 'anonymous_limit_reached' :
165 this.anonymousLimit( exception );
166 break;
167 case 'free_limit_reached' :
168 this.freeLimit( exception );
169 break;
170
171 default:
172 // do nothing
173 break;
174 }
175 },
176
177 freeLimit( exception ) {
178 if ( jQuery( '#free-limit' ).length > 0 ) {
179 return;
180 }
181 this.dialog = new StockpackDialog( {
182 ...wp.media.view.l10n.stockpack.limit.free,
183 ...{
184 id: 'free-limit',
185 error: exception.message,
186 controller: this.controller,
187 },
188 },
189 );
190
191 this.dialog.open();
192 },
193
194 anonymousLimit( exception ) {
195 if ( jQuery( '#anonymous-limit' ).length > 0 ) {
196 return;
197 }
198
199 this.dialog = new StockpackDialog( {
200 ...wp.media.view.l10n.stockpack.limit.anonymous,
201 ...{
202 id: 'anonymous-limit',
203 error: exception.message,
204 controller: this.controller,
205 },
206 },
207 );
208
209 this.dialog.open();
210 },
211
212 premiumLimit( exception ) {
213 if ( jQuery( '#premium-limit' ).length > 0 ) {
214 return;
215 }
216 this.dialog = new StockpackDialog( {
217 ...wp.media.view.l10n.stockpack.limit.premium,
218 ...{
219 id: 'premium-limit',
220 error: exception.message,
221 controller: this.controller,
222 },
223 },
224 );
225 this.dialog.buttons( [
226 {
227 text: wp.media.view.l10n.stockpack.limit.cancel,
228 click: () => {
229 this.dialog.close();
230 },
231 },
232 {
233 text: wp.media.view.l10n.stockpack.limit.contact,
234 click: () => {
235 window.open( wp.media.view.settings.stockpack.contact_url, '_blank' );
236 },
237 class: 'button-primary',
238 },
239 ] );
240 this.dialog.open();
241 },
242
243 } );
244
245 export default StockpackImages;
246