mirror of
https://github.com/WordPress/WordPress.git
synced 2024-08-30 17:49:38 +02:00
* The way that the JS modules for media are currently set up turns the existing global `wp.media` namespace into a read-only API, this is bad. * For the existing module implementation to work with plugins, those looking to override or extend a class would have to modify their own plugin to use `browserify` - we can't expect this to happen * Because the general way that plugins override media classes is via machete (resetting them to something else), we cannot use `require( 'module' )` in the internal code for media modules We CAN continue to use `require( 'fun/js' )` in the manifests for media. Future code/projects should carefully consider what is made to be public API. In 3.5, EVERYTHING was made public, so everything shall remain public. See #31684, #28510. Built from https://develop.svn.wordpress.org/trunk@31935 git-svn-id: http://core.svn.wordpress.org/trunk@31914 1a063a9b-81f0-0310-95a4-ce76da25c4cd
98 lines
2.5 KiB
JavaScript
98 lines
2.5 KiB
JavaScript
/*globals wp, _ */
|
|
|
|
/**
|
|
* wp.media.model.Selection
|
|
*
|
|
* A selection of attachments.
|
|
*
|
|
* @class
|
|
* @augments wp.media.model.Attachments
|
|
* @augments Backbone.Collection
|
|
*/
|
|
var Attachments = wp.media.model.Attachments,
|
|
Selection;
|
|
|
|
Selection = Attachments.extend({
|
|
/**
|
|
* Refresh the `single` model whenever the selection changes.
|
|
* Binds `single` instead of using the context argument to ensure
|
|
* it receives no parameters.
|
|
*
|
|
* @param {Array} [models=[]] Array of models used to populate the collection.
|
|
* @param {Object} [options={}]
|
|
*/
|
|
initialize: function( models, options ) {
|
|
/**
|
|
* call 'initialize' directly on the parent class
|
|
*/
|
|
Attachments.prototype.initialize.apply( this, arguments );
|
|
this.multiple = options && options.multiple;
|
|
|
|
this.on( 'add remove reset', _.bind( this.single, this, false ) );
|
|
},
|
|
|
|
/**
|
|
* If the workflow does not support multi-select, clear out the selection
|
|
* before adding a new attachment to it.
|
|
*
|
|
* @param {Array} models
|
|
* @param {Object} options
|
|
* @returns {wp.media.model.Attachment[]}
|
|
*/
|
|
add: function( models, options ) {
|
|
if ( ! this.multiple ) {
|
|
this.remove( this.models );
|
|
}
|
|
/**
|
|
* call 'add' directly on the parent class
|
|
*/
|
|
return Attachments.prototype.add.call( this, models, options );
|
|
},
|
|
|
|
/**
|
|
* Fired when toggling (clicking on) an attachment in the modal.
|
|
*
|
|
* @param {undefined|boolean|wp.media.model.Attachment} model
|
|
*
|
|
* @fires wp.media.model.Selection#selection:single
|
|
* @fires wp.media.model.Selection#selection:unsingle
|
|
*
|
|
* @returns {Backbone.Model}
|
|
*/
|
|
single: function( model ) {
|
|
var previous = this._single;
|
|
|
|
// If a `model` is provided, use it as the single model.
|
|
if ( model ) {
|
|
this._single = model;
|
|
}
|
|
// If the single model isn't in the selection, remove it.
|
|
if ( this._single && ! this.get( this._single.cid ) ) {
|
|
delete this._single;
|
|
}
|
|
|
|
this._single = this._single || this.last();
|
|
|
|
// If single has changed, fire an event.
|
|
if ( this._single !== previous ) {
|
|
if ( previous ) {
|
|
previous.trigger( 'selection:unsingle', previous, this );
|
|
|
|
// If the model was already removed, trigger the collection
|
|
// event manually.
|
|
if ( ! this.get( previous.cid ) ) {
|
|
this.trigger( 'selection:unsingle', previous, this );
|
|
}
|
|
}
|
|
if ( this._single ) {
|
|
this._single.trigger( 'selection:single', this._single, this );
|
|
}
|
|
}
|
|
|
|
// Return the single model, or the last model as a fallback.
|
|
return this._single;
|
|
}
|
|
});
|
|
|
|
module.exports = Selection;
|