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