2015-02-22 01:56:27 -05:00
|
|
|
/*globals wp, _, jQuery */
|
|
|
|
|
2015-02-09 11:01:29 -05:00
|
|
|
var $ = jQuery,
|
|
|
|
Attachment, Attachments, l10n, media;
|
|
|
|
|
2015-02-08 19:43:50 -05:00
|
|
|
window.wp = window.wp || {};
|
|
|
|
|
2015-02-09 11:01:29 -05:00
|
|
|
/**
|
|
|
|
* Create and return a media frame.
|
|
|
|
*
|
|
|
|
* Handles the default media experience.
|
|
|
|
*
|
|
|
|
* @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'
|
|
|
|
});
|
2015-02-08 19:43:50 -05:00
|
|
|
|
2015-02-09 11:01:29 -05:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
_.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;
|
|
|
|
|
|
|
|
Attachments = media.model.Attachments = require( './models/attachments.js' );
|
|
|
|
Attachment = media.model.Attachment = require( './models/attachment.js' );
|
|
|
|
|
|
|
|
media.model.Query = require( './models/query.js' );
|
|
|
|
media.model.PostImage = require( './models/post-image.js' );
|
|
|
|
media.model.Selection = require( './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, {
|
2015-02-08 19:43:50 -05:00
|
|
|
/**
|
2015-02-09 11:01:29 -05:00
|
|
|
* media.template( id )
|
2015-02-08 19:43:50 -05:00
|
|
|
*
|
2015-02-09 11:01:29 -05:00
|
|
|
* Fetch a JavaScript template for an id, and return a templating function for it.
|
2015-02-08 19:43:50 -05:00
|
|
|
*
|
2015-02-09 11:01:29 -05:00
|
|
|
* See wp.template() in `wp-includes/js/wp-util.js`.
|
|
|
|
*
|
|
|
|
* @borrows wp.template as template
|
2015-02-08 19:43:50 -05:00
|
|
|
*/
|
2015-02-09 11:01:29 -05:00
|
|
|
template: wp.template,
|
2015-02-08 19:43:50 -05:00
|
|
|
|
|
|
|
/**
|
2015-02-09 11:01:29 -05:00
|
|
|
* media.post( [action], [data] )
|
2015-02-08 19:43:50 -05:00
|
|
|
*
|
2015-02-09 11:01:29 -05:00
|
|
|
* Sends a POST request to WordPress.
|
|
|
|
* See wp.ajax.post() in `wp-includes/js/wp-util.js`.
|
2015-02-08 19:43:50 -05:00
|
|
|
*
|
2015-02-09 11:01:29 -05:00
|
|
|
* @borrows wp.ajax.post as post
|
2015-02-08 19:43:50 -05:00
|
|
|
*/
|
2015-02-09 11:01:29 -05:00
|
|
|
post: wp.ajax.post,
|
2015-02-08 19:43:50 -05:00
|
|
|
|
|
|
|
/**
|
2015-02-09 11:01:29 -05:00
|
|
|
* media.ajax( [action], [options] )
|
2015-02-08 19:43:50 -05:00
|
|
|
*
|
2015-02-09 11:01:29 -05:00
|
|
|
* Sends an XHR request to WordPress.
|
|
|
|
* See wp.ajax.send() in `wp-includes/js/wp-util.js`.
|
|
|
|
*
|
|
|
|
* @borrows wp.ajax.send as ajax
|
2015-02-08 19:43:50 -05:00
|
|
|
*/
|
2015-02-09 11:01:29 -05:00
|
|
|
ajax: wp.ajax.send,
|
2015-02-08 19:43:50 -05:00
|
|
|
|
|
|
|
/**
|
2015-02-09 11:01:29 -05:00
|
|
|
* Scales a set of dimensions to fit within bounding dimensions.
|
2015-02-08 19:43:50 -05:00
|
|
|
*
|
2015-02-09 11:01:29 -05:00
|
|
|
* @param {Object} dimensions
|
|
|
|
* @returns {Object}
|
2015-02-08 19:43:50 -05:00
|
|
|
*/
|
2015-02-09 11:01:29 -05:00
|
|
|
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';
|
|
|
|
}
|
2015-02-08 19:43:50 -05:00
|
|
|
|
2015-02-09 11:01:29 -05:00
|
|
|
// 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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
2015-02-08 19:43:50 -05:00
|
|
|
/**
|
2015-02-09 11:01:29 -05:00
|
|
|
* Truncates a string by injecting an ellipsis into the middle.
|
|
|
|
* Useful for filenames.
|
2015-02-08 19:43:50 -05:00
|
|
|
*
|
2015-02-09 11:01:29 -05:00
|
|
|
* @param {String} string
|
|
|
|
* @param {Number} [length=30]
|
|
|
|
* @param {String} [replacement=…]
|
|
|
|
* @returns {String} The string, unless length is greater than string.length.
|
2015-02-08 19:43:50 -05:00
|
|
|
*/
|
2015-02-09 11:01:29 -05:00
|
|
|
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 } )
|
2015-02-08 19:43:50 -05:00
|
|
|
});
|
2015-02-09 11:01:29 -05:00
|
|
|
};
|
2015-02-08 19:43:50 -05:00
|
|
|
|
2015-02-09 11:01:29 -05:00
|
|
|
// Clean up. Prevents mobile browsers caching
|
|
|
|
$(window).on('unload', function(){
|
|
|
|
window.wp = null;
|
|
|
|
});
|