1 window.wp = window.wp || {};
4 var Attachment, Attachments, Query, compare, l10n, media;
7 * wp.media( attributes )
9 * Handles the default media experience. Automatically creates
10 * and opens a media frame, and returns the result.
11 * Does nothing if the controllers do not exist.
13 * @param {object} attributes The properties passed to the main media controller.
14 * @return {object} A media workflow.
16 media = wp.media = function( attributes ) {
17 var MediaFrame = media.view.MediaFrame,
23 attributes = _.defaults( attributes || {}, {
27 if ( 'select' === attributes.frame && MediaFrame.Select )
28 frame = new MediaFrame.Select( attributes );
29 else if ( 'post' === attributes.frame && MediaFrame.Post )
30 frame = new MediaFrame.Post( attributes );
32 delete attributes.frame;
37 _.extend( media, { model: {}, view: {}, controller: {}, frames: {} });
39 // Link any localized strings.
40 l10n = media.model.l10n = typeof _wpMediaModelsL10n === 'undefined' ? {} : _wpMediaModelsL10n;
43 media.model.settings = l10n.settings || {};
47 * ========================================================================
49 * ========================================================================
55 * @param {mixed} a The primary parameter to compare.
56 * @param {mixed} b The primary parameter to compare.
57 * @param {string} ac The fallback parameter to compare, a's cid.
58 * @param {string} bc The fallback parameter to compare, b's cid.
59 * @return {number} -1: a should come before b.
60 * 0: a and b are of the same rank.
61 * 1: b should come before a.
63 compare = function( a, b, ac, bc ) {
64 if ( _.isEqual( a, b ) )
65 return ac === bc ? 0 : (ac > bc ? -1 : 1);
67 return a > b ? -1 : 1;
72 * media.template( id )
74 * Fetches a template by id.
76 * @param {string} id A string that corresponds to a DOM element with an id prefixed with "tmpl-".
77 * For example, "attachment" maps to "tmpl-attachment".
78 * @return {function} A function that lazily-compiles the template requested.
80 template: _.memoize( function( id ) {
83 evaluate: /<#([\s\S]+?)#>/g,
84 interpolate: /\{\{\{([\s\S]+?)\}\}\}/g,
85 escape: /\{\{([^\}]+?)\}\}(?!\})/g,
89 return function( data ) {
90 compiled = compiled || _.template( $( '#tmpl-' + id ).html(), null, options );
91 return compiled( data );
96 * media.post( [action], [data] )
98 * Sends a POST request to WordPress.
100 * @param {string} action The slug of the action to fire in WordPress.
101 * @param {object} data The data to populate $_POST with.
102 * @return {$.promise} A jQuery promise that represents the request.
104 post: function( action, data ) {
106 data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
111 * media.ajax( [action], [options] )
113 * Sends a POST request to WordPress.
115 * @param {string} action The slug of the action to fire in WordPress.
116 * @param {object} options The options passed to jQuery.ajax.
117 * @return {$.promise} A jQuery promise that represents the request.
119 ajax: function( action, options ) {
120 if ( _.isObject( action ) ) {
123 options = options || {};
124 options.data = _.extend( options.data || {}, { action: action });
127 options = _.defaults( options || {}, {
129 url: media.model.settings.ajaxurl,
133 return $.Deferred( function( deferred ) {
134 // Transfer success/error callbacks.
135 if ( options.success )
136 deferred.done( options.success );
138 deferred.fail( options.error );
140 delete options.success;
141 delete options.error;
143 // Use with PHP's wp_send_json_success() and wp_send_json_error()
144 $.ajax( options ).done( function( response ) {
145 // Treat a response of `1` as successful for backwards
146 // compatibility with existing handlers.
147 if ( response === '1' || response === 1 )
148 response = { success: true };
150 if ( _.isObject( response ) && ! _.isUndefined( response.success ) )
151 deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
153 deferred.rejectWith( this, [response] );
154 }).fail( function() {
155 deferred.rejectWith( this, arguments );
160 // Scales a set of dimensions to fit within bounding dimensions.
161 fit: function( dimensions ) {
162 var width = dimensions.width,
163 height = dimensions.height,
164 maxWidth = dimensions.maxWidth,
165 maxHeight = dimensions.maxHeight,
168 // Compare ratios between the two values to determine which
169 // max to constrain by. If a max value doesn't exist, then the
170 // opposite side is the constraint.
171 if ( ! _.isUndefined( maxWidth ) && ! _.isUndefined( maxHeight ) ) {
172 constraint = ( width / height > maxWidth / maxHeight ) ? 'width' : 'height';
173 } else if ( _.isUndefined( maxHeight ) ) {
174 constraint = 'width';
175 } else if ( _.isUndefined( maxWidth ) && height > maxHeight ) {
176 constraint = 'height';
179 // If the value of the constrained side is larger than the max,
180 // then scale the values. Otherwise return the originals; they fit.
181 if ( 'width' === constraint && width > maxWidth ) {
184 height: Math.round( maxWidth * height / width )
186 } else if ( 'height' === constraint && height > maxHeight ) {
188 width : Math.round( maxHeight * width / height ),
199 // Truncates a string by injecting an ellipsis into the middle.
200 // Useful for filenames.
201 truncate: function( string, length, replacement ) {
202 length = length || 30;
203 replacement = replacement || '…';
205 if ( string.length <= length )
208 return string.substr( 0, length / 2 ) + replacement + string.substr( -1 * length / 2 );
214 * ========================================================================
216 * ========================================================================
220 * wp.media.attachment
222 media.attachment = function( id ) {
223 return Attachment.get( id );
227 * wp.media.model.Attachment
229 Attachment = media.model.Attachment = Backbone.Model.extend({
230 sync: function( method, model, options ) {
231 // If the attachment does not yet have an `id`, return an instantly
232 // rejected promise. Otherwise, all of our requests will fail.
233 if ( _.isUndefined( this.id ) )
234 return $.Deferred().rejectWith( this ).promise();
236 // Overload the `read` request so Attachment.fetch() functions correctly.
237 if ( 'read' === method ) {
238 options = options || {};
239 options.context = this;
240 options.data = _.extend( options.data || {}, {
241 action: 'get-attachment',
244 return media.ajax( options );
246 // Overload the `update` request so properties can be saved.
247 } else if ( 'update' === method ) {
248 // If we do not have the necessary nonce, fail immeditately.
249 if ( ! this.get('nonces') || ! this.get('nonces').update )
250 return $.Deferred().rejectWith( this ).promise();
252 options = options || {};
253 options.context = this;
255 // Set the action and ID.
256 options.data = _.extend( options.data || {}, {
257 action: 'save-attachment',
259 nonce: this.get('nonces').update,
260 post_id: media.model.settings.post.id
263 // Record the values of the changed attributes.
264 if ( options.changes ) {
265 _.each( options.changes, function( value, key ) {
266 options.changes[ key ] = this.get( key );
269 options.data.changes = options.changes;
270 delete options.changes;
273 return media.ajax( options );
275 // Overload the `delete` request so attachments can be removed.
276 // This will permanently delete an attachment.
277 } else if ( 'delete' === method ) {
278 options = options || {};
280 if ( ! options.wait )
281 this.destroyed = true;
283 options.context = this;
284 options.data = _.extend( options.data || {}, {
285 action: 'delete-post',
287 _wpnonce: this.get('nonces')['delete']
290 return media.ajax( options ).done( function() {
291 this.destroyed = true;
292 }).fail( function() {
293 this.destroyed = false;
298 parse: function( resp, xhr ) {
302 // Convert date strings into Date objects.
303 resp.date = new Date( resp.date );
304 resp.modified = new Date( resp.modified );
308 saveCompat: function( data, options ) {
311 // If we do not have the necessary nonce, fail immeditately.
312 if ( ! this.get('nonces') || ! this.get('nonces').update )
313 return $.Deferred().rejectWith( this ).promise();
315 return media.post( 'save-attachment-compat', _.defaults({
317 nonce: this.get('nonces').update,
318 post_id: media.model.settings.post.id
319 }, data ) ).done( function( resp, status, xhr ) {
320 model.set( model.parse( resp, xhr ), options );
324 create: function( attrs ) {
325 return Attachments.all.push( attrs );
328 get: _.memoize( function( id, attachment ) {
329 return Attachments.all.push( attachment || { id: id } );
334 * wp.media.model.Attachments
336 Attachments = media.model.Attachments = Backbone.Collection.extend({
339 initialize: function( models, options ) {
340 options = options || {};
342 this.props = new Backbone.Model();
343 this.filters = options.filters || {};
345 // Bind default `change` events to the `props` model.
346 this.props.on( 'change', this._changeFilteredProps, this );
348 this.props.on( 'change:order', this._changeOrder, this );
349 this.props.on( 'change:orderby', this._changeOrderby, this );
350 this.props.on( 'change:query', this._changeQuery, this );
352 // Set the `props` model and fill the default property values.
353 this.props.set( _.defaults( options.props || {} ) );
355 // Observe another `Attachments` collection if one is provided.
356 if ( options.observe )
357 this.observe( options.observe );
360 // Automatically sort the collection when the order changes.
361 _changeOrder: function( model, order ) {
362 if ( this.comparator )
366 // Set the default comparator only when the `orderby` property is set.
367 _changeOrderby: function( model, orderby ) {
368 // If a different comparator is defined, bail.
369 if ( this.comparator && this.comparator !== Attachments.comparator )
372 if ( orderby && 'post__in' !== orderby )
373 this.comparator = Attachments.comparator;
375 delete this.comparator;
378 // If the `query` property is set to true, query the server using
379 // the `props` values, and sync the results to this collection.
380 _changeQuery: function( model, query ) {
382 this.props.on( 'change', this._requery, this );
385 this.props.off( 'change', this._requery, this );
389 _changeFilteredProps: function( model, options ) {
390 // If this is a query, updating the collection will be handled by
391 // `this._requery()`.
392 if ( this.props.get('query') )
395 var changed = _.chain( options.changes ).map( function( t, prop ) {
396 var filter = Attachments.filters[ prop ],
397 term = model.get( prop );
402 if ( term && ! this.filters[ prop ] )
403 this.filters[ prop ] = filter;
404 else if ( ! term && this.filters[ prop ] === filter )
405 delete this.filters[ prop ];
409 // Record the change.
411 }, this ).any().value();
416 // If no `Attachments` model is provided to source the searches
417 // from, then automatically generate a source from the existing
419 if ( ! this._source )
420 this._source = new Attachments( this.models );
422 this.reset( this._source.filter( this.validator, this ) );
425 validateDestroyed: false,
427 validator: function( attachment ) {
428 if ( ! this.validateDestroyed && attachment.destroyed )
430 return _.all( this.filters, function( filter, key ) {
431 return !! filter.call( this, attachment );
435 validate: function( attachment, options ) {
436 var valid = this.validator( attachment ),
437 hasAttachment = !! this.getByCid( attachment.cid );
439 if ( ! valid && hasAttachment )
440 this.remove( attachment, options );
441 else if ( valid && ! hasAttachment )
442 this.add( attachment, options );
447 validateAll: function( attachments, options ) {
448 options = options || {};
450 _.each( attachments.models, function( attachment ) {
451 this.validate( attachment, { silent: true });
454 if ( ! options.silent )
455 this.trigger( 'reset', this, options );
460 observe: function( attachments ) {
461 this.observers = this.observers || [];
462 this.observers.push( attachments );
464 attachments.on( 'add change remove', this._validateHandler, this );
465 attachments.on( 'reset', this._validateAllHandler, this );
467 this.validateAll( attachments );
471 unobserve: function( attachments ) {
473 attachments.off( null, null, this );
474 this.observers = _.without( this.observers, attachments );
477 _.each( this.observers, function( attachments ) {
478 attachments.off( null, null, this );
480 delete this.observers;
486 _validateHandler: function( attachment, attachments, options ) {
487 // If we're not mirroring this `attachments` collection,
488 // only retain the `silent` option.
489 options = attachments === this.mirroring ? options : {
490 silent: options && options.silent
493 return this.validate( attachment, options );
496 _validateAllHandler: function( attachments, options ) {
497 return this.validateAll( attachments, options );
500 mirror: function( attachments ) {
501 if ( this.mirroring && this.mirroring === attachments )
505 this.mirroring = attachments;
507 // Clear the collection silently. A `reset` event will be fired
508 // when `observe()` calls `validateAll()`.
509 this.reset( [], { silent: true } );
510 this.observe( attachments );
515 unmirror: function() {
516 if ( ! this.mirroring )
519 this.unobserve( this.mirroring );
520 delete this.mirroring;
523 more: function( options ) {
524 var deferred = $.Deferred(),
525 mirroring = this.mirroring,
528 if ( ! mirroring || ! mirroring.more )
529 return deferred.resolveWith( this ).promise();
531 // If we're mirroring another collection, forward `more` to
532 // the mirrored collection. Account for a race condition by
533 // checking if we're still mirroring that collection when
534 // the request resolves.
535 mirroring.more( options ).done( function() {
536 if ( this === attachments.mirroring )
537 deferred.resolveWith( this );
540 return deferred.promise();
543 hasMore: function() {
544 return this.mirroring ? this.mirroring.hasMore() : false;
547 parse: function( resp, xhr ) {
548 return _.map( resp, function( attrs ) {
549 var attachment = Attachment.get( attrs.id );
550 return attachment.set( attachment.parse( attrs, xhr ) );
554 _requery: function() {
555 if ( this.props.get('query') )
556 this.mirror( Query.get( this.props.toJSON() ) );
559 // If this collection is sorted by `menuOrder`, recalculates and saves
560 // the menu order to the database.
561 saveMenuOrder: function() {
562 if ( 'menuOrder' !== this.props.get('orderby') )
565 // Removes any uploading attachments, updates each attachment's
566 // menu order, and returns an object with an { id: menuOrder }
567 // mapping to pass to the request.
568 var attachments = this.chain().filter( function( attachment ) {
569 return ! _.isUndefined( attachment.id );
570 }).map( function( attachment, index ) {
571 // Indices start at 1.
573 attachment.set( 'menuOrder', index );
574 return [ attachment.id, index ];
577 if ( _.isEmpty( attachments ) )
580 return media.post( 'save-attachment-order', {
581 nonce: media.model.settings.post.nonce,
582 post_id: media.model.settings.post.id,
583 attachments: attachments
587 comparator: function( a, b, options ) {
588 var key = this.props.get('orderby'),
589 order = this.props.get('order') || 'DESC',
596 if ( 'date' === key || 'modified' === key ) {
601 // If `options.ties` is set, don't enforce the `cid` tiebreaker.
602 if ( options && options.ties )
605 return ( 'DESC' === order ) ? compare( a, b, ac, bc ) : compare( b, a, bc, ac );
609 // Note that this client-side searching is *not* equivalent
610 // to our server-side searching.
611 search: function( attachment ) {
612 if ( ! this.props.get('search') )
615 return _.any(['title','filename','description','caption','name'], function( key ) {
616 var value = attachment.get( key );
617 return value && -1 !== value.search( this.props.get('search') );
621 type: function( attachment ) {
622 var type = this.props.get('type');
623 return ! type || -1 !== type.indexOf( attachment.get('type') );
626 uploadedTo: function( attachment ) {
627 var uploadedTo = this.props.get('uploadedTo');
628 if ( _.isUndefined( uploadedTo ) )
631 return uploadedTo === attachment.get('uploadedTo');
636 Attachments.all = new Attachments();
641 media.query = function( props ) {
642 return new Attachments( null, {
643 props: _.extend( _.defaults( props || {}, { orderby: 'date' } ), { query: true } )
648 * wp.media.model.Query
650 * A set of attachments that corresponds to a set of consecutively paged
651 * queries on the server.
653 * Note: Do NOT change this.args after the query has been initialized.
656 Query = media.model.Query = Attachments.extend({
657 initialize: function( models, options ) {
660 options = options || {};
661 Attachments.prototype.initialize.apply( this, arguments );
663 this.args = options.args;
664 this._hasMore = true;
665 this.created = new Date();
667 this.filters.order = function( attachment ) {
668 var orderby = this.props.get('orderby'),
669 order = this.props.get('order');
671 if ( ! this.comparator )
674 // We want any items that can be placed before the last
675 // item in the set. If we add any items after the last
676 // item, then we can't guarantee the set is complete.
678 return 1 !== this.comparator( attachment, this.last(), { ties: true });
680 // Handle the case where there are no items yet and
681 // we're sorting for recent items. In that case, we want
682 // changes that occurred after we created the query.
683 } else if ( 'DESC' === order && ( 'date' === orderby || 'modified' === orderby ) ) {
684 return attachment.get( orderby ) >= this.created;
686 // If we're sorting by menu order and we have no items,
687 // accept any items that have the default menu order (0).
688 } else if ( 'ASC' === order && 'menuOrder' === orderby ) {
689 return attachment.get( orderby ) === 0;
692 // Otherwise, we don't want any items yet.
696 // Observe the central `wp.Uploader.queue` collection to watch for
697 // new matches for the query.
699 // Only observe when a limited number of query args are set. There
700 // are no filters for other properties, so observing will result in
701 // false positives in those queries.
702 allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type', 'post_parent' ];
703 if ( wp.Uploader && _( this.args ).chain().keys().difference( allowed ).isEmpty().value() )
704 this.observe( wp.Uploader.queue );
707 hasMore: function() {
708 return this._hasMore;
711 more: function( options ) {
714 if ( this._more && 'pending' === this._more.state() )
717 if ( ! this.hasMore() )
718 return $.Deferred().resolveWith( this ).promise();
720 options = options || {};
723 return this._more = this.fetch( options ).done( function( resp ) {
724 if ( _.isEmpty( resp ) || -1 === this.args.posts_per_page || resp.length < this.args.posts_per_page )
725 query._hasMore = false;
729 sync: function( method, model, options ) {
732 // Overload the read method so Attachment.fetch() functions correctly.
733 if ( 'read' === method ) {
734 options = options || {};
735 options.context = this;
736 options.data = _.extend( options.data || {}, {
737 action: 'query-attachments',
738 post_id: media.model.settings.post.id
741 // Clone the args so manipulation is non-destructive.
742 args = _.clone( this.args );
744 // Determine which page to query.
745 if ( -1 !== args.posts_per_page )
746 args.paged = Math.floor( this.length / args.posts_per_page ) + 1;
748 options.data.query = args;
749 return media.ajax( options );
751 // Otherwise, fall back to Backbone.sync()
753 fallback = Attachments.prototype.sync ? Attachments.prototype : Backbone;
754 return fallback.sync.apply( this, arguments );
768 allowed: [ 'name', 'author', 'date', 'title', 'modified', 'uploadedTo', 'id', 'post__in', 'menuOrder' ],
771 'uploadedTo': 'parent',
772 'menuOrder': 'menu_order ID'
778 'type': 'post_mime_type',
779 'perPage': 'posts_per_page',
780 'menuOrder': 'menu_order',
781 'uploadedTo': 'post_parent'
784 // Caches query objects so queries can be easily reused.
788 return function( props, options ) {
790 orderby = Query.orderby,
791 defaults = Query.defaultProps,
794 // Remove the `query` property. This isn't linked to a query,
795 // this *is* the query.
798 // Fill default args.
799 _.defaults( props, defaults );
801 // Normalize the order.
802 props.order = props.order.toUpperCase();
803 if ( 'DESC' !== props.order && 'ASC' !== props.order )
804 props.order = defaults.order.toUpperCase();
806 // Ensure we have a valid orderby value.
807 if ( ! _.contains( orderby.allowed, props.orderby ) )
808 props.orderby = defaults.orderby;
810 // Generate the query `args` object.
811 // Correct any differing property names.
812 _.each( props, function( value, prop ) {
813 if ( _.isNull( value ) )
816 args[ Query.propmap[ prop ] || prop ] = value;
819 // Fill any other default query args.
820 _.defaults( args, Query.defaultArgs );
822 // `props.orderby` does not always map directly to `args.orderby`.
823 // Substitute exceptions specified in orderby.keymap.
824 args.orderby = orderby.valuemap[ props.orderby ] || props.orderby;
826 // Search the query cache for matches.
827 query = _.find( queries, function( query ) {
828 return _.isEqual( query.args, args );
831 // Otherwise, create a new query and add it to the cache.
833 query = new Query( [], _.extend( options || {}, {
837 queries.push( query );
846 * wp.media.model.Selection
848 * Used to manage a selection of attachments in the views.
850 media.model.Selection = Attachments.extend({
851 initialize: function( models, options ) {
852 Attachments.prototype.initialize.apply( this, arguments );
853 this.multiple = options && options.multiple;
855 // Refresh the `single` model whenever the selection changes.
856 // Binds `single` instead of using the context argument to ensure
857 // it receives no parameters.
858 this.on( 'add remove reset', _.bind( this.single, this, false ) );
861 // Override the selection's add method.
862 // If the workflow does not support multiple
863 // selected attachments, reset the selection.
864 add: function( models, options ) {
865 if ( ! this.multiple )
866 this.remove( this.models );
868 return Attachments.prototype.add.call( this, models, options );
871 single: function( model ) {
872 var previous = this._single;
874 // If a `model` is provided, use it as the single model.
876 this._single = model;
878 // If the single model isn't in the selection, remove it.
879 if ( this._single && ! this.getByCid( this._single.cid ) )
882 this._single = this._single || this.last();
884 // If single has changed, fire an event.
885 if ( this._single !== previous ) {
887 previous.trigger( 'selection:unsingle', previous, this );
889 // If the model was already removed, trigger the collection
891 if ( ! this.getByCid( previous.cid ) )
892 this.trigger( 'selection:unsingle', previous, this );
895 this._single.trigger( 'selection:single', this._single, this );
898 // Return the single model, or the last model as a fallback.
903 // Clean up. Prevents mobile browsers caching
904 $(window).on('unload', function(){