PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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