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 / models / stockpack-selection-model.js

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

83 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import StockpackImagesModel from './stockpack-images-model';
2
3 const StockpackSelection = StockpackImagesModel.extend( {
4 /**
5 * Refresh the `single` model whenever the selection changes.
6 * Binds `single` instead of using the context argument to ensure
7 * it receives no parameters.
8 *
9 * @param {Array} [models=[]] Array of models used to populate the collection.
10 * @param {Object} [options={}]
11 */
12 initialize( models, options ) {
13 /**
14 * call 'initialize' directly on the parent class
15 */
16 StockpackImagesModel.prototype.initialize.apply( this, arguments );
17 this.multiple = options && options.multiple;
18
19 this.on( 'add remove reset', _.bind( this.single, this, false ) );
20 },
21
22 /**
23 * If the workflow does not support multi-select, clear out the selection
24 * before adding a new attachment to it.
25 *
26 * @param {Array} models
27 * @param {Object} options
28 * @return {StockpackImagesModel[]} attachments
29 */
30 add( models, options ) {
31 if ( ! this.multiple ) {
32 this.remove( this.models );
33 }
34 /**
35 * call 'add' directly on the parent class
36 */
37 return StockpackImagesModel.prototype.add.call( this, models, options );
38 },
39
40 /**
41 * Fired when toggling (clicking on) an attachment in the modal.
42 *
43 * @param {undefined|boolean|StockpackImagesModel} model
44 *
45 * @return {Backbone.Model} model
46 */
47 single( model ) {
48 const previous = this._single;
49
50 // If a `model` is provided, use it as the single model.
51 if ( model ) {
52 this._single = model;
53 }
54 // If the single model isn't in the selection, remove it.
55 if ( this._single && ! this.get( this._single.cid ) ) {
56 delete this._single;
57 }
58
59 this._single = this._single || this.last();
60
61 // If single has changed, fire an event.
62 if ( this._single !== previous ) {
63 if ( previous ) {
64 previous.trigger( 'selection:unsingle', previous, this );
65
66 // If the model was already removed, trigger the collection
67 // event manually.
68 if ( ! this.get( previous.cid ) ) {
69 this.trigger( 'selection:unsingle', previous, this );
70 }
71 }
72 if ( this._single ) {
73 this._single.trigger( 'selection:single', this._single, this );
74 }
75 }
76
77 // Return the single model, or the last model as a fallback.
78 return this._single;
79 },
80 } );
81
82 export default StockpackSelection;
83