From a3210a100d155e23abbfac997dd6be8a78d4f284 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 9 Feb 2015 16:00:44 +0000 Subject: [PATCH] Media JS files: * In media manifests, ditch IIFEs and global injection, these get dynamically scoped via Browserify * Remove the `debug` option from `browserify:media` * Add `jshint:media` to `jshint:corejs` * Add a trailing newline to all new module files Props iseulde. See #28510. git-svn-id: https://develop.svn.wordpress.org/trunk@31385 602fd350-edb4-49c9-b593-d223f7449a82 --- Gruntfile.js | 23 +- src/wp-includes/js/media/audio-video.js | 566 ++++++++---------- .../js/media/audio-video.manifest.js | 413 +++++++------ .../js/media/controllers/audio-details.js | 4 +- .../js/media/controllers/collection-add.js | 4 +- .../js/media/controllers/collection-edit.js | 4 +- .../js/media/controllers/cropper.js | 6 +- .../controllers/edit-attachment-metadata.js | 4 +- .../js/media/controllers/edit-image.js | 4 +- src/wp-includes/js/media/controllers/embed.js | 4 +- .../js/media/controllers/featured-image.js | 4 +- .../js/media/controllers/gallery-add.js | 10 +- .../js/media/controllers/gallery-edit.js | 11 +- .../js/media/controllers/image-details.js | 4 +- .../js/media/controllers/library.js | 8 +- .../js/media/controllers/media-library.js | 4 +- .../js/media/controllers/region.js | 4 +- .../js/media/controllers/replace-image.js | 4 +- .../js/media/controllers/state-machine.js | 4 +- src/wp-includes/js/media/controllers/state.js | 4 +- .../js/media/controllers/video-details.js | 4 +- src/wp-includes/js/media/grid.js | 177 +++--- src/wp-includes/js/media/grid.manifest.js | 24 +- src/wp-includes/js/media/models.js | 448 +++++++------- src/wp-includes/js/media/models.manifest.js | 429 +++++++------ src/wp-includes/js/media/models/attachment.js | 6 +- .../js/media/models/attachments.js | 7 +- src/wp-includes/js/media/models/post-image.js | 4 +- src/wp-includes/js/media/models/post-media.js | 4 +- src/wp-includes/js/media/models/query.js | 4 +- src/wp-includes/js/media/models/selection.js | 4 +- src/wp-includes/js/media/routers/manage.js | 4 +- .../js/media/utils/selection-sync.js | 4 +- src/wp-includes/js/media/views.js | 483 +++++++-------- src/wp-includes/js/media/views.manifest.js | 259 ++++---- .../js/media/views/attachment-compat.js | 4 +- .../js/media/views/attachment-filters.js | 4 +- .../js/media/views/attachment-filters/all.js | 4 +- .../js/media/views/attachment-filters/date.js | 4 +- .../views/attachment-filters/uploaded.js | 4 +- src/wp-includes/js/media/views/attachment.js | 4 +- .../views/attachment/details-two-column.js | 6 +- .../js/media/views/attachment/details.js | 6 +- .../js/media/views/attachment/edit-library.js | 2 +- .../media/views/attachment/edit-selection.js | 2 +- .../js/media/views/attachment/library.js | 2 +- .../js/media/views/attachment/selection.js | 2 +- src/wp-includes/js/media/views/attachments.js | 6 +- .../js/media/views/attachments/browser.js | 10 +- .../js/media/views/attachments/selection.js | 4 +- .../js/media/views/audio-details.js | 4 +- .../js/media/views/button-group.js | 4 +- src/wp-includes/js/media/views/button.js | 4 +- .../button/delete-selected-permanently.js | 2 +- .../js/media/views/button/delete-selected.js | 4 +- .../media/views/button/select-mode-toggle.js | 4 +- src/wp-includes/js/media/views/cropper.js | 6 +- .../js/media/views/edit-image-details.js | 2 +- src/wp-includes/js/media/views/edit-image.js | 4 +- src/wp-includes/js/media/views/embed.js | 2 +- src/wp-includes/js/media/views/embed/image.js | 4 +- src/wp-includes/js/media/views/embed/link.js | 4 +- src/wp-includes/js/media/views/embed/url.js | 4 +- .../js/media/views/focus-manager.js | 2 +- src/wp-includes/js/media/views/frame.js | 4 +- .../js/media/views/frame/audio-details.js | 4 +- .../js/media/views/frame/edit-attachments.js | 4 +- .../js/media/views/frame/image-details.js | 4 +- .../js/media/views/frame/manage.js | 6 +- .../js/media/views/frame/media-details.js | 4 +- src/wp-includes/js/media/views/frame/post.js | 4 +- .../js/media/views/frame/select.js | 4 +- .../js/media/views/frame/video-details.js | 4 +- src/wp-includes/js/media/views/iframe.js | 2 +- .../js/media/views/image-details.js | 8 +- src/wp-includes/js/media/views/label.js | 2 +- .../js/media/views/media-details.js | 8 +- src/wp-includes/js/media/views/media-frame.js | 4 +- src/wp-includes/js/media/views/menu-item.js | 4 +- src/wp-includes/js/media/views/menu.js | 2 +- src/wp-includes/js/media/views/modal.js | 4 +- .../js/media/views/priority-list.js | 4 +- src/wp-includes/js/media/views/router-item.js | 2 +- src/wp-includes/js/media/views/router.js | 2 +- src/wp-includes/js/media/views/search.js | 4 +- src/wp-includes/js/media/views/selection.js | 4 +- src/wp-includes/js/media/views/settings.js | 6 +- .../views/settings/attachment-display.js | 4 +- .../js/media/views/settings/gallery.js | 4 +- .../js/media/views/settings/playlist.js | 4 +- src/wp-includes/js/media/views/sidebar.js | 2 +- src/wp-includes/js/media/views/spinner.js | 4 +- src/wp-includes/js/media/views/toolbar.js | 4 +- .../js/media/views/toolbar/embed.js | 4 +- .../js/media/views/toolbar/select.js | 4 +- .../js/media/views/uploader/editor.js | 4 +- .../js/media/views/uploader/inline.js | 4 +- .../js/media/views/uploader/status-error.js | 4 +- .../js/media/views/uploader/status.js | 4 +- .../js/media/views/uploader/window.js | 4 +- .../js/media/views/video-details.js | 4 +- src/wp-includes/js/media/views/view.js | 4 +- 102 files changed, 1473 insertions(+), 1743 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index f4defd3306..a6a9901902 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -123,8 +123,7 @@ module.exports = function(grunt) { 'src/wp-includes/js/media/views.js' : [ SOURCE_DIR + 'wp-includes/js/media/views.manifest.js' ], 'src/wp-includes/js/media/audio-video.js' : [ SOURCE_DIR + 'wp-includes/js/media/audio-video.manifest.js' ], 'src/wp-includes/js/media/grid.js' : [ SOURCE_DIR + 'wp-includes/js/media/grid.manifest.js' ] - }, - options: { debug: true } + } } }, sass: { @@ -235,6 +234,18 @@ module.exports = function(grunt) { '!twenty{fourteen,fifteen}/js/html5.js' ] }, + media: { + options: { + browserify: true + }, + expand: true, + cwd: SOURCE_DIR, + src: [ + 'wp-includes/js/media/**/*.js', + '!wp-includes/js/media/*.js', + 'wp-includes/js/media/*.manifest.js' + ] + }, core: { expand: true, cwd: SOURCE_DIR, @@ -507,7 +518,13 @@ module.exports = function(grunt) { grunt.registerTask('colors', ['sass:colors', 'autoprefixer:colors']); // JSHint task. - grunt.registerTask('jshint:corejs', ['jshint:grunt', 'jshint:tests', 'jshint:themes', 'jshint:core']); + grunt.registerTask( 'jshint:corejs', [ + 'jshint:grunt', + 'jshint:tests', + 'jshint:themes', + 'jshint:core', + 'jshint:media' + ] ); // Pre-commit task. grunt.registerTask('precommit', 'Runs front-end dev/test tasks in preparation for a commit.', diff --git a/src/wp-includes/js/media/audio-video.js b/src/wp-includes/js/media/audio-video.js index ca4ef75f91..2e0b287db5 100644 --- a/src/wp-includes/js/media/audio-video.js +++ b/src/wp-includes/js/media/audio-video.js @@ -1,231 +1,221 @@ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o bc ? -1 : 1); + } else { + return a > b ? -1 : 1; + } +}; + +_.extend( 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, /** - * Create and return a media frame. + * media.post( [action], [data] ) * - * Handles the default media experience. + * Sends a POST request to WordPress. + * See wp.ajax.post() in `wp-includes/js/wp-util.js`. * - * @param {object} attributes The properties passed to the main media controller. - * @return {wp.media.view.MediaFrame} A media workflow. + * @borrows wp.ajax.post as post */ - media = wp.media = function( attributes ) { - var MediaFrame = media.view.MediaFrame, - frame; + post: wp.ajax.post, - if ( ! MediaFrame ) { - return; + /** + * 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'; } - 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; - }; - - _.extend( media, { model: {}, view: {}, controller: {}, frames: {} }); - - // Link any localized strings. - l10n = media.model.l10n = typeof _wpMediaModelsL10n === 'undefined' ? {} : _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); + // 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 a > b ? -1 : 1; + 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 || '…'; - _.extend( 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 ); + 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 } ) }); +}; - /** - * ======================================================================== - * 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 ); - }; +// Clean up. Prevents mobile browsers caching +$(window).on('unload', function(){ + window.wp = null; +}); - /** - * 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; - }); - -}(jQuery)); },{"./models/attachment.js":2,"./models/attachments.js":3,"./models/post-image.js":4,"./models/query.js":5,"./models/selection.js":6}],2:[function(require,module,exports){ -/*globals jQuery, Backbone, _, wp */ - /** * wp.media.model.Attachment * @@ -362,7 +358,7 @@ Attachment = Backbone.Model.extend({ return $.Deferred().rejectWith( this ).promise(); } - return media.post( 'save-attachment-compat', _.defaults({ + return wp.media.post( 'save-attachment-compat', _.defaults({ id: this.id, nonce: this.get('nonces').update, post_id: wp.media.model.settings.post.id @@ -400,9 +396,8 @@ Attachment = Backbone.Model.extend({ }); module.exports = Attachment; -},{"./attachments.js":3}],3:[function(require,module,exports){ -/*globals jQuery, Backbone, _, wp */ +},{"./attachments.js":3}],3:[function(require,module,exports){ /** * wp.media.model.Attachments * @@ -726,8 +721,9 @@ Attachments = Backbone.Collection.extend({ // checking if we're still mirroring that collection when // the request resolves. mirroring.more( options ).done( function() { - if ( this === attachments.mirroring ) + if ( this === attachments.mirroring ) { deferred.resolveWith( this ); + } }); return deferred.promise(); @@ -936,9 +932,8 @@ Attachments = Backbone.Collection.extend({ }); module.exports = Attachments; -},{"./attachment.js":2,"./query.js":5}],4:[function(require,module,exports){ -/*globals jQuery, Backbone */ +},{"./attachment.js":2,"./query.js":5}],4:[function(require,module,exports){ /** * wp.media.model.PostImage * @@ -1093,9 +1088,8 @@ PostImage = Backbone.Model.extend({ }); module.exports = PostImage; -},{"./attachment":2}],5:[function(require,module,exports){ -/*globals jQuery, _, wp */ +},{"./attachment":2}],5:[function(require,module,exports){ /** * wp.media.model.Query * @@ -1402,9 +1396,8 @@ Query = Attachments.extend({ }); module.exports = Query; -},{"./attachments.js":3}],6:[function(require,module,exports){ -/*globals _ */ +},{"./attachments.js":3}],6:[function(require,module,exports){ /** * wp.media.model.Selection * @@ -1500,4 +1493,5 @@ Selection = Attachments.extend({ }); module.exports = Selection; + },{"./attachments.js":3}]},{},[1]); diff --git a/src/wp-includes/js/media/models.manifest.js b/src/wp-includes/js/media/models.manifest.js index 86d42a9740..a6fd5b4c49 100644 --- a/src/wp-includes/js/media/models.manifest.js +++ b/src/wp-includes/js/media/models.manifest.js @@ -1,232 +1,229 @@ -/* global _wpMediaModelsL10n:false */ +var $ = jQuery, + Attachment, Attachments, l10n, media; + window.wp = window.wp || {}; -(function($){ - var Attachment, Attachments, l10n, media; +/** + * 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' + }); + + 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, { + /** + * 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, /** - * Create and return a media frame. + * media.post( [action], [data] ) * - * Handles the default media experience. + * Sends a POST request to WordPress. + * See wp.ajax.post() in `wp-includes/js/wp-util.js`. * - * @param {object} attributes The properties passed to the main media controller. - * @return {wp.media.view.MediaFrame} A media workflow. + * @borrows wp.ajax.post as post */ - media = wp.media = function( attributes ) { - var MediaFrame = media.view.MediaFrame, - frame; + post: wp.ajax.post, - if ( ! MediaFrame ) { - return; + /** + * 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'; } - 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; - }; - - _.extend( media, { model: {}, view: {}, controller: {}, frames: {} }); - - // Link any localized strings. - l10n = media.model.l10n = typeof _wpMediaModelsL10n === 'undefined' ? {} : _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); + // 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 a > b ? -1 : 1; + 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 || '…'; - _.extend( 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 ); + 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 } ) }); +}; - /** - * ======================================================================== - * 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; - }); - -}(jQuery)); \ No newline at end of file +// Clean up. Prevents mobile browsers caching +$(window).on('unload', function(){ + window.wp = null; +}); diff --git a/src/wp-includes/js/media/models/attachment.js b/src/wp-includes/js/media/models/attachment.js index c9852d8078..e89fa5c195 100644 --- a/src/wp-includes/js/media/models/attachment.js +++ b/src/wp-includes/js/media/models/attachment.js @@ -1,5 +1,3 @@ -/*globals jQuery, Backbone, _, wp */ - /** * wp.media.model.Attachment * @@ -128,7 +126,7 @@ Attachment = Backbone.Model.extend({ return $.Deferred().rejectWith( this ).promise(); } - return media.post( 'save-attachment-compat', _.defaults({ + return wp.media.post( 'save-attachment-compat', _.defaults({ id: this.id, nonce: this.get('nonces').update, post_id: wp.media.model.settings.post.id @@ -165,4 +163,4 @@ Attachment = Backbone.Model.extend({ }) }); -module.exports = Attachment; \ No newline at end of file +module.exports = Attachment; diff --git a/src/wp-includes/js/media/models/attachments.js b/src/wp-includes/js/media/models/attachments.js index 608e675e80..9d6f178882 100644 --- a/src/wp-includes/js/media/models/attachments.js +++ b/src/wp-includes/js/media/models/attachments.js @@ -1,5 +1,3 @@ -/*globals jQuery, Backbone, _, wp */ - /** * wp.media.model.Attachments * @@ -323,8 +321,9 @@ Attachments = Backbone.Collection.extend({ // checking if we're still mirroring that collection when // the request resolves. mirroring.more( options ).done( function() { - if ( this === attachments.mirroring ) + if ( this === attachments.mirroring ) { deferred.resolveWith( this ); + } }); return deferred.promise(); @@ -532,4 +531,4 @@ Attachments = Backbone.Collection.extend({ } }); -module.exports = Attachments; \ No newline at end of file +module.exports = Attachments; diff --git a/src/wp-includes/js/media/models/post-image.js b/src/wp-includes/js/media/models/post-image.js index 407206e547..1cdbce4557 100644 --- a/src/wp-includes/js/media/models/post-image.js +++ b/src/wp-includes/js/media/models/post-image.js @@ -1,5 +1,3 @@ -/*globals jQuery, Backbone */ - /** * wp.media.model.PostImage * @@ -153,4 +151,4 @@ PostImage = Backbone.Model.extend({ } }); -module.exports = PostImage; \ No newline at end of file +module.exports = PostImage; diff --git a/src/wp-includes/js/media/models/post-media.js b/src/wp-includes/js/media/models/post-media.js index 658e7b3613..4b484f2e16 100644 --- a/src/wp-includes/js/media/models/post-media.js +++ b/src/wp-includes/js/media/models/post-media.js @@ -1,5 +1,3 @@ -/*globals Backbone, _, wp */ - /** * Shared model class for audio and video. Updates the model after * "Add Audio|Video Source" and "Replace Audio|Video" states return @@ -37,4 +35,4 @@ var PostMedia = Backbone.Model.extend({ } }); -module.exports = PostMedia; \ No newline at end of file +module.exports = PostMedia; diff --git a/src/wp-includes/js/media/models/query.js b/src/wp-includes/js/media/models/query.js index 12280cf174..0ca84a788c 100644 --- a/src/wp-includes/js/media/models/query.js +++ b/src/wp-includes/js/media/models/query.js @@ -1,5 +1,3 @@ -/*globals jQuery, _, wp */ - /** * wp.media.model.Query * @@ -305,4 +303,4 @@ Query = Attachments.extend({ }()) }); -module.exports = Query; \ No newline at end of file +module.exports = Query; diff --git a/src/wp-includes/js/media/models/selection.js b/src/wp-includes/js/media/models/selection.js index 803fbe4cb6..df64f0a2c4 100644 --- a/src/wp-includes/js/media/models/selection.js +++ b/src/wp-includes/js/media/models/selection.js @@ -1,5 +1,3 @@ -/*globals _ */ - /** * wp.media.model.Selection * @@ -94,4 +92,4 @@ Selection = Attachments.extend({ } }); -module.exports = Selection; \ No newline at end of file +module.exports = Selection; diff --git a/src/wp-includes/js/media/routers/manage.js b/src/wp-includes/js/media/routers/manage.js index d9e5835aa0..00a227e6af 100644 --- a/src/wp-includes/js/media/routers/manage.js +++ b/src/wp-includes/js/media/routers/manage.js @@ -1,5 +1,3 @@ -/*globals jQuery, Backbone */ - /** * A router for handling the browser history and application state. * @@ -43,4 +41,4 @@ var Router = Backbone.Router.extend({ } }); -module.exports = Router; \ No newline at end of file +module.exports = Router; diff --git a/src/wp-includes/js/media/utils/selection-sync.js b/src/wp-includes/js/media/utils/selection-sync.js index 41f5b5bdf1..d22b419d89 100644 --- a/src/wp-includes/js/media/utils/selection-sync.js +++ b/src/wp-includes/js/media/utils/selection-sync.js @@ -1,5 +1,3 @@ -/*globals _ */ - /** * wp.media.selectionSync * @@ -63,4 +61,4 @@ var selectionSync = { } }; -module.exports = selectionSync; \ No newline at end of file +module.exports = selectionSync; diff --git a/src/wp-includes/js/media/views.js b/src/wp-includes/js/media/views.js index b33b013bd7..df178b83ea 100644 --- a/src/wp-includes/js/media/views.js +++ b/src/wp-includes/js/media/views.js @@ -1,6 +1,4 @@ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o