1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
/**
* wp.media.view.PriorityList
*
* @memberOf wp.media.view
*
* @class
* @augments wp.media.View
* @augments wp.Backbone.View
* @augments Backbone.View
*/
var PriorityList = wp.media.View.extend(/** @lends wp.media.view.PriorityList.prototype */{
tagName: 'div',
initialize: function() {
this._views = {};
this.set( _.extend( {}, this._views, this.options.views ), { silent: true });
delete this.options.views;
if ( ! this.options.silent ) {
this.render();
}
},
/**
* @param {string} id
* @param {wp.media.View|Object} view
* @param {Object} options
* @return {wp.media.view.PriorityList} Returns itself to allow chaining.
*/
set: function( id, view, options ) {
var priority, views, index;
options = options || {};
// Accept an object with an `id` : `view` mapping.
if ( _.isObject( id ) ) {
_.each( id, function( view, id ) {
this.set( id, view );
}, this );
return this;
}
if ( ! (view instanceof Backbone.View) ) {
view = this.toView( view, id, options );
}
view.controller = view.controller || this.controller;
this.unset( id );
priority = view.options.priority || 10;
views = this.views.get() || [];
_.find( views, function( existing, i ) {
if ( existing.options.priority > priority ) {
index = i;
return true;
}
});
this._views[ id ] = view;
this.views.add( view, {
at: _.isNumber( index ) ? index : views.length || 0
});
return this;
},
/**
* @param {string} id
* @return {wp.media.View}
*/
get: function( id ) {
return this._views[ id ];
},
/**
* @param {string} id
* @return {wp.media.view.PriorityList}
*/
unset: function( id ) {
var view = this.get( id );
if ( view ) {
view.remove();
}
delete this._views[ id ];
return this;
},
/**
* @param {Object} options
* @return {wp.media.View}
*/
toView: function( options ) {
return new wp.media.View( options );
}
});
module.exports = PriorityList;
|