- };
-
- _.extend( media, {
- /**
- * media.template( id )
- *
- * Fetches a template by id.
- * See wp.template() in `wp-includes/js/wp-util.js`.
- *
- * @borrows wp.template as template
- */
- template: wp.template,
-
- /**
- * media.post( [action], [data] )
- *
- * Sends a POST request to WordPress.
- * See wp.ajax.post() in `wp-includes/js/wp-util.js`.
- *
- * @borrows wp.ajax.post as post
- */
- post: wp.ajax.post,
-
- /**
- * media.ajax( [action], [options] )
- *
- * Sends an XHR request to WordPress.
- * See wp.ajax.send() in `wp-includes/js/wp-util.js`.
- *
- * @borrows wp.ajax.send as ajax
- */
- ajax: wp.ajax.send,
-
- /**
- * Scales a set of dimensions to fit within bounding dimensions.
- *
- * @param {Object} dimensions
- * @returns {Object}
- */
- fit: function( dimensions ) {
- var width = dimensions.width,
- height = dimensions.height,
- maxWidth = dimensions.maxWidth,
- maxHeight = dimensions.maxHeight,
- constraint;
-
- // Compare ratios between the two values to determine which
- // max to constrain by. If a max value doesn't exist, then the
- // opposite side is the constraint.
- if ( ! _.isUndefined( maxWidth ) && ! _.isUndefined( maxHeight ) ) {
- constraint = ( width / height > maxWidth / maxHeight ) ? 'width' : 'height';
- } else if ( _.isUndefined( maxHeight ) ) {
- constraint = 'width';
- } else if ( _.isUndefined( maxWidth ) && height > maxHeight ) {
- constraint = 'height';
- }
-
- // If the value of the constrained side is larger than the max,
- // then scale the values. Otherwise return the originals; they fit.
- if ( 'width' === constraint && width > maxWidth ) {
- return {
- width : maxWidth,
- height: Math.round( maxWidth * height / width )
- };
- } else if ( 'height' === constraint && height > maxHeight ) {
- return {
- width : Math.round( maxHeight * width / height ),
- height: maxHeight
- };
- } else {
- return {
- width : width,
- height: height
- };
- }
- },
- /**
- * Truncates a string by injecting an ellipsis into the middle.
- * Useful for filenames.
- *
- * @param {String} string
- * @param {Number} [length=30]
- * @param {String} [replacement=…]
- * @returns {String} The string, unless length is greater than string.length.
- */
- truncate: function( string, length, replacement ) {
- length = length || 30;
- replacement = replacement || '…';
-
- if ( string.length <= length ) {
- return string;
- }