0
|
1 |
window.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 |
* See wp.template() in `wp-includes/js/wp-util.js`. |
|
76 |
*/ |
|
77 |
template: wp.template, |
|
78 |
|
|
79 |
/** |
|
80 |
* media.post( [action], [data] ) |
|
81 |
* |
|
82 |
* Sends a POST request to WordPress. |
|
83 |
* See wp.ajax.post() in `wp-includes/js/wp-util.js`. |
|
84 |
*/ |
|
85 |
post: wp.ajax.post, |
|
86 |
|
|
87 |
/** |
|
88 |
* media.ajax( [action], [options] ) |
|
89 |
* |
|
90 |
* Sends an XHR request to WordPress. |
|
91 |
* See wp.ajax.send() in `wp-includes/js/wp-util.js`. |
|
92 |
*/ |
|
93 |
ajax: wp.ajax.send, |
|
94 |
|
|
95 |
// Scales a set of dimensions to fit within bounding dimensions. |
|
96 |
fit: function( dimensions ) { |
|
97 |
var width = dimensions.width, |
|
98 |
height = dimensions.height, |
|
99 |
maxWidth = dimensions.maxWidth, |
|
100 |
maxHeight = dimensions.maxHeight, |
|
101 |
constraint; |
|
102 |
|
|
103 |
// Compare ratios between the two values to determine which |
|
104 |
// max to constrain by. If a max value doesn't exist, then the |
|
105 |
// opposite side is the constraint. |
|
106 |
if ( ! _.isUndefined( maxWidth ) && ! _.isUndefined( maxHeight ) ) { |
|
107 |
constraint = ( width / height > maxWidth / maxHeight ) ? 'width' : 'height'; |
|
108 |
} else if ( _.isUndefined( maxHeight ) ) { |
|
109 |
constraint = 'width'; |
|
110 |
} else if ( _.isUndefined( maxWidth ) && height > maxHeight ) { |
|
111 |
constraint = 'height'; |
|
112 |
} |
|
113 |
|
|
114 |
// If the value of the constrained side is larger than the max, |
|
115 |
// then scale the values. Otherwise return the originals; they fit. |
|
116 |
if ( 'width' === constraint && width > maxWidth ) { |
|
117 |
return { |
|
118 |
width : maxWidth, |
|
119 |
height: Math.round( maxWidth * height / width ) |
|
120 |
}; |
|
121 |
} else if ( 'height' === constraint && height > maxHeight ) { |
|
122 |
return { |
|
123 |
width : Math.round( maxHeight * width / height ), |
|
124 |
height: maxHeight |
|
125 |
}; |
|
126 |
} else { |
|
127 |
return { |
|
128 |
width : width, |
|
129 |
height: height |
|
130 |
}; |
|
131 |
} |
|
132 |
}, |
|
133 |
|
|
134 |
// Truncates a string by injecting an ellipsis into the middle. |
|
135 |
// Useful for filenames. |
|
136 |
truncate: function( string, length, replacement ) { |
|
137 |
length = length || 30; |
|
138 |
replacement = replacement || '…'; |
|
139 |
|
|
140 |
if ( string.length <= length ) |
|
141 |
return string; |
|
142 |
|
|
143 |
return string.substr( 0, length / 2 ) + replacement + string.substr( -1 * length / 2 ); |
|
144 |
} |
|
145 |
}); |
|
146 |
|
|
147 |
|
|
148 |
/** |
|
149 |
* ======================================================================== |
|
150 |
* MODELS |
|
151 |
* ======================================================================== |
|
152 |
*/ |
|
153 |
|
|
154 |
/** |
|
155 |
* wp.media.attachment |
|
156 |
*/ |
|
157 |
media.attachment = function( id ) { |
|
158 |
return Attachment.get( id ); |
|
159 |
}; |
|
160 |
|
|
161 |
/** |
|
162 |
* wp.media.model.Attachment |
|
163 |
*/ |
|
164 |
Attachment = media.model.Attachment = Backbone.Model.extend({ |
|
165 |
sync: function( method, model, options ) { |
|
166 |
// If the attachment does not yet have an `id`, return an instantly |
|
167 |
// rejected promise. Otherwise, all of our requests will fail. |
|
168 |
if ( _.isUndefined( this.id ) ) |
|
169 |
return $.Deferred().rejectWith( this ).promise(); |
|
170 |
|
|
171 |
// Overload the `read` request so Attachment.fetch() functions correctly. |
|
172 |
if ( 'read' === method ) { |
|
173 |
options = options || {}; |
|
174 |
options.context = this; |
|
175 |
options.data = _.extend( options.data || {}, { |
|
176 |
action: 'get-attachment', |
|
177 |
id: this.id |
|
178 |
}); |
|
179 |
return media.ajax( options ); |
|
180 |
|
|
181 |
// Overload the `update` request so properties can be saved. |
|
182 |
} else if ( 'update' === method ) { |
|
183 |
// If we do not have the necessary nonce, fail immeditately. |
|
184 |
if ( ! this.get('nonces') || ! this.get('nonces').update ) |
|
185 |
return $.Deferred().rejectWith( this ).promise(); |
|
186 |
|
|
187 |
options = options || {}; |
|
188 |
options.context = this; |
|
189 |
|
|
190 |
// Set the action and ID. |
|
191 |
options.data = _.extend( options.data || {}, { |
|
192 |
action: 'save-attachment', |
|
193 |
id: this.id, |
|
194 |
nonce: this.get('nonces').update, |
|
195 |
post_id: media.model.settings.post.id |
|
196 |
}); |
|
197 |
|
|
198 |
// Record the values of the changed attributes. |
|
199 |
if ( model.hasChanged() ) { |
|
200 |
options.data.changes = {}; |
|
201 |
|
|
202 |
_.each( model.changed, function( value, key ) { |
|
203 |
options.data.changes[ key ] = this.get( key ); |
|
204 |
}, this ); |
|
205 |
} |
|
206 |
|
|
207 |
return media.ajax( options ); |
|
208 |
|
|
209 |
// Overload the `delete` request so attachments can be removed. |
|
210 |
// This will permanently delete an attachment. |
|
211 |
} else if ( 'delete' === method ) { |
|
212 |
options = options || {}; |
|
213 |
|
|
214 |
if ( ! options.wait ) |
|
215 |
this.destroyed = true; |
|
216 |
|
|
217 |
options.context = this; |
|
218 |
options.data = _.extend( options.data || {}, { |
|
219 |
action: 'delete-post', |
|
220 |
id: this.id, |
|
221 |
_wpnonce: this.get('nonces')['delete'] |
|
222 |
}); |
|
223 |
|
|
224 |
return media.ajax( options ).done( function() { |
|
225 |
this.destroyed = true; |
|
226 |
}).fail( function() { |
|
227 |
this.destroyed = false; |
|
228 |
}); |
|
229 |
|
|
230 |
// Otherwise, fall back to `Backbone.sync()`. |
|
231 |
} else { |
|
232 |
return Backbone.Model.prototype.sync.apply( this, arguments ); |
|
233 |
} |
|
234 |
}, |
|
235 |
|
|
236 |
parse: function( resp, xhr ) { |
|
237 |
if ( ! resp ) |
|
238 |
return resp; |
|
239 |
|
|
240 |
// Convert date strings into Date objects. |
|
241 |
resp.date = new Date( resp.date ); |
|
242 |
resp.modified = new Date( resp.modified ); |
|
243 |
return resp; |
|
244 |
}, |
|
245 |
|
|
246 |
saveCompat: function( data, options ) { |
|
247 |
var model = this; |
|
248 |
|
|
249 |
// If we do not have the necessary nonce, fail immeditately. |
|
250 |
if ( ! this.get('nonces') || ! this.get('nonces').update ) |
|
251 |
return $.Deferred().rejectWith( this ).promise(); |
|
252 |
|
|
253 |
return media.post( 'save-attachment-compat', _.defaults({ |
|
254 |
id: this.id, |
|
255 |
nonce: this.get('nonces').update, |
|
256 |
post_id: media.model.settings.post.id |
|
257 |
}, data ) ).done( function( resp, status, xhr ) { |
|
258 |
model.set( model.parse( resp, xhr ), options ); |
|
259 |
}); |
|
260 |
} |
|
261 |
}, { |
|
262 |
create: function( attrs ) { |
|
263 |
return Attachments.all.push( attrs ); |
|
264 |
}, |
|
265 |
|
|
266 |
get: _.memoize( function( id, attachment ) { |
|
267 |
return Attachments.all.push( attachment || { id: id } ); |
|
268 |
}) |
|
269 |
}); |
|
270 |
|
|
271 |
/** |
|
272 |
* wp.media.model.Attachments |
|
273 |
*/ |
|
274 |
Attachments = media.model.Attachments = Backbone.Collection.extend({ |
|
275 |
model: Attachment, |
|
276 |
|
|
277 |
initialize: function( models, options ) { |
|
278 |
options = options || {}; |
|
279 |
|
|
280 |
this.props = new Backbone.Model(); |
|
281 |
this.filters = options.filters || {}; |
|
282 |
|
|
283 |
// Bind default `change` events to the `props` model. |
|
284 |
this.props.on( 'change', this._changeFilteredProps, this ); |
|
285 |
|
|
286 |
this.props.on( 'change:order', this._changeOrder, this ); |
|
287 |
this.props.on( 'change:orderby', this._changeOrderby, this ); |
|
288 |
this.props.on( 'change:query', this._changeQuery, this ); |
|
289 |
|
|
290 |
// Set the `props` model and fill the default property values. |
|
291 |
this.props.set( _.defaults( options.props || {} ) ); |
|
292 |
|
|
293 |
// Observe another `Attachments` collection if one is provided. |
|
294 |
if ( options.observe ) |
|
295 |
this.observe( options.observe ); |
|
296 |
}, |
|
297 |
|
|
298 |
// Automatically sort the collection when the order changes. |
|
299 |
_changeOrder: function( model, order ) { |
|
300 |
if ( this.comparator ) |
|
301 |
this.sort(); |
|
302 |
}, |
|
303 |
|
|
304 |
// Set the default comparator only when the `orderby` property is set. |
|
305 |
_changeOrderby: function( model, orderby ) { |
|
306 |
// If a different comparator is defined, bail. |
|
307 |
if ( this.comparator && this.comparator !== Attachments.comparator ) |
|
308 |
return; |
|
309 |
|
|
310 |
if ( orderby && 'post__in' !== orderby ) |
|
311 |
this.comparator = Attachments.comparator; |
|
312 |
else |
|
313 |
delete this.comparator; |
|
314 |
}, |
|
315 |
|
|
316 |
// If the `query` property is set to true, query the server using |
|
317 |
// the `props` values, and sync the results to this collection. |
|
318 |
_changeQuery: function( model, query ) { |
|
319 |
if ( query ) { |
|
320 |
this.props.on( 'change', this._requery, this ); |
|
321 |
this._requery(); |
|
322 |
} else { |
|
323 |
this.props.off( 'change', this._requery, this ); |
|
324 |
} |
|
325 |
}, |
|
326 |
|
|
327 |
_changeFilteredProps: function( model, options ) { |
|
328 |
// If this is a query, updating the collection will be handled by |
|
329 |
// `this._requery()`. |
|
330 |
if ( this.props.get('query') ) |
|
331 |
return; |
|
332 |
|
|
333 |
var changed = _.chain( model.changed ).map( function( t, prop ) { |
|
334 |
var filter = Attachments.filters[ prop ], |
|
335 |
term = model.get( prop ); |
|
336 |
|
|
337 |
if ( ! filter ) |
|
338 |
return; |
|
339 |
|
|
340 |
if ( term && ! this.filters[ prop ] ) |
|
341 |
this.filters[ prop ] = filter; |
|
342 |
else if ( ! term && this.filters[ prop ] === filter ) |
|
343 |
delete this.filters[ prop ]; |
|
344 |
else |
|
345 |
return; |
|
346 |
|
|
347 |
// Record the change. |
|
348 |
return true; |
|
349 |
}, this ).any().value(); |
|
350 |
|
|
351 |
if ( ! changed ) |
|
352 |
return; |
|
353 |
|
|
354 |
// If no `Attachments` model is provided to source the searches |
|
355 |
// from, then automatically generate a source from the existing |
|
356 |
// models. |
|
357 |
if ( ! this._source ) |
|
358 |
this._source = new Attachments( this.models ); |
|
359 |
|
|
360 |
this.reset( this._source.filter( this.validator, this ) ); |
|
361 |
}, |
|
362 |
|
|
363 |
validateDestroyed: false, |
|
364 |
|
|
365 |
validator: function( attachment ) { |
|
366 |
if ( ! this.validateDestroyed && attachment.destroyed ) |
|
367 |
return false; |
|
368 |
return _.all( this.filters, function( filter, key ) { |
|
369 |
return !! filter.call( this, attachment ); |
|
370 |
}, this ); |
|
371 |
}, |
|
372 |
|
|
373 |
validate: function( attachment, options ) { |
|
374 |
var valid = this.validator( attachment ), |
|
375 |
hasAttachment = !! this.get( attachment.cid ); |
|
376 |
|
|
377 |
if ( ! valid && hasAttachment ) |
|
378 |
this.remove( attachment, options ); |
|
379 |
else if ( valid && ! hasAttachment ) |
|
380 |
this.add( attachment, options ); |
|
381 |
|
|
382 |
return this; |
|
383 |
}, |
|
384 |
|
|
385 |
validateAll: function( attachments, options ) { |
|
386 |
options = options || {}; |
|
387 |
|
|
388 |
_.each( attachments.models, function( attachment ) { |
|
389 |
this.validate( attachment, { silent: true }); |
|
390 |
}, this ); |
|
391 |
|
|
392 |
if ( ! options.silent ) |
|
393 |
this.trigger( 'reset', this, options ); |
|
394 |
|
|
395 |
return this; |
|
396 |
}, |
|
397 |
|
|
398 |
observe: function( attachments ) { |
|
399 |
this.observers = this.observers || []; |
|
400 |
this.observers.push( attachments ); |
|
401 |
|
|
402 |
attachments.on( 'add change remove', this._validateHandler, this ); |
|
403 |
attachments.on( 'reset', this._validateAllHandler, this ); |
|
404 |
this.validateAll( attachments ); |
|
405 |
return this; |
|
406 |
}, |
|
407 |
|
|
408 |
unobserve: function( attachments ) { |
|
409 |
if ( attachments ) { |
|
410 |
attachments.off( null, null, this ); |
|
411 |
this.observers = _.without( this.observers, attachments ); |
|
412 |
|
|
413 |
} else { |
|
414 |
_.each( this.observers, function( attachments ) { |
|
415 |
attachments.off( null, null, this ); |
|
416 |
}, this ); |
|
417 |
delete this.observers; |
|
418 |
} |
|
419 |
|
|
420 |
return this; |
|
421 |
}, |
|
422 |
|
|
423 |
_validateHandler: function( attachment, attachments, options ) { |
|
424 |
// If we're not mirroring this `attachments` collection, |
|
425 |
// only retain the `silent` option. |
|
426 |
options = attachments === this.mirroring ? options : { |
|
427 |
silent: options && options.silent |
|
428 |
}; |
|
429 |
|
|
430 |
return this.validate( attachment, options ); |
|
431 |
}, |
|
432 |
|
|
433 |
_validateAllHandler: function( attachments, options ) { |
|
434 |
return this.validateAll( attachments, options ); |
|
435 |
}, |
|
436 |
|
|
437 |
mirror: function( attachments ) { |
|
438 |
if ( this.mirroring && this.mirroring === attachments ) |
|
439 |
return this; |
|
440 |
|
|
441 |
this.unmirror(); |
|
442 |
this.mirroring = attachments; |
|
443 |
|
|
444 |
// Clear the collection silently. A `reset` event will be fired |
|
445 |
// when `observe()` calls `validateAll()`. |
|
446 |
this.reset( [], { silent: true } ); |
|
447 |
this.observe( attachments ); |
|
448 |
|
|
449 |
return this; |
|
450 |
}, |
|
451 |
|
|
452 |
unmirror: function() { |
|
453 |
if ( ! this.mirroring ) |
|
454 |
return; |
|
455 |
|
|
456 |
this.unobserve( this.mirroring ); |
|
457 |
delete this.mirroring; |
|
458 |
}, |
|
459 |
|
|
460 |
more: function( options ) { |
|
461 |
var deferred = $.Deferred(), |
|
462 |
mirroring = this.mirroring, |
|
463 |
attachments = this; |
|
464 |
|
|
465 |
if ( ! mirroring || ! mirroring.more ) |
|
466 |
return deferred.resolveWith( this ).promise(); |
|
467 |
|
|
468 |
// If we're mirroring another collection, forward `more` to |
|
469 |
// the mirrored collection. Account for a race condition by |
|
470 |
// checking if we're still mirroring that collection when |
|
471 |
// the request resolves. |
|
472 |
mirroring.more( options ).done( function() { |
|
473 |
if ( this === attachments.mirroring ) |
|
474 |
deferred.resolveWith( this ); |
|
475 |
}); |
|
476 |
|
|
477 |
return deferred.promise(); |
|
478 |
}, |
|
479 |
|
|
480 |
hasMore: function() { |
|
481 |
return this.mirroring ? this.mirroring.hasMore() : false; |
|
482 |
}, |
|
483 |
|
|
484 |
parse: function( resp, xhr ) { |
|
485 |
if ( ! _.isArray( resp ) ) |
|
486 |
resp = [resp]; |
|
487 |
|
|
488 |
return _.map( resp, function( attrs ) { |
|
489 |
var id, attachment, newAttributes; |
|
490 |
|
|
491 |
if ( attrs instanceof Backbone.Model ) { |
|
492 |
id = attrs.get( 'id' ); |
|
493 |
attrs = attrs.attributes; |
|
494 |
} else { |
|
495 |
id = attrs.id; |
|
496 |
} |
|
497 |
|
|
498 |
attachment = Attachment.get( id ); |
|
499 |
newAttributes = attachment.parse( attrs, xhr ); |
|
500 |
|
|
501 |
if ( ! _.isEqual( attachment.attributes, newAttributes ) ) |
|
502 |
attachment.set( newAttributes ); |
|
503 |
|
|
504 |
return attachment; |
|
505 |
}); |
|
506 |
}, |
|
507 |
|
|
508 |
_requery: function() { |
|
509 |
if ( this.props.get('query') ) |
|
510 |
this.mirror( Query.get( this.props.toJSON() ) ); |
|
511 |
}, |
|
512 |
|
|
513 |
// If this collection is sorted by `menuOrder`, recalculates and saves |
|
514 |
// the menu order to the database. |
|
515 |
saveMenuOrder: function() { |
|
516 |
if ( 'menuOrder' !== this.props.get('orderby') ) |
|
517 |
return; |
|
518 |
|
|
519 |
// Removes any uploading attachments, updates each attachment's |
|
520 |
// menu order, and returns an object with an { id: menuOrder } |
|
521 |
// mapping to pass to the request. |
|
522 |
var attachments = this.chain().filter( function( attachment ) { |
|
523 |
return ! _.isUndefined( attachment.id ); |
|
524 |
}).map( function( attachment, index ) { |
|
525 |
// Indices start at 1. |
|
526 |
index = index + 1; |
|
527 |
attachment.set( 'menuOrder', index ); |
|
528 |
return [ attachment.id, index ]; |
|
529 |
}).object().value(); |
|
530 |
|
|
531 |
if ( _.isEmpty( attachments ) ) |
|
532 |
return; |
|
533 |
|
|
534 |
return media.post( 'save-attachment-order', { |
|
535 |
nonce: media.model.settings.post.nonce, |
|
536 |
post_id: media.model.settings.post.id, |
|
537 |
attachments: attachments |
|
538 |
}); |
|
539 |
} |
|
540 |
}, { |
|
541 |
comparator: function( a, b, options ) { |
|
542 |
var key = this.props.get('orderby'), |
|
543 |
order = this.props.get('order') || 'DESC', |
|
544 |
ac = a.cid, |
|
545 |
bc = b.cid; |
|
546 |
|
|
547 |
a = a.get( key ); |
|
548 |
b = b.get( key ); |
|
549 |
|
|
550 |
if ( 'date' === key || 'modified' === key ) { |
|
551 |
a = a || new Date(); |
|
552 |
b = b || new Date(); |
|
553 |
} |
|
554 |
|
|
555 |
// If `options.ties` is set, don't enforce the `cid` tiebreaker. |
|
556 |
if ( options && options.ties ) |
|
557 |
ac = bc = null; |
|
558 |
|
|
559 |
return ( 'DESC' === order ) ? compare( a, b, ac, bc ) : compare( b, a, bc, ac ); |
|
560 |
}, |
|
561 |
|
|
562 |
filters: { |
|
563 |
// Note that this client-side searching is *not* equivalent |
|
564 |
// to our server-side searching. |
|
565 |
search: function( attachment ) { |
|
566 |
if ( ! this.props.get('search') ) |
|
567 |
return true; |
|
568 |
|
|
569 |
return _.any(['title','filename','description','caption','name'], function( key ) { |
|
570 |
var value = attachment.get( key ); |
|
571 |
return value && -1 !== value.search( this.props.get('search') ); |
|
572 |
}, this ); |
|
573 |
}, |
|
574 |
|
|
575 |
type: function( attachment ) { |
|
576 |
var type = this.props.get('type'); |
|
577 |
return ! type || -1 !== type.indexOf( attachment.get('type') ); |
|
578 |
}, |
|
579 |
|
|
580 |
uploadedTo: function( attachment ) { |
|
581 |
var uploadedTo = this.props.get('uploadedTo'); |
|
582 |
if ( _.isUndefined( uploadedTo ) ) |
|
583 |
return true; |
|
584 |
|
|
585 |
return uploadedTo === attachment.get('uploadedTo'); |
|
586 |
} |
|
587 |
} |
|
588 |
}); |
|
589 |
|
|
590 |
Attachments.all = new Attachments(); |
|
591 |
|
|
592 |
/** |
|
593 |
* wp.media.query |
|
594 |
*/ |
|
595 |
media.query = function( props ) { |
|
596 |
return new Attachments( null, { |
|
597 |
props: _.extend( _.defaults( props || {}, { orderby: 'date' } ), { query: true } ) |
|
598 |
}); |
|
599 |
}; |
|
600 |
|
|
601 |
/** |
|
602 |
* wp.media.model.Query |
|
603 |
* |
|
604 |
* A set of attachments that corresponds to a set of consecutively paged |
|
605 |
* queries on the server. |
|
606 |
* |
|
607 |
* Note: Do NOT change this.args after the query has been initialized. |
|
608 |
* Things will break. |
|
609 |
*/ |
|
610 |
Query = media.model.Query = Attachments.extend({ |
|
611 |
initialize: function( models, options ) { |
|
612 |
var allowed; |
|
613 |
|
|
614 |
options = options || {}; |
|
615 |
Attachments.prototype.initialize.apply( this, arguments ); |
|
616 |
|
|
617 |
this.args = options.args; |
|
618 |
this._hasMore = true; |
|
619 |
this.created = new Date(); |
|
620 |
|
|
621 |
this.filters.order = function( attachment ) { |
|
622 |
var orderby = this.props.get('orderby'), |
|
623 |
order = this.props.get('order'); |
|
624 |
|
|
625 |
if ( ! this.comparator ) |
|
626 |
return true; |
|
627 |
|
|
628 |
// We want any items that can be placed before the last |
|
629 |
// item in the set. If we add any items after the last |
|
630 |
// item, then we can't guarantee the set is complete. |
|
631 |
if ( this.length ) { |
|
632 |
return 1 !== this.comparator( attachment, this.last(), { ties: true }); |
|
633 |
|
|
634 |
// Handle the case where there are no items yet and |
|
635 |
// we're sorting for recent items. In that case, we want |
|
636 |
// changes that occurred after we created the query. |
|
637 |
} else if ( 'DESC' === order && ( 'date' === orderby || 'modified' === orderby ) ) { |
|
638 |
return attachment.get( orderby ) >= this.created; |
|
639 |
|
|
640 |
// If we're sorting by menu order and we have no items, |
|
641 |
// accept any items that have the default menu order (0). |
|
642 |
} else if ( 'ASC' === order && 'menuOrder' === orderby ) { |
|
643 |
return attachment.get( orderby ) === 0; |
|
644 |
} |
|
645 |
|
|
646 |
// Otherwise, we don't want any items yet. |
|
647 |
return false; |
|
648 |
}; |
|
649 |
|
|
650 |
// Observe the central `wp.Uploader.queue` collection to watch for |
|
651 |
// new matches for the query. |
|
652 |
// |
|
653 |
// Only observe when a limited number of query args are set. There |
|
654 |
// are no filters for other properties, so observing will result in |
|
655 |
// false positives in those queries. |
|
656 |
allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type', 'post_parent' ]; |
|
657 |
if ( wp.Uploader && _( this.args ).chain().keys().difference( allowed ).isEmpty().value() ) |
|
658 |
this.observe( wp.Uploader.queue ); |
|
659 |
}, |
|
660 |
|
|
661 |
hasMore: function() { |
|
662 |
return this._hasMore; |
|
663 |
}, |
|
664 |
|
|
665 |
more: function( options ) { |
|
666 |
var query = this; |
|
667 |
|
|
668 |
if ( this._more && 'pending' === this._more.state() ) |
|
669 |
return this._more; |
|
670 |
|
|
671 |
if ( ! this.hasMore() ) |
|
672 |
return $.Deferred().resolveWith( this ).promise(); |
|
673 |
|
|
674 |
options = options || {}; |
|
675 |
options.remove = false; |
|
676 |
|
|
677 |
return this._more = this.fetch( options ).done( function( resp ) { |
|
678 |
if ( _.isEmpty( resp ) || -1 === this.args.posts_per_page || resp.length < this.args.posts_per_page ) |
|
679 |
query._hasMore = false; |
|
680 |
}); |
|
681 |
}, |
|
682 |
|
|
683 |
sync: function( method, model, options ) { |
|
684 |
var args, fallback; |
|
685 |
|
|
686 |
// Overload the read method so Attachment.fetch() functions correctly. |
|
687 |
if ( 'read' === method ) { |
|
688 |
options = options || {}; |
|
689 |
options.context = this; |
|
690 |
options.data = _.extend( options.data || {}, { |
|
691 |
action: 'query-attachments', |
|
692 |
post_id: media.model.settings.post.id |
|
693 |
}); |
|
694 |
|
|
695 |
// Clone the args so manipulation is non-destructive. |
|
696 |
args = _.clone( this.args ); |
|
697 |
|
|
698 |
// Determine which page to query. |
|
699 |
if ( -1 !== args.posts_per_page ) |
|
700 |
args.paged = Math.floor( this.length / args.posts_per_page ) + 1; |
|
701 |
|
|
702 |
options.data.query = args; |
|
703 |
return media.ajax( options ); |
|
704 |
|
|
705 |
// Otherwise, fall back to Backbone.sync() |
|
706 |
} else { |
|
707 |
fallback = Attachments.prototype.sync ? Attachments.prototype : Backbone; |
|
708 |
return fallback.sync.apply( this, arguments ); |
|
709 |
} |
|
710 |
} |
|
711 |
}, { |
|
712 |
defaultProps: { |
|
713 |
orderby: 'date', |
|
714 |
order: 'DESC' |
|
715 |
}, |
|
716 |
|
|
717 |
defaultArgs: { |
|
718 |
posts_per_page: 40 |
|
719 |
}, |
|
720 |
|
|
721 |
orderby: { |
|
722 |
allowed: [ 'name', 'author', 'date', 'title', 'modified', 'uploadedTo', 'id', 'post__in', 'menuOrder' ], |
|
723 |
valuemap: { |
|
724 |
'id': 'ID', |
|
725 |
'uploadedTo': 'parent', |
|
726 |
'menuOrder': 'menu_order ID' |
|
727 |
} |
|
728 |
}, |
|
729 |
|
|
730 |
propmap: { |
|
731 |
'search': 's', |
|
732 |
'type': 'post_mime_type', |
|
733 |
'perPage': 'posts_per_page', |
|
734 |
'menuOrder': 'menu_order', |
|
735 |
'uploadedTo': 'post_parent' |
|
736 |
}, |
|
737 |
|
|
738 |
// Caches query objects so queries can be easily reused. |
|
739 |
get: (function(){ |
|
740 |
var queries = []; |
|
741 |
|
|
742 |
return function( props, options ) { |
|
743 |
var args = {}, |
|
744 |
orderby = Query.orderby, |
|
745 |
defaults = Query.defaultProps, |
|
746 |
query; |
|
747 |
|
|
748 |
// Remove the `query` property. This isn't linked to a query, |
|
749 |
// this *is* the query. |
|
750 |
delete props.query; |
|
751 |
|
|
752 |
// Fill default args. |
|
753 |
_.defaults( props, defaults ); |
|
754 |
|
|
755 |
// Normalize the order. |
|
756 |
props.order = props.order.toUpperCase(); |
|
757 |
if ( 'DESC' !== props.order && 'ASC' !== props.order ) |
|
758 |
props.order = defaults.order.toUpperCase(); |
|
759 |
|
|
760 |
// Ensure we have a valid orderby value. |
|
761 |
if ( ! _.contains( orderby.allowed, props.orderby ) ) |
|
762 |
props.orderby = defaults.orderby; |
|
763 |
|
|
764 |
// Generate the query `args` object. |
|
765 |
// Correct any differing property names. |
|
766 |
_.each( props, function( value, prop ) { |
|
767 |
if ( _.isNull( value ) ) |
|
768 |
return; |
|
769 |
|
|
770 |
args[ Query.propmap[ prop ] || prop ] = value; |
|
771 |
}); |
|
772 |
|
|
773 |
// Fill any other default query args. |
|
774 |
_.defaults( args, Query.defaultArgs ); |
|
775 |
|
|
776 |
// `props.orderby` does not always map directly to `args.orderby`. |
|
777 |
// Substitute exceptions specified in orderby.keymap. |
|
778 |
args.orderby = orderby.valuemap[ props.orderby ] || props.orderby; |
|
779 |
|
|
780 |
// Search the query cache for matches. |
|
781 |
query = _.find( queries, function( query ) { |
|
782 |
return _.isEqual( query.args, args ); |
|
783 |
}); |
|
784 |
|
|
785 |
// Otherwise, create a new query and add it to the cache. |
|
786 |
if ( ! query ) { |
|
787 |
query = new Query( [], _.extend( options || {}, { |
|
788 |
props: props, |
|
789 |
args: args |
|
790 |
} ) ); |
|
791 |
queries.push( query ); |
|
792 |
} |
|
793 |
|
|
794 |
return query; |
|
795 |
}; |
|
796 |
}()) |
|
797 |
}); |
|
798 |
|
|
799 |
/** |
|
800 |
* wp.media.model.Selection |
|
801 |
* |
|
802 |
* Used to manage a selection of attachments in the views. |
|
803 |
*/ |
|
804 |
media.model.Selection = Attachments.extend({ |
|
805 |
initialize: function( models, options ) { |
|
806 |
Attachments.prototype.initialize.apply( this, arguments ); |
|
807 |
this.multiple = options && options.multiple; |
|
808 |
|
|
809 |
// Refresh the `single` model whenever the selection changes. |
|
810 |
// Binds `single` instead of using the context argument to ensure |
|
811 |
// it receives no parameters. |
|
812 |
this.on( 'add remove reset', _.bind( this.single, this, false ) ); |
|
813 |
}, |
|
814 |
|
|
815 |
// Override the selection's add method. |
|
816 |
// If the workflow does not support multiple |
|
817 |
// selected attachments, reset the selection. |
|
818 |
add: function( models, options ) { |
|
819 |
if ( ! this.multiple ) |
|
820 |
this.remove( this.models ); |
|
821 |
|
|
822 |
return Attachments.prototype.add.call( this, models, options ); |
|
823 |
}, |
|
824 |
|
|
825 |
single: function( model ) { |
|
826 |
var previous = this._single; |
|
827 |
|
|
828 |
// If a `model` is provided, use it as the single model. |
|
829 |
if ( model ) |
|
830 |
this._single = model; |
|
831 |
|
|
832 |
// If the single model isn't in the selection, remove it. |
|
833 |
if ( this._single && ! this.get( this._single.cid ) ) |
|
834 |
delete this._single; |
|
835 |
|
|
836 |
this._single = this._single || this.last(); |
|
837 |
|
|
838 |
// If single has changed, fire an event. |
|
839 |
if ( this._single !== previous ) { |
|
840 |
if ( previous ) { |
|
841 |
previous.trigger( 'selection:unsingle', previous, this ); |
|
842 |
|
|
843 |
// If the model was already removed, trigger the collection |
|
844 |
// event manually. |
|
845 |
if ( ! this.get( previous.cid ) ) |
|
846 |
this.trigger( 'selection:unsingle', previous, this ); |
|
847 |
} |
|
848 |
if ( this._single ) |
|
849 |
this._single.trigger( 'selection:single', this._single, this ); |
|
850 |
} |
|
851 |
|
|
852 |
// Return the single model, or the last model as a fallback. |
|
853 |
return this._single; |
|
854 |
} |
|
855 |
}); |
|
856 |
|
|
857 |
// Clean up. Prevents mobile browsers caching |
|
858 |
$(window).on('unload', function(){ |
|
859 |
window.wp = null; |
|
860 |
}); |
|
861 |
|
|
862 |
}(jQuery)); |