wp/wp-includes/js/media-models.js
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
       
     1 (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
       
     2 /*globals wp, _, jQuery */
       
     3 
       
     4 var $ = jQuery,
       
     5 	Attachment, Attachments, l10n, media;
       
     6 
     1 window.wp = window.wp || {};
     7 window.wp = window.wp || {};
     2 
     8 
     3 (function($){
     9 /**
     4 	var Attachment, Attachments, Query, compare, l10n, media;
    10  * Create and return a media frame.
     5 
    11  *
     6 	/**
    12  * Handles the default media experience.
     7 	 * wp.media( attributes )
    13  *
     8 	 *
    14  * @param  {object} attributes The properties passed to the main media controller.
     9 	 * Handles the default media experience. Automatically creates
    15  * @return {wp.media.view.MediaFrame} A media workflow.
    10 	 * and opens a media frame, and returns the result.
    16  */
    11 	 * Does nothing if the controllers do not exist.
    17 media = wp.media = function( attributes ) {
    12 	 *
    18 	var MediaFrame = media.view.MediaFrame,
    13 	 * @param  {object} attributes The properties passed to the main media controller.
    19 		frame;
    14 	 * @return {object}            A media workflow.
    20 
    15 	 */
    21 	if ( ! MediaFrame ) {
    16 	media = wp.media = function( attributes ) {
    22 		return;
    17 		var MediaFrame = media.view.MediaFrame,
    23 	}
    18 			frame;
    24 
    19 
    25 	attributes = _.defaults( attributes || {}, {
    20 		if ( ! MediaFrame )
    26 		frame: 'select'
    21 			return;
       
    22 
       
    23 		attributes = _.defaults( attributes || {}, {
       
    24 			frame: 'select'
       
    25 		});
       
    26 
       
    27 		if ( 'select' === attributes.frame && MediaFrame.Select )
       
    28 			frame = new MediaFrame.Select( attributes );
       
    29 		else if ( 'post' === attributes.frame && MediaFrame.Post )
       
    30 			frame = new MediaFrame.Post( attributes );
       
    31 
       
    32 		delete attributes.frame;
       
    33 
       
    34 		return frame;
       
    35 	};
       
    36 
       
    37 	_.extend( media, { model: {}, view: {}, controller: {}, frames: {} });
       
    38 
       
    39 	// Link any localized strings.
       
    40 	l10n = media.model.l10n = typeof _wpMediaModelsL10n === 'undefined' ? {} : _wpMediaModelsL10n;
       
    41 
       
    42 	// Link any settings.
       
    43 	media.model.settings = l10n.settings || {};
       
    44 	delete l10n.settings;
       
    45 
       
    46 	/**
       
    47 	 * ========================================================================
       
    48 	 * UTILITIES
       
    49 	 * ========================================================================
       
    50 	 */
       
    51 
       
    52 	/**
       
    53 	 * A basic comparator.
       
    54 	 *
       
    55 	 * @param  {mixed}  a  The primary parameter to compare.
       
    56 	 * @param  {mixed}  b  The primary parameter to compare.
       
    57 	 * @param  {string} ac The fallback parameter to compare, a's cid.
       
    58 	 * @param  {string} bc The fallback parameter to compare, b's cid.
       
    59 	 * @return {number}    -1: a should come before b.
       
    60 	 *                      0: a and b are of the same rank.
       
    61 	 *                      1: b should come before a.
       
    62 	 */
       
    63 	compare = function( a, b, ac, bc ) {
       
    64 		if ( _.isEqual( a, b ) )
       
    65 			return ac === bc ? 0 : (ac > bc ? -1 : 1);
       
    66 		else
       
    67 			return a > b ? -1 : 1;
       
    68 	};
       
    69 
       
    70 	_.extend( media, {
       
    71 		/**
       
    72 		 * media.template( id )
       
    73 		 *
       
    74 		 * Fetches a template by id.
       
    75 		 * See wp.template() in `wp-includes/js/wp-util.js`.
       
    76 		 */
       
    77 		template: wp.template,
       
    78 
       
    79 		/**
       
    80 		 * media.post( [action], [data] )
       
    81 		 *
       
    82 		 * Sends a POST request to WordPress.
       
    83 		 * See wp.ajax.post() in `wp-includes/js/wp-util.js`.
       
    84 		 */
       
    85 		post: wp.ajax.post,
       
    86 
       
    87 		/**
       
    88 		 * media.ajax( [action], [options] )
       
    89 		 *
       
    90 		 * Sends an XHR request to WordPress.
       
    91 		 * See wp.ajax.send() in `wp-includes/js/wp-util.js`.
       
    92 		 */
       
    93 		ajax: wp.ajax.send,
       
    94 
       
    95 		// Scales a set of dimensions to fit within bounding dimensions.
       
    96 		fit: function( dimensions ) {
       
    97 			var width     = dimensions.width,
       
    98 				height    = dimensions.height,
       
    99 				maxWidth  = dimensions.maxWidth,
       
   100 				maxHeight = dimensions.maxHeight,
       
   101 				constraint;
       
   102 
       
   103 			// Compare ratios between the two values to determine which
       
   104 			// max to constrain by. If a max value doesn't exist, then the
       
   105 			// opposite side is the constraint.
       
   106 			if ( ! _.isUndefined( maxWidth ) && ! _.isUndefined( maxHeight ) ) {
       
   107 				constraint = ( width / height > maxWidth / maxHeight ) ? 'width' : 'height';
       
   108 			} else if ( _.isUndefined( maxHeight ) ) {
       
   109 				constraint = 'width';
       
   110 			} else if (  _.isUndefined( maxWidth ) && height > maxHeight ) {
       
   111 				constraint = 'height';
       
   112 			}
       
   113 
       
   114 			// If the value of the constrained side is larger than the max,
       
   115 			// then scale the values. Otherwise return the originals; they fit.
       
   116 			if ( 'width' === constraint && width > maxWidth ) {
       
   117 				return {
       
   118 					width : maxWidth,
       
   119 					height: Math.round( maxWidth * height / width )
       
   120 				};
       
   121 			} else if ( 'height' === constraint && height > maxHeight ) {
       
   122 				return {
       
   123 					width : Math.round( maxHeight * width / height ),
       
   124 					height: maxHeight
       
   125 				};
       
   126 			} else {
       
   127 				return {
       
   128 					width : width,
       
   129 					height: height
       
   130 				};
       
   131 			}
       
   132 		},
       
   133 
       
   134 		// Truncates a string by injecting an ellipsis into the middle.
       
   135 		// Useful for filenames.
       
   136 		truncate: function( string, length, replacement ) {
       
   137 			length = length || 30;
       
   138 			replacement = replacement || '&hellip;';
       
   139 
       
   140 			if ( string.length <= length )
       
   141 				return string;
       
   142 
       
   143 			return string.substr( 0, length / 2 ) + replacement + string.substr( -1 * length / 2 );
       
   144 		}
       
   145 	});
    27 	});
   146 
    28 
   147 
    29 	if ( 'select' === attributes.frame && MediaFrame.Select ) {
   148 	/**
    30 		frame = new MediaFrame.Select( attributes );
   149 	 * ========================================================================
    31 	} else if ( 'post' === attributes.frame && MediaFrame.Post ) {
   150 	 * MODELS
    32 		frame = new MediaFrame.Post( attributes );
   151 	 * ========================================================================
    33 	} else if ( 'manage' === attributes.frame && MediaFrame.Manage ) {
   152 	 */
    34 		frame = new MediaFrame.Manage( attributes );
   153 
    35 	} else if ( 'image' === attributes.frame && MediaFrame.ImageDetails ) {
   154 	 /**
    36 		frame = new MediaFrame.ImageDetails( attributes );
   155 	  * wp.media.attachment
    37 	} else if ( 'audio' === attributes.frame && MediaFrame.AudioDetails ) {
   156 	  */
    38 		frame = new MediaFrame.AudioDetails( attributes );
   157 	 media.attachment = function( id ) {
    39 	} else if ( 'video' === attributes.frame && MediaFrame.VideoDetails ) {
   158 		return Attachment.get( id );
    40 		frame = new MediaFrame.VideoDetails( attributes );
   159 	 };
    41 	} else if ( 'edit-attachments' === attributes.frame && MediaFrame.EditAttachments ) {
   160 
    42 		frame = new MediaFrame.EditAttachments( attributes );
   161 	/**
    43 	}
   162 	 * wp.media.model.Attachment
    44 
   163 	 */
    45 	delete attributes.frame;
   164 	Attachment = media.model.Attachment = Backbone.Model.extend({
    46 
   165 		sync: function( method, model, options ) {
    47 	media.frame = frame;
   166 			// If the attachment does not yet have an `id`, return an instantly
    48 
   167 			// rejected promise. Otherwise, all of our requests will fail.
    49 	return frame;
   168 			if ( _.isUndefined( this.id ) )
    50 };
       
    51 
       
    52 _.extend( media, { model: {}, view: {}, controller: {}, frames: {} });
       
    53 
       
    54 // Link any localized strings.
       
    55 l10n = media.model.l10n = window._wpMediaModelsL10n || {};
       
    56 
       
    57 // Link any settings.
       
    58 media.model.settings = l10n.settings || {};
       
    59 delete l10n.settings;
       
    60 
       
    61 Attachment = media.model.Attachment = require( './models/attachment.js' );
       
    62 Attachments = media.model.Attachments = require( './models/attachments.js' );
       
    63 
       
    64 media.model.Query = require( './models/query.js' );
       
    65 media.model.PostImage = require( './models/post-image.js' );
       
    66 media.model.Selection = require( './models/selection.js' );
       
    67 
       
    68 /**
       
    69  * ========================================================================
       
    70  * UTILITIES
       
    71  * ========================================================================
       
    72  */
       
    73 
       
    74 /**
       
    75  * A basic equality comparator for Backbone models.
       
    76  *
       
    77  * Used to order models within a collection - @see wp.media.model.Attachments.comparator().
       
    78  *
       
    79  * @param  {mixed}  a  The primary parameter to compare.
       
    80  * @param  {mixed}  b  The primary parameter to compare.
       
    81  * @param  {string} ac The fallback parameter to compare, a's cid.
       
    82  * @param  {string} bc The fallback parameter to compare, b's cid.
       
    83  * @return {number}    -1: a should come before b.
       
    84  *                      0: a and b are of the same rank.
       
    85  *                      1: b should come before a.
       
    86  */
       
    87 media.compare = function( a, b, ac, bc ) {
       
    88 	if ( _.isEqual( a, b ) ) {
       
    89 		return ac === bc ? 0 : (ac > bc ? -1 : 1);
       
    90 	} else {
       
    91 		return a > b ? -1 : 1;
       
    92 	}
       
    93 };
       
    94 
       
    95 _.extend( media, {
       
    96 	/**
       
    97 	 * media.template( id )
       
    98 	 *
       
    99 	 * Fetch a JavaScript template for an id, and return a templating function for it.
       
   100 	 *
       
   101 	 * See wp.template() in `wp-includes/js/wp-util.js`.
       
   102 	 *
       
   103 	 * @borrows wp.template as template
       
   104 	 */
       
   105 	template: wp.template,
       
   106 
       
   107 	/**
       
   108 	 * media.post( [action], [data] )
       
   109 	 *
       
   110 	 * Sends a POST request to WordPress.
       
   111 	 * See wp.ajax.post() in `wp-includes/js/wp-util.js`.
       
   112 	 *
       
   113 	 * @borrows wp.ajax.post as post
       
   114 	 */
       
   115 	post: wp.ajax.post,
       
   116 
       
   117 	/**
       
   118 	 * media.ajax( [action], [options] )
       
   119 	 *
       
   120 	 * Sends an XHR request to WordPress.
       
   121 	 * See wp.ajax.send() in `wp-includes/js/wp-util.js`.
       
   122 	 *
       
   123 	 * @borrows wp.ajax.send as ajax
       
   124 	 */
       
   125 	ajax: wp.ajax.send,
       
   126 
       
   127 	/**
       
   128 	 * Scales a set of dimensions to fit within bounding dimensions.
       
   129 	 *
       
   130 	 * @param {Object} dimensions
       
   131 	 * @returns {Object}
       
   132 	 */
       
   133 	fit: function( dimensions ) {
       
   134 		var width     = dimensions.width,
       
   135 			height    = dimensions.height,
       
   136 			maxWidth  = dimensions.maxWidth,
       
   137 			maxHeight = dimensions.maxHeight,
       
   138 			constraint;
       
   139 
       
   140 		// Compare ratios between the two values to determine which
       
   141 		// max to constrain by. If a max value doesn't exist, then the
       
   142 		// opposite side is the constraint.
       
   143 		if ( ! _.isUndefined( maxWidth ) && ! _.isUndefined( maxHeight ) ) {
       
   144 			constraint = ( width / height > maxWidth / maxHeight ) ? 'width' : 'height';
       
   145 		} else if ( _.isUndefined( maxHeight ) ) {
       
   146 			constraint = 'width';
       
   147 		} else if (  _.isUndefined( maxWidth ) && height > maxHeight ) {
       
   148 			constraint = 'height';
       
   149 		}
       
   150 
       
   151 		// If the value of the constrained side is larger than the max,
       
   152 		// then scale the values. Otherwise return the originals; they fit.
       
   153 		if ( 'width' === constraint && width > maxWidth ) {
       
   154 			return {
       
   155 				width : maxWidth,
       
   156 				height: Math.round( maxWidth * height / width )
       
   157 			};
       
   158 		} else if ( 'height' === constraint && height > maxHeight ) {
       
   159 			return {
       
   160 				width : Math.round( maxHeight * width / height ),
       
   161 				height: maxHeight
       
   162 			};
       
   163 		} else {
       
   164 			return {
       
   165 				width : width,
       
   166 				height: height
       
   167 			};
       
   168 		}
       
   169 	},
       
   170 	/**
       
   171 	 * Truncates a string by injecting an ellipsis into the middle.
       
   172 	 * Useful for filenames.
       
   173 	 *
       
   174 	 * @param {String} string
       
   175 	 * @param {Number} [length=30]
       
   176 	 * @param {String} [replacement=&hellip;]
       
   177 	 * @returns {String} The string, unless length is greater than string.length.
       
   178 	 */
       
   179 	truncate: function( string, length, replacement ) {
       
   180 		length = length || 30;
       
   181 		replacement = replacement || '&hellip;';
       
   182 
       
   183 		if ( string.length <= length ) {
       
   184 			return string;
       
   185 		}
       
   186 
       
   187 		return string.substr( 0, length / 2 ) + replacement + string.substr( -1 * length / 2 );
       
   188 	}
       
   189 });
       
   190 
       
   191 /**
       
   192  * ========================================================================
       
   193  * MODELS
       
   194  * ========================================================================
       
   195  */
       
   196 /**
       
   197  * wp.media.attachment
       
   198  *
       
   199  * @static
       
   200  * @param {String} id A string used to identify a model.
       
   201  * @returns {wp.media.model.Attachment}
       
   202  */
       
   203 media.attachment = function( id ) {
       
   204 	return Attachment.get( id );
       
   205 };
       
   206 
       
   207 /**
       
   208  * A collection of all attachments that have been fetched from the server.
       
   209  *
       
   210  * @static
       
   211  * @member {wp.media.model.Attachments}
       
   212  */
       
   213 Attachments.all = new Attachments();
       
   214 
       
   215 /**
       
   216  * wp.media.query
       
   217  *
       
   218  * Shorthand for creating a new Attachments Query.
       
   219  *
       
   220  * @param {object} [props]
       
   221  * @returns {wp.media.model.Attachments}
       
   222  */
       
   223 media.query = function( props ) {
       
   224 	return new Attachments( null, {
       
   225 		props: _.extend( _.defaults( props || {}, { orderby: 'date' } ), { query: true } )
       
   226 	});
       
   227 };
       
   228 
       
   229 // Clean up. Prevents mobile browsers caching
       
   230 $(window).on('unload', function(){
       
   231 	window.wp = null;
       
   232 });
       
   233 
       
   234 },{"./models/attachment.js":2,"./models/attachments.js":3,"./models/post-image.js":4,"./models/query.js":5,"./models/selection.js":6}],2:[function(require,module,exports){
       
   235 /*globals wp, _, Backbone */
       
   236 
       
   237 /**
       
   238  * wp.media.model.Attachment
       
   239  *
       
   240  * @class
       
   241  * @augments Backbone.Model
       
   242  */
       
   243 var $ = Backbone.$,
       
   244 	Attachment;
       
   245 
       
   246 Attachment = Backbone.Model.extend({
       
   247 	/**
       
   248 	 * Triggered when attachment details change
       
   249 	 * Overrides Backbone.Model.sync
       
   250 	 *
       
   251 	 * @param {string} method
       
   252 	 * @param {wp.media.model.Attachment} model
       
   253 	 * @param {Object} [options={}]
       
   254 	 *
       
   255 	 * @returns {Promise}
       
   256 	 */
       
   257 	sync: function( method, model, options ) {
       
   258 		// If the attachment does not yet have an `id`, return an instantly
       
   259 		// rejected promise. Otherwise, all of our requests will fail.
       
   260 		if ( _.isUndefined( this.id ) ) {
       
   261 			return $.Deferred().rejectWith( this ).promise();
       
   262 		}
       
   263 
       
   264 		// Overload the `read` request so Attachment.fetch() functions correctly.
       
   265 		if ( 'read' === method ) {
       
   266 			options = options || {};
       
   267 			options.context = this;
       
   268 			options.data = _.extend( options.data || {}, {
       
   269 				action: 'get-attachment',
       
   270 				id: this.id
       
   271 			});
       
   272 			return wp.media.ajax( options );
       
   273 
       
   274 		// Overload the `update` request so properties can be saved.
       
   275 		} else if ( 'update' === method ) {
       
   276 			// If we do not have the necessary nonce, fail immeditately.
       
   277 			if ( ! this.get('nonces') || ! this.get('nonces').update ) {
   169 				return $.Deferred().rejectWith( this ).promise();
   278 				return $.Deferred().rejectWith( this ).promise();
   170 
   279 			}
   171 			// Overload the `read` request so Attachment.fetch() functions correctly.
   280 
   172 			if ( 'read' === method ) {
   281 			options = options || {};
   173 				options = options || {};
   282 			options.context = this;
   174 				options.context = this;
   283 
   175 				options.data = _.extend( options.data || {}, {
   284 			// Set the action and ID.
   176 					action: 'get-attachment',
   285 			options.data = _.extend( options.data || {}, {
   177 					id: this.id
   286 				action:  'save-attachment',
   178 				});
       
   179 				return media.ajax( options );
       
   180 
       
   181 			// Overload the `update` request so properties can be saved.
       
   182 			} else if ( 'update' === method ) {
       
   183 				// If we do not have the necessary nonce, fail immeditately.
       
   184 				if ( ! this.get('nonces') || ! this.get('nonces').update )
       
   185 					return $.Deferred().rejectWith( this ).promise();
       
   186 
       
   187 				options = options || {};
       
   188 				options.context = this;
       
   189 
       
   190 				// Set the action and ID.
       
   191 				options.data = _.extend( options.data || {}, {
       
   192 					action:  'save-attachment',
       
   193 					id:      this.id,
       
   194 					nonce:   this.get('nonces').update,
       
   195 					post_id: media.model.settings.post.id
       
   196 				});
       
   197 
       
   198 				// Record the values of the changed attributes.
       
   199 				if ( model.hasChanged() ) {
       
   200 					options.data.changes = {};
       
   201 
       
   202 					_.each( model.changed, function( value, key ) {
       
   203 						options.data.changes[ key ] = this.get( key );
       
   204 					}, this );
       
   205 				}
       
   206 
       
   207 				return media.ajax( options );
       
   208 
       
   209 			// Overload the `delete` request so attachments can be removed.
       
   210 			// This will permanently delete an attachment.
       
   211 			} else if ( 'delete' === method ) {
       
   212 				options = options || {};
       
   213 
       
   214 				if ( ! options.wait )
       
   215 					this.destroyed = true;
       
   216 
       
   217 				options.context = this;
       
   218 				options.data = _.extend( options.data || {}, {
       
   219 					action:   'delete-post',
       
   220 					id:       this.id,
       
   221 					_wpnonce: this.get('nonces')['delete']
       
   222 				});
       
   223 
       
   224 				return media.ajax( options ).done( function() {
       
   225 					this.destroyed = true;
       
   226 				}).fail( function() {
       
   227 					this.destroyed = false;
       
   228 				});
       
   229 
       
   230 			// Otherwise, fall back to `Backbone.sync()`.
       
   231 			} else {
       
   232 				return Backbone.Model.prototype.sync.apply( this, arguments );
       
   233 			}
       
   234 		},
       
   235 
       
   236 		parse: function( resp, xhr ) {
       
   237 			if ( ! resp )
       
   238 				return resp;
       
   239 
       
   240 			// Convert date strings into Date objects.
       
   241 			resp.date = new Date( resp.date );
       
   242 			resp.modified = new Date( resp.modified );
       
   243 			return resp;
       
   244 		},
       
   245 
       
   246 		saveCompat: function( data, options ) {
       
   247 			var model = this;
       
   248 
       
   249 			// If we do not have the necessary nonce, fail immeditately.
       
   250 			if ( ! this.get('nonces') || ! this.get('nonces').update )
       
   251 				return $.Deferred().rejectWith( this ).promise();
       
   252 
       
   253 			return media.post( 'save-attachment-compat', _.defaults({
       
   254 				id:      this.id,
   287 				id:      this.id,
   255 				nonce:   this.get('nonces').update,
   288 				nonce:   this.get('nonces').update,
   256 				post_id: media.model.settings.post.id
   289 				post_id: wp.media.model.settings.post.id
   257 			}, data ) ).done( function( resp, status, xhr ) {
       
   258 				model.set( model.parse( resp, xhr ), options );
       
   259 			});
   290 			});
   260 		}
   291 
   261 	}, {
   292 			// Record the values of the changed attributes.
   262 		create: function( attrs ) {
   293 			if ( model.hasChanged() ) {
   263 			return Attachments.all.push( attrs );
   294 				options.data.changes = {};
   264 		},
   295 
   265 
   296 				_.each( model.changed, function( value, key ) {
   266 		get: _.memoize( function( id, attachment ) {
   297 					options.data.changes[ key ] = this.get( key );
   267 			return Attachments.all.push( attachment || { id: id } );
   298 				}, this );
   268 		})
   299 			}
   269 	});
   300 
   270 
   301 			return wp.media.ajax( options );
   271 	/**
   302 
   272 	 * wp.media.model.Attachments
   303 		// Overload the `delete` request so attachments can be removed.
   273 	 */
   304 		// This will permanently delete an attachment.
   274 	Attachments = media.model.Attachments = Backbone.Collection.extend({
   305 		} else if ( 'delete' === method ) {
   275 		model: Attachment,
       
   276 
       
   277 		initialize: function( models, options ) {
       
   278 			options = options || {};
   306 			options = options || {};
   279 
   307 
   280 			this.props   = new Backbone.Model();
   308 			if ( ! options.wait ) {
   281 			this.filters = options.filters || {};
   309 				this.destroyed = true;
   282 
   310 			}
   283 			// Bind default `change` events to the `props` model.
   311 
   284 			this.props.on( 'change', this._changeFilteredProps, this );
   312 			options.context = this;
   285 
   313 			options.data = _.extend( options.data || {}, {
   286 			this.props.on( 'change:order',   this._changeOrder,   this );
   314 				action:   'delete-post',
   287 			this.props.on( 'change:orderby', this._changeOrderby, this );
   315 				id:       this.id,
   288 			this.props.on( 'change:query',   this._changeQuery,   this );
   316 				_wpnonce: this.get('nonces')['delete']
   289 
   317 			});
   290 			// Set the `props` model and fill the default property values.
   318 
   291 			this.props.set( _.defaults( options.props || {} ) );
   319 			return wp.media.ajax( options ).done( function() {
   292 
   320 				this.destroyed = true;
   293 			// Observe another `Attachments` collection if one is provided.
   321 			}).fail( function() {
   294 			if ( options.observe )
   322 				this.destroyed = false;
   295 				this.observe( options.observe );
   323 			});
   296 		},
   324 
   297 
   325 		// Otherwise, fall back to `Backbone.sync()`.
   298 		// Automatically sort the collection when the order changes.
   326 		} else {
   299 		_changeOrder: function( model, order ) {
   327 			/**
   300 			if ( this.comparator )
   328 			 * Call `sync` directly on Backbone.Model
   301 				this.sort();
   329 			 */
   302 		},
   330 			return Backbone.Model.prototype.sync.apply( this, arguments );
   303 
   331 		}
   304 		// Set the default comparator only when the `orderby` property is set.
   332 	},
   305 		_changeOrderby: function( model, orderby ) {
   333 	/**
   306 			// If a different comparator is defined, bail.
   334 	 * Convert date strings into Date objects.
   307 			if ( this.comparator && this.comparator !== Attachments.comparator )
   335 	 *
       
   336 	 * @param {Object} resp The raw response object, typically returned by fetch()
       
   337 	 * @returns {Object} The modified response object, which is the attributes hash
       
   338 	 *    to be set on the model.
       
   339 	 */
       
   340 	parse: function( resp ) {
       
   341 		if ( ! resp ) {
       
   342 			return resp;
       
   343 		}
       
   344 
       
   345 		resp.date = new Date( resp.date );
       
   346 		resp.modified = new Date( resp.modified );
       
   347 		return resp;
       
   348 	},
       
   349 	/**
       
   350 	 * @param {Object} data The properties to be saved.
       
   351 	 * @param {Object} options Sync options. e.g. patch, wait, success, error.
       
   352 	 *
       
   353 	 * @this Backbone.Model
       
   354 	 *
       
   355 	 * @returns {Promise}
       
   356 	 */
       
   357 	saveCompat: function( data, options ) {
       
   358 		var model = this;
       
   359 
       
   360 		// If we do not have the necessary nonce, fail immeditately.
       
   361 		if ( ! this.get('nonces') || ! this.get('nonces').update ) {
       
   362 			return $.Deferred().rejectWith( this ).promise();
       
   363 		}
       
   364 
       
   365 		return wp.media.post( 'save-attachment-compat', _.defaults({
       
   366 			id:      this.id,
       
   367 			nonce:   this.get('nonces').update,
       
   368 			post_id: wp.media.model.settings.post.id
       
   369 		}, data ) ).done( function( resp, status, xhr ) {
       
   370 			model.set( model.parse( resp, xhr ), options );
       
   371 		});
       
   372 	}
       
   373 }, {
       
   374 	/**
       
   375 	 * Create a new model on the static 'all' attachments collection and return it.
       
   376 	 *
       
   377 	 * @static
       
   378 	 * @param {Object} attrs
       
   379 	 * @returns {wp.media.model.Attachment}
       
   380 	 */
       
   381 	create: function( attrs ) {
       
   382 		var Attachments = wp.media.model.Attachments;
       
   383 		return Attachments.all.push( attrs );
       
   384 	},
       
   385 	/**
       
   386 	 * Create a new model on the static 'all' attachments collection and return it.
       
   387 	 *
       
   388 	 * If this function has already been called for the id,
       
   389 	 * it returns the specified attachment.
       
   390 	 *
       
   391 	 * @static
       
   392 	 * @param {string} id A string used to identify a model.
       
   393 	 * @param {Backbone.Model|undefined} attachment
       
   394 	 * @returns {wp.media.model.Attachment}
       
   395 	 */
       
   396 	get: _.memoize( function( id, attachment ) {
       
   397 		var Attachments = wp.media.model.Attachments;
       
   398 		return Attachments.all.push( attachment || { id: id } );
       
   399 	})
       
   400 });
       
   401 
       
   402 module.exports = Attachment;
       
   403 
       
   404 },{}],3:[function(require,module,exports){
       
   405 /*globals wp, _, Backbone */
       
   406 
       
   407 /**
       
   408  * wp.media.model.Attachments
       
   409  *
       
   410  * A collection of attachments.
       
   411  *
       
   412  * This collection has no persistence with the server without supplying
       
   413  * 'options.props.query = true', which will mirror the collection
       
   414  * to an Attachments Query collection - @see wp.media.model.Attachments.mirror().
       
   415  *
       
   416  * @class
       
   417  * @augments Backbone.Collection
       
   418  *
       
   419  * @param {array}  [models]                Models to initialize with the collection.
       
   420  * @param {object} [options]               Options hash for the collection.
       
   421  * @param {string} [options.props]         Options hash for the initial query properties.
       
   422  * @param {string} [options.props.order]   Initial order (ASC or DESC) for the collection.
       
   423  * @param {string} [options.props.orderby] Initial attribute key to order the collection by.
       
   424  * @param {string} [options.props.query]   Whether the collection is linked to an attachments query.
       
   425  * @param {string} [options.observe]
       
   426  * @param {string} [options.filters]
       
   427  *
       
   428  */
       
   429 var Attachments = Backbone.Collection.extend({
       
   430 	/**
       
   431 	 * @type {wp.media.model.Attachment}
       
   432 	 */
       
   433 	model: wp.media.model.Attachment,
       
   434 	/**
       
   435 	 * @param {Array} [models=[]] Array of models used to populate the collection.
       
   436 	 * @param {Object} [options={}]
       
   437 	 */
       
   438 	initialize: function( models, options ) {
       
   439 		options = options || {};
       
   440 
       
   441 		this.props   = new Backbone.Model();
       
   442 		this.filters = options.filters || {};
       
   443 
       
   444 		// Bind default `change` events to the `props` model.
       
   445 		this.props.on( 'change', this._changeFilteredProps, this );
       
   446 
       
   447 		this.props.on( 'change:order',   this._changeOrder,   this );
       
   448 		this.props.on( 'change:orderby', this._changeOrderby, this );
       
   449 		this.props.on( 'change:query',   this._changeQuery,   this );
       
   450 
       
   451 		this.props.set( _.defaults( options.props || {} ) );
       
   452 
       
   453 		if ( options.observe ) {
       
   454 			this.observe( options.observe );
       
   455 		}
       
   456 	},
       
   457 	/**
       
   458 	 * Sort the collection when the order attribute changes.
       
   459 	 *
       
   460 	 * @access private
       
   461 	 */
       
   462 	_changeOrder: function() {
       
   463 		if ( this.comparator ) {
       
   464 			this.sort();
       
   465 		}
       
   466 	},
       
   467 	/**
       
   468 	 * Set the default comparator only when the `orderby` property is set.
       
   469 	 *
       
   470 	 * @access private
       
   471 	 *
       
   472 	 * @param {Backbone.Model} model
       
   473 	 * @param {string} orderby
       
   474 	 */
       
   475 	_changeOrderby: function( model, orderby ) {
       
   476 		// If a different comparator is defined, bail.
       
   477 		if ( this.comparator && this.comparator !== Attachments.comparator ) {
       
   478 			return;
       
   479 		}
       
   480 
       
   481 		if ( orderby && 'post__in' !== orderby ) {
       
   482 			this.comparator = Attachments.comparator;
       
   483 		} else {
       
   484 			delete this.comparator;
       
   485 		}
       
   486 	},
       
   487 	/**
       
   488 	 * If the `query` property is set to true, query the server using
       
   489 	 * the `props` values, and sync the results to this collection.
       
   490 	 *
       
   491 	 * @access private
       
   492 	 *
       
   493 	 * @param {Backbone.Model} model
       
   494 	 * @param {Boolean} query
       
   495 	 */
       
   496 	_changeQuery: function( model, query ) {
       
   497 		if ( query ) {
       
   498 			this.props.on( 'change', this._requery, this );
       
   499 			this._requery();
       
   500 		} else {
       
   501 			this.props.off( 'change', this._requery, this );
       
   502 		}
       
   503 	},
       
   504 	/**
       
   505 	 * @access private
       
   506 	 *
       
   507 	 * @param {Backbone.Model} model
       
   508 	 */
       
   509 	_changeFilteredProps: function( model ) {
       
   510 		// If this is a query, updating the collection will be handled by
       
   511 		// `this._requery()`.
       
   512 		if ( this.props.get('query') ) {
       
   513 			return;
       
   514 		}
       
   515 
       
   516 		var changed = _.chain( model.changed ).map( function( t, prop ) {
       
   517 			var filter = Attachments.filters[ prop ],
       
   518 				term = model.get( prop );
       
   519 
       
   520 			if ( ! filter ) {
   308 				return;
   521 				return;
   309 
   522 			}
   310 			if ( orderby && 'post__in' !== orderby )
   523 
   311 				this.comparator = Attachments.comparator;
   524 			if ( term && ! this.filters[ prop ] ) {
   312 			else
   525 				this.filters[ prop ] = filter;
   313 				delete this.comparator;
   526 			} else if ( ! term && this.filters[ prop ] === filter ) {
   314 		},
   527 				delete this.filters[ prop ];
   315 
       
   316 		// If the `query` property is set to true, query the server using
       
   317 		// the `props` values, and sync the results to this collection.
       
   318 		_changeQuery: function( model, query ) {
       
   319 			if ( query ) {
       
   320 				this.props.on( 'change', this._requery, this );
       
   321 				this._requery();
       
   322 			} else {
   528 			} else {
   323 				this.props.off( 'change', this._requery, this );
       
   324 			}
       
   325 		},
       
   326 
       
   327 		_changeFilteredProps: function( model, options ) {
       
   328 			// If this is a query, updating the collection will be handled by
       
   329 			// `this._requery()`.
       
   330 			if ( this.props.get('query') )
       
   331 				return;
   529 				return;
   332 
   530 			}
   333 			var changed = _.chain( model.changed ).map( function( t, prop ) {
   531 
   334 				var filter = Attachments.filters[ prop ],
   532 			// Record the change.
   335 					term = model.get( prop );
   533 			return true;
   336 
   534 		}, this ).any().value();
   337 				if ( ! filter )
   535 
   338 					return;
   536 		if ( ! changed ) {
   339 
   537 			return;
   340 				if ( term && ! this.filters[ prop ] )
   538 		}
   341 					this.filters[ prop ] = filter;
   539 
   342 				else if ( ! term && this.filters[ prop ] === filter )
   540 		// If no `Attachments` model is provided to source the searches
   343 					delete this.filters[ prop ];
   541 		// from, then automatically generate a source from the existing
   344 				else
   542 		// models.
   345 					return;
   543 		if ( ! this._source ) {
   346 
   544 			this._source = new Attachments( this.models );
   347 				// Record the change.
   545 		}
       
   546 
       
   547 		this.reset( this._source.filter( this.validator, this ) );
       
   548 	},
       
   549 
       
   550 	validateDestroyed: false,
       
   551 	/**
       
   552 	 * Checks whether an attachment is valid.
       
   553 	 *
       
   554 	 * @param {wp.media.model.Attachment} attachment
       
   555 	 * @returns {Boolean}
       
   556 	 */
       
   557 	validator: function( attachment ) {
       
   558 		if ( ! this.validateDestroyed && attachment.destroyed ) {
       
   559 			return false;
       
   560 		}
       
   561 		return _.all( this.filters, function( filter ) {
       
   562 			return !! filter.call( this, attachment );
       
   563 		}, this );
       
   564 	},
       
   565 	/**
       
   566 	 * Add or remove an attachment to the collection depending on its validity.
       
   567 	 *
       
   568 	 * @param {wp.media.model.Attachment} attachment
       
   569 	 * @param {Object} options
       
   570 	 * @returns {wp.media.model.Attachments} Returns itself to allow chaining
       
   571 	 */
       
   572 	validate: function( attachment, options ) {
       
   573 		var valid = this.validator( attachment ),
       
   574 			hasAttachment = !! this.get( attachment.cid );
       
   575 
       
   576 		if ( ! valid && hasAttachment ) {
       
   577 			this.remove( attachment, options );
       
   578 		} else if ( valid && ! hasAttachment ) {
       
   579 			this.add( attachment, options );
       
   580 		}
       
   581 
       
   582 		return this;
       
   583 	},
       
   584 
       
   585 	/**
       
   586 	 * Add or remove all attachments from another collection depending on each one's validity.
       
   587 	 *
       
   588 	 * @param {wp.media.model.Attachments} attachments
       
   589 	 * @param {object} [options={}]
       
   590 	 *
       
   591 	 * @fires wp.media.model.Attachments#reset
       
   592 	 *
       
   593 	 * @returns {wp.media.model.Attachments} Returns itself to allow chaining
       
   594 	 */
       
   595 	validateAll: function( attachments, options ) {
       
   596 		options = options || {};
       
   597 
       
   598 		_.each( attachments.models, function( attachment ) {
       
   599 			this.validate( attachment, { silent: true });
       
   600 		}, this );
       
   601 
       
   602 		if ( ! options.silent ) {
       
   603 			this.trigger( 'reset', this, options );
       
   604 		}
       
   605 		return this;
       
   606 	},
       
   607 	/**
       
   608 	 * Start observing another attachments collection change events
       
   609 	 * and replicate them on this collection.
       
   610 	 *
       
   611 	 * @param {wp.media.model.Attachments} The attachments collection to observe.
       
   612 	 * @returns {wp.media.model.Attachments} Returns itself to allow chaining.
       
   613 	 */
       
   614 	observe: function( attachments ) {
       
   615 		this.observers = this.observers || [];
       
   616 		this.observers.push( attachments );
       
   617 
       
   618 		attachments.on( 'add change remove', this._validateHandler, this );
       
   619 		attachments.on( 'reset', this._validateAllHandler, this );
       
   620 		this.validateAll( attachments );
       
   621 		return this;
       
   622 	},
       
   623 	/**
       
   624 	 * Stop replicating collection change events from another attachments collection.
       
   625 	 *
       
   626 	 * @param {wp.media.model.Attachments} The attachments collection to stop observing.
       
   627 	 * @returns {wp.media.model.Attachments} Returns itself to allow chaining
       
   628 	 */
       
   629 	unobserve: function( attachments ) {
       
   630 		if ( attachments ) {
       
   631 			attachments.off( null, null, this );
       
   632 			this.observers = _.without( this.observers, attachments );
       
   633 
       
   634 		} else {
       
   635 			_.each( this.observers, function( attachments ) {
       
   636 				attachments.off( null, null, this );
       
   637 			}, this );
       
   638 			delete this.observers;
       
   639 		}
       
   640 
       
   641 		return this;
       
   642 	},
       
   643 	/**
       
   644 	 * @access private
       
   645 	 *
       
   646 	 * @param {wp.media.model.Attachments} attachment
       
   647 	 * @param {wp.media.model.Attachments} attachments
       
   648 	 * @param {Object} options
       
   649 	 *
       
   650 	 * @returns {wp.media.model.Attachments} Returns itself to allow chaining
       
   651 	 */
       
   652 	_validateHandler: function( attachment, attachments, options ) {
       
   653 		// If we're not mirroring this `attachments` collection,
       
   654 		// only retain the `silent` option.
       
   655 		options = attachments === this.mirroring ? options : {
       
   656 			silent: options && options.silent
       
   657 		};
       
   658 
       
   659 		return this.validate( attachment, options );
       
   660 	},
       
   661 	/**
       
   662 	 * @access private
       
   663 	 *
       
   664 	 * @param {wp.media.model.Attachments} attachments
       
   665 	 * @param {Object} options
       
   666 	 * @returns {wp.media.model.Attachments} Returns itself to allow chaining
       
   667 	 */
       
   668 	_validateAllHandler: function( attachments, options ) {
       
   669 		return this.validateAll( attachments, options );
       
   670 	},
       
   671 	/**
       
   672 	 * Start mirroring another attachments collection, clearing out any models already
       
   673 	 * in the collection.
       
   674 	 *
       
   675 	 * @param {wp.media.model.Attachments} The attachments collection to mirror.
       
   676 	 * @returns {wp.media.model.Attachments} Returns itself to allow chaining
       
   677 	 */
       
   678 	mirror: function( attachments ) {
       
   679 		if ( this.mirroring && this.mirroring === attachments ) {
       
   680 			return this;
       
   681 		}
       
   682 
       
   683 		this.unmirror();
       
   684 		this.mirroring = attachments;
       
   685 
       
   686 		// Clear the collection silently. A `reset` event will be fired
       
   687 		// when `observe()` calls `validateAll()`.
       
   688 		this.reset( [], { silent: true } );
       
   689 		this.observe( attachments );
       
   690 
       
   691 		return this;
       
   692 	},
       
   693 	/**
       
   694 	 * Stop mirroring another attachments collection.
       
   695 	 */
       
   696 	unmirror: function() {
       
   697 		if ( ! this.mirroring ) {
       
   698 			return;
       
   699 		}
       
   700 
       
   701 		this.unobserve( this.mirroring );
       
   702 		delete this.mirroring;
       
   703 	},
       
   704 	/**
       
   705 	 * Retrive more attachments from the server for the collection.
       
   706 	 *
       
   707 	 * Only works if the collection is mirroring a Query Attachments collection,
       
   708 	 * and forwards to its `more` method. This collection class doesn't have
       
   709 	 * server persistence by itself.
       
   710 	 *
       
   711 	 * @param {object} options
       
   712 	 * @returns {Promise}
       
   713 	 */
       
   714 	more: function( options ) {
       
   715 		var deferred = jQuery.Deferred(),
       
   716 			mirroring = this.mirroring,
       
   717 			attachments = this;
       
   718 
       
   719 		if ( ! mirroring || ! mirroring.more ) {
       
   720 			return deferred.resolveWith( this ).promise();
       
   721 		}
       
   722 		// If we're mirroring another collection, forward `more` to
       
   723 		// the mirrored collection. Account for a race condition by
       
   724 		// checking if we're still mirroring that collection when
       
   725 		// the request resolves.
       
   726 		mirroring.more( options ).done( function() {
       
   727 			if ( this === attachments.mirroring ) {
       
   728 				deferred.resolveWith( this );
       
   729 			}
       
   730 		});
       
   731 
       
   732 		return deferred.promise();
       
   733 	},
       
   734 	/**
       
   735 	 * Whether there are more attachments that haven't been sync'd from the server
       
   736 	 * that match the collection's query.
       
   737 	 *
       
   738 	 * Only works if the collection is mirroring a Query Attachments collection,
       
   739 	 * and forwards to its `hasMore` method. This collection class doesn't have
       
   740 	 * server persistence by itself.
       
   741 	 *
       
   742 	 * @returns {boolean}
       
   743 	 */
       
   744 	hasMore: function() {
       
   745 		return this.mirroring ? this.mirroring.hasMore() : false;
       
   746 	},
       
   747 	/**
       
   748 	 * A custom AJAX-response parser.
       
   749 	 *
       
   750 	 * See trac ticket #24753
       
   751 	 *
       
   752 	 * @param {Object|Array} resp The raw response Object/Array.
       
   753 	 * @param {Object} xhr
       
   754 	 * @returns {Array} The array of model attributes to be added to the collection
       
   755 	 */
       
   756 	parse: function( resp, xhr ) {
       
   757 		if ( ! _.isArray( resp ) ) {
       
   758 			resp = [resp];
       
   759 		}
       
   760 
       
   761 		return _.map( resp, function( attrs ) {
       
   762 			var id, attachment, newAttributes;
       
   763 
       
   764 			if ( attrs instanceof Backbone.Model ) {
       
   765 				id = attrs.get( 'id' );
       
   766 				attrs = attrs.attributes;
       
   767 			} else {
       
   768 				id = attrs.id;
       
   769 			}
       
   770 
       
   771 			attachment = wp.media.model.Attachment.get( id );
       
   772 			newAttributes = attachment.parse( attrs, xhr );
       
   773 
       
   774 			if ( ! _.isEqual( attachment.attributes, newAttributes ) ) {
       
   775 				attachment.set( newAttributes );
       
   776 			}
       
   777 
       
   778 			return attachment;
       
   779 		});
       
   780 	},
       
   781 	/**
       
   782 	 * If the collection is a query, create and mirror an Attachments Query collection.
       
   783 	 *
       
   784 	 * @access private
       
   785 	 */
       
   786 	_requery: function( refresh ) {
       
   787 		var props;
       
   788 		if ( this.props.get('query') ) {
       
   789 			props = this.props.toJSON();
       
   790 			props.cache = ( true !== refresh );
       
   791 			this.mirror( wp.media.model.Query.get( props ) );
       
   792 		}
       
   793 	},
       
   794 	/**
       
   795 	 * If this collection is sorted by `menuOrder`, recalculates and saves
       
   796 	 * the menu order to the database.
       
   797 	 *
       
   798 	 * @returns {undefined|Promise}
       
   799 	 */
       
   800 	saveMenuOrder: function() {
       
   801 		if ( 'menuOrder' !== this.props.get('orderby') ) {
       
   802 			return;
       
   803 		}
       
   804 
       
   805 		// Removes any uploading attachments, updates each attachment's
       
   806 		// menu order, and returns an object with an { id: menuOrder }
       
   807 		// mapping to pass to the request.
       
   808 		var attachments = this.chain().filter( function( attachment ) {
       
   809 			return ! _.isUndefined( attachment.id );
       
   810 		}).map( function( attachment, index ) {
       
   811 			// Indices start at 1.
       
   812 			index = index + 1;
       
   813 			attachment.set( 'menuOrder', index );
       
   814 			return [ attachment.id, index ];
       
   815 		}).object().value();
       
   816 
       
   817 		if ( _.isEmpty( attachments ) ) {
       
   818 			return;
       
   819 		}
       
   820 
       
   821 		return wp.media.post( 'save-attachment-order', {
       
   822 			nonce:       wp.media.model.settings.post.nonce,
       
   823 			post_id:     wp.media.model.settings.post.id,
       
   824 			attachments: attachments
       
   825 		});
       
   826 	}
       
   827 }, {
       
   828 	/**
       
   829 	 * A function to compare two attachment models in an attachments collection.
       
   830 	 *
       
   831 	 * Used as the default comparator for instances of wp.media.model.Attachments
       
   832 	 * and its subclasses. @see wp.media.model.Attachments._changeOrderby().
       
   833 	 *
       
   834 	 * @static
       
   835 	 *
       
   836 	 * @param {Backbone.Model} a
       
   837 	 * @param {Backbone.Model} b
       
   838 	 * @param {Object} options
       
   839 	 * @returns {Number} -1 if the first model should come before the second,
       
   840 	 *    0 if they are of the same rank and
       
   841 	 *    1 if the first model should come after.
       
   842 	 */
       
   843 	comparator: function( a, b, options ) {
       
   844 		var key   = this.props.get('orderby'),
       
   845 			order = this.props.get('order') || 'DESC',
       
   846 			ac    = a.cid,
       
   847 			bc    = b.cid;
       
   848 
       
   849 		a = a.get( key );
       
   850 		b = b.get( key );
       
   851 
       
   852 		if ( 'date' === key || 'modified' === key ) {
       
   853 			a = a || new Date();
       
   854 			b = b || new Date();
       
   855 		}
       
   856 
       
   857 		// If `options.ties` is set, don't enforce the `cid` tiebreaker.
       
   858 		if ( options && options.ties ) {
       
   859 			ac = bc = null;
       
   860 		}
       
   861 
       
   862 		return ( 'DESC' === order ) ? wp.media.compare( a, b, ac, bc ) : wp.media.compare( b, a, bc, ac );
       
   863 	},
       
   864 	/**
       
   865 	 * @namespace
       
   866 	 */
       
   867 	filters: {
       
   868 		/**
       
   869 		 * @static
       
   870 		 * Note that this client-side searching is *not* equivalent
       
   871 		 * to our server-side searching.
       
   872 		 *
       
   873 		 * @param {wp.media.model.Attachment} attachment
       
   874 		 *
       
   875 		 * @this wp.media.model.Attachments
       
   876 		 *
       
   877 		 * @returns {Boolean}
       
   878 		 */
       
   879 		search: function( attachment ) {
       
   880 			if ( ! this.props.get('search') ) {
   348 				return true;
   881 				return true;
   349 			}, this ).any().value();
   882 			}
   350 
   883 
   351 			if ( ! changed )
   884 			return _.any(['title','filename','description','caption','name'], function( key ) {
   352 				return;
   885 				var value = attachment.get( key );
   353 
   886 				return value && -1 !== value.search( this.props.get('search') );
   354 			// If no `Attachments` model is provided to source the searches
       
   355 			// from, then automatically generate a source from the existing
       
   356 			// models.
       
   357 			if ( ! this._source )
       
   358 				this._source = new Attachments( this.models );
       
   359 
       
   360 			this.reset( this._source.filter( this.validator, this ) );
       
   361 		},
       
   362 
       
   363 		validateDestroyed: false,
       
   364 
       
   365 		validator: function( attachment ) {
       
   366 			if ( ! this.validateDestroyed && attachment.destroyed )
       
   367 				return false;
       
   368 			return _.all( this.filters, function( filter, key ) {
       
   369 				return !! filter.call( this, attachment );
       
   370 			}, this );
   887 			}, this );
   371 		},
   888 		},
   372 
   889 		/**
   373 		validate: function( attachment, options ) {
   890 		 * @static
   374 			var valid = this.validator( attachment ),
   891 		 * @param {wp.media.model.Attachment} attachment
   375 				hasAttachment = !! this.get( attachment.cid );
   892 		 *
   376 
   893 		 * @this wp.media.model.Attachments
   377 			if ( ! valid && hasAttachment )
   894 		 *
   378 				this.remove( attachment, options );
   895 		 * @returns {Boolean}
   379 			else if ( valid && ! hasAttachment )
   896 		 */
   380 				this.add( attachment, options );
   897 		type: function( attachment ) {
   381 
   898 			var type = this.props.get('type');
   382 			return this;
   899 			return ! type || -1 !== type.indexOf( attachment.get('type') );
   383 		},
   900 		},
   384 
   901 		/**
   385 		validateAll: function( attachments, options ) {
   902 		 * @static
       
   903 		 * @param {wp.media.model.Attachment} attachment
       
   904 		 *
       
   905 		 * @this wp.media.model.Attachments
       
   906 		 *
       
   907 		 * @returns {Boolean}
       
   908 		 */
       
   909 		uploadedTo: function( attachment ) {
       
   910 			var uploadedTo = this.props.get('uploadedTo');
       
   911 			if ( _.isUndefined( uploadedTo ) ) {
       
   912 				return true;
       
   913 			}
       
   914 
       
   915 			return uploadedTo === attachment.get('uploadedTo');
       
   916 		},
       
   917 		/**
       
   918 		 * @static
       
   919 		 * @param {wp.media.model.Attachment} attachment
       
   920 		 *
       
   921 		 * @this wp.media.model.Attachments
       
   922 		 *
       
   923 		 * @returns {Boolean}
       
   924 		 */
       
   925 		status: function( attachment ) {
       
   926 			var status = this.props.get('status');
       
   927 			if ( _.isUndefined( status ) ) {
       
   928 				return true;
       
   929 			}
       
   930 
       
   931 			return status === attachment.get('status');
       
   932 		}
       
   933 	}
       
   934 });
       
   935 
       
   936 module.exports = Attachments;
       
   937 
       
   938 },{}],4:[function(require,module,exports){
       
   939 /*globals Backbone */
       
   940 
       
   941 /**
       
   942  * wp.media.model.PostImage
       
   943  *
       
   944  * An instance of an image that's been embedded into a post.
       
   945  *
       
   946  * Used in the embedded image attachment display settings modal - @see wp.media.view.MediaFrame.ImageDetails.
       
   947  *
       
   948  * @class
       
   949  * @augments Backbone.Model
       
   950  *
       
   951  * @param {int} [attributes]               Initial model attributes.
       
   952  * @param {int} [attributes.attachment_id] ID of the attachment.
       
   953  **/
       
   954 var PostImage = Backbone.Model.extend({
       
   955 
       
   956 	initialize: function( attributes ) {
       
   957 		var Attachment = wp.media.model.Attachment;
       
   958 		this.attachment = false;
       
   959 
       
   960 		if ( attributes.attachment_id ) {
       
   961 			this.attachment = Attachment.get( attributes.attachment_id );
       
   962 			if ( this.attachment.get( 'url' ) ) {
       
   963 				this.dfd = jQuery.Deferred();
       
   964 				this.dfd.resolve();
       
   965 			} else {
       
   966 				this.dfd = this.attachment.fetch();
       
   967 			}
       
   968 			this.bindAttachmentListeners();
       
   969 		}
       
   970 
       
   971 		// keep url in sync with changes to the type of link
       
   972 		this.on( 'change:link', this.updateLinkUrl, this );
       
   973 		this.on( 'change:size', this.updateSize, this );
       
   974 
       
   975 		this.setLinkTypeFromUrl();
       
   976 		this.setAspectRatio();
       
   977 
       
   978 		this.set( 'originalUrl', attributes.url );
       
   979 	},
       
   980 
       
   981 	bindAttachmentListeners: function() {
       
   982 		this.listenTo( this.attachment, 'sync', this.setLinkTypeFromUrl );
       
   983 		this.listenTo( this.attachment, 'sync', this.setAspectRatio );
       
   984 		this.listenTo( this.attachment, 'change', this.updateSize );
       
   985 	},
       
   986 
       
   987 	changeAttachment: function( attachment, props ) {
       
   988 		this.stopListening( this.attachment );
       
   989 		this.attachment = attachment;
       
   990 		this.bindAttachmentListeners();
       
   991 
       
   992 		this.set( 'attachment_id', this.attachment.get( 'id' ) );
       
   993 		this.set( 'caption', this.attachment.get( 'caption' ) );
       
   994 		this.set( 'alt', this.attachment.get( 'alt' ) );
       
   995 		this.set( 'size', props.get( 'size' ) );
       
   996 		this.set( 'align', props.get( 'align' ) );
       
   997 		this.set( 'link', props.get( 'link' ) );
       
   998 		this.updateLinkUrl();
       
   999 		this.updateSize();
       
  1000 	},
       
  1001 
       
  1002 	setLinkTypeFromUrl: function() {
       
  1003 		var linkUrl = this.get( 'linkUrl' ),
       
  1004 			type;
       
  1005 
       
  1006 		if ( ! linkUrl ) {
       
  1007 			this.set( 'link', 'none' );
       
  1008 			return;
       
  1009 		}
       
  1010 
       
  1011 		// default to custom if there is a linkUrl
       
  1012 		type = 'custom';
       
  1013 
       
  1014 		if ( this.attachment ) {
       
  1015 			if ( this.attachment.get( 'url' ) === linkUrl ) {
       
  1016 				type = 'file';
       
  1017 			} else if ( this.attachment.get( 'link' ) === linkUrl ) {
       
  1018 				type = 'post';
       
  1019 			}
       
  1020 		} else {
       
  1021 			if ( this.get( 'url' ) === linkUrl ) {
       
  1022 				type = 'file';
       
  1023 			}
       
  1024 		}
       
  1025 
       
  1026 		this.set( 'link', type );
       
  1027 	},
       
  1028 
       
  1029 	updateLinkUrl: function() {
       
  1030 		var link = this.get( 'link' ),
       
  1031 			url;
       
  1032 
       
  1033 		switch( link ) {
       
  1034 			case 'file':
       
  1035 				if ( this.attachment ) {
       
  1036 					url = this.attachment.get( 'url' );
       
  1037 				} else {
       
  1038 					url = this.get( 'url' );
       
  1039 				}
       
  1040 				this.set( 'linkUrl', url );
       
  1041 				break;
       
  1042 			case 'post':
       
  1043 				this.set( 'linkUrl', this.attachment.get( 'link' ) );
       
  1044 				break;
       
  1045 			case 'none':
       
  1046 				this.set( 'linkUrl', '' );
       
  1047 				break;
       
  1048 		}
       
  1049 	},
       
  1050 
       
  1051 	updateSize: function() {
       
  1052 		var size;
       
  1053 
       
  1054 		if ( ! this.attachment ) {
       
  1055 			return;
       
  1056 		}
       
  1057 
       
  1058 		if ( this.get( 'size' ) === 'custom' ) {
       
  1059 			this.set( 'width', this.get( 'customWidth' ) );
       
  1060 			this.set( 'height', this.get( 'customHeight' ) );
       
  1061 			this.set( 'url', this.get( 'originalUrl' ) );
       
  1062 			return;
       
  1063 		}
       
  1064 
       
  1065 		size = this.attachment.get( 'sizes' )[ this.get( 'size' ) ];
       
  1066 
       
  1067 		if ( ! size ) {
       
  1068 			return;
       
  1069 		}
       
  1070 
       
  1071 		this.set( 'url', size.url );
       
  1072 		this.set( 'width', size.width );
       
  1073 		this.set( 'height', size.height );
       
  1074 	},
       
  1075 
       
  1076 	setAspectRatio: function() {
       
  1077 		var full;
       
  1078 
       
  1079 		if ( this.attachment && this.attachment.get( 'sizes' ) ) {
       
  1080 			full = this.attachment.get( 'sizes' ).full;
       
  1081 
       
  1082 			if ( full ) {
       
  1083 				this.set( 'aspectRatio', full.width / full.height );
       
  1084 				return;
       
  1085 			}
       
  1086 		}
       
  1087 
       
  1088 		this.set( 'aspectRatio', this.get( 'customWidth' ) / this.get( 'customHeight' ) );
       
  1089 	}
       
  1090 });
       
  1091 
       
  1092 module.exports = PostImage;
       
  1093 
       
  1094 },{}],5:[function(require,module,exports){
       
  1095 /*globals wp, _ */
       
  1096 
       
  1097 /**
       
  1098  * wp.media.model.Query
       
  1099  *
       
  1100  * A collection of attachments that match the supplied query arguments.
       
  1101  *
       
  1102  * Note: Do NOT change this.args after the query has been initialized.
       
  1103  *       Things will break.
       
  1104  *
       
  1105  * @class
       
  1106  * @augments wp.media.model.Attachments
       
  1107  * @augments Backbone.Collection
       
  1108  *
       
  1109  * @param {array}  [models]                      Models to initialize with the collection.
       
  1110  * @param {object} [options]                     Options hash.
       
  1111  * @param {object} [options.args]                Attachments query arguments.
       
  1112  * @param {object} [options.args.posts_per_page]
       
  1113  */
       
  1114 var Attachments = wp.media.model.Attachments,
       
  1115 	Query;
       
  1116 
       
  1117 Query = Attachments.extend({
       
  1118 	/**
       
  1119 	 * @global wp.Uploader
       
  1120 	 *
       
  1121 	 * @param {array}  [models=[]]  Array of initial models to populate the collection.
       
  1122 	 * @param {object} [options={}]
       
  1123 	 */
       
  1124 	initialize: function( models, options ) {
       
  1125 		var allowed;
       
  1126 
       
  1127 		options = options || {};
       
  1128 		Attachments.prototype.initialize.apply( this, arguments );
       
  1129 
       
  1130 		this.args     = options.args;
       
  1131 		this._hasMore = true;
       
  1132 		this.created  = new Date();
       
  1133 
       
  1134 		this.filters.order = function( attachment ) {
       
  1135 			var orderby = this.props.get('orderby'),
       
  1136 				order = this.props.get('order');
       
  1137 
       
  1138 			if ( ! this.comparator ) {
       
  1139 				return true;
       
  1140 			}
       
  1141 
       
  1142 			// We want any items that can be placed before the last
       
  1143 			// item in the set. If we add any items after the last
       
  1144 			// item, then we can't guarantee the set is complete.
       
  1145 			if ( this.length ) {
       
  1146 				return 1 !== this.comparator( attachment, this.last(), { ties: true });
       
  1147 
       
  1148 			// Handle the case where there are no items yet and
       
  1149 			// we're sorting for recent items. In that case, we want
       
  1150 			// changes that occurred after we created the query.
       
  1151 			} else if ( 'DESC' === order && ( 'date' === orderby || 'modified' === orderby ) ) {
       
  1152 				return attachment.get( orderby ) >= this.created;
       
  1153 
       
  1154 			// If we're sorting by menu order and we have no items,
       
  1155 			// accept any items that have the default menu order (0).
       
  1156 			} else if ( 'ASC' === order && 'menuOrder' === orderby ) {
       
  1157 				return attachment.get( orderby ) === 0;
       
  1158 			}
       
  1159 
       
  1160 			// Otherwise, we don't want any items yet.
       
  1161 			return false;
       
  1162 		};
       
  1163 
       
  1164 		// Observe the central `wp.Uploader.queue` collection to watch for
       
  1165 		// new matches for the query.
       
  1166 		//
       
  1167 		// Only observe when a limited number of query args are set. There
       
  1168 		// are no filters for other properties, so observing will result in
       
  1169 		// false positives in those queries.
       
  1170 		allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type', 'post_parent' ];
       
  1171 		if ( wp.Uploader && _( this.args ).chain().keys().difference( allowed ).isEmpty().value() ) {
       
  1172 			this.observe( wp.Uploader.queue );
       
  1173 		}
       
  1174 	},
       
  1175 	/**
       
  1176 	 * Whether there are more attachments that haven't been sync'd from the server
       
  1177 	 * that match the collection's query.
       
  1178 	 *
       
  1179 	 * @returns {boolean}
       
  1180 	 */
       
  1181 	hasMore: function() {
       
  1182 		return this._hasMore;
       
  1183 	},
       
  1184 	/**
       
  1185 	 * Fetch more attachments from the server for the collection.
       
  1186 	 *
       
  1187 	 * @param   {object}  [options={}]
       
  1188 	 * @returns {Promise}
       
  1189 	 */
       
  1190 	more: function( options ) {
       
  1191 		var query = this;
       
  1192 
       
  1193 		// If there is already a request pending, return early with the Deferred object.
       
  1194 		if ( this._more && 'pending' === this._more.state() ) {
       
  1195 			return this._more;
       
  1196 		}
       
  1197 
       
  1198 		if ( ! this.hasMore() ) {
       
  1199 			return jQuery.Deferred().resolveWith( this ).promise();
       
  1200 		}
       
  1201 
       
  1202 		options = options || {};
       
  1203 		options.remove = false;
       
  1204 
       
  1205 		return this._more = this.fetch( options ).done( function( resp ) {
       
  1206 			if ( _.isEmpty( resp ) || -1 === this.args.posts_per_page || resp.length < this.args.posts_per_page ) {
       
  1207 				query._hasMore = false;
       
  1208 			}
       
  1209 		});
       
  1210 	},
       
  1211 	/**
       
  1212 	 * Overrides Backbone.Collection.sync
       
  1213 	 * Overrides wp.media.model.Attachments.sync
       
  1214 	 *
       
  1215 	 * @param {String} method
       
  1216 	 * @param {Backbone.Model} model
       
  1217 	 * @param {Object} [options={}]
       
  1218 	 * @returns {Promise}
       
  1219 	 */
       
  1220 	sync: function( method, model, options ) {
       
  1221 		var args, fallback;
       
  1222 
       
  1223 		// Overload the read method so Attachment.fetch() functions correctly.
       
  1224 		if ( 'read' === method ) {
   386 			options = options || {};
  1225 			options = options || {};
   387 
  1226 			options.context = this;
   388 			_.each( attachments.models, function( attachment ) {
  1227 			options.data = _.extend( options.data || {}, {
   389 				this.validate( attachment, { silent: true });
  1228 				action:  'query-attachments',
   390 			}, this );
  1229 				post_id: wp.media.model.settings.post.id
   391 
       
   392 			if ( ! options.silent )
       
   393 				this.trigger( 'reset', this, options );
       
   394 
       
   395 			return this;
       
   396 		},
       
   397 
       
   398 		observe: function( attachments ) {
       
   399 			this.observers = this.observers || [];
       
   400 			this.observers.push( attachments );
       
   401 
       
   402 			attachments.on( 'add change remove', this._validateHandler, this );
       
   403 			attachments.on( 'reset', this._validateAllHandler, this );
       
   404 			this.validateAll( attachments );
       
   405 			return this;
       
   406 		},
       
   407 
       
   408 		unobserve: function( attachments ) {
       
   409 			if ( attachments ) {
       
   410 				attachments.off( null, null, this );
       
   411 				this.observers = _.without( this.observers, attachments );
       
   412 
       
   413 			} else {
       
   414 				_.each( this.observers, function( attachments ) {
       
   415 					attachments.off( null, null, this );
       
   416 				}, this );
       
   417 				delete this.observers;
       
   418 			}
       
   419 
       
   420 			return this;
       
   421 		},
       
   422 
       
   423 		_validateHandler: function( attachment, attachments, options ) {
       
   424 			// If we're not mirroring this `attachments` collection,
       
   425 			// only retain the `silent` option.
       
   426 			options = attachments === this.mirroring ? options : {
       
   427 				silent: options && options.silent
       
   428 			};
       
   429 
       
   430 			return this.validate( attachment, options );
       
   431 		},
       
   432 
       
   433 		_validateAllHandler: function( attachments, options ) {
       
   434 			return this.validateAll( attachments, options );
       
   435 		},
       
   436 
       
   437 		mirror: function( attachments ) {
       
   438 			if ( this.mirroring && this.mirroring === attachments )
       
   439 				return this;
       
   440 
       
   441 			this.unmirror();
       
   442 			this.mirroring = attachments;
       
   443 
       
   444 			// Clear the collection silently. A `reset` event will be fired
       
   445 			// when `observe()` calls `validateAll()`.
       
   446 			this.reset( [], { silent: true } );
       
   447 			this.observe( attachments );
       
   448 
       
   449 			return this;
       
   450 		},
       
   451 
       
   452 		unmirror: function() {
       
   453 			if ( ! this.mirroring )
       
   454 				return;
       
   455 
       
   456 			this.unobserve( this.mirroring );
       
   457 			delete this.mirroring;
       
   458 		},
       
   459 
       
   460 		more: function( options ) {
       
   461 			var deferred = $.Deferred(),
       
   462 				mirroring = this.mirroring,
       
   463 				attachments = this;
       
   464 
       
   465 			if ( ! mirroring || ! mirroring.more )
       
   466 				return deferred.resolveWith( this ).promise();
       
   467 
       
   468 			// If we're mirroring another collection, forward `more` to
       
   469 			// the mirrored collection. Account for a race condition by
       
   470 			// checking if we're still mirroring that collection when
       
   471 			// the request resolves.
       
   472 			mirroring.more( options ).done( function() {
       
   473 				if ( this === attachments.mirroring )
       
   474 					deferred.resolveWith( this );
       
   475 			});
  1230 			});
   476 
  1231 
   477 			return deferred.promise();
  1232 			// Clone the args so manipulation is non-destructive.
   478 		},
  1233 			args = _.clone( this.args );
   479 
  1234 
   480 		hasMore: function() {
  1235 			// Determine which page to query.
   481 			return this.mirroring ? this.mirroring.hasMore() : false;
  1236 			if ( -1 !== args.posts_per_page ) {
   482 		},
  1237 				args.paged = Math.round( this.length / args.posts_per_page ) + 1;
   483 
  1238 			}
   484 		parse: function( resp, xhr ) {
  1239 
   485 			if ( ! _.isArray( resp ) )
  1240 			options.data.query = args;
   486 				resp = [resp];
  1241 			return wp.media.ajax( options );
   487 
  1242 
   488 			return _.map( resp, function( attrs ) {
  1243 		// Otherwise, fall back to Backbone.sync()
   489 				var id, attachment, newAttributes;
  1244 		} else {
   490 
  1245 			/**
   491 				if ( attrs instanceof Backbone.Model ) {
  1246 			 * Call wp.media.model.Attachments.sync or Backbone.sync
   492 					id = attrs.get( 'id' );
  1247 			 */
   493 					attrs = attrs.attributes;
  1248 			fallback = Attachments.prototype.sync ? Attachments.prototype : Backbone;
   494 				} else {
  1249 			return fallback.sync.apply( this, arguments );
   495 					id = attrs.id;
  1250 		}
       
  1251 	}
       
  1252 }, {
       
  1253 	/**
       
  1254 	 * @readonly
       
  1255 	 */
       
  1256 	defaultProps: {
       
  1257 		orderby: 'date',
       
  1258 		order:   'DESC'
       
  1259 	},
       
  1260 	/**
       
  1261 	 * @readonly
       
  1262 	 */
       
  1263 	defaultArgs: {
       
  1264 		posts_per_page: 40
       
  1265 	},
       
  1266 	/**
       
  1267 	 * @readonly
       
  1268 	 */
       
  1269 	orderby: {
       
  1270 		allowed:  [ 'name', 'author', 'date', 'title', 'modified', 'uploadedTo', 'id', 'post__in', 'menuOrder' ],
       
  1271 		/**
       
  1272 		 * A map of JavaScript orderby values to their WP_Query equivalents.
       
  1273 		 * @type {Object}
       
  1274 		 */
       
  1275 		valuemap: {
       
  1276 			'id':         'ID',
       
  1277 			'uploadedTo': 'parent',
       
  1278 			'menuOrder':  'menu_order ID'
       
  1279 		}
       
  1280 	},
       
  1281 	/**
       
  1282 	 * A map of JavaScript query properties to their WP_Query equivalents.
       
  1283 	 *
       
  1284 	 * @readonly
       
  1285 	 */
       
  1286 	propmap: {
       
  1287 		'search':    's',
       
  1288 		'type':      'post_mime_type',
       
  1289 		'perPage':   'posts_per_page',
       
  1290 		'menuOrder': 'menu_order',
       
  1291 		'uploadedTo': 'post_parent',
       
  1292 		'status':     'post_status',
       
  1293 		'include':    'post__in',
       
  1294 		'exclude':    'post__not_in'
       
  1295 	},
       
  1296 	/**
       
  1297 	 * Creates and returns an Attachments Query collection given the properties.
       
  1298 	 *
       
  1299 	 * Caches query objects and reuses where possible.
       
  1300 	 *
       
  1301 	 * @static
       
  1302 	 * @method
       
  1303 	 *
       
  1304 	 * @param {object} [props]
       
  1305 	 * @param {Object} [props.cache=true]   Whether to use the query cache or not.
       
  1306 	 * @param {Object} [props.order]
       
  1307 	 * @param {Object} [props.orderby]
       
  1308 	 * @param {Object} [props.include]
       
  1309 	 * @param {Object} [props.exclude]
       
  1310 	 * @param {Object} [props.s]
       
  1311 	 * @param {Object} [props.post_mime_type]
       
  1312 	 * @param {Object} [props.posts_per_page]
       
  1313 	 * @param {Object} [props.menu_order]
       
  1314 	 * @param {Object} [props.post_parent]
       
  1315 	 * @param {Object} [props.post_status]
       
  1316 	 * @param {Object} [options]
       
  1317 	 *
       
  1318 	 * @returns {wp.media.model.Query} A new Attachments Query collection.
       
  1319 	 */
       
  1320 	get: (function(){
       
  1321 		/**
       
  1322 		 * @static
       
  1323 		 * @type Array
       
  1324 		 */
       
  1325 		var queries = [];
       
  1326 
       
  1327 		/**
       
  1328 		 * @returns {Query}
       
  1329 		 */
       
  1330 		return function( props, options ) {
       
  1331 			var args     = {},
       
  1332 				orderby  = Query.orderby,
       
  1333 				defaults = Query.defaultProps,
       
  1334 				query,
       
  1335 				cache    = !! props.cache || _.isUndefined( props.cache );
       
  1336 
       
  1337 			// Remove the `query` property. This isn't linked to a query,
       
  1338 			// this *is* the query.
       
  1339 			delete props.query;
       
  1340 			delete props.cache;
       
  1341 
       
  1342 			// Fill default args.
       
  1343 			_.defaults( props, defaults );
       
  1344 
       
  1345 			// Normalize the order.
       
  1346 			props.order = props.order.toUpperCase();
       
  1347 			if ( 'DESC' !== props.order && 'ASC' !== props.order ) {
       
  1348 				props.order = defaults.order.toUpperCase();
       
  1349 			}
       
  1350 
       
  1351 			// Ensure we have a valid orderby value.
       
  1352 			if ( ! _.contains( orderby.allowed, props.orderby ) ) {
       
  1353 				props.orderby = defaults.orderby;
       
  1354 			}
       
  1355 
       
  1356 			_.each( [ 'include', 'exclude' ], function( prop ) {
       
  1357 				if ( props[ prop ] && ! _.isArray( props[ prop ] ) ) {
       
  1358 					props[ prop ] = [ props[ prop ] ];
   496 				}
  1359 				}
   497 
  1360 			} );
   498 				attachment = Attachment.get( id );
  1361 
   499 				newAttributes = attachment.parse( attrs, xhr );
  1362 			// Generate the query `args` object.
   500 
  1363 			// Correct any differing property names.
   501 				if ( ! _.isEqual( attachment.attributes, newAttributes ) )
  1364 			_.each( props, function( value, prop ) {
   502 					attachment.set( newAttributes );
  1365 				if ( _.isNull( value ) ) {
   503 
  1366 					return;
   504 				return attachment;
  1367 				}
       
  1368 
       
  1369 				args[ Query.propmap[ prop ] || prop ] = value;
   505 			});
  1370 			});
   506 		},
  1371 
   507 
  1372 			// Fill any other default query args.
   508 		_requery: function() {
  1373 			_.defaults( args, Query.defaultArgs );
   509 			if ( this.props.get('query') )
  1374 
   510 				this.mirror( Query.get( this.props.toJSON() ) );
  1375 			// `props.orderby` does not always map directly to `args.orderby`.
   511 		},
  1376 			// Substitute exceptions specified in orderby.keymap.
   512 
  1377 			args.orderby = orderby.valuemap[ props.orderby ] || props.orderby;
   513 		// If this collection is sorted by `menuOrder`, recalculates and saves
  1378 
   514 		// the menu order to the database.
  1379 			// Search the query cache for a matching query.
   515 		saveMenuOrder: function() {
  1380 			if ( cache ) {
   516 			if ( 'menuOrder' !== this.props.get('orderby') )
       
   517 				return;
       
   518 
       
   519 			// Removes any uploading attachments, updates each attachment's
       
   520 			// menu order, and returns an object with an { id: menuOrder }
       
   521 			// mapping to pass to the request.
       
   522 			var attachments = this.chain().filter( function( attachment ) {
       
   523 				return ! _.isUndefined( attachment.id );
       
   524 			}).map( function( attachment, index ) {
       
   525 				// Indices start at 1.
       
   526 				index = index + 1;
       
   527 				attachment.set( 'menuOrder', index );
       
   528 				return [ attachment.id, index ];
       
   529 			}).object().value();
       
   530 
       
   531 			if ( _.isEmpty( attachments ) )
       
   532 				return;
       
   533 
       
   534 			return media.post( 'save-attachment-order', {
       
   535 				nonce:       media.model.settings.post.nonce,
       
   536 				post_id:     media.model.settings.post.id,
       
   537 				attachments: attachments
       
   538 			});
       
   539 		}
       
   540 	}, {
       
   541 		comparator: function( a, b, options ) {
       
   542 			var key   = this.props.get('orderby'),
       
   543 				order = this.props.get('order') || 'DESC',
       
   544 				ac    = a.cid,
       
   545 				bc    = b.cid;
       
   546 
       
   547 			a = a.get( key );
       
   548 			b = b.get( key );
       
   549 
       
   550 			if ( 'date' === key || 'modified' === key ) {
       
   551 				a = a || new Date();
       
   552 				b = b || new Date();
       
   553 			}
       
   554 
       
   555 			// If `options.ties` is set, don't enforce the `cid` tiebreaker.
       
   556 			if ( options && options.ties )
       
   557 				ac = bc = null;
       
   558 
       
   559 			return ( 'DESC' === order ) ? compare( a, b, ac, bc ) : compare( b, a, bc, ac );
       
   560 		},
       
   561 
       
   562 		filters: {
       
   563 			// Note that this client-side searching is *not* equivalent
       
   564 			// to our server-side searching.
       
   565 			search: function( attachment ) {
       
   566 				if ( ! this.props.get('search') )
       
   567 					return true;
       
   568 
       
   569 				return _.any(['title','filename','description','caption','name'], function( key ) {
       
   570 					var value = attachment.get( key );
       
   571 					return value && -1 !== value.search( this.props.get('search') );
       
   572 				}, this );
       
   573 			},
       
   574 
       
   575 			type: function( attachment ) {
       
   576 				var type = this.props.get('type');
       
   577 				return ! type || -1 !== type.indexOf( attachment.get('type') );
       
   578 			},
       
   579 
       
   580 			uploadedTo: function( attachment ) {
       
   581 				var uploadedTo = this.props.get('uploadedTo');
       
   582 				if ( _.isUndefined( uploadedTo ) )
       
   583 					return true;
       
   584 
       
   585 				return uploadedTo === attachment.get('uploadedTo');
       
   586 			}
       
   587 		}
       
   588 	});
       
   589 
       
   590 	Attachments.all = new Attachments();
       
   591 
       
   592 	/**
       
   593 	 * wp.media.query
       
   594 	 */
       
   595 	media.query = function( props ) {
       
   596 		return new Attachments( null, {
       
   597 			props: _.extend( _.defaults( props || {}, { orderby: 'date' } ), { query: true } )
       
   598 		});
       
   599 	};
       
   600 
       
   601 	/**
       
   602 	 * wp.media.model.Query
       
   603 	 *
       
   604 	 * A set of attachments that corresponds to a set of consecutively paged
       
   605 	 * queries on the server.
       
   606 	 *
       
   607 	 * Note: Do NOT change this.args after the query has been initialized.
       
   608 	 *       Things will break.
       
   609 	 */
       
   610 	Query = media.model.Query = Attachments.extend({
       
   611 		initialize: function( models, options ) {
       
   612 			var allowed;
       
   613 
       
   614 			options = options || {};
       
   615 			Attachments.prototype.initialize.apply( this, arguments );
       
   616 
       
   617 			this.args     = options.args;
       
   618 			this._hasMore = true;
       
   619 			this.created  = new Date();
       
   620 
       
   621 			this.filters.order = function( attachment ) {
       
   622 				var orderby = this.props.get('orderby'),
       
   623 					order = this.props.get('order');
       
   624 
       
   625 				if ( ! this.comparator )
       
   626 					return true;
       
   627 
       
   628 				// We want any items that can be placed before the last
       
   629 				// item in the set. If we add any items after the last
       
   630 				// item, then we can't guarantee the set is complete.
       
   631 				if ( this.length ) {
       
   632 					return 1 !== this.comparator( attachment, this.last(), { ties: true });
       
   633 
       
   634 				// Handle the case where there are no items yet and
       
   635 				// we're sorting for recent items. In that case, we want
       
   636 				// changes that occurred after we created the query.
       
   637 				} else if ( 'DESC' === order && ( 'date' === orderby || 'modified' === orderby ) ) {
       
   638 					return attachment.get( orderby ) >= this.created;
       
   639 
       
   640 				// If we're sorting by menu order and we have no items,
       
   641 				// accept any items that have the default menu order (0).
       
   642 				} else if ( 'ASC' === order && 'menuOrder' === orderby ) {
       
   643 					return attachment.get( orderby ) === 0;
       
   644 				}
       
   645 
       
   646 				// Otherwise, we don't want any items yet.
       
   647 				return false;
       
   648 			};
       
   649 
       
   650 			// Observe the central `wp.Uploader.queue` collection to watch for
       
   651 			// new matches for the query.
       
   652 			//
       
   653 			// Only observe when a limited number of query args are set. There
       
   654 			// are no filters for other properties, so observing will result in
       
   655 			// false positives in those queries.
       
   656 			allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type', 'post_parent' ];
       
   657 			if ( wp.Uploader && _( this.args ).chain().keys().difference( allowed ).isEmpty().value() )
       
   658 				this.observe( wp.Uploader.queue );
       
   659 		},
       
   660 
       
   661 		hasMore: function() {
       
   662 			return this._hasMore;
       
   663 		},
       
   664 
       
   665 		more: function( options ) {
       
   666 			var query = this;
       
   667 
       
   668 			if ( this._more && 'pending' === this._more.state() )
       
   669 				return this._more;
       
   670 
       
   671 			if ( ! this.hasMore() )
       
   672 				return $.Deferred().resolveWith( this ).promise();
       
   673 
       
   674 			options = options || {};
       
   675 			options.remove = false;
       
   676 
       
   677 			return this._more = this.fetch( options ).done( function( resp ) {
       
   678 				if ( _.isEmpty( resp ) || -1 === this.args.posts_per_page || resp.length < this.args.posts_per_page )
       
   679 					query._hasMore = false;
       
   680 			});
       
   681 		},
       
   682 
       
   683 		sync: function( method, model, options ) {
       
   684 			var args, fallback;
       
   685 
       
   686 			// Overload the read method so Attachment.fetch() functions correctly.
       
   687 			if ( 'read' === method ) {
       
   688 				options = options || {};
       
   689 				options.context = this;
       
   690 				options.data = _.extend( options.data || {}, {
       
   691 					action:  'query-attachments',
       
   692 					post_id: media.model.settings.post.id
       
   693 				});
       
   694 
       
   695 				// Clone the args so manipulation is non-destructive.
       
   696 				args = _.clone( this.args );
       
   697 
       
   698 				// Determine which page to query.
       
   699 				if ( -1 !== args.posts_per_page )
       
   700 					args.paged = Math.floor( this.length / args.posts_per_page ) + 1;
       
   701 
       
   702 				options.data.query = args;
       
   703 				return media.ajax( options );
       
   704 
       
   705 			// Otherwise, fall back to Backbone.sync()
       
   706 			} else {
       
   707 				fallback = Attachments.prototype.sync ? Attachments.prototype : Backbone;
       
   708 				return fallback.sync.apply( this, arguments );
       
   709 			}
       
   710 		}
       
   711 	}, {
       
   712 		defaultProps: {
       
   713 			orderby: 'date',
       
   714 			order:   'DESC'
       
   715 		},
       
   716 
       
   717 		defaultArgs: {
       
   718 			posts_per_page: 40
       
   719 		},
       
   720 
       
   721 		orderby: {
       
   722 			allowed:  [ 'name', 'author', 'date', 'title', 'modified', 'uploadedTo', 'id', 'post__in', 'menuOrder' ],
       
   723 			valuemap: {
       
   724 				'id':         'ID',
       
   725 				'uploadedTo': 'parent',
       
   726 				'menuOrder':  'menu_order ID'
       
   727 			}
       
   728 		},
       
   729 
       
   730 		propmap: {
       
   731 			'search':    's',
       
   732 			'type':      'post_mime_type',
       
   733 			'perPage':   'posts_per_page',
       
   734 			'menuOrder': 'menu_order',
       
   735 			'uploadedTo': 'post_parent'
       
   736 		},
       
   737 
       
   738 		// Caches query objects so queries can be easily reused.
       
   739 		get: (function(){
       
   740 			var queries = [];
       
   741 
       
   742 			return function( props, options ) {
       
   743 				var args     = {},
       
   744 					orderby  = Query.orderby,
       
   745 					defaults = Query.defaultProps,
       
   746 					query;
       
   747 
       
   748 				// Remove the `query` property. This isn't linked to a query,
       
   749 				// this *is* the query.
       
   750 				delete props.query;
       
   751 
       
   752 				// Fill default args.
       
   753 				_.defaults( props, defaults );
       
   754 
       
   755 				// Normalize the order.
       
   756 				props.order = props.order.toUpperCase();
       
   757 				if ( 'DESC' !== props.order && 'ASC' !== props.order )
       
   758 					props.order = defaults.order.toUpperCase();
       
   759 
       
   760 				// Ensure we have a valid orderby value.
       
   761 				if ( ! _.contains( orderby.allowed, props.orderby ) )
       
   762 					props.orderby = defaults.orderby;
       
   763 
       
   764 				// Generate the query `args` object.
       
   765 				// Correct any differing property names.
       
   766 				_.each( props, function( value, prop ) {
       
   767 					if ( _.isNull( value ) )
       
   768 						return;
       
   769 
       
   770 					args[ Query.propmap[ prop ] || prop ] = value;
       
   771 				});
       
   772 
       
   773 				// Fill any other default query args.
       
   774 				_.defaults( args, Query.defaultArgs );
       
   775 
       
   776 				// `props.orderby` does not always map directly to `args.orderby`.
       
   777 				// Substitute exceptions specified in orderby.keymap.
       
   778 				args.orderby = orderby.valuemap[ props.orderby ] || props.orderby;
       
   779 
       
   780 				// Search the query cache for matches.
       
   781 				query = _.find( queries, function( query ) {
  1381 				query = _.find( queries, function( query ) {
   782 					return _.isEqual( query.args, args );
  1382 					return _.isEqual( query.args, args );
   783 				});
  1383 				});
   784 
  1384 			} else {
   785 				// Otherwise, create a new query and add it to the cache.
  1385 				queries = [];
   786 				if ( ! query ) {
  1386 			}
   787 					query = new Query( [], _.extend( options || {}, {
  1387 
   788 						props: props,
  1388 			// Otherwise, create a new query and add it to the cache.
   789 						args:  args
  1389 			if ( ! query ) {
   790 					} ) );
  1390 				query = new Query( [], _.extend( options || {}, {
   791 					queries.push( query );
  1391 					props: props,
       
  1392 					args:  args
       
  1393 				} ) );
       
  1394 				queries.push( query );
       
  1395 			}
       
  1396 
       
  1397 			return query;
       
  1398 		};
       
  1399 	}())
       
  1400 });
       
  1401 
       
  1402 module.exports = Query;
       
  1403 
       
  1404 },{}],6:[function(require,module,exports){
       
  1405 /*globals wp, _ */
       
  1406 
       
  1407 /**
       
  1408  * wp.media.model.Selection
       
  1409  *
       
  1410  * A selection of attachments.
       
  1411  *
       
  1412  * @class
       
  1413  * @augments wp.media.model.Attachments
       
  1414  * @augments Backbone.Collection
       
  1415  */
       
  1416 var Attachments = wp.media.model.Attachments,
       
  1417 	Selection;
       
  1418 
       
  1419 Selection = Attachments.extend({
       
  1420 	/**
       
  1421 	 * Refresh the `single` model whenever the selection changes.
       
  1422 	 * Binds `single` instead of using the context argument to ensure
       
  1423 	 * it receives no parameters.
       
  1424 	 *
       
  1425 	 * @param {Array} [models=[]] Array of models used to populate the collection.
       
  1426 	 * @param {Object} [options={}]
       
  1427 	 */
       
  1428 	initialize: function( models, options ) {
       
  1429 		/**
       
  1430 		 * call 'initialize' directly on the parent class
       
  1431 		 */
       
  1432 		Attachments.prototype.initialize.apply( this, arguments );
       
  1433 		this.multiple = options && options.multiple;
       
  1434 
       
  1435 		this.on( 'add remove reset', _.bind( this.single, this, false ) );
       
  1436 	},
       
  1437 
       
  1438 	/**
       
  1439 	 * If the workflow does not support multi-select, clear out the selection
       
  1440 	 * before adding a new attachment to it.
       
  1441 	 *
       
  1442 	 * @param {Array} models
       
  1443 	 * @param {Object} options
       
  1444 	 * @returns {wp.media.model.Attachment[]}
       
  1445 	 */
       
  1446 	add: function( models, options ) {
       
  1447 		if ( ! this.multiple ) {
       
  1448 			this.remove( this.models );
       
  1449 		}
       
  1450 		/**
       
  1451 		 * call 'add' directly on the parent class
       
  1452 		 */
       
  1453 		return Attachments.prototype.add.call( this, models, options );
       
  1454 	},
       
  1455 
       
  1456 	/**
       
  1457 	 * Fired when toggling (clicking on) an attachment in the modal.
       
  1458 	 *
       
  1459 	 * @param {undefined|boolean|wp.media.model.Attachment} model
       
  1460 	 *
       
  1461 	 * @fires wp.media.model.Selection#selection:single
       
  1462 	 * @fires wp.media.model.Selection#selection:unsingle
       
  1463 	 *
       
  1464 	 * @returns {Backbone.Model}
       
  1465 	 */
       
  1466 	single: function( model ) {
       
  1467 		var previous = this._single;
       
  1468 
       
  1469 		// If a `model` is provided, use it as the single model.
       
  1470 		if ( model ) {
       
  1471 			this._single = model;
       
  1472 		}
       
  1473 		// If the single model isn't in the selection, remove it.
       
  1474 		if ( this._single && ! this.get( this._single.cid ) ) {
       
  1475 			delete this._single;
       
  1476 		}
       
  1477 
       
  1478 		this._single = this._single || this.last();
       
  1479 
       
  1480 		// If single has changed, fire an event.
       
  1481 		if ( this._single !== previous ) {
       
  1482 			if ( previous ) {
       
  1483 				previous.trigger( 'selection:unsingle', previous, this );
       
  1484 
       
  1485 				// If the model was already removed, trigger the collection
       
  1486 				// event manually.
       
  1487 				if ( ! this.get( previous.cid ) ) {
       
  1488 					this.trigger( 'selection:unsingle', previous, this );
   792 				}
  1489 				}
   793 
  1490 			}
   794 				return query;
  1491 			if ( this._single ) {
   795 			};
  1492 				this._single.trigger( 'selection:single', this._single, this );
   796 		}())
  1493 			}
   797 	});
  1494 		}
   798 
  1495 
   799 	/**
  1496 		// Return the single model, or the last model as a fallback.
   800 	 * wp.media.model.Selection
  1497 		return this._single;
   801 	 *
  1498 	}
   802 	 * Used to manage a selection of attachments in the views.
  1499 });
   803 	 */
  1500 
   804 	media.model.Selection = Attachments.extend({
  1501 module.exports = Selection;
   805 		initialize: function( models, options ) {
  1502 
   806 			Attachments.prototype.initialize.apply( this, arguments );
  1503 },{}]},{},[1]);
   807 			this.multiple = options && options.multiple;
       
   808 
       
   809 			// Refresh the `single` model whenever the selection changes.
       
   810 			// Binds `single` instead of using the context argument to ensure
       
   811 			// it receives no parameters.
       
   812 			this.on( 'add remove reset', _.bind( this.single, this, false ) );
       
   813 		},
       
   814 
       
   815 		// Override the selection's add method.
       
   816 		// If the workflow does not support multiple
       
   817 		// selected attachments, reset the selection.
       
   818 		add: function( models, options ) {
       
   819 			if ( ! this.multiple )
       
   820 				this.remove( this.models );
       
   821 
       
   822 			return Attachments.prototype.add.call( this, models, options );
       
   823 		},
       
   824 
       
   825 		single: function( model ) {
       
   826 			var previous = this._single;
       
   827 
       
   828 			// If a `model` is provided, use it as the single model.
       
   829 			if ( model )
       
   830 				this._single = model;
       
   831 
       
   832 			// If the single model isn't in the selection, remove it.
       
   833 			if ( this._single && ! this.get( this._single.cid ) )
       
   834 				delete this._single;
       
   835 
       
   836 			this._single = this._single || this.last();
       
   837 
       
   838 			// If single has changed, fire an event.
       
   839 			if ( this._single !== previous ) {
       
   840 				if ( previous ) {
       
   841 					previous.trigger( 'selection:unsingle', previous, this );
       
   842 
       
   843 					// If the model was already removed, trigger the collection
       
   844 					// event manually.
       
   845 					if ( ! this.get( previous.cid ) )
       
   846 						this.trigger( 'selection:unsingle', previous, this );
       
   847 				}
       
   848 				if ( this._single )
       
   849 					this._single.trigger( 'selection:single', this._single, this );
       
   850 			}
       
   851 
       
   852 			// Return the single model, or the last model as a fallback.
       
   853 			return this._single;
       
   854 		}
       
   855 	});
       
   856 
       
   857 	// Clean up. Prevents mobile browsers caching
       
   858 	$(window).on('unload', function(){
       
   859 		window.wp = null;
       
   860 	});
       
   861 
       
   862 }(jQuery));