/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
- var $ = jQuery,
- Attachment, Attachments, l10n, media;
- /** @namespace wp */
- window.wp = window.wp || {};
- /**
- * Create and return a media frame.
- *
- * Handles the default media experience.
- *
- * @alias wp.media
- * @memberOf wp
- * @namespace
- *
- * @param {object} attributes The properties passed to the main media controller.
- * @return {wp.media.view.MediaFrame} A media workflow.
- */
- media = wp.media = function( attributes ) {
- var MediaFrame = media.view.MediaFrame,
- frame;
- if ( ! MediaFrame ) {
- return;
- }
- attributes = _.defaults( attributes || {}, {
- frame: 'select'
- });
- if ( 'select' === attributes.frame && MediaFrame.Select ) {
- frame = new MediaFrame.Select( attributes );
- } else if ( 'post' === attributes.frame && MediaFrame.Post ) {
- frame = new MediaFrame.Post( attributes );
- } else if ( 'manage' === attributes.frame && MediaFrame.Manage ) {
- frame = new MediaFrame.Manage( attributes );
- } else if ( 'image' === attributes.frame && MediaFrame.ImageDetails ) {
- frame = new MediaFrame.ImageDetails( attributes );
- } else if ( 'audio' === attributes.frame && MediaFrame.AudioDetails ) {
- frame = new MediaFrame.AudioDetails( attributes );
- } else if ( 'video' === attributes.frame && MediaFrame.VideoDetails ) {
- frame = new MediaFrame.VideoDetails( attributes );
- } else if ( 'edit-attachments' === attributes.frame && MediaFrame.EditAttachments ) {
- frame = new MediaFrame.EditAttachments( attributes );
- }
- delete attributes.frame;
- media.frame = frame;
- return frame;
- };
- /** @namespace wp.media.model */
- /** @namespace wp.media.view */
- /** @namespace wp.media.controller */
- /** @namespace wp.media.frames */
- _.extend( media, { model: {}, view: {}, controller: {}, frames: {} });
- // Link any localized strings.
- l10n = media.model.l10n = window._wpMediaModelsL10n || {};
- // Link any settings.
- media.model.settings = l10n.settings || {};
- delete l10n.settings;
- Attachment = media.model.Attachment = require( '../../../media/models/attachment.js' );
- Attachments = media.model.Attachments = require( '../../../media/models/attachments.js' );
- media.model.Query = require( '../../../media/models/query.js' );
- media.model.PostImage = require( '../../../media/models/post-image.js' );
- media.model.Selection = require( '../../../media/models/selection.js' );
- /**
- * ========================================================================
- * UTILITIES
- * ========================================================================
- */
- /**
- * A basic equality comparator for Backbone models.
- *
- * Used to order models within a collection - @see wp.media.model.Attachments.comparator().
- *
- * @param {mixed} a The primary parameter to compare.
- * @param {mixed} b The primary parameter to compare.
- * @param {string} ac The fallback parameter to compare, a's cid.
- * @param {string} bc The fallback parameter to compare, b's cid.
- * @return {number} -1: a should come before b.
- * 0: a and b are of the same rank.
- * 1: b should come before a.
- */
- media.compare = function( a, b, ac, bc ) {
- if ( _.isEqual( a, b ) ) {
- return ac === bc ? 0 : (ac > bc ? -1 : 1);
- } else {
- return a > b ? -1 : 1;
- }
- };
- _.extend( media, /** @lends wp.media */{
- /**
- * media.template( id )
- *
- * Fetch a JavaScript template for an id, and return a templating function for it.
- *
- * 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;
- }
- return string.substr( 0, length / 2 ) + replacement + string.substr( -1 * length / 2 );
- }
- });
- /**
- * ========================================================================
- * MODELS
- * ========================================================================
- */
- /**
- * wp.media.attachment
- *
- * @static
- * @param {String} id A string used to identify a model.
- * @returns {wp.media.model.Attachment}
- */
- media.attachment = function( id ) {
- return Attachment.get( id );
- };
- /**
- * A collection of all attachments that have been fetched from the server.
- *
- * @static
- * @member {wp.media.model.Attachments}
- */
- Attachments.all = new Attachments();
- /**
- * wp.media.query
- *
- * Shorthand for creating a new Attachments Query.
- *
- * @param {object} [props]
- * @returns {wp.media.model.Attachments}
- */
- media.query = function( props ) {
- return new Attachments( null, {
- props: _.extend( _.defaults( props || {}, { orderby: 'date' } ), { query: true } )
- });
- };
- // Clean up. Prevents mobile browsers caching
- $(window).on('unload', function(){
- window.wp = null;
- });