/wp-includes/js/media-models.js
JavaScript | 908 lines | 559 code | 164 blank | 185 comment | 155 complexity | 92a5d9fdc580782fefac7329ae7c43ab MD5 | raw file
1window.wp = window.wp || {};
2
3(function($){
4 var Attachment, Attachments, Query, compare, l10n, media;
5
6 /**
7 * wp.media( attributes )
8 *
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.
12 *
13 * @param {object} attributes The properties passed to the main media controller.
14 * @return {object} A media workflow.
15 */
16 media = wp.media = function( attributes ) {
17 var MediaFrame = media.view.MediaFrame,
18 frame;
19
20 if ( ! MediaFrame )
21 return;
22
23 attributes = _.defaults( attributes || {}, {
24 frame: 'select'
25 });
26
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 );
31
32 delete attributes.frame;
33
34 return frame;
35 };
36
37 _.extend( media, { model: {}, view: {}, controller: {}, frames: {} });
38
39 // Link any localized strings.
40 l10n = media.model.l10n = typeof _wpMediaModelsL10n === 'undefined' ? {} : _wpMediaModelsL10n;
41
42 // Link any settings.
43 media.model.settings = l10n.settings || {};
44 delete l10n.settings;
45
46 /**
47 * ========================================================================
48 * UTILITIES
49 * ========================================================================
50 */
51
52 /**
53 * A basic comparator.
54 *
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.
62 */
63 compare = function( a, b, ac, bc ) {
64 if ( _.isEqual( a, b ) )
65 return ac === bc ? 0 : (ac > bc ? -1 : 1);
66 else
67 return a > b ? -1 : 1;
68 };
69
70 _.extend( media, {
71 /**
72 * media.template( id )
73 *
74 * Fetches a template by id.
75 *
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.
79 */
80 template: _.memoize( function( id ) {
81 var compiled,
82 options = {
83 evaluate: /<#([\s\S]+?)#>/g,
84 interpolate: /\{\{\{([\s\S]+?)\}\}\}/g,
85 escape: /\{\{([^\}]+?)\}\}(?!\})/g,
86 variable: 'data'
87 };
88
89 return function( data ) {
90 compiled = compiled || _.template( $( '#tmpl-' + id ).html(), null, options );
91 return compiled( data );
92 };
93 }),
94
95 /**
96 * media.post( [action], [data] )
97 *
98 * Sends a POST request to WordPress.
99 *
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.
103 */
104 post: function( action, data ) {
105 return media.ajax({
106 data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
107 });
108 },
109
110 /**
111 * media.ajax( [action], [options] )
112 *
113 * Sends a POST request to WordPress.
114 *
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.
118 */
119 ajax: function( action, options ) {
120 if ( _.isObject( action ) ) {
121 options = action;
122 } else {
123 options = options || {};
124 options.data = _.extend( options.data || {}, { action: action });
125 }
126
127 options = _.defaults( options || {}, {
128 type: 'POST',
129 url: media.model.settings.ajaxurl,
130 context: this
131 });
132
133 return $.Deferred( function( deferred ) {
134 // Transfer success/error callbacks.
135 if ( options.success )
136 deferred.done( options.success );
137 if ( options.error )
138 deferred.fail( options.error );
139
140 delete options.success;
141 delete options.error;
142
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 };
149
150 if ( _.isObject( response ) && ! _.isUndefined( response.success ) )
151 deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
152 else
153 deferred.rejectWith( this, [response] );
154 }).fail( function() {
155 deferred.rejectWith( this, arguments );
156 });
157 }).promise();
158 },
159
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,
166 constraint;
167
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';
177 }
178
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 ) {
182 return {
183 width : maxWidth,
184 height: Math.round( maxWidth * height / width )
185 };
186 } else if ( 'height' === constraint && height > maxHeight ) {
187 return {
188 width : Math.round( maxHeight * width / height ),
189 height: maxHeight
190 };
191 } else {
192 return {
193 width : width,
194 height: height
195 };
196 }
197 },
198
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 || '…';
204
205 if ( string.length <= length )
206 return string;
207
208 return string.substr( 0, length / 2 ) + replacement + string.substr( -1 * length / 2 );
209 }
210 });
211
212
213 /**
214 * ========================================================================
215 * MODELS
216 * ========================================================================
217 */
218
219 /**
220 * wp.media.attachment
221 */
222 media.attachment = function( id ) {
223 return Attachment.get( id );
224 };
225
226 /**
227 * wp.media.model.Attachment
228 */
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();
235
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',
242 id: this.id
243 });
244 return media.ajax( options );
245
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();
251
252 options = options || {};
253 options.context = this;
254
255 // Set the action and ID.
256 options.data = _.extend( options.data || {}, {
257 action: 'save-attachment',
258 id: this.id,
259 nonce: this.get('nonces').update,
260 post_id: media.model.settings.post.id
261 });
262
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 );
267 }, this );
268
269 options.data.changes = options.changes;
270 delete options.changes;
271 }
272
273 return media.ajax( options );
274
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 || {};
279
280 if ( ! options.wait )
281 this.destroyed = true;
282
283 options.context = this;
284 options.data = _.extend( options.data || {}, {
285 action: 'delete-post',
286 id: this.id,
287 _wpnonce: this.get('nonces')['delete']
288 });
289
290 return media.ajax( options ).done( function() {
291 this.destroyed = true;
292 }).fail( function() {
293 this.destroyed = false;
294 });
295 }
296 },
297
298 parse: function( resp, xhr ) {
299 if ( ! resp )
300 return resp;
301
302 // Convert date strings into Date objects.
303 resp.date = new Date( resp.date );
304 resp.modified = new Date( resp.modified );
305 return resp;
306 },
307
308 saveCompat: function( data, options ) {
309 var model = this;
310
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();
314
315 return media.post( 'save-attachment-compat', _.defaults({
316 id: this.id,
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 );
321 });
322 }
323 }, {
324 create: function( attrs ) {
325 return Attachments.all.push( attrs );
326 },
327
328 get: _.memoize( function( id, attachment ) {
329 return Attachments.all.push( attachment || { id: id } );
330 })
331 });
332
333 /**
334 * wp.media.model.Attachments
335 */
336 Attachments = media.model.Attachments = Backbone.Collection.extend({
337 model: Attachment,
338
339 initialize: function( models, options ) {
340 options = options || {};
341
342 this.props = new Backbone.Model();
343 this.filters = options.filters || {};
344
345 // Bind default `change` events to the `props` model.
346 this.props.on( 'change', this._changeFilteredProps, this );
347
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 );
351
352 // Set the `props` model and fill the default property values.
353 this.props.set( _.defaults( options.props || {} ) );
354
355 // Observe another `Attachments` collection if one is provided.
356 if ( options.observe )
357 this.observe( options.observe );
358 },
359
360 // Automatically sort the collection when the order changes.
361 _changeOrder: function( model, order ) {
362 if ( this.comparator )
363 this.sort();
364 },
365
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 )
370 return;
371
372 if ( orderby && 'post__in' !== orderby )
373 this.comparator = Attachments.comparator;
374 else
375 delete this.comparator;
376 },
377
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 ) {
381 if ( query ) {
382 this.props.on( 'change', this._requery, this );
383 this._requery();
384 } else {
385 this.props.off( 'change', this._requery, this );
386 }
387 },
388
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') )
393 return;
394
395 var changed = _.chain( options.changes ).map( function( t, prop ) {
396 var filter = Attachments.filters[ prop ],
397 term = model.get( prop );
398
399 if ( ! filter )
400 return;
401
402 if ( term && ! this.filters[ prop ] )
403 this.filters[ prop ] = filter;
404 else if ( ! term && this.filters[ prop ] === filter )
405 delete this.filters[ prop ];
406 else
407 return;
408
409 // Record the change.
410 return true;
411 }, this ).any().value();
412
413 if ( ! changed )
414 return;
415
416 // If no `Attachments` model is provided to source the searches
417 // from, then automatically generate a source from the existing
418 // models.
419 if ( ! this._source )
420 this._source = new Attachments( this.models );
421
422 this.reset( this._source.filter( this.validator, this ) );
423 },
424
425 validateDestroyed: false,
426
427 validator: function( attachment ) {
428 if ( ! this.validateDestroyed && attachment.destroyed )
429 return false;
430 return _.all( this.filters, function( filter, key ) {
431 return !! filter.call( this, attachment );
432 }, this );
433 },
434
435 validate: function( attachment, options ) {
436 var valid = this.validator( attachment ),
437 hasAttachment = !! this.getByCid( attachment.cid );
438
439 if ( ! valid && hasAttachment )
440 this.remove( attachment, options );
441 else if ( valid && ! hasAttachment )
442 this.add( attachment, options );
443
444 return this;
445 },
446
447 validateAll: function( attachments, options ) {
448 options = options || {};
449
450 _.each( attachments.models, function( attachment ) {
451 this.validate( attachment, { silent: true });
452 }, this );
453
454 if ( ! options.silent )
455 this.trigger( 'reset', this, options );
456
457 return this;
458 },
459
460 observe: function( attachments ) {
461 this.observers = this.observers || [];
462 this.observers.push( attachments );
463
464 attachments.on( 'add change remove', this._validateHandler, this );
465 attachments.on( 'reset', this._validateAllHandler, this );
466
467 this.validateAll( attachments );
468 return this;
469 },
470
471 unobserve: function( attachments ) {
472 if ( attachments ) {
473 attachments.off( null, null, this );
474 this.observers = _.without( this.observers, attachments );
475
476 } else {
477 _.each( this.observers, function( attachments ) {
478 attachments.off( null, null, this );
479 }, this );
480 delete this.observers;
481 }
482
483 return this;
484 },
485
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
491 };
492
493 return this.validate( attachment, options );
494 },
495
496 _validateAllHandler: function( attachments, options ) {
497 return this.validateAll( attachments, options );
498 },
499
500 mirror: function( attachments ) {
501 if ( this.mirroring && this.mirroring === attachments )
502 return this;
503
504 this.unmirror();
505 this.mirroring = attachments;
506
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 );
511
512 return this;
513 },
514
515 unmirror: function() {
516 if ( ! this.mirroring )
517 return;
518
519 this.unobserve( this.mirroring );
520 delete this.mirroring;
521 },
522
523 more: function( options ) {
524 var deferred = $.Deferred(),
525 mirroring = this.mirroring,
526 attachments = this;
527
528 if ( ! mirroring || ! mirroring.more )
529 return deferred.resolveWith( this ).promise();
530
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 );
538 });
539
540 return deferred.promise();
541 },
542
543 hasMore: function() {
544 return this.mirroring ? this.mirroring.hasMore() : false;
545 },
546
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 ) );
551 });
552 },
553
554 _requery: function() {
555 if ( this.props.get('query') )
556 this.mirror( Query.get( this.props.toJSON() ) );
557 },
558
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') )
563 return;
564
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.
572 index = index + 1;
573 attachment.set( 'menuOrder', index );
574 return [ attachment.id, index ];
575 }).object().value();
576
577 if ( _.isEmpty( attachments ) )
578 return;
579
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
584 });
585 }
586 }, {
587 comparator: function( a, b, options ) {
588 var key = this.props.get('orderby'),
589 order = this.props.get('order') || 'DESC',
590 ac = a.cid,
591 bc = b.cid;
592
593 a = a.get( key );
594 b = b.get( key );
595
596 if ( 'date' === key || 'modified' === key ) {
597 a = a || new Date();
598 b = b || new Date();
599 }
600
601 // If `options.ties` is set, don't enforce the `cid` tiebreaker.
602 if ( options && options.ties )
603 ac = bc = null;
604
605 return ( 'DESC' === order ) ? compare( a, b, ac, bc ) : compare( b, a, bc, ac );
606 },
607
608 filters: {
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') )
613 return true;
614
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') );
618 }, this );
619 },
620
621 type: function( attachment ) {
622 var type = this.props.get('type');
623 return ! type || -1 !== type.indexOf( attachment.get('type') );
624 },
625
626 uploadedTo: function( attachment ) {
627 var uploadedTo = this.props.get('uploadedTo');
628 if ( _.isUndefined( uploadedTo ) )
629 return true;
630
631 return uploadedTo === attachment.get('uploadedTo');
632 }
633 }
634 });
635
636 Attachments.all = new Attachments();
637
638 /**
639 * wp.media.query
640 */
641 media.query = function( props ) {
642 return new Attachments( null, {
643 props: _.extend( _.defaults( props || {}, { orderby: 'date' } ), { query: true } )
644 });
645 };
646
647 /**
648 * wp.media.model.Query
649 *
650 * A set of attachments that corresponds to a set of consecutively paged
651 * queries on the server.
652 *
653 * Note: Do NOT change this.args after the query has been initialized.
654 * Things will break.
655 */
656 Query = media.model.Query = Attachments.extend({
657 initialize: function( models, options ) {
658 var allowed;
659
660 options = options || {};
661 Attachments.prototype.initialize.apply( this, arguments );
662
663 this.args = options.args;
664 this._hasMore = true;
665 this.created = new Date();
666
667 this.filters.order = function( attachment ) {
668 var orderby = this.props.get('orderby'),
669 order = this.props.get('order');
670
671 if ( ! this.comparator )
672 return true;
673
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.
677 if ( this.length ) {
678 return 1 !== this.comparator( attachment, this.last(), { ties: true });
679
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;
685
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;
690 }
691
692 // Otherwise, we don't want any items yet.
693 return false;
694 };
695
696 // Observe the central `wp.Uploader.queue` collection to watch for
697 // new matches for the query.
698 //
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 );
705 },
706
707 hasMore: function() {
708 return this._hasMore;
709 },
710
711 more: function( options ) {
712 var query = this;
713
714 if ( this._more && 'pending' === this._more.state() )
715 return this._more;
716
717 if ( ! this.hasMore() )
718 return $.Deferred().resolveWith( this ).promise();
719
720 options = options || {};
721 options.add = true;
722
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;
726 });
727 },
728
729 sync: function( method, model, options ) {
730 var fallback;
731
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
739 });
740
741 // Clone the args so manipulation is non-destructive.
742 args = _.clone( this.args );
743
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;
747
748 options.data.query = args;
749 return media.ajax( options );
750
751 // Otherwise, fall back to Backbone.sync()
752 } else {
753 fallback = Attachments.prototype.sync ? Attachments.prototype : Backbone;
754 return fallback.sync.apply( this, arguments );
755 }
756 }
757 }, {
758 defaultProps: {
759 orderby: 'date',
760 order: 'DESC'
761 },
762
763 defaultArgs: {
764 posts_per_page: 40
765 },
766
767 orderby: {
768 allowed: [ 'name', 'author', 'date', 'title', 'modified', 'uploadedTo', 'id', 'post__in', 'menuOrder' ],
769 valuemap: {
770 'id': 'ID',
771 'uploadedTo': 'parent',
772 'menuOrder': 'menu_order ID'
773 }
774 },
775
776 propmap: {
777 'search': 's',
778 'type': 'post_mime_type',
779 'perPage': 'posts_per_page',
780 'menuOrder': 'menu_order',
781 'uploadedTo': 'post_parent'
782 },
783
784 // Caches query objects so queries can be easily reused.
785 get: (function(){
786 var queries = [];
787
788 return function( props, options ) {
789 var args = {},
790 orderby = Query.orderby,
791 defaults = Query.defaultProps,
792 query;
793
794 // Remove the `query` property. This isn't linked to a query,
795 // this *is* the query.
796 delete props.query;
797
798 // Fill default args.
799 _.defaults( props, defaults );
800
801 // Normalize the order.
802 props.order = props.order.toUpperCase();
803 if ( 'DESC' !== props.order && 'ASC' !== props.order )
804 props.order = defaults.order.toUpperCase();
805
806 // Ensure we have a valid orderby value.
807 if ( ! _.contains( orderby.allowed, props.orderby ) )
808 props.orderby = defaults.orderby;
809
810 // Generate the query `args` object.
811 // Correct any differing property names.
812 _.each( props, function( value, prop ) {
813 if ( _.isNull( value ) )
814 return;
815
816 args[ Query.propmap[ prop ] || prop ] = value;
817 });
818
819 // Fill any other default query args.
820 _.defaults( args, Query.defaultArgs );
821
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;
825
826 // Search the query cache for matches.
827 query = _.find( queries, function( query ) {
828 return _.isEqual( query.args, args );
829 });
830
831 // Otherwise, create a new query and add it to the cache.
832 if ( ! query ) {
833 query = new Query( [], _.extend( options || {}, {
834 props: props,
835 args: args
836 } ) );
837 queries.push( query );
838 }
839
840 return query;
841 };
842 }())
843 });
844
845 /**
846 * wp.media.model.Selection
847 *
848 * Used to manage a selection of attachments in the views.
849 */
850 media.model.Selection = Attachments.extend({
851 initialize: function( models, options ) {
852 Attachments.prototype.initialize.apply( this, arguments );
853 this.multiple = options && options.multiple;
854
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 ) );
859 },
860
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 );
867
868 return Attachments.prototype.add.call( this, models, options );
869 },
870
871 single: function( model ) {
872 var previous = this._single;
873
874 // If a `model` is provided, use it as the single model.
875 if ( model )
876 this._single = model;
877
878 // If the single model isn't in the selection, remove it.
879 if ( this._single && ! this.getByCid( this._single.cid ) )
880 delete this._single;
881
882 this._single = this._single || this.last();
883
884 // If single has changed, fire an event.
885 if ( this._single !== previous ) {
886 if ( previous ) {
887 previous.trigger( 'selection:unsingle', previous, this );
888
889 // If the model was already removed, trigger the collection
890 // event manually.
891 if ( ! this.getByCid( previous.cid ) )
892 this.trigger( 'selection:unsingle', previous, this );
893 }
894 if ( this._single )
895 this._single.trigger( 'selection:single', this._single, this );
896 }
897
898 // Return the single model, or the last model as a fallback.
899 return this._single;
900 }
901 });
902
903 // Clean up. Prevents mobile browsers caching
904 $(window).on('unload', function(){
905 window.wp = null;
906 });
907
908}(jQuery));