PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 2.5.0
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v2.5.0
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.0, at assets/js/stockpack-browser.js

366 lines 9.7 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
195 this.toolbar.set( 'provider', new StockpackProviderFilters( {
196 controller: this.controller,
197 model: this.collection.props,
198 priority: 70,
199 } ).render() );
200
201 // this.advancedFilters.set('advanced-link', new StockpackAdvancedLink({
202 // controller: this.controller,
203 // model: this.collection.props,
204 // text: wp.media.view.l10n.stockpack.advanced,
205 // priority: -10
206 // }).render());
207
208 // this.filtersContainer = new StockpackAdvancedFilters({
209 // controller: this.controller,
210 // model: this.collection.props,
211 // priority: 10
212 // });
213 // this.advancedFilters.set('filters-container', this.filtersContainer).render();
214 //
215 // var ImageTypeFilter = new StockpackImageTypeFilter({
216 // controller: this.controller,
217 // model: this.collection.props,
218 // priority: -100
219 // });
220 // this.filtersContainer.set('image-type-filter', ImageTypeFilter.render());
221 //
222 // var CategoryFilter = new StockpackCategoryFilters({
223 // controller: this.controller,
224 // model: this.collection.props,
225 // priority: -95
226 // });
227 // this.filtersContainer.set('category-filter', CategoryFilter.render());
228 //
229 // var GenderFilters = new StockpackGenderFilter({
230 // controller: this.controller,
231 // model: this.collection.props,
232 // priority: -90
233 // });
234 // this.filtersContainer.set('gender-filter', GenderFilters.render());
235 //
236 // var SafeFilter = new StockpackSafeFilters({
237 // controller: this.controller,
238 // model: this.collection.props,
239 // priority: -85
240 // });
241 // this.filtersContainer.set('safe-filter', SafeFilter.render());
242 //
243 // var OrientationFilter = new StockpackOrientationFilter({
244 // controller: this.controller,
245 // model: this.collection.props,
246 // priority: -80
247 // });
248 // this.filtersContainer.set('orientation-filter', OrientationFilter.render());
249 //
250 // var ColorFilter = new StockpackColorFilter({
251 // controller: this.controller,
252 // model: this.collection.props,
253 // priority: -70
254 // });
255 // this.filtersContainer.set('color-filter', ColorFilter.render());
256 //
257 //
258 // this.filtersContainer.set('update-filter', new StockpackFilterButton({
259 // controller: this.controller,
260 // model: this.collection.props,
261 // priority: 20,
262 // text: wp.media.view.l10n.stockpack.search,
263 // }));
264 //
265 // this.filtersContainer.set('close-advanced-filter', new StockpackFilterClose({
266 // controller: this.controller,
267 // model: this.collection.props,
268 // priority: 10,
269 // text: ' ',
270 // }));
271 },
272
273 createStatus() {
274 const statusOptions = {
275 controller: this.controller,
276 message: wp.media.view.l10n.stockpack.noMedia,
277 retry: wp.media.view.l10n.stockpack.retry,
278 browser: this,
279 };
280
281 if ( this.controller.isModeActive( 'grid' ) ) {
282 statusOptions.className = 'media-toolbar wp-filter';
283 }
284
285 /**
286 * @member {wp.media.view.Toolbar}
287 */
288 this.status = new StockpackEmpty( statusOptions );
289
290 this.status.hide();
291
292 this.views.add( this.status );
293 },
294
295 createNoSearch() {
296 const statusOptions = {
297 controller: this.controller,
298 message: wp.media.view.l10n.stockpack.noSearch,
299 inspiration: wp.media.view.l10n.stockpack.inspiration,
300
301 };
302
303 /**
304 * @member {wp.media.view.Toolbar}
305 */
306 this.noSearch = new StockpackNoSearch( statusOptions );
307
308 this.noSearch.hide();
309
310 this.views.add( this.noSearch );
311 },
312
313 displayEmpty( hasError ) {
314 if ( hasError ) {
315 this.noSearch.hide();
316 this.images.hide();
317 this.status.show();
318 } else {
319 this.clearErrors();
320 this.images.hide();
321 if ( this.collection.props.get( 'search' ) ) {
322 this.noSearch.hide();
323 this.status.show();
324 } else {
325 this.noSearch.show();
326 this.toolbar.get( 'attribution' ).hide();
327 this.status.hide();
328 }
329 }
330 },
331
332 displayImages() {
333 this.clearErrors();
334 this.status.hide();
335 this.noSearch.hide();
336 this.images.show();
337 },
338
339 displayError( error ) {
340 this.displayEmpty( true );
341 this.status.model.set( 'message', wp.media.view.l10n.stockpack.error );
342 this.status.model.set( 'error', error.message );
343 if ( error.code === 2 ) {
344 this.status.model.set( 'link', {
345 url: wp.media.view.settings.stockpack.settings_url,
346 text: wp.media.view.l10n.stockpack.link,
347 } );
348 }
349 },
350
351 clearErrors() {
352 this.status.model.set( 'message', wp.media.view.l10n.stockpack.noMedia );
353 this.status.model.set( 'error', '' );
354 },
355
356 stopLoading() {
357 this.toolbar.get( 'spinner' ).hide();
358 },
359 startLoading() {
360 this.toolbar.get( 'spinner' ).show();
361 },
362
363 } );
364
365 export default StockpackBrowser;
366