PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 2.5.7
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v2.5.7
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-browser.js

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

373 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import StockpackImages from './stockpack-images';
2 import StockpackToolbar from './stockpack-toolbar';
3 import StockpackSearch from './filters/stockpack-search';
4 import StockpackDetails from './stockpack-details';
5 import StockpackSidebar from './stockpack-sidebar';
6 import StockpackEmpty from './stockpack-empty';
7 // import StockpackDialog from './stockpack-dialog';
8 // import StockpackGenderFilter from './filters/stockpack-gender';
9 import StockpackDownloader from './stockpack-downloader';
10 // import StockpackAdvancedLink from './filters/stockpack-advanced-trigger';
11 // import StockpackAdvancedFilters from './filters/stockpack-advanced-filters';
12 // import StockpackAdvanced from './filters/stockpack-advanced';
13 // import StockpackFilterButton from './filters/stockpack-filter-button';
14 // import StockpackOrientationFilter from './filters/stockpack-orientations';
15 // import StockpackImageTypeFilter from './filters/stockpack-image-type';
16 // import StockpackCategoryFilters from './filters/stockpack-category';
17 // import StockpackSafeFilters from './filters/stockpack-safe';
18 // import StockpackColorFilter from './filters/stockpack-color';
19 // import StockpackFilterClose from './filters/stockpack-filter-close';
20 import StockpackAttribution from './stockpack-attribution';
21 import StockpackNoSearch from './stockpack-no-search';
22 import StockpackProviderFilters from "./filters/stockpack-provider";
23
24 const StockpackBrowser = wp.media.View.extend( /** @lends wp.media.view.AttachmentsBrowser.prototype */{
25 tagName: 'div',
26 className: 'stockpack-browser attachments-browser',
27 attribution: null,
28
29 initialize() {
30 _.defaults( this.options, {
31 search: true,
32 provider: false,
33 } );
34
35 this.createToolbar();
36 this.createSidebar();
37 this.createStatus();
38 this.createNoSearch();
39
40 // Create the list of images.
41 this.createImages();
42
43 this.updateContent();
44
45 this.collection.on( 'add remove reset', this.updateContent, this );
46 },
47
48 /**
49 * @return {StockpackBrowser} Returns itself to allow chaining
50 */
51 dispose() {
52 this.options.selection.off( null, null, this );
53 wp.media.View.prototype.dispose.apply( this, arguments );
54 return this;
55 },
56
57 createImages() {
58 this.images = new StockpackImages( {
59 controller: this.controller,
60 collection: this.collection,
61 selection: this.options.selection,
62 scrollElement: this.options.scrollElement,
63 idealColumnWidth: this.options.idealColumnWidth,
64 provider: this.options.provider,
65 } );
66
67 this.views.add( this.images );
68 this.images.hide();
69 },
70
71 createSidebar() {
72 const options = this.options,
73 selection = options.selection,
74 sidebar = this.sidebar = new StockpackSidebar( {
75 controller: this.controller,
76 } );
77
78 this.views.add( sidebar );
79
80 selection.on( 'selection:single', this.createSingle, this );
81 selection.on( 'selection:unsingle', this.disposeSingle, this );
82
83 if ( selection.single() ) {
84 this.createSingle();
85 }
86 },
87
88 createSingle() {
89 const sidebar = this.sidebar,
90 single = this.options.selection.single();
91
92 sidebar.set( 'details', new StockpackDetails( {
93 controller: this.controller,
94 model: single,
95 priority: 80,
96 } ) );
97
98 sidebar.set( 'downloader', new StockpackDownloader( {
99 controller: this.controller,
100 model: single,
101 state: this.model,
102 search: this.collection.props.get( 'search' ),
103 priority: 90,
104 } ) );
105
106 // Show the sidebar on mobile
107 if ( this.model.id === 'insert' ) {
108 sidebar.$el.addClass( 'visible' );
109 }
110 },
111
112 disposeSingle() {
113 const sidebar = this.sidebar;
114 sidebar.unset( 'details' );
115 sidebar.unset( 'downloader' );
116 // Hide the sidebar on mobile
117 sidebar.$el.removeClass( 'visible' );
118 },
119
120 updateContent() {
121 this.toolbar.get( 'attribution' ).show();
122 if ( ! this.collection.length ) {
123 this.startLoading();
124 this.clearErrors();
125 this.dfd = this.collection.more().done( () => {
126 if ( ! this.collection.length ) {
127 this.displayEmpty();
128 } else {
129 this.displayImages();
130 }
131 this.stopLoading();
132 } ).fail(
133 ( message ) => {
134 this.displayError( message );
135
136 this.stopLoading();
137 },
138 );
139 } else {
140 this.displayImages();
141 this.stopLoading();
142 }
143 },
144
145 createToolbar() {
146 const toolbarOptions = {
147 controller: this.controller,
148 };
149
150 if ( this.controller.isModeActive( 'grid' ) ) {
151 toolbarOptions.className = 'media-toolbar wp-filter';
152 }
153
154 /**
155 * @member {StockpackToolbar}
156 */
157 this.toolbar = new StockpackToolbar( toolbarOptions );
158
159 this.views.add( this.toolbar );
160
161 // this.advancedFilters = new StockpackAdvanced({
162 // controller: this.controller,
163 // priority: 50
164 // });
165 // this.toolbar.set('advanced-filters', this.advancedFilters);
166
167 this.toolbar.set( 'spinner', new wp.media.view.Spinner( {
168 priority: -60,
169 } ) );
170 this.addToolbarFilters();
171 },
172
173 addToolbarFilters() {
174 // attribution
175
176 this.toolbar.set( 'attribution', new StockpackAttribution( {
177 controller: this.controller,
178 priority: -70,
179 } ).render() );
180
181 // Search is an input, screen reader text needs to be rendered before
182 this.toolbar.set( 'searchLabel', new wp.media.view.Label( {
183 value: wp.media.view.l10n.stockpack.search,
184 attributes: {
185 for: 'media-search-input',
186 },
187 priority: 60,
188 } ).render() );
189 this.toolbar.set( 'search', new StockpackSearch( {
190 controller: this.controller,
191 model: this.collection.props,
192 priority: 60,
193 } ).render() );
194 this.createProviderFilter();
195
196 // this.advancedFilters.set('advanced-link', new StockpackAdvancedLink({
197 // controller: this.controller,
198 // model: this.collection.props,
199 // text: wp.media.view.l10n.stockpack.advanced,
200 // priority: -10
201 // }).render());
202
203 // this.filtersContainer = new StockpackAdvancedFilters({
204 // controller: this.controller,
205 // model: this.collection.props,
206 // priority: 10
207 // });
208 // this.advancedFilters.set('filters-container', this.filtersContainer).render();
209 //
210 // var ImageTypeFilter = new StockpackImageTypeFilter({
211 // controller: this.controller,
212 // model: this.collection.props,
213 // priority: -100
214 // });
215 // this.filtersContainer.set('image-type-filter', ImageTypeFilter.render());
216 //
217 // var CategoryFilter = new StockpackCategoryFilters({
218 // controller: this.controller,
219 // model: this.collection.props,
220 // priority: -95
221 // });
222 // this.filtersContainer.set('category-filter', CategoryFilter.render());
223 //
224 // var GenderFilters = new StockpackGenderFilter({
225 // controller: this.controller,
226 // model: this.collection.props,
227 // priority: -90
228 // });
229 // this.filtersContainer.set('gender-filter', GenderFilters.render());
230 //
231 // var SafeFilter = new StockpackSafeFilters({
232 // controller: this.controller,
233 // model: this.collection.props,
234 // priority: -85
235 // });
236 // this.filtersContainer.set('safe-filter', SafeFilter.render());
237 //
238 // var OrientationFilter = new StockpackOrientationFilter({
239 // controller: this.controller,
240 // model: this.collection.props,
241 // priority: -80
242 // });
243 // this.filtersContainer.set('orientation-filter', OrientationFilter.render());
244 //
245 // var ColorFilter = new StockpackColorFilter({
246 // controller: this.controller,
247 // model: this.collection.props,
248 // priority: -70
249 // });
250 // this.filtersContainer.set('color-filter', ColorFilter.render());
251 //
252 //
253 // this.filtersContainer.set('update-filter', new StockpackFilterButton({
254 // controller: this.controller,
255 // model: this.collection.props,
256 // priority: 20,
257 // text: wp.media.view.l10n.stockpack.search,
258 // }));
259 //
260 // this.filtersContainer.set('close-advanced-filter', new StockpackFilterClose({
261 // controller: this.controller,
262 // model: this.collection.props,
263 // priority: 10,
264 // text: ' ',
265 // }));
266 },
267
268 createProviderFilter() {
269 this.toolbar.set( 'provider', new StockpackProviderFilters( {
270 controller: this.controller,
271 model: this.collection.props,
272 priority: 70,
273 } ).render() );
274
275 if ( this.toolbar.get( 'provider' ).hidden ) {
276 this.toolbar.$el.addClass( 'provider-hidden' );
277 }
278 },
279
280 createStatus() {
281 const statusOptions = {
282 controller: this.controller,
283 message: wp.media.view.l10n.stockpack.noMedia,
284 retry: wp.media.view.l10n.stockpack.retry,
285 browser: this,
286 };
287
288 if ( this.controller.isModeActive( 'grid' ) ) {
289 statusOptions.className = 'media-toolbar wp-filter';
290 }
291
292 /**
293 * @member {wp.media.view.Toolbar}
294 */
295 this.status = new StockpackEmpty( statusOptions );
296
297 this.status.hide();
298
299 this.views.add( this.status );
300 },
301
302 createNoSearch() {
303 const statusOptions = {
304 controller: this.controller,
305 message: wp.media.view.l10n.stockpack.noSearch,
306 inspiration: wp.media.view.l10n.stockpack.inspiration,
307
308 };
309
310 /**
311 * @member {wp.media.view.Toolbar}
312 */
313 this.noSearch = new StockpackNoSearch( statusOptions );
314
315 this.noSearch.hide();
316
317 this.views.add( this.noSearch );
318 },
319
320 displayEmpty( hasError ) {
321 if ( hasError ) {
322 this.noSearch.hide();
323 this.images.hide();
324 this.status.show();
325 } else {
326 this.clearErrors();
327 this.images.hide();
328 if ( this.collection.props.get( 'search' ) ) {
329 this.noSearch.hide();
330 this.status.show();
331 } else {
332 this.noSearch.show();
333 this.toolbar.get( 'attribution' ).hide();
334 this.status.hide();
335 }
336 }
337 },
338
339 displayImages() {
340 this.clearErrors();
341 this.status.hide();
342 this.noSearch.hide();
343 this.images.show();
344 },
345
346 displayError( error ) {
347 this.displayEmpty( true );
348 this.status.model.set( 'message', wp.media.view.l10n.stockpack.error );
349 this.status.model.set( 'error', error.message );
350 if ( error.code === 2 ) {
351 this.status.model.set( 'link', {
352 url: wp.media.view.settings.stockpack.settings_url,
353 text: wp.media.view.l10n.stockpack.link,
354 } );
355 }
356 },
357
358 clearErrors() {
359 this.status.model.set( 'message', wp.media.view.l10n.stockpack.noMedia );
360 this.status.model.set( 'error', '' );
361 },
362
363 stopLoading() {
364 this.toolbar.get( 'spinner' ).hide();
365 },
366 startLoading() {
367 this.toolbar.get( 'spinner' ).show();
368 },
369
370 } );
371
372 export default StockpackBrowser;
373