]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/js/media-models.js
WordPress 4.1
[autoinstalls/wordpress.git] / wp-includes / js / media-models.js
index ac7ee938d6af31f6ab0b5d26c4d6c7f175a74012..84bb02fa05c57cccc75d6a9353daa5ecef65ddc7 100644 (file)
@@ -7,9 +7,7 @@ window.wp = window.wp || {};
        /**
         * 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.
@@ -65,7 +63,9 @@ window.wp = window.wp || {};
         */
 
        /**
-        * 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.
@@ -87,7 +87,8 @@ window.wp = window.wp || {};
                /**
                 * 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
@@ -197,7 +198,7 @@ window.wp = window.wp || {};
        /**
         * wp.media.model.Attachment
         *
-        * @constructor
+        * @class
         * @augments Backbone.Model
         */
        Attachment = media.model.Attachment = Backbone.Model.extend({
@@ -329,7 +330,7 @@ window.wp = window.wp || {};
                }
        }, {
                /**
-                * 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
@@ -339,7 +340,10 @@ window.wp = window.wp || {};
                        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.
@@ -354,8 +358,15 @@ window.wp = window.wp || {};
        /**
         * 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({
 
@@ -497,8 +508,24 @@ window.wp = window.wp || {};
        /**
         * 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({
                /**
@@ -522,16 +549,14 @@ window.wp = window.wp || {};
                        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
                 */
@@ -625,6 +650,8 @@ window.wp = window.wp || {};
 
                validateDestroyed: false,
                /**
+                * Checks whether an attachment is valid.
+                *
                 * @param {wp.media.model.Attachment} attachment
                 * @returns {Boolean}
                 */
@@ -637,6 +664,8 @@ window.wp = window.wp || {};
                        }, 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
@@ -655,6 +684,8 @@ window.wp = window.wp || {};
                },
 
                /**
+                * Add or remove all attachments from another collection depending on each one's validity.
+                *
                 * @param {wp.media.model.Attachments} attachments
                 * @param {object} [options={}]
                 *
@@ -675,8 +706,11 @@ window.wp = window.wp || {};
                        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 || [];
@@ -688,7 +722,9 @@ window.wp = window.wp || {};
                        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 ) {
@@ -734,7 +770,10 @@ window.wp = window.wp || {};
                        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 ) {
@@ -752,6 +791,9 @@ window.wp = window.wp || {};
 
                        return this;
                },
+               /**
+                * Stop mirroring another attachments collection.
+                */
                unmirror: function() {
                        if ( ! this.mirroring ) {
                                return;
@@ -761,7 +803,13 @@ window.wp = window.wp || {};
                        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 ) {
@@ -784,13 +832,22 @@ window.wp = window.wp || {};
                        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
@@ -822,6 +879,8 @@ window.wp = window.wp || {};
                        });
                },
                /**
+                * If the collection is a query, create and mirror an Attachments Query collection.
+                *
                 * @access private
                 */
                _requery: function( refresh ) {
@@ -867,8 +926,12 @@ window.wp = window.wp || {};
                }
        }, {
                /**
+                * 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
@@ -971,6 +1034,8 @@ window.wp = window.wp || {};
        });
 
        /**
+        * A collection of all attachments that have been fetched from the server.
+        *
         * @static
         * @member {wp.media.model.Attachments}
         */
@@ -979,7 +1044,9 @@ window.wp = window.wp || {};
        /**
         * wp.media.query
         *
-        * @static
+        * Shorthand for creating a new Attachments Query.
+        *
+        * @param {object} [props]
         * @returns {wp.media.model.Attachments}
         */
        media.query = function( props ) {
@@ -991,22 +1058,26 @@ window.wp = window.wp || {};
        /**
         * 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;
@@ -1060,18 +1131,24 @@ window.wp = window.wp || {};
                        }
                },
                /**
-                * @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;
                        }
@@ -1149,6 +1226,10 @@ window.wp = window.wp || {};
                 */
                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',
@@ -1156,6 +1237,8 @@ window.wp = window.wp || {};
                        }
                },
                /**
+                * A map of JavaScript query properties to their WP_Query equivalents.
+                *
                 * @readonly
                 */
                propmap: {
@@ -1164,15 +1247,34 @@ window.wp = window.wp || {};
                        '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
@@ -1181,8 +1283,6 @@ window.wp = window.wp || {};
                        var queries = [];
 
                        /**
-                        * @param {Object} props
-                        * @param {Object} options
                         * @returns {Query}
                         */
                        return function( props, options ) {
@@ -1211,6 +1311,12 @@ window.wp = window.wp || {};
                                        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 ) {
@@ -1228,7 +1334,7 @@ window.wp = window.wp || {};
                                // 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 );
@@ -1254,9 +1360,9 @@ window.wp = window.wp || {};
        /**
         * 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
         */
@@ -1280,12 +1386,8 @@ window.wp = window.wp || {};
                },
 
                /**
-                * 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
@@ -1302,7 +1404,7 @@ window.wp = window.wp || {};
                },
 
                /**
-                * 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
                 *