- md5
- d00a73d26ba6829e355f726ea77eb948
- sha1
- 51399d8950d4162dfba80d5d311030642a5d58e9
- sha256
- 033f4c36532669da710c64a3380663b5f580f3aee85158594a385a5f485aacd1
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
/**
* @output wp-includes/js/customize-views.js
*/
/**
* The WordPress Customizer views API.
*
* @param {JQueryStatic} $ The jQuery object.
* @param {Object} wp The WordPress global object.
* @param {_.UnderscoreStatic} _ The Underscore.js object.
*/
(function( $, wp, _ ) {
if ( ! wp || ! wp.customize ) { return; }
var api = wp.customize;
/**
* wp.customize.HeaderTool.CurrentView
*
* Displays the currently selected header image, or a placeholder in lack
* thereof.
*
* Instantiate with model wp.customize.HeaderTool.currentHeader.
*
* @memberOf wp.customize.HeaderTool
* @alias wp.customize.HeaderTool.CurrentView
*
* @class
* @augments wp.Backbone.View
*/
api.HeaderTool.CurrentView = wp.Backbone.View.extend(/** @lends wp.customize.HeaderTool.CurrentView.prototype */{
template: wp.template('header-current'),
/**
* Initializes the view, rendering it whenever the model changes.
*/
initialize: function() {
this.listenTo(this.model, 'change', this.render);
this.render();
},
/**
* Renders the currently selected header image.
*
* @return {wp.customize.HeaderTool.CurrentView} Current view.
*/
render: function() {
this.$el.html(this.template(this.model.toJSON()));
this.setButtons();
return this;
},
/**
* Shows or hides the header's action buttons.
*/
setButtons: function() {
var elements = $('#customize-control-header_image .actions .remove');
var addButton = $('#customize-control-header_image .actions .new');
if (this.model.get('choice')) {
elements.show();
addButton.removeClass('upload-button');
} else {
elements.hide();
addButton.addClass('upload-button');
}
}
});
/**
* wp.customize.HeaderTool.ChoiceView
*
* Represents a choosable header image, be it user-uploaded,
* theme-suggested or a special Randomize choice.
*
* Takes a wp.customize.HeaderTool.ImageModel.
*
* Manually changes model wp.customize.HeaderTool.currentHeader via the
* `select` method.
*
* @memberOf wp.customize.HeaderTool
* @alias wp.customize.HeaderTool.ChoiceView
*
* @class
* @augments wp.Backbone.View
*/
api.HeaderTool.ChoiceView = wp.Backbone.View.extend(/** @lends wp.customize.HeaderTool.ChoiceView.prototype */{
template: wp.template('header-choice'),
className: 'header-view',
events: {
'click .choice,.random': 'select',
'click .close': 'removeImage'
},
/**
* Initializes the view, marking it current when it matches the header image.
*/
initialize: function() {
var properties = [
this.model.get('header').url,
this.model.get('choice')
];
this.listenTo(this.model, 'change:selected', this.toggleSelected);
if (_.contains(properties, api.get().header_image)) {
api.HeaderTool.currentHeader.set(this.extendedModel());
}
},
/**
* Renders the choice.
*
* @return {wp.customize.HeaderTool.ChoiceView} Choice view.
*/
render: function() {
this.$el.html(this.template(this.extendedModel()));
this.toggleSelected();
return this;
},
/**
* Toggles the selected class on the choice.
*/
toggleSelected: function() {
this.$el.toggleClass('selected', this.model.get('selected'));
},
/**
* Returns the model's attributes together with its collection type.
*
* @return {Object} Extended model.
*/
extendedModel: function() {
var c = this.model.get('collection');
return _.extend(this.model.toJSON(), {
type: c.type
});
},
/**
* Selects the choice as the header image.
*/
select: function() {
this.preventJump();
this.model.save();
api.HeaderTool.currentHeader.set(this.extendedModel());
},
/**
* Keeps the sidebar at its current scroll position.
*/
preventJump: function() {
var container = $('.wp-full-overlay-sidebar-content'),
scroll = container.scrollTop();
_.defer(function() {
container.scrollTop(scroll);
});
},
/**
* Removes the image from the collection.
*
* @param {JQuery.Event} e Event.
*/
removeImage: function(e) {
e.stopPropagation();
this.model.destroy();
this.remove();
}
});
/**
* wp.customize.HeaderTool.ChoiceListView
*
* A container for ChoiceViews. These choices should be of one same type:
* user-uploaded headers or theme-defined ones.
*
* Takes a wp.customize.HeaderTool.ChoiceList.
*
* @memberOf wp.customize.HeaderTool
* @alias wp.customize.HeaderTool.ChoiceListView
*
* @class
* @augments wp.Backbone.View
*/
api.HeaderTool.ChoiceListView = wp.Backbone.View.extend(/** @lends wp.customize.HeaderTool.ChoiceListView.prototype */{
/**
* Initializes the view, rendering it as the collection changes.
*/
initialize: function() {
this.listenTo(this.collection, 'add', this.addOne);
this.listenTo(this.collection, 'remove', this.render);
this.listenTo(this.collection, 'sort', this.render);
this.listenTo(this.collection, 'change', this.toggleList);
this.render();
},
/**
* Renders each choice in the collection.
*
* @return {wp.customize.HeaderTool.ChoiceListView} Choice list view.
*/
render: function() {
this.$el.empty();
this.collection.each(this.addOne, this);
this.toggleList();
return this;
},
/**
* Renders a single choice into the list.
*
* @param {Backbone.Model} choice Choice.
*/
addOne: function(choice) {
var view;
choice.set({ collection: this.collection });
view = new api.HeaderTool.ChoiceView({ model: choice });
this.$el.append(view.render().el);
},
/**
* Shows or hides the list title and the random button.
*/
toggleList: function() {
var title = this.$el.parents().prev('.customize-control-title'),
randomButton = this.$el.find('.random').parent();
if (this.collection.shouldHideTitle()) {
title.add(randomButton).hide();
} else {
title.add(randomButton).show();
}
}
});
/**
* wp.customize.HeaderTool.CombinedList
*
* Aggregates wp.customize.HeaderTool.ChoiceList collections (or any
* Backbone object, really) and acts as a bus to feed them events.
*
* @memberOf wp.customize.HeaderTool
* @alias wp.customize.HeaderTool.CombinedList
*
* @class
* @augments wp.Backbone.View
*/
api.HeaderTool.CombinedList = wp.Backbone.View.extend(/** @lends wp.customize.HeaderTool.CombinedList.prototype */{
/**
* Initializes the view with the collections to combine.
*
* @param {Backbone.Collection[]} collections Collections.
*/
initialize: function(collections) {
this.collections = collections;
this.on('all', this.propagate, this);
},
/**
* Passes an event on to each of the combined collections.
*
* @param {string} event Event.
* @param {*} arg Argument.
*/
propagate: function(event, arg) {
_.each(this.collections, function(collection) {
collection.trigger(event, arg);
});
}
});
})( jQuery, window.wp, _ );
The same content is in 7 releases, WordPress 7.2-beta1 to 7.2-RC2. See them all, or every version of this file.