/src/js/_enqueues/wp/media/models.js

https://gitlab.com/morganestes/wordpress-develop · JavaScript · 238 lines · 102 code · 27 blank · 109 comment · 44 complexity · 9c80a3c17e69805583ffe5f94b13c33b MD5 · raw file

  1. var $ = jQuery,
  2. Attachment, Attachments, l10n, media;
  3. /** @namespace wp */
  4. window.wp = window.wp || {};
  5. /**
  6. * Create and return a media frame.
  7. *
  8. * Handles the default media experience.
  9. *
  10. * @alias wp.media
  11. * @memberOf wp
  12. * @namespace
  13. *
  14. * @param {object} attributes The properties passed to the main media controller.
  15. * @return {wp.media.view.MediaFrame} A media workflow.
  16. */
  17. media = wp.media = function( attributes ) {
  18. var MediaFrame = media.view.MediaFrame,
  19. frame;
  20. if ( ! MediaFrame ) {
  21. return;
  22. }
  23. attributes = _.defaults( attributes || {}, {
  24. frame: 'select'
  25. });
  26. if ( 'select' === attributes.frame && MediaFrame.Select ) {
  27. frame = new MediaFrame.Select( attributes );
  28. } else if ( 'post' === attributes.frame && MediaFrame.Post ) {
  29. frame = new MediaFrame.Post( attributes );
  30. } else if ( 'manage' === attributes.frame && MediaFrame.Manage ) {
  31. frame = new MediaFrame.Manage( attributes );
  32. } else if ( 'image' === attributes.frame && MediaFrame.ImageDetails ) {
  33. frame = new MediaFrame.ImageDetails( attributes );
  34. } else if ( 'audio' === attributes.frame && MediaFrame.AudioDetails ) {
  35. frame = new MediaFrame.AudioDetails( attributes );
  36. } else if ( 'video' === attributes.frame && MediaFrame.VideoDetails ) {
  37. frame = new MediaFrame.VideoDetails( attributes );
  38. } else if ( 'edit-attachments' === attributes.frame && MediaFrame.EditAttachments ) {
  39. frame = new MediaFrame.EditAttachments( attributes );
  40. }
  41. delete attributes.frame;
  42. media.frame = frame;
  43. return frame;
  44. };
  45. /** @namespace wp.media.model */
  46. /** @namespace wp.media.view */
  47. /** @namespace wp.media.controller */
  48. /** @namespace wp.media.frames */
  49. _.extend( media, { model: {}, view: {}, controller: {}, frames: {} });
  50. // Link any localized strings.
  51. l10n = media.model.l10n = window._wpMediaModelsL10n || {};
  52. // Link any settings.
  53. media.model.settings = l10n.settings || {};
  54. delete l10n.settings;
  55. Attachment = media.model.Attachment = require( '../../../media/models/attachment.js' );
  56. Attachments = media.model.Attachments = require( '../../../media/models/attachments.js' );
  57. media.model.Query = require( '../../../media/models/query.js' );
  58. media.model.PostImage = require( '../../../media/models/post-image.js' );
  59. media.model.Selection = require( '../../../media/models/selection.js' );
  60. /**
  61. * ========================================================================
  62. * UTILITIES
  63. * ========================================================================
  64. */
  65. /**
  66. * A basic equality comparator for Backbone models.
  67. *
  68. * Used to order models within a collection - @see wp.media.model.Attachments.comparator().
  69. *
  70. * @param {mixed} a The primary parameter to compare.
  71. * @param {mixed} b The primary parameter to compare.
  72. * @param {string} ac The fallback parameter to compare, a's cid.
  73. * @param {string} bc The fallback parameter to compare, b's cid.
  74. * @return {number} -1: a should come before b.
  75. * 0: a and b are of the same rank.
  76. * 1: b should come before a.
  77. */
  78. media.compare = function( a, b, ac, bc ) {
  79. if ( _.isEqual( a, b ) ) {
  80. return ac === bc ? 0 : (ac > bc ? -1 : 1);
  81. } else {
  82. return a > b ? -1 : 1;
  83. }
  84. };
  85. _.extend( media, /** @lends wp.media */{
  86. /**
  87. * media.template( id )
  88. *
  89. * Fetch a JavaScript template for an id, and return a templating function for it.
  90. *
  91. * See wp.template() in `wp-includes/js/wp-util.js`.
  92. *
  93. * @borrows wp.template as template
  94. */
  95. template: wp.template,
  96. /**
  97. * media.post( [action], [data] )
  98. *
  99. * Sends a POST request to WordPress.
  100. * See wp.ajax.post() in `wp-includes/js/wp-util.js`.
  101. *
  102. * @borrows wp.ajax.post as post
  103. */
  104. post: wp.ajax.post,
  105. /**
  106. * media.ajax( [action], [options] )
  107. *
  108. * Sends an XHR request to WordPress.
  109. * See wp.ajax.send() in `wp-includes/js/wp-util.js`.
  110. *
  111. * @borrows wp.ajax.send as ajax
  112. */
  113. ajax: wp.ajax.send,
  114. /**
  115. * Scales a set of dimensions to fit within bounding dimensions.
  116. *
  117. * @param {Object} dimensions
  118. * @returns {Object}
  119. */
  120. fit: function( dimensions ) {
  121. var width = dimensions.width,
  122. height = dimensions.height,
  123. maxWidth = dimensions.maxWidth,
  124. maxHeight = dimensions.maxHeight,
  125. constraint;
  126. // Compare ratios between the two values to determine which
  127. // max to constrain by. If a max value doesn't exist, then the
  128. // opposite side is the constraint.
  129. if ( ! _.isUndefined( maxWidth ) && ! _.isUndefined( maxHeight ) ) {
  130. constraint = ( width / height > maxWidth / maxHeight ) ? 'width' : 'height';
  131. } else if ( _.isUndefined( maxHeight ) ) {
  132. constraint = 'width';
  133. } else if ( _.isUndefined( maxWidth ) && height > maxHeight ) {
  134. constraint = 'height';
  135. }
  136. // If the value of the constrained side is larger than the max,
  137. // then scale the values. Otherwise return the originals; they fit.
  138. if ( 'width' === constraint && width > maxWidth ) {
  139. return {
  140. width : maxWidth,
  141. height: Math.round( maxWidth * height / width )
  142. };
  143. } else if ( 'height' === constraint && height > maxHeight ) {
  144. return {
  145. width : Math.round( maxHeight * width / height ),
  146. height: maxHeight
  147. };
  148. } else {
  149. return {
  150. width : width,
  151. height: height
  152. };
  153. }
  154. },
  155. /**
  156. * Truncates a string by injecting an ellipsis into the middle.
  157. * Useful for filenames.
  158. *
  159. * @param {String} string
  160. * @param {Number} [length=30]
  161. * @param {String} [replacement=…]
  162. * @returns {String} The string, unless length is greater than string.length.
  163. */
  164. truncate: function( string, length, replacement ) {
  165. length = length || 30;
  166. replacement = replacement || '…';
  167. if ( string.length <= length ) {
  168. return string;
  169. }
  170. return string.substr( 0, length / 2 ) + replacement + string.substr( -1 * length / 2 );
  171. }
  172. });
  173. /**
  174. * ========================================================================
  175. * MODELS
  176. * ========================================================================
  177. */
  178. /**
  179. * wp.media.attachment
  180. *
  181. * @static
  182. * @param {String} id A string used to identify a model.
  183. * @returns {wp.media.model.Attachment}
  184. */
  185. media.attachment = function( id ) {
  186. return Attachment.get( id );
  187. };
  188. /**
  189. * A collection of all attachments that have been fetched from the server.
  190. *
  191. * @static
  192. * @member {wp.media.model.Attachments}
  193. */
  194. Attachments.all = new Attachments();
  195. /**
  196. * wp.media.query
  197. *
  198. * Shorthand for creating a new Attachments Query.
  199. *
  200. * @param {object} [props]
  201. * @returns {wp.media.model.Attachments}
  202. */
  203. media.query = function( props ) {
  204. return new Attachments( null, {
  205. props: _.extend( _.defaults( props || {}, { orderby: 'date' } ), { query: true } )
  206. });
  207. };
  208. // Clean up. Prevents mobile browsers caching
  209. $(window).on('unload', function(){
  210. window.wp = null;
  211. });