1 /* globals _wpCustomizeHeader, _ */
3 var api = wp.customize;
8 * wp.customize.HeaderTool.ImageModel
10 * A header image. This is where saves via the Customizer API are
11 * abstracted away, plus our own AJAX calls to add images to and remove
12 * images from the user's recently uploaded images setting on the server.
13 * These calls are made regardless of whether the user actually saves new
14 * Customizer settings.
17 * @augments Backbone.Model
19 api.HeaderTool.ImageModel = Backbone.Model.extend({
20 defaults: function() {
34 initialize: function() {
35 this.on('hide', this.hide, this);
39 this.set('choice', '');
40 api('header_image').set('remove-header');
41 api('header_image_data').set('remove-header');
45 var data = this.get('header'),
46 curr = api.HeaderTool.currentHeader.get('header').attachment_id;
48 // If the image we're removing is also the current header, unset
50 if (curr && data.attachment_id === curr) {
51 api.HeaderTool.currentHeader.trigger('hide');
54 wp.ajax.post( 'custom-header-remove', {
55 nonce: _wpCustomizeHeader.nonces.remove,
57 theme: api.settings.theme.stylesheet,
58 attachment_id: data.attachment_id
61 this.trigger('destroy', this, this.collection);
65 if (this.get('random')) {
66 api('header_image').set(this.get('header').random);
67 api('header_image_data').set(this.get('header').random);
69 if (this.get('header').defaultName) {
70 api('header_image').set(this.get('header').url);
71 api('header_image_data').set(this.get('header').defaultName);
73 api('header_image').set(this.get('header').url);
74 api('header_image_data').set(this.get('header'));
78 api.HeaderTool.combinedList.trigger('control:setImage', this);
81 importImage: function() {
82 var data = this.get('header');
83 if (data.attachment_id === undefined) {
87 wp.ajax.post( 'custom-header-add', {
88 nonce: _wpCustomizeHeader.nonces.add,
90 theme: api.settings.theme.stylesheet,
91 attachment_id: data.attachment_id
95 shouldBeCropped: function() {
96 if (this.get('themeFlexWidth') === true &&
97 this.get('themeFlexHeight') === true) {
101 if (this.get('themeFlexWidth') === true &&
102 this.get('themeHeight') === this.get('imageHeight')) {
106 if (this.get('themeFlexHeight') === true &&
107 this.get('themeWidth') === this.get('imageWidth')) {
111 if (this.get('themeWidth') === this.get('imageWidth') &&
112 this.get('themeHeight') === this.get('imageHeight')) {
122 * wp.customize.HeaderTool.ChoiceList
125 * @augments Backbone.Collection
127 api.HeaderTool.ChoiceList = Backbone.Collection.extend({
128 model: api.HeaderTool.ImageModel,
130 // Ordered from most recently used to least
131 comparator: function(model) {
132 return -model.get('header').timestamp;
135 initialize: function() {
136 var current = api.HeaderTool.currentHeader.get('choice').replace(/^https?:\/\//, ''),
137 isRandom = this.isRandomChoice(api.get().header_image);
139 // Overridable by an extending class
141 this.type = 'uploaded';
144 // Overridable by an extending class
145 if (typeof this.data === 'undefined') {
146 this.data = _wpCustomizeHeader.uploads;
150 // So that when adding data we don't hide regular images
151 current = api.get().header_image;
154 this.on('control:setImage', this.setImage, this);
155 this.on('control:removeImage', this.removeImage, this);
156 this.on('add', this.maybeAddRandomChoice, this);
158 _.each(this.data, function(elt, index) {
159 if (!elt.attachment_id) {
160 elt.defaultName = index;
163 if (typeof elt.timestamp === 'undefined') {
169 choice: elt.url.split('/').pop(),
170 selected: current === elt.url.replace(/^https?:\/\//, '')
171 }, { silent: true });
174 if (this.size() > 0) {
175 this.addRandomChoice(current);
179 maybeAddRandomChoice: function() {
180 if (this.size() === 1) {
181 this.addRandomChoice();
185 addRandomChoice: function(initialChoice) {
186 var isRandomSameType = RegExp(this.type).test(initialChoice),
187 randomChoice = 'random-' + this.type + '-image';
192 random: randomChoice,
196 choice: randomChoice,
198 selected: isRandomSameType
202 isRandomChoice: function(choice) {
203 return (/^random-(uploaded|default)-image$/).test(choice);
206 shouldHideTitle: function() {
207 return this.size() < 2;
210 setImage: function(model) {
211 this.each(function(m) {
212 m.set('selected', false);
216 model.set('selected', true);
220 removeImage: function() {
221 this.each(function(m) {
222 m.set('selected', false);
229 * wp.customize.HeaderTool.DefaultsList
232 * @augments wp.customize.HeaderTool.ChoiceList
233 * @augments Backbone.Collection
235 api.HeaderTool.DefaultsList = api.HeaderTool.ChoiceList.extend({
236 initialize: function() {
237 this.type = 'default';
238 this.data = _wpCustomizeHeader.defaults;
239 api.HeaderTool.ChoiceList.prototype.initialize.apply(this);
243 })( jQuery, window.wp );