PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 2.2
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v2.2
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 3.0.0 3.0.1 All 71 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.2, at assets/js/stockpack-browser.js

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