/**
* Create and return a media frame.
*
- * Handles the default media experience. Automatically creates
- * and opens a media frame, and returns the result.
- * Does nothing if the controllers do not exist.
+ * Handles the default media experience.
*
* @param {object} attributes The properties passed to the main media controller.
* @return {wp.media.view.MediaFrame} A media workflow.
*/
/**
- * A basic comparator.
+ * A basic equality comparator for Backbone models.
+ *
+ * Used to order models within a collection - @see wp.media.model.Attachments.comparator().
*
* @param {mixed} a The primary parameter to compare.
* @param {mixed} b The primary parameter to compare.
/**
* media.template( id )
*
- * Fetches a template by id.
+ * Fetch a JavaScript template for an id, and return a templating function for it.
+ *
* See wp.template() in `wp-includes/js/wp-util.js`.
*
* @borrows wp.template as template
/**
* wp.media.model.Attachment
*
- * @constructor
+ * @class
* @augments Backbone.Model
*/
Attachment = media.model.Attachment = Backbone.Model.extend({
}
}, {
/**
- * Add a model to the end of the static 'all' collection and return it.
+ * Create a new model on the static 'all' attachments collection and return it.
*
* @static
* @param {Object} attrs
return Attachments.all.push( attrs );
},
/**
- * Retrieve a model, or add it to the end of the static 'all' collection before returning it.
+ * Create a new model on the static 'all' attachments collection and return it.
+ *
+ * If this function has already been called for the id,
+ * it returns the specified attachment.
*
* @static
* @param {string} id A string used to identify a model.
/**
* wp.media.model.PostImage
*
- * @constructor
+ * An instance of an image that's been embedded into a post.
+ *
+ * Used in the embedded image attachment display settings modal - @see wp.media.view.MediaFrame.ImageDetails.
+ *
+ * @class
* @augments Backbone.Model
+ *
+ * @param {int} [attributes] Initial model attributes.
+ * @param {int} [attributes.attachment_id] ID of the attachment.
**/
PostImage = media.model.PostImage = Backbone.Model.extend({
/**
* wp.media.model.Attachments
*
- * @constructor
+ * A collection of attachments.
+ *
+ * This collection has no persistence with the server without supplying
+ * 'options.props.query = true', which will mirror the collection
+ * to an Attachments Query collection - @see wp.media.model.Attachments.mirror().
+ *
+ * @class
* @augments Backbone.Collection
+ *
+ * @param {array} [models] Models to initialize with the collection.
+ * @param {object} [options] Options hash for the collection.
+ * @param {string} [options.props] Options hash for the initial query properties.
+ * @param {string} [options.props.order] Initial order (ASC or DESC) for the collection.
+ * @param {string} [options.props.orderby] Initial attribute key to order the collection by.
+ * @param {string} [options.props.query] Whether the collection is linked to an attachments query.
+ * @param {string} [options.observe]
+ * @param {string} [options.filters]
+ *
*/
Attachments = media.model.Attachments = Backbone.Collection.extend({
/**
this.props.on( 'change:orderby', this._changeOrderby, this );
this.props.on( 'change:query', this._changeQuery, this );
- // Set the `props` model and fill the default property values.
this.props.set( _.defaults( options.props || {} ) );
- // Observe another `Attachments` collection if one is provided.
if ( options.observe ) {
this.observe( options.observe );
}
},
/**
- * Automatically sort the collection when the order changes.
+ * Sort the collection when the order attribute changes.
*
* @access private
*/
validateDestroyed: false,
/**
+ * Checks whether an attachment is valid.
+ *
* @param {wp.media.model.Attachment} attachment
* @returns {Boolean}
*/
}, this );
},
/**
+ * Add or remove an attachment to the collection depending on its validity.
+ *
* @param {wp.media.model.Attachment} attachment
* @param {Object} options
* @returns {wp.media.model.Attachments} Returns itself to allow chaining
},
/**
+ * Add or remove all attachments from another collection depending on each one's validity.
+ *
* @param {wp.media.model.Attachments} attachments
* @param {object} [options={}]
*
return this;
},
/**
- * @param {wp.media.model.Attachments} attachments
- * @returns {wp.media.model.Attachments} Returns itself to allow chaining
+ * Start observing another attachments collection change events
+ * and replicate them on this collection.
+ *
+ * @param {wp.media.model.Attachments} The attachments collection to observe.
+ * @returns {wp.media.model.Attachments} Returns itself to allow chaining.
*/
observe: function( attachments ) {
this.observers = this.observers || [];
return this;
},
/**
- * @param {wp.media.model.Attachments} attachments
+ * Stop replicating collection change events from another attachments collection.
+ *
+ * @param {wp.media.model.Attachments} The attachments collection to stop observing.
* @returns {wp.media.model.Attachments} Returns itself to allow chaining
*/
unobserve: function( attachments ) {
return this.validateAll( attachments, options );
},
/**
- * @param {wp.media.model.Attachments} attachments
+ * Start mirroring another attachments collection, clearing out any models already
+ * in the collection.
+ *
+ * @param {wp.media.model.Attachments} The attachments collection to mirror.
* @returns {wp.media.model.Attachments} Returns itself to allow chaining
*/
mirror: function( attachments ) {
return this;
},
+ /**
+ * Stop mirroring another attachments collection.
+ */
unmirror: function() {
if ( ! this.mirroring ) {
return;
delete this.mirroring;
},
/**
- * @param {Object} options
+ * Retrive more attachments from the server for the collection.
+ *
+ * Only works if the collection is mirroring a Query Attachments collection,
+ * and forwards to its `more` method. This collection class doesn't have
+ * server persistence by itself.
+ *
+ * @param {object} options
* @returns {Promise}
*/
more: function( options ) {
return deferred.promise();
},
/**
- * @returns {Boolean}
+ * Whether there are more attachments that haven't been sync'd from the server
+ * that match the collection's query.
+ *
+ * Only works if the collection is mirroring a Query Attachments collection,
+ * and forwards to its `hasMore` method. This collection class doesn't have
+ * server persistence by itself.
+ *
+ * @returns {boolean}
*/
hasMore: function() {
return this.mirroring ? this.mirroring.hasMore() : false;
},
/**
- * Overrides Backbone.Collection.parse
+ * A custom AJAX-response parser.
+ *
+ * See trac ticket #24753
*
* @param {Object|Array} resp The raw response Object/Array.
* @param {Object} xhr
});
},
/**
+ * If the collection is a query, create and mirror an Attachments Query collection.
+ *
* @access private
*/
_requery: function( refresh ) {
}
}, {
/**
+ * A function to compare two attachment models in an attachments collection.
+ *
+ * Used as the default comparator for instances of wp.media.model.Attachments
+ * and its subclasses. @see wp.media.model.Attachments._changeOrderby().
+ *
* @static
- * Overrides Backbone.Collection.comparator
*
* @param {Backbone.Model} a
* @param {Backbone.Model} b
});
/**
+ * A collection of all attachments that have been fetched from the server.
+ *
* @static
* @member {wp.media.model.Attachments}
*/
/**
* wp.media.query
*
- * @static
+ * Shorthand for creating a new Attachments Query.
+ *
+ * @param {object} [props]
* @returns {wp.media.model.Attachments}
*/
media.query = function( props ) {
/**
* wp.media.model.Query
*
- * A set of attachments that corresponds to a set of consecutively paged
- * queries on the server.
+ * A collection of attachments that match the supplied query arguments.
*
* Note: Do NOT change this.args after the query has been initialized.
* Things will break.
*
- * @constructor
+ * @class
* @augments wp.media.model.Attachments
* @augments Backbone.Collection
+ *
+ * @param {array} [models] Models to initialize with the collection.
+ * @param {object} [options] Options hash.
+ * @param {object} [options.args] Attachments query arguments.
+ * @param {object} [options.args.posts_per_page]
*/
Query = media.model.Query = Attachments.extend({
/**
* @global wp.Uploader
*
- * @param {Array} [models=[]] Array of models used to populate the collection.
- * @param {Object} [options={}]
+ * @param {array} [models=[]] Array of initial models to populate the collection.
+ * @param {object} [options={}]
*/
initialize: function( models, options ) {
var allowed;
}
},
/**
- * @returns {Boolean}
+ * Whether there are more attachments that haven't been sync'd from the server
+ * that match the collection's query.
+ *
+ * @returns {boolean}
*/
hasMore: function() {
return this._hasMore;
},
/**
- * @param {Object} [options={}]
+ * Fetch more attachments from the server for the collection.
+ *
+ * @param {object} [options={}]
* @returns {Promise}
*/
more: function( options ) {
var query = this;
+ // If there is already a request pending, return early with the Deferred object.
if ( this._more && 'pending' === this._more.state() ) {
return this._more;
}
*/
orderby: {
allowed: [ 'name', 'author', 'date', 'title', 'modified', 'uploadedTo', 'id', 'post__in', 'menuOrder' ],
+ /**
+ * A map of JavaScript orderby values to their WP_Query equivalents.
+ * @type {Object}
+ */
valuemap: {
'id': 'ID',
'uploadedTo': 'parent',
}
},
/**
+ * A map of JavaScript query properties to their WP_Query equivalents.
+ *
* @readonly
*/
propmap: {
'perPage': 'posts_per_page',
'menuOrder': 'menu_order',
'uploadedTo': 'post_parent',
- 'status': 'post_status'
+ 'status': 'post_status',
+ 'include': 'post__in',
+ 'exclude': 'post__not_in'
},
/**
+ * Creates and returns an Attachments Query collection given the properties.
+ *
+ * Caches query objects and reuses where possible.
+ *
* @static
* @method
*
- * @returns {wp.media.model.Query} A new query.
+ * @param {object} [props]
+ * @param {Object} [props.cache=true] Whether to use the query cache or not.
+ * @param {Object} [props.order]
+ * @param {Object} [props.orderby]
+ * @param {Object} [props.include]
+ * @param {Object} [props.exclude]
+ * @param {Object} [props.s]
+ * @param {Object} [props.post_mime_type]
+ * @param {Object} [props.posts_per_page]
+ * @param {Object} [props.menu_order]
+ * @param {Object} [props.post_parent]
+ * @param {Object} [props.post_status]
+ * @param {Object} [options]
+ *
+ * @returns {wp.media.model.Query} A new Attachments Query collection.
*/
- // Caches query objects so queries can be easily reused.
get: (function(){
/**
* @static
var queries = [];
/**
- * @param {Object} props
- * @param {Object} options
* @returns {Query}
*/
return function( props, options ) {
props.orderby = defaults.orderby;
}
+ _.each( [ 'include', 'exclude' ], function( prop ) {
+ if ( props[ prop ] && ! _.isArray( props[ prop ] ) ) {
+ props[ prop ] = [ props[ prop ] ];
+ }
+ } );
+
// Generate the query `args` object.
// Correct any differing property names.
_.each( props, function( value, prop ) {
// Substitute exceptions specified in orderby.keymap.
args.orderby = orderby.valuemap[ props.orderby ] || props.orderby;
- // Search the query cache for matches.
+ // Search the query cache for a matching query.
if ( cache ) {
query = _.find( queries, function( query ) {
return _.isEqual( query.args, args );
/**
* wp.media.model.Selection
*
- * Used to manage a selection of attachments in the views.
+ * A selection of attachments.
*
- * @constructor
+ * @class
* @augments wp.media.model.Attachments
* @augments Backbone.Collection
*/
},
/**
- * Override the selection's add method.
- * If the workflow does not support multiple
- * selected attachments, reset the selection.
- *
- * Overrides Backbone.Collection.add
- * Overrides wp.media.model.Attachments.add
+ * 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
},
/**
- * Triggered when toggling (clicking on) an attachment in the modal
+ * Fired when toggling (clicking on) an attachment in the modal.
*
* @param {undefined|boolean|wp.media.model.Attachment} model
*