wp/wp-includes/js/media-models.js
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     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){
     1 /******/ (function(modules) { // webpackBootstrap
     2 /*globals wp, _, jQuery */
     2 /******/ 	// The module cache
       
     3 /******/ 	var installedModules = {};
       
     4 /******/
       
     5 /******/ 	// The require function
       
     6 /******/ 	function __webpack_require__(moduleId) {
       
     7 /******/
       
     8 /******/ 		// Check if module is in cache
       
     9 /******/ 		if(installedModules[moduleId]) {
       
    10 /******/ 			return installedModules[moduleId].exports;
       
    11 /******/ 		}
       
    12 /******/ 		// Create a new module (and put it into the cache)
       
    13 /******/ 		var module = installedModules[moduleId] = {
       
    14 /******/ 			i: moduleId,
       
    15 /******/ 			l: false,
       
    16 /******/ 			exports: {}
       
    17 /******/ 		};
       
    18 /******/
       
    19 /******/ 		// Execute the module function
       
    20 /******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
       
    21 /******/
       
    22 /******/ 		// Flag the module as loaded
       
    23 /******/ 		module.l = true;
       
    24 /******/
       
    25 /******/ 		// Return the exports of the module
       
    26 /******/ 		return module.exports;
       
    27 /******/ 	}
       
    28 /******/
       
    29 /******/
       
    30 /******/ 	// expose the modules object (__webpack_modules__)
       
    31 /******/ 	__webpack_require__.m = modules;
       
    32 /******/
       
    33 /******/ 	// expose the module cache
       
    34 /******/ 	__webpack_require__.c = installedModules;
       
    35 /******/
       
    36 /******/ 	// define getter function for harmony exports
       
    37 /******/ 	__webpack_require__.d = function(exports, name, getter) {
       
    38 /******/ 		if(!__webpack_require__.o(exports, name)) {
       
    39 /******/ 			Object.defineProperty(exports, name, {
       
    40 /******/ 				configurable: false,
       
    41 /******/ 				enumerable: true,
       
    42 /******/ 				get: getter
       
    43 /******/ 			});
       
    44 /******/ 		}
       
    45 /******/ 	};
       
    46 /******/
       
    47 /******/ 	// getDefaultExport function for compatibility with non-harmony modules
       
    48 /******/ 	__webpack_require__.n = function(module) {
       
    49 /******/ 		var getter = module && module.__esModule ?
       
    50 /******/ 			function getDefault() { return module['default']; } :
       
    51 /******/ 			function getModuleExports() { return module; };
       
    52 /******/ 		__webpack_require__.d(getter, 'a', getter);
       
    53 /******/ 		return getter;
       
    54 /******/ 	};
       
    55 /******/
       
    56 /******/ 	// Object.prototype.hasOwnProperty.call
       
    57 /******/ 	__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
       
    58 /******/
       
    59 /******/ 	// __webpack_public_path__
       
    60 /******/ 	__webpack_require__.p = "";
       
    61 /******/
       
    62 /******/ 	// Load entry module and return exports
       
    63 /******/ 	return __webpack_require__(__webpack_require__.s = 20);
       
    64 /******/ })
       
    65 /************************************************************************/
       
    66 /******/ ({
       
    67 
       
    68 /***/ 20:
       
    69 /***/ (function(module, exports, __webpack_require__) {
     3 
    70 
     4 var $ = jQuery,
    71 var $ = jQuery,
     5 	Attachment, Attachments, l10n, media;
    72 	Attachment, Attachments, l10n, media;
     6 
    73 
       
    74 /** @namespace wp */
     7 window.wp = window.wp || {};
    75 window.wp = window.wp || {};
     8 
    76 
     9 /**
    77 /**
    10  * Create and return a media frame.
    78  * Create and return a media frame.
    11  *
    79  *
    12  * Handles the default media experience.
    80  * Handles the default media experience.
       
    81  *
       
    82  * @alias wp.media
       
    83  * @memberOf wp
       
    84  * @namespace
    13  *
    85  *
    14  * @param  {object} attributes The properties passed to the main media controller.
    86  * @param  {object} attributes The properties passed to the main media controller.
    15  * @return {wp.media.view.MediaFrame} A media workflow.
    87  * @return {wp.media.view.MediaFrame} A media workflow.
    16  */
    88  */
    17 media = wp.media = function( attributes ) {
    89 media = wp.media = function( attributes ) {
    47 	media.frame = frame;
   119 	media.frame = frame;
    48 
   120 
    49 	return frame;
   121 	return frame;
    50 };
   122 };
    51 
   123 
       
   124 /** @namespace wp.media.model */
       
   125 /** @namespace wp.media.view */
       
   126 /** @namespace wp.media.controller */
       
   127 /** @namespace wp.media.frames */
    52 _.extend( media, { model: {}, view: {}, controller: {}, frames: {} });
   128 _.extend( media, { model: {}, view: {}, controller: {}, frames: {} });
    53 
   129 
    54 // Link any localized strings.
   130 // Link any localized strings.
    55 l10n = media.model.l10n = window._wpMediaModelsL10n || {};
   131 l10n = media.model.l10n = window._wpMediaModelsL10n || {};
    56 
   132 
    57 // Link any settings.
   133 // Link any settings.
    58 media.model.settings = l10n.settings || {};
   134 media.model.settings = l10n.settings || {};
    59 delete l10n.settings;
   135 delete l10n.settings;
    60 
   136 
    61 Attachment = media.model.Attachment = require( './models/attachment.js' );
   137 Attachment = media.model.Attachment = __webpack_require__( 21 );
    62 Attachments = media.model.Attachments = require( './models/attachments.js' );
   138 Attachments = media.model.Attachments = __webpack_require__( 22 );
    63 
   139 
    64 media.model.Query = require( './models/query.js' );
   140 media.model.Query = __webpack_require__( 23 );
    65 media.model.PostImage = require( './models/post-image.js' );
   141 media.model.PostImage = __webpack_require__( 24 );
    66 media.model.Selection = require( './models/selection.js' );
   142 media.model.Selection = __webpack_require__( 25 );
    67 
   143 
    68 /**
   144 /**
    69  * ========================================================================
   145  * ========================================================================
    70  * UTILITIES
   146  * UTILITIES
    71  * ========================================================================
   147  * ========================================================================
    90 	} else {
   166 	} else {
    91 		return a > b ? -1 : 1;
   167 		return a > b ? -1 : 1;
    92 	}
   168 	}
    93 };
   169 };
    94 
   170 
    95 _.extend( media, {
   171 _.extend( media, /** @lends wp.media */{
    96 	/**
   172 	/**
    97 	 * media.template( id )
   173 	 * media.template( id )
    98 	 *
   174 	 *
    99 	 * Fetch a JavaScript template for an id, and return a templating function for it.
   175 	 * Fetch a JavaScript template for an id, and return a templating function for it.
   100 	 *
   176 	 *
   229 // Clean up. Prevents mobile browsers caching
   305 // Clean up. Prevents mobile browsers caching
   230 $(window).on('unload', function(){
   306 $(window).on('unload', function(){
   231 	window.wp = null;
   307 	window.wp = null;
   232 });
   308 });
   233 
   309 
   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){
   310 
   235 /*globals wp, _, Backbone */
   311 /***/ }),
       
   312 
       
   313 /***/ 21:
       
   314 /***/ (function(module, exports) {
       
   315 
       
   316 var $ = Backbone.$,
       
   317 	Attachment;
   236 
   318 
   237 /**
   319 /**
   238  * wp.media.model.Attachment
   320  * wp.media.model.Attachment
       
   321  *
       
   322  * @memberOf wp.media.model
   239  *
   323  *
   240  * @class
   324  * @class
   241  * @augments Backbone.Model
   325  * @augments Backbone.Model
   242  */
   326  */
   243 var $ = Backbone.$,
   327 Attachment = Backbone.Model.extend(/** @lends wp.media.model.Attachment.prototype */{
   244 	Attachment;
       
   245 
       
   246 Attachment = Backbone.Model.extend({
       
   247 	/**
   328 	/**
   248 	 * Triggered when attachment details change
   329 	 * Triggered when attachment details change
   249 	 * Overrides Backbone.Model.sync
   330 	 * Overrides Backbone.Model.sync
   250 	 *
   331 	 *
   251 	 * @param {string} method
   332 	 * @param {string} method
   368 			post_id: wp.media.model.settings.post.id
   449 			post_id: wp.media.model.settings.post.id
   369 		}, data ) ).done( function( resp, status, xhr ) {
   450 		}, data ) ).done( function( resp, status, xhr ) {
   370 			model.set( model.parse( resp, xhr ), options );
   451 			model.set( model.parse( resp, xhr ), options );
   371 		});
   452 		});
   372 	}
   453 	}
   373 }, {
   454 },/** @lends wp.media.model.Attachment */{
   374 	/**
   455 	/**
   375 	 * Create a new model on the static 'all' attachments collection and return it.
   456 	 * Create a new model on the static 'all' attachments collection and return it.
   376 	 *
   457 	 *
   377 	 * @static
   458 	 * @static
       
   459 	 *
   378 	 * @param {Object} attrs
   460 	 * @param {Object} attrs
   379 	 * @returns {wp.media.model.Attachment}
   461 	 * @returns {wp.media.model.Attachment}
   380 	 */
   462 	 */
   381 	create: function( attrs ) {
   463 	create: function( attrs ) {
   382 		var Attachments = wp.media.model.Attachments;
   464 		var Attachments = wp.media.model.Attachments;
   399 	})
   481 	})
   400 });
   482 });
   401 
   483 
   402 module.exports = Attachment;
   484 module.exports = Attachment;
   403 
   485 
   404 },{}],3:[function(require,module,exports){
   486 
   405 /*globals wp, _, Backbone */
   487 /***/ }),
       
   488 
       
   489 /***/ 22:
       
   490 /***/ (function(module, exports) {
   406 
   491 
   407 /**
   492 /**
   408  * wp.media.model.Attachments
   493  * wp.media.model.Attachments
   409  *
   494  *
   410  * A collection of attachments.
   495  * A collection of attachments.
   411  *
   496  *
   412  * This collection has no persistence with the server without supplying
   497  * This collection has no persistence with the server without supplying
   413  * 'options.props.query = true', which will mirror the collection
   498  * 'options.props.query = true', which will mirror the collection
   414  * to an Attachments Query collection - @see wp.media.model.Attachments.mirror().
   499  * to an Attachments Query collection - @see wp.media.model.Attachments.mirror().
       
   500  *
       
   501  * @memberOf wp.media.model
   415  *
   502  *
   416  * @class
   503  * @class
   417  * @augments Backbone.Collection
   504  * @augments Backbone.Collection
   418  *
   505  *
   419  * @param {array}  [models]                Models to initialize with the collection.
   506  * @param {array}  [models]                Models to initialize with the collection.
   424  * @param {string} [options.props.query]   Whether the collection is linked to an attachments query.
   511  * @param {string} [options.props.query]   Whether the collection is linked to an attachments query.
   425  * @param {string} [options.observe]
   512  * @param {string} [options.observe]
   426  * @param {string} [options.filters]
   513  * @param {string} [options.filters]
   427  *
   514  *
   428  */
   515  */
   429 var Attachments = Backbone.Collection.extend({
   516 var Attachments = Backbone.Collection.extend(/** @lends wp.media.model.Attachments.prototype */{
   430 	/**
   517 	/**
   431 	 * @type {wp.media.model.Attachment}
   518 	 * @type {wp.media.model.Attachment}
   432 	 */
   519 	 */
   433 	model: wp.media.model.Attachment,
   520 	model: wp.media.model.Attachment,
   434 	/**
   521 	/**
   553 	 *
   640 	 *
   554 	 * @param {wp.media.model.Attachment} attachment
   641 	 * @param {wp.media.model.Attachment} attachment
   555 	 * @returns {Boolean}
   642 	 * @returns {Boolean}
   556 	 */
   643 	 */
   557 	validator: function( attachment ) {
   644 	validator: function( attachment ) {
       
   645 
       
   646 		// Filter out contextually created attachments (e.g. headers, logos, etc.).
       
   647 		if (
       
   648 			! _.isUndefined( attachment.attributes.context ) &&
       
   649 			'' !== attachment.attributes.context
       
   650 		) {
       
   651 			return false;
       
   652 		}
       
   653 
   558 		if ( ! this.validateDestroyed && attachment.destroyed ) {
   654 		if ( ! this.validateDestroyed && attachment.destroyed ) {
   559 			return false;
   655 			return false;
   560 		}
   656 		}
   561 		return _.all( this.filters, function( filter ) {
   657 		return _.all( this.filters, function( filter ) {
   562 			return !! filter.call( this, attachment );
   658 			return !! filter.call( this, attachment );
   700 
   796 
   701 		this.unobserve( this.mirroring );
   797 		this.unobserve( this.mirroring );
   702 		delete this.mirroring;
   798 		delete this.mirroring;
   703 	},
   799 	},
   704 	/**
   800 	/**
   705 	 * Retrive more attachments from the server for the collection.
   801 	 * Retrieve more attachments from the server for the collection.
   706 	 *
   802 	 *
   707 	 * Only works if the collection is mirroring a Query Attachments collection,
   803 	 * Only works if the collection is mirroring a Query Attachments collection,
   708 	 * and forwards to its `more` method. This collection class doesn't have
   804 	 * and forwards to its `more` method. This collection class doesn't have
   709 	 * server persistence by itself.
   805 	 * server persistence by itself.
   710 	 *
   806 	 *
   822 			nonce:       wp.media.model.settings.post.nonce,
   918 			nonce:       wp.media.model.settings.post.nonce,
   823 			post_id:     wp.media.model.settings.post.id,
   919 			post_id:     wp.media.model.settings.post.id,
   824 			attachments: attachments
   920 			attachments: attachments
   825 		});
   921 		});
   826 	}
   922 	}
   827 }, {
   923 },/** @lends wp.media.model.Attachments */{
   828 	/**
   924 	/**
   829 	 * A function to compare two attachment models in an attachments collection.
   925 	 * A function to compare two attachment models in an attachments collection.
   830 	 *
   926 	 *
   831 	 * Used as the default comparator for instances of wp.media.model.Attachments
   927 	 * Used as the default comparator for instances of wp.media.model.Attachments
   832 	 * and its subclasses. @see wp.media.model.Attachments._changeOrderby().
   928 	 * and its subclasses. @see wp.media.model.Attachments._changeOrderby().
   833 	 *
       
   834 	 * @static
       
   835 	 *
   929 	 *
   836 	 * @param {Backbone.Model} a
   930 	 * @param {Backbone.Model} a
   837 	 * @param {Backbone.Model} b
   931 	 * @param {Backbone.Model} b
   838 	 * @param {Object} options
   932 	 * @param {Object} options
   839 	 * @returns {Number} -1 if the first model should come before the second,
   933 	 * @returns {Number} -1 if the first model should come before the second,
   859 			ac = bc = null;
   953 			ac = bc = null;
   860 		}
   954 		}
   861 
   955 
   862 		return ( 'DESC' === order ) ? wp.media.compare( a, b, ac, bc ) : wp.media.compare( b, a, bc, ac );
   956 		return ( 'DESC' === order ) ? wp.media.compare( a, b, ac, bc ) : wp.media.compare( b, a, bc, ac );
   863 	},
   957 	},
   864 	/**
   958 	/** @namespace wp.media.model.Attachments.filters */
   865 	 * @namespace
       
   866 	 */
       
   867 	filters: {
   959 	filters: {
   868 		/**
   960 		/**
   869 		 * @static
   961 		 * @static
   870 		 * Note that this client-side searching is *not* equivalent
   962 		 * Note that this client-side searching is *not* equivalent
   871 		 * to our server-side searching.
   963 		 * to our server-side searching.
   893 		 * @this wp.media.model.Attachments
   985 		 * @this wp.media.model.Attachments
   894 		 *
   986 		 *
   895 		 * @returns {Boolean}
   987 		 * @returns {Boolean}
   896 		 */
   988 		 */
   897 		type: function( attachment ) {
   989 		type: function( attachment ) {
   898 			var type = this.props.get('type');
   990 			var type = this.props.get('type'), atts = attachment.toJSON(), mime, found;
   899 			return ! type || -1 !== type.indexOf( attachment.get('type') );
   991 
       
   992 			if ( ! type || ( _.isArray( type ) && ! type.length ) ) {
       
   993 				return true;
       
   994 			}
       
   995 
       
   996 			mime = atts.mime || ( atts.file && atts.file.type ) || '';
       
   997 
       
   998 			if ( _.isArray( type ) ) {
       
   999 				found = _.find( type, function (t) {
       
  1000 					return -1 !== mime.indexOf( t );
       
  1001 				} );
       
  1002 			} else {
       
  1003 				found = -1 !== mime.indexOf( type );
       
  1004 			}
       
  1005 
       
  1006 			return found;
   900 		},
  1007 		},
   901 		/**
  1008 		/**
   902 		 * @static
  1009 		 * @static
   903 		 * @param {wp.media.model.Attachment} attachment
  1010 		 * @param {wp.media.model.Attachment} attachment
   904 		 *
  1011 		 *
   933 	}
  1040 	}
   934 });
  1041 });
   935 
  1042 
   936 module.exports = Attachments;
  1043 module.exports = Attachments;
   937 
  1044 
   938 },{}],4:[function(require,module,exports){
  1045 
   939 /*globals Backbone */
  1046 /***/ }),
       
  1047 
       
  1048 /***/ 23:
       
  1049 /***/ (function(module, exports) {
       
  1050 
       
  1051 var Attachments = wp.media.model.Attachments,
       
  1052 	Query;
       
  1053 
       
  1054 /**
       
  1055  * wp.media.model.Query
       
  1056  *
       
  1057  * A collection of attachments that match the supplied query arguments.
       
  1058  *
       
  1059  * Note: Do NOT change this.args after the query has been initialized.
       
  1060  *       Things will break.
       
  1061  *
       
  1062  * @memberOf wp.media.model
       
  1063  *
       
  1064  * @class
       
  1065  * @augments wp.media.model.Attachments
       
  1066  * @augments Backbone.Collection
       
  1067  *
       
  1068  * @param {array}  [models]                      Models to initialize with the collection.
       
  1069  * @param {object} [options]                     Options hash.
       
  1070  * @param {object} [options.args]                Attachments query arguments.
       
  1071  * @param {object} [options.args.posts_per_page]
       
  1072  */
       
  1073 Query = Attachments.extend(/** @lends wp.media.model.Query.prototype */{
       
  1074 	/**
       
  1075 	 * @param {array}  [models=[]]  Array of initial models to populate the collection.
       
  1076 	 * @param {object} [options={}]
       
  1077 	 */
       
  1078 	initialize: function( models, options ) {
       
  1079 		var allowed;
       
  1080 
       
  1081 		options = options || {};
       
  1082 		Attachments.prototype.initialize.apply( this, arguments );
       
  1083 
       
  1084 		this.args     = options.args;
       
  1085 		this._hasMore = true;
       
  1086 		this.created  = new Date();
       
  1087 
       
  1088 		this.filters.order = function( attachment ) {
       
  1089 			var orderby = this.props.get('orderby'),
       
  1090 				order = this.props.get('order');
       
  1091 
       
  1092 			if ( ! this.comparator ) {
       
  1093 				return true;
       
  1094 			}
       
  1095 
       
  1096 			// We want any items that can be placed before the last
       
  1097 			// item in the set. If we add any items after the last
       
  1098 			// item, then we can't guarantee the set is complete.
       
  1099 			if ( this.length ) {
       
  1100 				return 1 !== this.comparator( attachment, this.last(), { ties: true });
       
  1101 
       
  1102 			// Handle the case where there are no items yet and
       
  1103 			// we're sorting for recent items. In that case, we want
       
  1104 			// changes that occurred after we created the query.
       
  1105 			} else if ( 'DESC' === order && ( 'date' === orderby || 'modified' === orderby ) ) {
       
  1106 				return attachment.get( orderby ) >= this.created;
       
  1107 
       
  1108 			// If we're sorting by menu order and we have no items,
       
  1109 			// accept any items that have the default menu order (0).
       
  1110 			} else if ( 'ASC' === order && 'menuOrder' === orderby ) {
       
  1111 				return attachment.get( orderby ) === 0;
       
  1112 			}
       
  1113 
       
  1114 			// Otherwise, we don't want any items yet.
       
  1115 			return false;
       
  1116 		};
       
  1117 
       
  1118 		// Observe the central `wp.Uploader.queue` collection to watch for
       
  1119 		// new matches for the query.
       
  1120 		//
       
  1121 		// Only observe when a limited number of query args are set. There
       
  1122 		// are no filters for other properties, so observing will result in
       
  1123 		// false positives in those queries.
       
  1124 		allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type', 'post_parent', 'author' ];
       
  1125 		if ( wp.Uploader && _( this.args ).chain().keys().difference( allowed ).isEmpty().value() ) {
       
  1126 			this.observe( wp.Uploader.queue );
       
  1127 		}
       
  1128 	},
       
  1129 	/**
       
  1130 	 * Whether there are more attachments that haven't been sync'd from the server
       
  1131 	 * that match the collection's query.
       
  1132 	 *
       
  1133 	 * @returns {boolean}
       
  1134 	 */
       
  1135 	hasMore: function() {
       
  1136 		return this._hasMore;
       
  1137 	},
       
  1138 	/**
       
  1139 	 * Fetch more attachments from the server for the collection.
       
  1140 	 *
       
  1141 	 * @param   {object}  [options={}]
       
  1142 	 * @returns {Promise}
       
  1143 	 */
       
  1144 	more: function( options ) {
       
  1145 		var query = this;
       
  1146 
       
  1147 		// If there is already a request pending, return early with the Deferred object.
       
  1148 		if ( this._more && 'pending' === this._more.state() ) {
       
  1149 			return this._more;
       
  1150 		}
       
  1151 
       
  1152 		if ( ! this.hasMore() ) {
       
  1153 			return jQuery.Deferred().resolveWith( this ).promise();
       
  1154 		}
       
  1155 
       
  1156 		options = options || {};
       
  1157 		options.remove = false;
       
  1158 
       
  1159 		return this._more = this.fetch( options ).done( function( resp ) {
       
  1160 			if ( _.isEmpty( resp ) || -1 === this.args.posts_per_page || resp.length < this.args.posts_per_page ) {
       
  1161 				query._hasMore = false;
       
  1162 			}
       
  1163 		});
       
  1164 	},
       
  1165 	/**
       
  1166 	 * Overrides Backbone.Collection.sync
       
  1167 	 * Overrides wp.media.model.Attachments.sync
       
  1168 	 *
       
  1169 	 * @param {String} method
       
  1170 	 * @param {Backbone.Model} model
       
  1171 	 * @param {Object} [options={}]
       
  1172 	 * @returns {Promise}
       
  1173 	 */
       
  1174 	sync: function( method, model, options ) {
       
  1175 		var args, fallback;
       
  1176 
       
  1177 		// Overload the read method so Attachment.fetch() functions correctly.
       
  1178 		if ( 'read' === method ) {
       
  1179 			options = options || {};
       
  1180 			options.context = this;
       
  1181 			options.data = _.extend( options.data || {}, {
       
  1182 				action:  'query-attachments',
       
  1183 				post_id: wp.media.model.settings.post.id
       
  1184 			});
       
  1185 
       
  1186 			// Clone the args so manipulation is non-destructive.
       
  1187 			args = _.clone( this.args );
       
  1188 
       
  1189 			// Determine which page to query.
       
  1190 			if ( -1 !== args.posts_per_page ) {
       
  1191 				args.paged = Math.round( this.length / args.posts_per_page ) + 1;
       
  1192 			}
       
  1193 
       
  1194 			options.data.query = args;
       
  1195 			return wp.media.ajax( options );
       
  1196 
       
  1197 		// Otherwise, fall back to Backbone.sync()
       
  1198 		} else {
       
  1199 			/**
       
  1200 			 * Call wp.media.model.Attachments.sync or Backbone.sync
       
  1201 			 */
       
  1202 			fallback = Attachments.prototype.sync ? Attachments.prototype : Backbone;
       
  1203 			return fallback.sync.apply( this, arguments );
       
  1204 		}
       
  1205 	}
       
  1206 }, /** @lends wp.media.model.Query */{
       
  1207 	/**
       
  1208 	 * @readonly
       
  1209 	 */
       
  1210 	defaultProps: {
       
  1211 		orderby: 'date',
       
  1212 		order:   'DESC'
       
  1213 	},
       
  1214 	/**
       
  1215 	 * @readonly
       
  1216 	 */
       
  1217 	defaultArgs: {
       
  1218 		posts_per_page: 40
       
  1219 	},
       
  1220 	/**
       
  1221 	 * @readonly
       
  1222 	 */
       
  1223 	orderby: {
       
  1224 		allowed:  [ 'name', 'author', 'date', 'title', 'modified', 'uploadedTo', 'id', 'post__in', 'menuOrder' ],
       
  1225 		/**
       
  1226 		 * A map of JavaScript orderby values to their WP_Query equivalents.
       
  1227 		 * @type {Object}
       
  1228 		 */
       
  1229 		valuemap: {
       
  1230 			'id':         'ID',
       
  1231 			'uploadedTo': 'parent',
       
  1232 			'menuOrder':  'menu_order ID'
       
  1233 		}
       
  1234 	},
       
  1235 	/**
       
  1236 	 * A map of JavaScript query properties to their WP_Query equivalents.
       
  1237 	 *
       
  1238 	 * @readonly
       
  1239 	 */
       
  1240 	propmap: {
       
  1241 		'search':		's',
       
  1242 		'type':			'post_mime_type',
       
  1243 		'perPage':		'posts_per_page',
       
  1244 		'menuOrder':	'menu_order',
       
  1245 		'uploadedTo':	'post_parent',
       
  1246 		'status':		'post_status',
       
  1247 		'include':		'post__in',
       
  1248 		'exclude':		'post__not_in',
       
  1249 		'author':		'author'
       
  1250 	},
       
  1251 	/**
       
  1252 	 * Creates and returns an Attachments Query collection given the properties.
       
  1253 	 *
       
  1254 	 * Caches query objects and reuses where possible.
       
  1255 	 *
       
  1256 	 * @static
       
  1257 	 * @method
       
  1258 	 *
       
  1259 	 * @param {object} [props]
       
  1260 	 * @param {Object} [props.cache=true]   Whether to use the query cache or not.
       
  1261 	 * @param {Object} [props.order]
       
  1262 	 * @param {Object} [props.orderby]
       
  1263 	 * @param {Object} [props.include]
       
  1264 	 * @param {Object} [props.exclude]
       
  1265 	 * @param {Object} [props.s]
       
  1266 	 * @param {Object} [props.post_mime_type]
       
  1267 	 * @param {Object} [props.posts_per_page]
       
  1268 	 * @param {Object} [props.menu_order]
       
  1269 	 * @param {Object} [props.post_parent]
       
  1270 	 * @param {Object} [props.post_status]
       
  1271 	 * @param {Object} [props.author]
       
  1272 	 * @param {Object} [options]
       
  1273 	 *
       
  1274 	 * @returns {wp.media.model.Query} A new Attachments Query collection.
       
  1275 	 */
       
  1276 	get: (function(){
       
  1277 		/**
       
  1278 		 * @static
       
  1279 		 * @type Array
       
  1280 		 */
       
  1281 		var queries = [];
       
  1282 
       
  1283 		/**
       
  1284 		 * @returns {Query}
       
  1285 		 */
       
  1286 		return function( props, options ) {
       
  1287 			var args     = {},
       
  1288 				orderby  = Query.orderby,
       
  1289 				defaults = Query.defaultProps,
       
  1290 				query,
       
  1291 				cache    = !! props.cache || _.isUndefined( props.cache );
       
  1292 
       
  1293 			// Remove the `query` property. This isn't linked to a query,
       
  1294 			// this *is* the query.
       
  1295 			delete props.query;
       
  1296 			delete props.cache;
       
  1297 
       
  1298 			// Fill default args.
       
  1299 			_.defaults( props, defaults );
       
  1300 
       
  1301 			// Normalize the order.
       
  1302 			props.order = props.order.toUpperCase();
       
  1303 			if ( 'DESC' !== props.order && 'ASC' !== props.order ) {
       
  1304 				props.order = defaults.order.toUpperCase();
       
  1305 			}
       
  1306 
       
  1307 			// Ensure we have a valid orderby value.
       
  1308 			if ( ! _.contains( orderby.allowed, props.orderby ) ) {
       
  1309 				props.orderby = defaults.orderby;
       
  1310 			}
       
  1311 
       
  1312 			_.each( [ 'include', 'exclude' ], function( prop ) {
       
  1313 				if ( props[ prop ] && ! _.isArray( props[ prop ] ) ) {
       
  1314 					props[ prop ] = [ props[ prop ] ];
       
  1315 				}
       
  1316 			} );
       
  1317 
       
  1318 			// Generate the query `args` object.
       
  1319 			// Correct any differing property names.
       
  1320 			_.each( props, function( value, prop ) {
       
  1321 				if ( _.isNull( value ) ) {
       
  1322 					return;
       
  1323 				}
       
  1324 
       
  1325 				args[ Query.propmap[ prop ] || prop ] = value;
       
  1326 			});
       
  1327 
       
  1328 			// Fill any other default query args.
       
  1329 			_.defaults( args, Query.defaultArgs );
       
  1330 
       
  1331 			// `props.orderby` does not always map directly to `args.orderby`.
       
  1332 			// Substitute exceptions specified in orderby.keymap.
       
  1333 			args.orderby = orderby.valuemap[ props.orderby ] || props.orderby;
       
  1334 
       
  1335 			// Search the query cache for a matching query.
       
  1336 			if ( cache ) {
       
  1337 				query = _.find( queries, function( query ) {
       
  1338 					return _.isEqual( query.args, args );
       
  1339 				});
       
  1340 			} else {
       
  1341 				queries = [];
       
  1342 			}
       
  1343 
       
  1344 			// Otherwise, create a new query and add it to the cache.
       
  1345 			if ( ! query ) {
       
  1346 				query = new Query( [], _.extend( options || {}, {
       
  1347 					props: props,
       
  1348 					args:  args
       
  1349 				} ) );
       
  1350 				queries.push( query );
       
  1351 			}
       
  1352 
       
  1353 			return query;
       
  1354 		};
       
  1355 	}())
       
  1356 });
       
  1357 
       
  1358 module.exports = Query;
       
  1359 
       
  1360 
       
  1361 /***/ }),
       
  1362 
       
  1363 /***/ 24:
       
  1364 /***/ (function(module, exports) {
   940 
  1365 
   941 /**
  1366 /**
   942  * wp.media.model.PostImage
  1367  * wp.media.model.PostImage
   943  *
  1368  *
   944  * An instance of an image that's been embedded into a post.
  1369  * An instance of an image that's been embedded into a post.
   945  *
  1370  *
   946  * Used in the embedded image attachment display settings modal - @see wp.media.view.MediaFrame.ImageDetails.
  1371  * Used in the embedded image attachment display settings modal - @see wp.media.view.MediaFrame.ImageDetails.
       
  1372  *
       
  1373  * @memberOf wp.media.model
   947  *
  1374  *
   948  * @class
  1375  * @class
   949  * @augments Backbone.Model
  1376  * @augments Backbone.Model
   950  *
  1377  *
   951  * @param {int} [attributes]               Initial model attributes.
  1378  * @param {int} [attributes]               Initial model attributes.
   952  * @param {int} [attributes.attachment_id] ID of the attachment.
  1379  * @param {int} [attributes.attachment_id] ID of the attachment.
   953  **/
  1380  **/
   954 var PostImage = Backbone.Model.extend({
  1381 var PostImage = Backbone.Model.extend(/** @lends wp.media.model.PostImage.prototype */{
   955 
  1382 
   956 	initialize: function( attributes ) {
  1383 	initialize: function( attributes ) {
   957 		var Attachment = wp.media.model.Attachment;
  1384 		var Attachment = wp.media.model.Attachment;
   958 		this.attachment = false;
  1385 		this.attachment = false;
   959 
  1386 
  1089 	}
  1516 	}
  1090 });
  1517 });
  1091 
  1518 
  1092 module.exports = PostImage;
  1519 module.exports = PostImage;
  1093 
  1520 
  1094 },{}],5:[function(require,module,exports){
  1521 
  1095 /*globals wp, _ */
  1522 /***/ }),
  1096 
  1523 
  1097 /**
  1524 /***/ 25:
  1098  * wp.media.model.Query
  1525 /***/ (function(module, exports) {
  1099  *
  1526 
  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,
  1527 var Attachments = wp.media.model.Attachments,
  1115 	Query;
  1528 	Selection;
  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 ) {
       
  1225 			options = options || {};
       
  1226 			options.context = this;
       
  1227 			options.data = _.extend( options.data || {}, {
       
  1228 				action:  'query-attachments',
       
  1229 				post_id: wp.media.model.settings.post.id
       
  1230 			});
       
  1231 
       
  1232 			// Clone the args so manipulation is non-destructive.
       
  1233 			args = _.clone( this.args );
       
  1234 
       
  1235 			// Determine which page to query.
       
  1236 			if ( -1 !== args.posts_per_page ) {
       
  1237 				args.paged = Math.round( this.length / args.posts_per_page ) + 1;
       
  1238 			}
       
  1239 
       
  1240 			options.data.query = args;
       
  1241 			return wp.media.ajax( options );
       
  1242 
       
  1243 		// Otherwise, fall back to Backbone.sync()
       
  1244 		} else {
       
  1245 			/**
       
  1246 			 * Call wp.media.model.Attachments.sync or Backbone.sync
       
  1247 			 */
       
  1248 			fallback = Attachments.prototype.sync ? Attachments.prototype : Backbone;
       
  1249 			return fallback.sync.apply( this, arguments );
       
  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 ] ];
       
  1359 				}
       
  1360 			} );
       
  1361 
       
  1362 			// Generate the query `args` object.
       
  1363 			// Correct any differing property names.
       
  1364 			_.each( props, function( value, prop ) {
       
  1365 				if ( _.isNull( value ) ) {
       
  1366 					return;
       
  1367 				}
       
  1368 
       
  1369 				args[ Query.propmap[ prop ] || prop ] = value;
       
  1370 			});
       
  1371 
       
  1372 			// Fill any other default query args.
       
  1373 			_.defaults( args, Query.defaultArgs );
       
  1374 
       
  1375 			// `props.orderby` does not always map directly to `args.orderby`.
       
  1376 			// Substitute exceptions specified in orderby.keymap.
       
  1377 			args.orderby = orderby.valuemap[ props.orderby ] || props.orderby;
       
  1378 
       
  1379 			// Search the query cache for a matching query.
       
  1380 			if ( cache ) {
       
  1381 				query = _.find( queries, function( query ) {
       
  1382 					return _.isEqual( query.args, args );
       
  1383 				});
       
  1384 			} else {
       
  1385 				queries = [];
       
  1386 			}
       
  1387 
       
  1388 			// Otherwise, create a new query and add it to the cache.
       
  1389 			if ( ! query ) {
       
  1390 				query = new Query( [], _.extend( options || {}, {
       
  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 
  1529 
  1407 /**
  1530 /**
  1408  * wp.media.model.Selection
  1531  * wp.media.model.Selection
  1409  *
  1532  *
  1410  * A selection of attachments.
  1533  * A selection of attachments.
       
  1534  *
       
  1535  * @memberOf wp.media.model
  1411  *
  1536  *
  1412  * @class
  1537  * @class
  1413  * @augments wp.media.model.Attachments
  1538  * @augments wp.media.model.Attachments
  1414  * @augments Backbone.Collection
  1539  * @augments Backbone.Collection
  1415  */
  1540  */
  1416 var Attachments = wp.media.model.Attachments,
  1541 Selection = Attachments.extend(/** @lends wp.media.model.Selection.prototype */{
  1417 	Selection;
       
  1418 
       
  1419 Selection = Attachments.extend({
       
  1420 	/**
  1542 	/**
  1421 	 * Refresh the `single` model whenever the selection changes.
  1543 	 * Refresh the `single` model whenever the selection changes.
  1422 	 * Binds `single` instead of using the context argument to ensure
  1544 	 * Binds `single` instead of using the context argument to ensure
  1423 	 * it receives no parameters.
  1545 	 * it receives no parameters.
  1424 	 *
  1546 	 *
  1498 	}
  1620 	}
  1499 });
  1621 });
  1500 
  1622 
  1501 module.exports = Selection;
  1623 module.exports = Selection;
  1502 
  1624 
  1503 },{}]},{},[1]);
  1625 
       
  1626 /***/ })
       
  1627 
       
  1628 /******/ });