PageRenderTime 103ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/js/media-models.js

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