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')) {
116
if (this.get('imageWidth') <= this.get('themeWidth')) {
126
* wp.customize.HeaderTool.ChoiceList
129
* @augments Backbone.Collection
131
api.HeaderTool.ChoiceList = Backbone.Collection.extend({
132
model: api.HeaderTool.ImageModel,
134
// Ordered from most recently used to least
135
comparator: function(model) {
136
return -model.get('header').timestamp;
139
initialize: function() {
140
var current = api.HeaderTool.currentHeader.get('choice').replace(/^https?:\/\//, ''),
141
isRandom = this.isRandomChoice(api.get().header_image);
143
// Overridable by an extending class
145
this.type = 'uploaded';
148
// Overridable by an extending class
149
if (typeof this.data === 'undefined') {
150
this.data = _wpCustomizeHeader.uploads;
154
// So that when adding data we don't hide regular images
155
current = api.get().header_image;
158
this.on('control:setImage', this.setImage, this);
159
this.on('control:removeImage', this.removeImage, this);
160
this.on('add', this.maybeAddRandomChoice, this);
162
_.each(this.data, function(elt, index) {
163
if (!elt.attachment_id) {
164
elt.defaultName = index;
167
if (typeof elt.timestamp === 'undefined') {
173
choice: elt.url.split('/').pop(),
174
selected: current === elt.url.replace(/^https?:\/\//, '')
175
}, { silent: true });
178
if (this.size() > 0) {
179
this.addRandomChoice(current);
183
maybeAddRandomChoice: function() {
184
if (this.size() === 1) {
185
this.addRandomChoice();
189
addRandomChoice: function(initialChoice) {
190
var isRandomSameType = RegExp(this.type).test(initialChoice),
191
randomChoice = 'random-' + this.type + '-image';
196
random: randomChoice,
200
choice: randomChoice,
202
selected: isRandomSameType
206
isRandomChoice: function(choice) {
207
return (/^random-(uploaded|default)-image$/).test(choice);
210
shouldHideTitle: function() {
211
return this.size() < 2;
214
setImage: function(model) {
215
this.each(function(m) {
216
m.set('selected', false);
220
model.set('selected', true);
224
removeImage: function() {
225
this.each(function(m) {
226
m.set('selected', false);
233
* wp.customize.HeaderTool.DefaultsList
236
* @augments wp.customize.HeaderTool.ChoiceList
237
* @augments Backbone.Collection
239
api.HeaderTool.DefaultsList = api.HeaderTool.ChoiceList.extend({
240
initialize: function() {
241
this.type = 'default';
242
this.data = _wpCustomizeHeader.defaults;
243
api.HeaderTool.ChoiceList.prototype.initialize.apply(this);
247
})( jQuery, window.wp );