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

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

147 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 const StockpackToolbar = wp.media.View.extend( /** @lends wp.media.view.Toolbar.prototype */{
2 tagName: 'div',
3 className: 'media-toolbar',
4
5 initialize() {
6 const state = this.controller.state(),
7 selection = this.selection = state.get( 'stockpack-selection' ),
8 library = this.library = state.get( 'stockpack-images' );
9
10 this._views = {};
11
12 // The toolbar is composed of two `PriorityList` views.
13 this.primary = new wp.media.view.PriorityList();
14 this.secondary = new wp.media.view.PriorityList();
15 this.primary.$el.addClass( 'media-toolbar-primary search-form' );
16 this.secondary.$el.addClass( 'media-toolbar-secondary' );
17
18 this.views.set( [ this.secondary, this.primary ] );
19
20 if ( this.options.items ) {
21 this.set( this.options.items, { silent: true } );
22 }
23
24 if ( ! this.options.silent ) {
25 this.render();
26 }
27
28 if ( selection ) {
29 selection.on( 'add remove reset', this.refresh, this );
30 }
31
32 if ( library ) {
33 library.on( 'add remove reset', this.refresh, this );
34 }
35 },
36 /**
37 * @return {wp.media.view.Toolbar} Returns itsef to allow chaining
38 */
39 dispose() {
40 if ( this.selection ) {
41 this.selection.off( null, null, this );
42 }
43
44 if ( this.library ) {
45 this.library.off( null, null, this );
46 }
47 /**
48 * call 'dispose' directly on the parent class
49 */
50 return wp.media.View.prototype.dispose.apply( this, arguments );
51 },
52
53 ready() {
54 this.refresh();
55 },
56
57 /**
58 * @param {string} id
59 * @param {Backbone.View|Object} view
60 * @param {Object} [options={}]
61 * @return {wp.media.view.Toolbar} Returns itself to allow chaining
62 */
63 set( id, view, options ) {
64 let list;
65 options = options || {};
66
67 // Accept an object with an `id` : `view` mapping.
68 if ( _.isObject( id ) ) {
69 _.each( id, function( objectView, index ) {
70 this.set( index, objectView, { silent: true } );
71 }, this );
72 } else {
73 if ( ! ( view instanceof Backbone.View ) ) {
74 view.classes = [ 'media-button-' + id ].concat( view.classes || [] );
75 view = new wp.media.view.Button( view ).render();
76 }
77
78 view.controller = view.controller || this.controller;
79
80 this._views[ id ] = view;
81
82 list = view.options.priority < 0 ? 'secondary' : 'primary';
83 this[ list ].set( id, view, options );
84 }
85
86 if ( ! options.silent ) {
87 this.refresh();
88 }
89
90 return this;
91 },
92
93 /**
94 * @param {string} id
95 * @return {wp.media.view.Button} view
96 */
97 get( id ) {
98 return this._views[ id ];
99 },
100 /**
101 * @param {string} id
102 * @param {Object} options
103 * @return {wp.media.view.Toolbar} Returns itself to allow chaining
104 */
105 unset( id, options ) {
106 delete this._views[ id ];
107 this.primary.unset( id, options );
108 this.secondary.unset( id, options );
109
110 if ( ! options || ! options.silent ) {
111 this.refresh();
112 }
113 return this;
114 },
115
116 refresh() {
117 const state = this.controller.state(),
118 library = state.get( 'stockpack-images' ),
119 selection = state.get( 'selection' );
120
121 _.each( this._views, function( button ) {
122 if ( ! button.model || ! button.options || ! button.options.requires ) {
123 return;
124 }
125
126 const requires = button.options.requires;
127 let disabled = false;
128
129 // Prevent insertion of attachments if any of them are still uploading
130 if ( selection && selection.models ) {
131 disabled = _.some( selection.models, function( attachment ) {
132 return attachment.get( 'uploading' ) === true;
133 } );
134 }
135
136 if ( requires.selection && selection && ! selection.length ) {
137 disabled = true;
138 } else if ( requires.library && library && ! library.length ) {
139 disabled = true;
140 }
141 button.model.set( 'disabled', disabled );
142 } );
143 },
144 } );
145
146 export default StockpackToolbar;
147