2018-06-27 22:30:15 -04:00
|
|
|
/**
|
|
|
|
* @output wp-includes/js/wp-backbone.js
|
|
|
|
*/
|
|
|
|
|
2017-09-08 14:42:49 -04:00
|
|
|
/** @namespace wp */
|
2013-02-28 10:14:34 -05:00
|
|
|
window.wp = window.wp || {};
|
|
|
|
|
|
|
|
(function ($) {
|
2017-09-08 14:42:49 -04:00
|
|
|
/**
|
|
|
|
* Create the WordPress Backbone namespace.
|
|
|
|
*
|
|
|
|
* @namespace wp.Backbone
|
|
|
|
*/
|
2013-05-26 02:34:33 -04:00
|
|
|
wp.Backbone = {};
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
|
|
|
* A backbone subview manager.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @since 3.6.0 Moved wp.media.Views to wp.Backbone.Subviews.
|
|
|
|
*
|
|
|
|
* @memberOf wp.Backbone
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*
|
2020-06-20 08:58:10 -04:00
|
|
|
* @param {wp.Backbone.View} view The main view.
|
|
|
|
* @param {Array|Object} views The subviews for the main view.
|
2018-04-19 10:02:22 -04:00
|
|
|
*/
|
2013-05-26 02:34:33 -04:00
|
|
|
wp.Backbone.Subviews = function( view, views ) {
|
2013-05-25 17:03:04 -04:00
|
|
|
this.view = view;
|
|
|
|
this._views = _.isArray( views ) ? { '': views } : views || {};
|
|
|
|
};
|
|
|
|
|
2013-05-26 02:34:33 -04:00
|
|
|
wp.Backbone.Subviews.extend = Backbone.Model.extend;
|
2013-05-25 17:03:04 -04:00
|
|
|
|
2013-05-26 02:34:33 -04:00
|
|
|
_.extend( wp.Backbone.Subviews.prototype, {
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
|
|
|
* Fetches all of the subviews.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2018-04-22 10:37:21 -04:00
|
|
|
* @return {Array} All the subviews.
|
2018-04-19 10:02:22 -04:00
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
all: function() {
|
2018-04-19 10:02:22 -04:00
|
|
|
return _.flatten( _.values( this._views ) );
|
2013-05-25 17:03:04 -04:00
|
|
|
},
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
|
|
|
* Fetches all subviews that match a given `selector`.
|
|
|
|
*
|
|
|
|
* If no `selector` is provided, it will grab all subviews attached
|
|
|
|
* to the view's root.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param {string} selector A jQuery selector.
|
|
|
|
*
|
2018-04-22 10:37:21 -04:00
|
|
|
* @return {Array} All the subviews that match the selector.
|
2018-04-19 10:02:22 -04:00
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
get: function( selector ) {
|
|
|
|
selector = selector || '';
|
|
|
|
return this._views[ selector ];
|
|
|
|
},
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
|
|
|
* Fetches the first subview that matches a given `selector`.
|
|
|
|
*
|
|
|
|
* If no `selector` is provided, it will grab the first subview attached to the
|
|
|
|
* view's root.
|
|
|
|
*
|
|
|
|
* Useful when a selector only has one subview at a time.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param {string} selector A jQuery selector.
|
|
|
|
*
|
|
|
|
* @return {Backbone.View} The view.
|
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
first: function( selector ) {
|
|
|
|
var views = this.get( selector );
|
|
|
|
return views && views.length ? views[0] : null;
|
|
|
|
},
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
|
|
|
* Registers subview(s).
|
|
|
|
*
|
|
|
|
* Registers any number of `views` to a `selector`.
|
|
|
|
*
|
|
|
|
* When no `selector` is provided, the root selector (the empty string)
|
|
|
|
* is used. `views` accepts a `Backbone.View` instance or an array of
|
|
|
|
* `Backbone.View` instances.
|
|
|
|
*
|
|
|
|
* ---
|
|
|
|
*
|
|
|
|
* Accepts an `options` object, which has a significant effect on the
|
|
|
|
* resulting behavior.
|
|
|
|
*
|
2018-04-22 10:37:21 -04:00
|
|
|
* `options.silent` - *boolean, `false`*
|
|
|
|
* If `options.silent` is true, no DOM modifications will be made.
|
2018-04-19 10:02:22 -04:00
|
|
|
*
|
2018-04-22 10:37:21 -04:00
|
|
|
* `options.add` - *boolean, `false`*
|
|
|
|
* Use `Views.add()` as a shortcut for setting `options.add` to true.
|
2018-04-19 10:02:22 -04:00
|
|
|
*
|
2018-04-22 10:37:21 -04:00
|
|
|
* By default, the provided `views` will replace any existing views
|
|
|
|
* associated with the selector. If `options.add` is true, the provided
|
|
|
|
* `views` will be added to the existing views.
|
2018-04-19 10:02:22 -04:00
|
|
|
*
|
2018-04-22 10:37:21 -04:00
|
|
|
* `options.at` - *integer, `undefined`*
|
|
|
|
* When adding, to insert `views` at a specific index, use `options.at`.
|
|
|
|
* By default, `views` are added to the end of the array.
|
2018-04-19 10:02:22 -04:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param {string} selector A jQuery selector.
|
2018-04-22 10:37:21 -04:00
|
|
|
* @param {Array|Object} views The subviews for the main view.
|
2018-04-19 10:02:22 -04:00
|
|
|
* @param {Object} options Options for call. If `options.silent` is true,
|
|
|
|
* no DOM modifications will be made. Use
|
|
|
|
* `Views.add()` as a shortcut for setting
|
|
|
|
* `options.add` to true. If `options.add` is
|
|
|
|
* true, the provided `views` will be added to
|
|
|
|
* the existing views. When adding, to insert
|
|
|
|
* `views` at a specific index, use `options.at`.
|
|
|
|
*
|
2018-04-22 10:37:21 -04:00
|
|
|
* @return {wp.Backbone.Subviews} The current Subviews instance.
|
2018-04-19 10:02:22 -04:00
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
set: function( selector, views, options ) {
|
|
|
|
var existing, next;
|
|
|
|
|
|
|
|
if ( ! _.isString( selector ) ) {
|
|
|
|
options = views;
|
|
|
|
views = selector;
|
|
|
|
selector = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
views = _.isArray( views ) ? views : [ views ];
|
|
|
|
existing = this.get( selector );
|
|
|
|
next = views;
|
|
|
|
|
|
|
|
if ( existing ) {
|
|
|
|
if ( options.add ) {
|
|
|
|
if ( _.isUndefined( options.at ) ) {
|
|
|
|
next = existing.concat( views );
|
|
|
|
} else {
|
|
|
|
next = existing;
|
|
|
|
next.splice.apply( next, [ options.at, 0 ].concat( views ) );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_.each( next, function( view ) {
|
|
|
|
view.__detach = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
_.each( existing, function( view ) {
|
|
|
|
if ( view.__detach )
|
|
|
|
view.$el.detach();
|
|
|
|
else
|
2013-05-25 17:38:12 -04:00
|
|
|
view.remove();
|
2013-05-25 17:03:04 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
_.each( next, function( view ) {
|
|
|
|
delete view.__detach;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._views[ selector ] = next;
|
|
|
|
|
|
|
|
_.each( views, function( subview ) {
|
2013-05-26 02:34:33 -04:00
|
|
|
var constructor = subview.Views || wp.Backbone.Subviews,
|
2013-05-25 17:03:04 -04:00
|
|
|
subviews = subview.views = subview.views || new constructor( subview );
|
|
|
|
subviews.parent = this.view;
|
|
|
|
subviews.selector = selector;
|
|
|
|
}, this );
|
|
|
|
|
|
|
|
if ( ! options.silent )
|
|
|
|
this._attach( selector, views, _.extend({ ready: this._isReady() }, options ) );
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
|
|
|
* Add subview(s) to existing subviews.
|
|
|
|
*
|
|
|
|
* An alias to `Views.set()`, which defaults `options.add` to true.
|
|
|
|
*
|
|
|
|
* Adds any number of `views` to a `selector`.
|
|
|
|
*
|
|
|
|
* When no `selector` is provided, the root selector (the empty string)
|
|
|
|
* is used. `views` accepts a `Backbone.View` instance or an array of
|
|
|
|
* `Backbone.View` instances.
|
|
|
|
*
|
|
|
|
* Uses `Views.set()` when setting `options.add` to `false`.
|
|
|
|
*
|
|
|
|
* Accepts an `options` object. By default, provided `views` will be
|
|
|
|
* inserted at the end of the array of existing views. To insert
|
|
|
|
* `views` at a specific index, use `options.at`. If `options.silent`
|
|
|
|
* is true, no DOM modifications will be made.
|
|
|
|
*
|
|
|
|
* For more information on the `options` object, see `Views.set()`.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param {string} selector A jQuery selector.
|
2018-04-22 10:37:21 -04:00
|
|
|
* @param {Array|Object} views The subviews for the main view.
|
2018-04-19 10:02:22 -04:00
|
|
|
* @param {Object} options Options for call. To insert `views` at a
|
|
|
|
* specific index, use `options.at`. If
|
|
|
|
* `options.silent` is true, no DOM modifications
|
|
|
|
* will be made.
|
|
|
|
*
|
2018-04-22 10:37:21 -04:00
|
|
|
* @return {wp.Backbone.Subviews} The current subviews instance.
|
2018-04-19 10:02:22 -04:00
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
add: function( selector, views, options ) {
|
|
|
|
if ( ! _.isString( selector ) ) {
|
|
|
|
options = views;
|
|
|
|
views = selector;
|
|
|
|
selector = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.set( selector, views, _.extend({ add: true }, options ) );
|
|
|
|
},
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
|
|
|
* Removes an added subview.
|
|
|
|
*
|
|
|
|
* Stops tracking `views` registered to a `selector`. If no `views` are
|
|
|
|
* set, then all of the `selector`'s subviews will be unregistered and
|
|
|
|
* removed.
|
|
|
|
*
|
|
|
|
* Accepts an `options` object. If `options.silent` is set, `remove`
|
|
|
|
* will *not* be triggered on the unregistered views.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param {string} selector A jQuery selector.
|
2018-04-22 10:37:21 -04:00
|
|
|
* @param {Array|Object} views The subviews for the main view.
|
2018-04-19 10:02:22 -04:00
|
|
|
* @param {Object} options Options for call. If `options.silent` is set,
|
|
|
|
* `remove` will *not* be triggered on the
|
|
|
|
* unregistered views.
|
|
|
|
*
|
|
|
|
* @return {wp.Backbone.Subviews} The current Subviews instance.
|
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
unset: function( selector, views, options ) {
|
|
|
|
var existing;
|
|
|
|
|
|
|
|
if ( ! _.isString( selector ) ) {
|
|
|
|
options = views;
|
|
|
|
views = selector;
|
|
|
|
selector = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
views = views || [];
|
|
|
|
|
|
|
|
if ( existing = this.get( selector ) ) {
|
|
|
|
views = _.isArray( views ) ? views : [ views ];
|
|
|
|
this._views[ selector ] = views.length ? _.difference( existing, views ) : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! options || ! options.silent )
|
2013-05-25 17:38:12 -04:00
|
|
|
_.invoke( views, 'remove' );
|
2013-05-25 17:03:04 -04:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
|
|
|
* Detaches all subviews.
|
|
|
|
*
|
|
|
|
* Helps to preserve all subview events when re-rendering the master
|
|
|
|
* view. Used in conjunction with `Views.render()`.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @return {wp.Backbone.Subviews} The current Subviews instance.
|
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
detach: function() {
|
|
|
|
$( _.pluck( this.all(), 'el' ) ).detach();
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
2018-04-22 10:37:21 -04:00
|
|
|
* Renders all subviews.
|
2018-04-19 10:02:22 -04:00
|
|
|
*
|
|
|
|
* Used in conjunction with `Views.detach()`.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @return {wp.Backbone.Subviews} The current Subviews instance.
|
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
render: function() {
|
|
|
|
var options = {
|
|
|
|
ready: this._isReady()
|
|
|
|
};
|
|
|
|
|
|
|
|
_.each( this._views, function( views, selector ) {
|
|
|
|
this._attach( selector, views, options );
|
|
|
|
}, this );
|
|
|
|
|
|
|
|
this.rendered = true;
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
2018-04-22 10:37:21 -04:00
|
|
|
* Removes all subviews.
|
2018-04-19 10:02:22 -04:00
|
|
|
*
|
|
|
|
* Triggers the `remove()` method on all subviews. Detaches the master
|
|
|
|
* view from its parent. Resets the internals of the views manager.
|
|
|
|
*
|
|
|
|
* Accepts an `options` object. If `options.silent` is set, `unset`
|
|
|
|
* will *not* be triggered on the master view's parent.
|
|
|
|
*
|
|
|
|
* @since 3.6.0
|
|
|
|
*
|
|
|
|
* @param {Object} options Options for call.
|
2024-04-12 13:47:13 -04:00
|
|
|
* @param {boolean} options.silent If true, `unset` will *not* be triggered on
|
2018-04-19 10:02:22 -04:00
|
|
|
* the master views' parent.
|
|
|
|
*
|
|
|
|
* @return {wp.Backbone.Subviews} The current Subviews instance.
|
|
|
|
*/
|
2013-05-25 17:38:12 -04:00
|
|
|
remove: function( options ) {
|
2013-05-25 17:03:04 -04:00
|
|
|
if ( ! options || ! options.silent ) {
|
|
|
|
if ( this.parent && this.parent.views )
|
|
|
|
this.parent.views.unset( this.selector, this.view, { silent: true });
|
|
|
|
delete this.parent;
|
|
|
|
delete this.selector;
|
|
|
|
}
|
|
|
|
|
2013-05-25 17:38:12 -04:00
|
|
|
_.invoke( this.all(), 'remove' );
|
2013-05-25 17:03:04 -04:00
|
|
|
this._views = [];
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
|
|
|
* Replaces a selector's subviews
|
|
|
|
*
|
|
|
|
* By default, sets the `$target` selector's html to the subview `els`.
|
|
|
|
*
|
|
|
|
* Can be overridden in subclasses.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param {string} $target Selector where to put the elements.
|
|
|
|
* @param {*} els HTML or elements to put into the selector's HTML.
|
|
|
|
*
|
|
|
|
* @return {wp.Backbone.Subviews} The current Subviews instance.
|
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
replace: function( $target, els ) {
|
|
|
|
$target.html( els );
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
2018-04-22 10:37:21 -04:00
|
|
|
* Insert subviews into a selector.
|
2018-04-19 10:02:22 -04:00
|
|
|
*
|
|
|
|
* By default, appends the subview `els` to the end of the `$target`
|
|
|
|
* selector. If `options.at` is set, inserts the subview `els` at the
|
|
|
|
* provided index.
|
|
|
|
*
|
|
|
|
* Can be overridden in subclasses.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param {string} $target Selector where to put the elements.
|
|
|
|
* @param {*} els HTML or elements to put at the end of the
|
|
|
|
* $target.
|
|
|
|
* @param {?Object} options Options for call.
|
|
|
|
* @param {?number} options.at At which index to put the elements.
|
|
|
|
*
|
|
|
|
* @return {wp.Backbone.Subviews} The current Subviews instance.
|
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
insert: function( $target, els, options ) {
|
|
|
|
var at = options && options.at,
|
|
|
|
$children;
|
|
|
|
|
|
|
|
if ( _.isNumber( at ) && ($children = $target.children()).length > at )
|
|
|
|
$children.eq( at ).before( els );
|
|
|
|
else
|
|
|
|
$target.append( els );
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
|
|
|
* Triggers the ready event.
|
|
|
|
*
|
|
|
|
* Only use this method if you know what you're doing. For performance reasons,
|
|
|
|
* this method does not check if the view is actually attached to the DOM. It's
|
|
|
|
* taking your word for it.
|
|
|
|
*
|
|
|
|
* Fires the ready event on the current view and all attached subviews.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
ready: function() {
|
|
|
|
this.view.trigger('ready');
|
|
|
|
|
|
|
|
// Find all attached subviews, and call ready on them.
|
|
|
|
_.chain( this.all() ).map( function( view ) {
|
|
|
|
return view.views;
|
|
|
|
}).flatten().where({ attached: true }).invoke('ready');
|
|
|
|
},
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
|
|
|
* Attaches a series of views to a selector. Internal.
|
|
|
|
*
|
|
|
|
* Checks to see if a matching selector exists, renders the views,
|
|
|
|
* performs the proper DOM operation, and then checks if the view is
|
|
|
|
* attached to the document.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*
|
2018-04-22 10:37:21 -04:00
|
|
|
* @param {string} selector A jQuery selector.
|
|
|
|
* @param {Array|Object} views The subviews for the main view.
|
|
|
|
* @param {Object} options Options for call.
|
|
|
|
* @param {boolean} options.add If true the provided views will be added.
|
2018-04-19 10:02:22 -04:00
|
|
|
*
|
|
|
|
* @return {wp.Backbone.Subviews} The current Subviews instance.
|
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
_attach: function( selector, views, options ) {
|
|
|
|
var $selector = selector ? this.view.$( selector ) : this.view.$el,
|
|
|
|
managers;
|
|
|
|
|
|
|
|
// Check if we found a location to attach the views.
|
|
|
|
if ( ! $selector.length )
|
|
|
|
return this;
|
|
|
|
|
|
|
|
managers = _.chain( views ).pluck('views').flatten().value();
|
|
|
|
|
|
|
|
// Render the views if necessary.
|
|
|
|
_.each( managers, function( manager ) {
|
|
|
|
if ( manager.rendered )
|
|
|
|
return;
|
|
|
|
|
|
|
|
manager.view.render();
|
|
|
|
manager.rendered = true;
|
|
|
|
}, this );
|
|
|
|
|
|
|
|
// Insert or replace the views.
|
|
|
|
this[ options.add ? 'insert' : 'replace' ]( $selector, _.pluck( views, 'el' ), options );
|
|
|
|
|
2018-04-22 10:37:21 -04:00
|
|
|
/*
|
|
|
|
* Set attached and trigger ready if the current view is already
|
|
|
|
* attached to the DOM.
|
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
_.each( managers, function( manager ) {
|
|
|
|
manager.attached = true;
|
|
|
|
|
|
|
|
if ( options.ready )
|
|
|
|
manager.ready();
|
|
|
|
}, this );
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
|
|
|
* Determines whether or not the current view is in the DOM.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*
|
2018-04-22 10:37:21 -04:00
|
|
|
* @return {boolean} Whether or not the current view is in the DOM.
|
2018-04-19 10:02:22 -04:00
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
_isReady: function() {
|
|
|
|
var node = this.view.el;
|
|
|
|
while ( node ) {
|
|
|
|
if ( node === document.body )
|
|
|
|
return true;
|
|
|
|
node = node.parentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-05-26 02:34:33 -04:00
|
|
|
wp.Backbone.View = Backbone.View.extend({
|
2018-04-19 10:02:22 -04:00
|
|
|
|
2013-05-25 17:03:04 -04:00
|
|
|
// The constructor for the `Views` manager.
|
2013-05-26 02:34:33 -04:00
|
|
|
Subviews: wp.Backbone.Subviews,
|
2013-05-25 17:03:04 -04:00
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
|
|
|
* The base view class.
|
|
|
|
*
|
|
|
|
* This extends the backbone view to have a build-in way to use subviews. This
|
|
|
|
* makes it easier to have nested views.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @since 3.6.0 Moved wp.media.View to wp.Backbone.View
|
|
|
|
*
|
|
|
|
* @constructs
|
|
|
|
* @augments Backbone.View
|
|
|
|
*
|
2018-04-22 10:37:21 -04:00
|
|
|
* @memberOf wp.Backbone
|
|
|
|
*
|
|
|
|
*
|
2018-04-19 10:02:22 -04:00
|
|
|
* @param {Object} options The options for this view.
|
|
|
|
*/
|
2014-02-13 02:36:13 -05:00
|
|
|
constructor: function( options ) {
|
2013-05-25 17:03:04 -04:00
|
|
|
this.views = new this.Subviews( this, this.views );
|
|
|
|
this.on( 'ready', this.ready, this );
|
|
|
|
|
2014-02-13 02:36:13 -05:00
|
|
|
this.options = options || {};
|
|
|
|
|
2013-05-25 17:03:04 -04:00
|
|
|
Backbone.View.apply( this, arguments );
|
|
|
|
},
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
2018-04-22 10:37:21 -04:00
|
|
|
* Removes this view and all subviews.
|
|
|
|
*
|
2018-04-19 10:02:22 -04:00
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2018-04-22 10:37:21 -04:00
|
|
|
* @return {wp.Backbone.Subviews} The current Subviews instance.
|
2018-04-19 10:02:22 -04:00
|
|
|
*/
|
2013-05-25 17:38:12 -04:00
|
|
|
remove: function() {
|
|
|
|
var result = Backbone.View.prototype.remove.apply( this, arguments );
|
2013-05-25 17:03:04 -04:00
|
|
|
|
2013-05-25 17:38:12 -04:00
|
|
|
// Recursively remove child views.
|
2013-05-25 17:03:04 -04:00
|
|
|
if ( this.views )
|
2013-05-25 17:38:12 -04:00
|
|
|
this.views.remove();
|
2013-05-25 17:03:04 -04:00
|
|
|
|
2013-05-25 17:38:12 -04:00
|
|
|
return result;
|
2013-05-25 17:03:04 -04:00
|
|
|
},
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
|
|
|
* Renders this view and all subviews.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2018-04-22 10:37:21 -04:00
|
|
|
* @return {wp.Backbone.View} The current instance of the view.
|
2018-04-19 10:02:22 -04:00
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
render: function() {
|
|
|
|
var options;
|
|
|
|
|
|
|
|
if ( this.prepare )
|
|
|
|
options = this.prepare();
|
|
|
|
|
|
|
|
this.views.detach();
|
|
|
|
|
|
|
|
if ( this.template ) {
|
|
|
|
options = options || {};
|
|
|
|
this.trigger( 'prepare', options );
|
|
|
|
this.$el.html( this.template( options ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.views.render();
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
|
|
|
* Returns the options for this view.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2018-04-22 10:37:21 -04:00
|
|
|
* @return {Object} The options for this view.
|
2018-04-19 10:02:22 -04:00
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
prepare: function() {
|
|
|
|
return this.options;
|
|
|
|
},
|
|
|
|
|
2018-04-19 10:02:22 -04:00
|
|
|
/**
|
|
|
|
* Method that is called when the ready event is triggered.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
2013-05-25 17:03:04 -04:00
|
|
|
ready: function() {}
|
|
|
|
});
|
2013-02-28 10:14:34 -05:00
|
|
|
}(jQuery));
|