media-models.js 43 KB

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