author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
9 | 1 |
/** |
2 |
* @output wp-includes/js/customize-base.js |
|
3 |
*/ |
|
4 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5 |
/** @namespace wp */ |
0 | 6 |
window.wp = window.wp || {}; |
7 |
||
8 |
(function( exports, $ ){ |
|
5 | 9 |
var api = {}, ctor, inherits, |
0 | 10 |
slice = Array.prototype.slice; |
11 |
||
12 |
// Shared empty constructor function to aid in prototype-chain creation. |
|
13 |
ctor = function() {}; |
|
14 |
||
5 | 15 |
/** |
16 |
* Helper function to correctly set up the prototype chain, for subclasses. |
|
17 |
* Similar to `goog.inherits`, but uses a hash of prototype properties and |
|
18 |
* class properties to be extended. |
|
19 |
* |
|
16 | 20 |
* @param object parent Parent class constructor to inherit from. |
21 |
* @param object protoProps Properties to apply to the prototype for use as class instance properties. |
|
22 |
* @param object staticProps Properties to apply directly to the class constructor. |
|
23 |
* @return child The subclassed constructor. |
|
5 | 24 |
*/ |
0 | 25 |
inherits = function( parent, protoProps, staticProps ) { |
26 |
var child; |
|
27 |
||
16 | 28 |
/* |
29 |
* The constructor function for the new subclass is either defined by you |
|
30 |
* (the "constructor" property in your `extend` definition), or defaulted |
|
31 |
* by us to simply call `super()`. |
|
32 |
*/ |
|
0 | 33 |
if ( protoProps && protoProps.hasOwnProperty( 'constructor' ) ) { |
34 |
child = protoProps.constructor; |
|
35 |
} else { |
|
36 |
child = function() { |
|
16 | 37 |
/* |
38 |
* Storing the result `super()` before returning the value |
|
39 |
* prevents a bug in Opera where, if the constructor returns |
|
40 |
* a function, Opera will reject the return value in favor of |
|
41 |
* the original object. This causes all sorts of trouble. |
|
42 |
*/ |
|
0 | 43 |
var result = parent.apply( this, arguments ); |
44 |
return result; |
|
45 |
}; |
|
46 |
} |
|
47 |
||
48 |
// Inherit class (static) properties from parent. |
|
49 |
$.extend( child, parent ); |
|
50 |
||
16 | 51 |
// Set the prototype chain to inherit from `parent`, |
52 |
// without calling `parent`'s constructor function. |
|
0 | 53 |
ctor.prototype = parent.prototype; |
54 |
child.prototype = new ctor(); |
|
55 |
||
56 |
// Add prototype properties (instance properties) to the subclass, |
|
57 |
// if supplied. |
|
16 | 58 |
if ( protoProps ) { |
0 | 59 |
$.extend( child.prototype, protoProps ); |
16 | 60 |
} |
0 | 61 |
|
62 |
// Add static properties to the constructor function, if supplied. |
|
16 | 63 |
if ( staticProps ) { |
0 | 64 |
$.extend( child, staticProps ); |
16 | 65 |
} |
0 | 66 |
|
67 |
// Correctly set child's `prototype.constructor`. |
|
68 |
child.prototype.constructor = child; |
|
69 |
||
70 |
// Set a convenience property in case the parent's prototype is needed later. |
|
71 |
child.__super__ = parent.prototype; |
|
72 |
||
73 |
return child; |
|
74 |
}; |
|
75 |
||
5 | 76 |
/** |
77 |
* Base class for object inheritance. |
|
78 |
*/ |
|
0 | 79 |
api.Class = function( applicator, argsArray, options ) { |
80 |
var magic, args = arguments; |
|
81 |
||
82 |
if ( applicator && argsArray && api.Class.applicator === applicator ) { |
|
83 |
args = argsArray; |
|
84 |
$.extend( this, options || {} ); |
|
85 |
} |
|
86 |
||
87 |
magic = this; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
* If the class has a method called "instance", |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
91 |
* the return value from the class' constructor will be a function that |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
* calls the "instance" method. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
* It is also an object that has properties and methods inside it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
*/ |
0 | 96 |
if ( this.instance ) { |
97 |
magic = function() { |
|
98 |
return magic.instance.apply( magic, arguments ); |
|
99 |
}; |
|
100 |
||
101 |
$.extend( magic, this ); |
|
102 |
} |
|
103 |
||
104 |
magic.initialize.apply( magic, args ); |
|
105 |
return magic; |
|
106 |
}; |
|
107 |
||
5 | 108 |
/** |
109 |
* Creates a subclass of the class. |
|
110 |
* |
|
16 | 111 |
* @param object protoProps Properties to apply to the prototype. |
112 |
* @param object staticProps Properties to apply directly to the class. |
|
113 |
* @return child The subclass. |
|
5 | 114 |
*/ |
18 | 115 |
api.Class.extend = function( protoProps, staticProps ) { |
116 |
var child = inherits( this, protoProps, staticProps ); |
|
5 | 117 |
child.extend = this.extend; |
118 |
return child; |
|
119 |
}; |
|
120 |
||
0 | 121 |
api.Class.applicator = {}; |
122 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
123 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
124 |
* Initialize a class instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
126 |
* Override this function in a subclass as needed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
127 |
*/ |
0 | 128 |
api.Class.prototype.initialize = function() {}; |
129 |
||
130 |
/* |
|
131 |
* Checks whether a given instance extended a constructor. |
|
132 |
* |
|
133 |
* The magic surrounding the instance parameter causes the instanceof |
|
134 |
* keyword to return inaccurate results; it defaults to the function's |
|
135 |
* prototype instead of the constructor chain. Hence this function. |
|
136 |
*/ |
|
137 |
api.Class.prototype.extended = function( constructor ) { |
|
138 |
var proto = this; |
|
139 |
||
140 |
while ( typeof proto.constructor !== 'undefined' ) { |
|
16 | 141 |
if ( proto.constructor === constructor ) { |
0 | 142 |
return true; |
16 | 143 |
} |
144 |
if ( typeof proto.constructor.__super__ === 'undefined' ) { |
|
0 | 145 |
return false; |
16 | 146 |
} |
0 | 147 |
proto = proto.constructor.__super__; |
148 |
} |
|
149 |
return false; |
|
150 |
}; |
|
151 |
||
5 | 152 |
/** |
153 |
* An events manager object, offering the ability to bind to and trigger events. |
|
154 |
* |
|
155 |
* Used as a mixin. |
|
156 |
*/ |
|
0 | 157 |
api.Events = { |
158 |
trigger: function( id ) { |
|
16 | 159 |
if ( this.topics && this.topics[ id ] ) { |
0 | 160 |
this.topics[ id ].fireWith( this, slice.call( arguments, 1 ) ); |
16 | 161 |
} |
0 | 162 |
return this; |
163 |
}, |
|
164 |
||
5 | 165 |
bind: function( id ) { |
0 | 166 |
this.topics = this.topics || {}; |
167 |
this.topics[ id ] = this.topics[ id ] || $.Callbacks(); |
|
168 |
this.topics[ id ].add.apply( this.topics[ id ], slice.call( arguments, 1 ) ); |
|
169 |
return this; |
|
170 |
}, |
|
171 |
||
5 | 172 |
unbind: function( id ) { |
16 | 173 |
if ( this.topics && this.topics[ id ] ) { |
0 | 174 |
this.topics[ id ].remove.apply( this.topics[ id ], slice.call( arguments, 1 ) ); |
16 | 175 |
} |
0 | 176 |
return this; |
177 |
} |
|
178 |
}; |
|
179 |
||
5 | 180 |
/** |
0 | 181 |
* Observable values that support two-way binding. |
5 | 182 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
183 |
* @memberOf wp.customize |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
184 |
* @alias wp.customize.Value |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
185 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
186 |
* @constructor |
5 | 187 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
188 |
api.Value = api.Class.extend(/** @lends wp.customize.Value.prototype */{ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
189 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
190 |
* @param {mixed} initial The initial value. |
16 | 191 |
* @param {Object} options |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
192 |
*/ |
0 | 193 |
initialize: function( initial, options ) { |
16 | 194 |
this._value = initial; // @todo Potentially change this to a this.set() call. |
0 | 195 |
this.callbacks = $.Callbacks(); |
5 | 196 |
this._dirty = false; |
0 | 197 |
|
198 |
$.extend( this, options || {} ); |
|
199 |
||
18 | 200 |
this.set = this.set.bind( this ); |
0 | 201 |
}, |
202 |
||
203 |
/* |
|
204 |
* Magic. Returns a function that will become the instance. |
|
205 |
* Set to null to prevent the instance from extending a function. |
|
206 |
*/ |
|
207 |
instance: function() { |
|
208 |
return arguments.length ? this.set.apply( this, arguments ) : this.get(); |
|
209 |
}, |
|
210 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
211 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
212 |
* Get the value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
213 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
214 |
* @return {mixed} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
215 |
*/ |
0 | 216 |
get: function() { |
217 |
return this._value; |
|
218 |
}, |
|
219 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
220 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
221 |
* Set the value and trigger all bound callbacks. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
222 |
* |
16 | 223 |
* @param {Object} to New value. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
224 |
*/ |
0 | 225 |
set: function( to ) { |
226 |
var from = this._value; |
|
227 |
||
228 |
to = this._setter.apply( this, arguments ); |
|
229 |
to = this.validate( to ); |
|
230 |
||
231 |
// Bail if the sanitized value is null or unchanged. |
|
5 | 232 |
if ( null === to || _.isEqual( from, to ) ) { |
0 | 233 |
return this; |
5 | 234 |
} |
0 | 235 |
|
236 |
this._value = to; |
|
5 | 237 |
this._dirty = true; |
0 | 238 |
|
239 |
this.callbacks.fireWith( this, [ to, from ] ); |
|
240 |
||
241 |
return this; |
|
242 |
}, |
|
243 |
||
244 |
_setter: function( to ) { |
|
245 |
return to; |
|
246 |
}, |
|
247 |
||
248 |
setter: function( callback ) { |
|
249 |
var from = this.get(); |
|
250 |
this._setter = callback; |
|
251 |
// Temporarily clear value so setter can decide if it's valid. |
|
252 |
this._value = null; |
|
253 |
this.set( from ); |
|
254 |
return this; |
|
255 |
}, |
|
256 |
||
257 |
resetSetter: function() { |
|
258 |
this._setter = this.constructor.prototype._setter; |
|
259 |
this.set( this.get() ); |
|
260 |
return this; |
|
261 |
}, |
|
262 |
||
263 |
validate: function( value ) { |
|
264 |
return value; |
|
265 |
}, |
|
266 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
267 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
268 |
* Bind a function to be invoked whenever the value changes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
269 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
270 |
* @param {...Function} A function, or multiple functions, to add to the callback stack. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
271 |
*/ |
5 | 272 |
bind: function() { |
0 | 273 |
this.callbacks.add.apply( this.callbacks, arguments ); |
274 |
return this; |
|
275 |
}, |
|
276 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
278 |
* Unbind a previously bound function. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
279 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
280 |
* @param {...Function} A function, or multiple functions, to remove from the callback stack. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
281 |
*/ |
5 | 282 |
unbind: function() { |
0 | 283 |
this.callbacks.remove.apply( this.callbacks, arguments ); |
284 |
return this; |
|
285 |
}, |
|
286 |
||
287 |
link: function() { // values* |
|
288 |
var set = this.set; |
|
289 |
$.each( arguments, function() { |
|
290 |
this.bind( set ); |
|
291 |
}); |
|
292 |
return this; |
|
293 |
}, |
|
294 |
||
295 |
unlink: function() { // values* |
|
296 |
var set = this.set; |
|
297 |
$.each( arguments, function() { |
|
298 |
this.unbind( set ); |
|
299 |
}); |
|
300 |
return this; |
|
301 |
}, |
|
302 |
||
303 |
sync: function() { // values* |
|
304 |
var that = this; |
|
305 |
$.each( arguments, function() { |
|
306 |
that.link( this ); |
|
307 |
this.link( that ); |
|
308 |
}); |
|
309 |
return this; |
|
310 |
}, |
|
311 |
||
312 |
unsync: function() { // values* |
|
313 |
var that = this; |
|
314 |
$.each( arguments, function() { |
|
315 |
that.unlink( this ); |
|
316 |
this.unlink( that ); |
|
317 |
}); |
|
318 |
return this; |
|
319 |
} |
|
320 |
}); |
|
321 |
||
5 | 322 |
/** |
0 | 323 |
* A collection of observable values. |
5 | 324 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
325 |
* @memberOf wp.customize |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
* @alias wp.customize.Values |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
328 |
* @constructor |
5 | 329 |
* @augments wp.customize.Class |
330 |
* @mixes wp.customize.Events |
|
331 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
332 |
api.Values = api.Class.extend(/** @lends wp.customize.Values.prototype */{ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
333 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
334 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
335 |
* The default constructor for items of the collection. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
336 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
337 |
* @type {object} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
338 |
*/ |
0 | 339 |
defaultConstructor: api.Value, |
340 |
||
341 |
initialize: function( options ) { |
|
342 |
$.extend( this, options || {} ); |
|
343 |
||
344 |
this._value = {}; |
|
345 |
this._deferreds = {}; |
|
346 |
}, |
|
347 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
* Get the instance of an item from the collection if only ID is specified. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
350 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
* If more than one argument is supplied, all are expected to be IDs and |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
352 |
* the last to be a function callback that will be invoked when the requested |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
353 |
* items are available. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
354 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
355 |
* @see {api.Values.when} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
356 |
* |
16 | 357 |
* @param {string} id ID of the item. |
358 |
* @param {...} Zero or more IDs of items to wait for and a callback |
|
359 |
* function to invoke when they're available. Optional. |
|
360 |
* @return {mixed} The item instance if only one ID was supplied. |
|
361 |
* A Deferred Promise object if a callback function is supplied. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
362 |
*/ |
0 | 363 |
instance: function( id ) { |
16 | 364 |
if ( arguments.length === 1 ) { |
0 | 365 |
return this.value( id ); |
16 | 366 |
} |
0 | 367 |
|
368 |
return this.when.apply( this, arguments ); |
|
369 |
}, |
|
370 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
371 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
372 |
* Get the instance of an item. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
373 |
* |
16 | 374 |
* @param {string} id The ID of the item. |
375 |
* @return {[type]} [description] |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
376 |
*/ |
0 | 377 |
value: function( id ) { |
378 |
return this._value[ id ]; |
|
379 |
}, |
|
380 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
381 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
382 |
* Whether the collection has an item with the given ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
383 |
* |
16 | 384 |
* @param {string} id The ID of the item to look for. |
385 |
* @return {boolean} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
386 |
*/ |
0 | 387 |
has: function( id ) { |
388 |
return typeof this._value[ id ] !== 'undefined'; |
|
389 |
}, |
|
390 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
391 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
392 |
* Add an item to the collection. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
393 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
394 |
* @param {string|wp.customize.Class} item - The item instance to add, or the ID for the instance to add. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
395 |
* When an ID string is supplied, then itemObject must be provided. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
396 |
* @param {wp.customize.Class} [itemObject] - The item instance when the first argument is an ID string. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
397 |
* @return {wp.customize.Class} The new item's instance, or an existing instance if already added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
add: function( item, itemObject ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
400 |
var collection = this, id, instance; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
401 |
if ( 'string' === typeof item ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
402 |
id = item; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
403 |
instance = itemObject; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
404 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
405 |
if ( 'string' !== typeof item.id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
406 |
throw new Error( 'Unknown key' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
id = item.id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
instance = item; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
410 |
} |
0 | 411 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
412 |
if ( collection.has( id ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
return collection.value( id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
414 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
416 |
collection._value[ id ] = instance; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
417 |
instance.parent = collection; |
0 | 418 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
419 |
// Propagate a 'change' event on an item up to the collection. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
420 |
if ( instance.extended( api.Value ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
421 |
instance.bind( collection._change ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
422 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
423 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
424 |
collection.trigger( 'add', instance ); |
0 | 425 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
426 |
// If a deferred object exists for this item, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
427 |
// resolve it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
428 |
if ( collection._deferreds[ id ] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
429 |
collection._deferreds[ id ].resolve(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
430 |
} |
0 | 431 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
432 |
return collection._value[ id ]; |
0 | 433 |
}, |
434 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
435 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
436 |
* Create a new item of the collection using the collection's default constructor |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
437 |
* and store it in the collection. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
438 |
* |
16 | 439 |
* @param {string} id The ID of the item. |
440 |
* @param {mixed} value Any extra arguments are passed into the item's initialize method. |
|
441 |
* @return {mixed} The new item's instance. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
442 |
*/ |
0 | 443 |
create: function( id ) { |
444 |
return this.add( id, new this.defaultConstructor( api.Class.applicator, slice.call( arguments, 1 ) ) ); |
|
445 |
}, |
|
446 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
447 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
448 |
* Iterate over all items in the collection invoking the provided callback. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
449 |
* |
16 | 450 |
* @param {Function} callback Function to invoke. |
451 |
* @param {Object} context Object context to invoke the function with. Optional. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
452 |
*/ |
0 | 453 |
each: function( callback, context ) { |
454 |
context = typeof context === 'undefined' ? this : context; |
|
455 |
||
456 |
$.each( this._value, function( key, obj ) { |
|
457 |
callback.call( context, obj, key ); |
|
458 |
}); |
|
459 |
}, |
|
460 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
461 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
462 |
* Remove an item from the collection. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
463 |
* |
16 | 464 |
* @param {string} id The ID of the item to remove. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
465 |
*/ |
0 | 466 |
remove: function( id ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
467 |
var value = this.value( id ); |
0 | 468 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
469 |
if ( value ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
470 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
471 |
// Trigger event right before the element is removed from the collection. |
0 | 472 |
this.trigger( 'remove', value ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
473 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
474 |
if ( value.extended( api.Value ) ) { |
0 | 475 |
value.unbind( this._change ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
476 |
} |
0 | 477 |
delete value.parent; |
478 |
} |
|
479 |
||
480 |
delete this._value[ id ]; |
|
481 |
delete this._deferreds[ id ]; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
482 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
483 |
// Trigger removed event after the item has been eliminated from the collection. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
484 |
if ( value ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
485 |
this.trigger( 'removed', value ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
486 |
} |
0 | 487 |
}, |
488 |
||
489 |
/** |
|
490 |
* Runs a callback once all requested values exist. |
|
491 |
* |
|
492 |
* when( ids*, [callback] ); |
|
493 |
* |
|
494 |
* For example: |
|
495 |
* when( id1, id2, id3, function( value1, value2, value3 ) {} ); |
|
496 |
* |
|
16 | 497 |
* @return $.Deferred.promise(); |
0 | 498 |
*/ |
499 |
when: function() { |
|
500 |
var self = this, |
|
501 |
ids = slice.call( arguments ), |
|
502 |
dfd = $.Deferred(); |
|
503 |
||
16 | 504 |
// If the last argument is a callback, bind it to .done(). |
18 | 505 |
if ( typeof ids[ ids.length - 1 ] === 'function' ) { |
0 | 506 |
dfd.done( ids.pop() ); |
16 | 507 |
} |
0 | 508 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
509 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
510 |
* Create a stack of deferred objects for each item that is not |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
511 |
* yet available, and invoke the supplied callback when they are. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
512 |
*/ |
0 | 513 |
$.when.apply( $, $.map( ids, function( id ) { |
16 | 514 |
if ( self.has( id ) ) { |
0 | 515 |
return; |
16 | 516 |
} |
0 | 517 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
518 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
519 |
* The requested item is not available yet, create a deferred |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
520 |
* object to resolve when it becomes available. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
521 |
*/ |
0 | 522 |
return self._deferreds[ id ] = self._deferreds[ id ] || $.Deferred(); |
523 |
})).done( function() { |
|
524 |
var values = $.map( ids, function( id ) { |
|
525 |
return self( id ); |
|
526 |
}); |
|
527 |
||
528 |
// If a value is missing, we've used at least one expired deferred. |
|
529 |
// Call Values.when again to generate a new deferred. |
|
530 |
if ( values.length !== ids.length ) { |
|
531 |
// ids.push( callback ); |
|
532 |
self.when.apply( self, ids ).done( function() { |
|
533 |
dfd.resolveWith( self, values ); |
|
534 |
}); |
|
535 |
return; |
|
536 |
} |
|
537 |
||
538 |
dfd.resolveWith( self, values ); |
|
539 |
}); |
|
540 |
||
541 |
return dfd.promise(); |
|
542 |
}, |
|
543 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
544 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
545 |
* A helper function to propagate a 'change' event from an item |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
546 |
* to the collection itself. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
547 |
*/ |
0 | 548 |
_change: function() { |
549 |
this.parent.trigger( 'change', this ); |
|
550 |
} |
|
551 |
}); |
|
552 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
553 |
// Create a global events bus on the Customizer. |
0 | 554 |
$.extend( api.Values.prototype, api.Events ); |
555 |
||
5 | 556 |
|
557 |
/** |
|
558 |
* Cast a string to a jQuery collection if it isn't already. |
|
0 | 559 |
* |
5 | 560 |
* @param {string|jQuery collection} element |
561 |
*/ |
|
0 | 562 |
api.ensure = function( element ) { |
16 | 563 |
return typeof element === 'string' ? $( element ) : element; |
0 | 564 |
}; |
565 |
||
5 | 566 |
/** |
567 |
* An observable value that syncs with an element. |
|
568 |
* |
|
569 |
* Handles inputs, selects, and textareas by default. |
|
570 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
571 |
* @memberOf wp.customize |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
572 |
* @alias wp.customize.Element |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
573 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
574 |
* @constructor |
5 | 575 |
* @augments wp.customize.Value |
576 |
* @augments wp.customize.Class |
|
577 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
578 |
api.Element = api.Value.extend(/** @lends wp.customize.Element */{ |
0 | 579 |
initialize: function( element, options ) { |
580 |
var self = this, |
|
581 |
synchronizer = api.Element.synchronizer.html, |
|
582 |
type, update, refresh; |
|
583 |
||
584 |
this.element = api.ensure( element ); |
|
585 |
this.events = ''; |
|
586 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
587 |
if ( this.element.is( 'input, select, textarea' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
588 |
type = this.element.prop( 'type' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
589 |
this.events += ' change input'; |
0 | 590 |
synchronizer = api.Element.synchronizer.val; |
591 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
592 |
if ( this.element.is( 'input' ) && api.Element.synchronizer[ type ] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
593 |
synchronizer = api.Element.synchronizer[ type ]; |
0 | 594 |
} |
595 |
} |
|
596 |
||
597 |
api.Value.prototype.initialize.call( this, null, $.extend( options || {}, synchronizer ) ); |
|
598 |
this._value = this.get(); |
|
599 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
600 |
update = this.update; |
0 | 601 |
refresh = this.refresh; |
602 |
||
603 |
this.update = function( to ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
604 |
if ( to !== refresh.call( self ) ) { |
0 | 605 |
update.apply( this, arguments ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
606 |
} |
0 | 607 |
}; |
608 |
this.refresh = function() { |
|
609 |
self.set( refresh.call( self ) ); |
|
610 |
}; |
|
611 |
||
612 |
this.bind( this.update ); |
|
18 | 613 |
this.element.on( this.events, this.refresh ); |
0 | 614 |
}, |
615 |
||
616 |
find: function( selector ) { |
|
617 |
return $( selector, this.element ); |
|
618 |
}, |
|
619 |
||
620 |
refresh: function() {}, |
|
621 |
||
622 |
update: function() {} |
|
623 |
}); |
|
624 |
||
625 |
api.Element.synchronizer = {}; |
|
626 |
||
5 | 627 |
$.each( [ 'html', 'val' ], function( index, method ) { |
0 | 628 |
api.Element.synchronizer[ method ] = { |
629 |
update: function( to ) { |
|
630 |
this.element[ method ]( to ); |
|
631 |
}, |
|
632 |
refresh: function() { |
|
633 |
return this.element[ method ](); |
|
634 |
} |
|
635 |
}; |
|
636 |
}); |
|
637 |
||
638 |
api.Element.synchronizer.checkbox = { |
|
639 |
update: function( to ) { |
|
640 |
this.element.prop( 'checked', to ); |
|
641 |
}, |
|
642 |
refresh: function() { |
|
643 |
return this.element.prop( 'checked' ); |
|
644 |
} |
|
645 |
}; |
|
646 |
||
647 |
api.Element.synchronizer.radio = { |
|
648 |
update: function( to ) { |
|
649 |
this.element.filter( function() { |
|
650 |
return this.value === to; |
|
651 |
}).prop( 'checked', true ); |
|
652 |
}, |
|
653 |
refresh: function() { |
|
654 |
return this.element.filter( ':checked' ).val(); |
|
655 |
} |
|
656 |
}; |
|
657 |
||
658 |
$.support.postMessage = !! window.postMessage; |
|
659 |
||
5 | 660 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
661 |
* A communicator for sending data from one window to another over postMessage. |
5 | 662 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
663 |
* @memberOf wp.customize |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
664 |
* @alias wp.customize.Messenger |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
665 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
666 |
* @constructor |
5 | 667 |
* @augments wp.customize.Class |
668 |
* @mixes wp.customize.Events |
|
669 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
670 |
api.Messenger = api.Class.extend(/** @lends wp.customize.Messenger.prototype */{ |
5 | 671 |
/** |
672 |
* Create a new Value. |
|
673 |
* |
|
16 | 674 |
* @param {string} key Unique identifier. |
675 |
* @param {mixed} initial Initial value. |
|
676 |
* @param {mixed} options Options hash. Optional. |
|
677 |
* @return {Value} Class instance of the Value. |
|
5 | 678 |
*/ |
0 | 679 |
add: function( key, initial, options ) { |
680 |
return this[ key ] = new api.Value( initial, options ); |
|
681 |
}, |
|
682 |
||
683 |
/** |
|
684 |
* Initialize Messenger. |
|
685 |
* |
|
16 | 686 |
* @param {Object} params - Parameters to configure the messenger. |
687 |
* {string} params.url - The URL to communicate with. |
|
688 |
* {window} params.targetWindow - The window instance to communicate with. Default window.parent. |
|
689 |
* {string} params.channel - If provided, will send the channel with each message and only accept messages a matching channel. |
|
690 |
* @param {Object} options - Extend any instance parameter or method with this object. |
|
0 | 691 |
*/ |
692 |
initialize: function( params, options ) { |
|
693 |
// Target the parent frame by default, but only if a parent frame exists. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
694 |
var defaultTarget = window.parent === window ? null : window.parent; |
0 | 695 |
|
696 |
$.extend( this, options || {} ); |
|
697 |
||
698 |
this.add( 'channel', params.channel ); |
|
699 |
this.add( 'url', params.url || '' ); |
|
700 |
this.add( 'origin', this.url() ).link( this.url ).setter( function( to ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
701 |
var urlParser = document.createElement( 'a' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
702 |
urlParser.href = to; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
703 |
// Port stripping needed by IE since it adds to host but not to event.origin. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
704 |
return urlParser.protocol + '//' + urlParser.host.replace( /:(80|443)$/, '' ); |
0 | 705 |
}); |
706 |
||
16 | 707 |
// First add with no value. |
5 | 708 |
this.add( 'targetWindow', null ); |
709 |
// This avoids SecurityErrors when setting a window object in x-origin iframe'd scenarios. |
|
710 |
this.targetWindow.set = function( to ) { |
|
711 |
var from = this._value; |
|
712 |
||
713 |
to = this._setter.apply( this, arguments ); |
|
714 |
to = this.validate( to ); |
|
715 |
||
716 |
if ( null === to || from === to ) { |
|
717 |
return this; |
|
718 |
} |
|
719 |
||
720 |
this._value = to; |
|
721 |
this._dirty = true; |
|
722 |
||
723 |
this.callbacks.fireWith( this, [ to, from ] ); |
|
724 |
||
725 |
return this; |
|
726 |
}; |
|
16 | 727 |
// Now set it. |
5 | 728 |
this.targetWindow( params.targetWindow || defaultTarget ); |
729 |
||
730 |
||
16 | 731 |
/* |
732 |
* Since we want jQuery to treat the receive function as unique |
|
733 |
* to this instance, we give the function a new guid. |
|
734 |
* |
|
735 |
* This will prevent every Messenger's receive function from being |
|
736 |
* unbound when calling $.off( 'message', this.receive ); |
|
737 |
*/ |
|
18 | 738 |
this.receive = this.receive.bind( this ); |
0 | 739 |
this.receive.guid = $.guid++; |
740 |
||
741 |
$( window ).on( 'message', this.receive ); |
|
742 |
}, |
|
743 |
||
744 |
destroy: function() { |
|
745 |
$( window ).off( 'message', this.receive ); |
|
746 |
}, |
|
747 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
748 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
749 |
* Receive data from the other window. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
750 |
* |
16 | 751 |
* @param {jQuery.Event} event Event with embedded data. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
752 |
*/ |
0 | 753 |
receive: function( event ) { |
754 |
var message; |
|
755 |
||
756 |
event = event.originalEvent; |
|
757 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
758 |
if ( ! this.targetWindow || ! this.targetWindow() ) { |
0 | 759 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
760 |
} |
0 | 761 |
|
762 |
// Check to make sure the origin is valid. |
|
16 | 763 |
if ( this.origin() && event.origin !== this.origin() ) { |
0 | 764 |
return; |
16 | 765 |
} |
0 | 766 |
|
16 | 767 |
// Ensure we have a string that's JSON.parse-able. |
5 | 768 |
if ( typeof event.data !== 'string' || event.data[0] !== '{' ) { |
769 |
return; |
|
770 |
} |
|
771 |
||
0 | 772 |
message = JSON.parse( event.data ); |
773 |
||
774 |
// Check required message properties. |
|
16 | 775 |
if ( ! message || ! message.id || typeof message.data === 'undefined' ) { |
0 | 776 |
return; |
16 | 777 |
} |
0 | 778 |
|
779 |
// Check if channel names match. |
|
16 | 780 |
if ( ( message.channel || this.channel() ) && this.channel() !== message.channel ) { |
0 | 781 |
return; |
16 | 782 |
} |
0 | 783 |
|
784 |
this.trigger( message.id, message.data ); |
|
785 |
}, |
|
786 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
787 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
788 |
* Send data to the other window. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
789 |
* |
16 | 790 |
* @param {string} id The event name. |
791 |
* @param {Object} data Data. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
792 |
*/ |
0 | 793 |
send: function( id, data ) { |
794 |
var message; |
|
795 |
||
796 |
data = typeof data === 'undefined' ? null : data; |
|
797 |
||
16 | 798 |
if ( ! this.url() || ! this.targetWindow() ) { |
0 | 799 |
return; |
16 | 800 |
} |
0 | 801 |
|
802 |
message = { id: id, data: data }; |
|
16 | 803 |
if ( this.channel() ) { |
0 | 804 |
message.channel = this.channel(); |
16 | 805 |
} |
0 | 806 |
|
807 |
this.targetWindow().postMessage( JSON.stringify( message ), this.origin() ); |
|
808 |
} |
|
809 |
}); |
|
810 |
||
811 |
// Add the Events mixin to api.Messenger. |
|
812 |
$.extend( api.Messenger.prototype, api.Events ); |
|
813 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
814 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
815 |
* Notification. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
816 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
817 |
* @class |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
818 |
* @augments wp.customize.Class |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
819 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
820 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
821 |
* @memberOf wp.customize |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
822 |
* @alias wp.customize.Notification |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
823 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
824 |
* @param {string} code - The error code. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
825 |
* @param {object} params - Params. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
826 |
* @param {string} params.message=null - The error message. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
827 |
* @param {string} [params.type=error] - The notification type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
828 |
* @param {boolean} [params.fromServer=false] - Whether the notification was server-sent. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
829 |
* @param {string} [params.setting=null] - The setting ID that the notification is related to. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
830 |
* @param {*} [params.data=null] - Any additional data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
831 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
832 |
api.Notification = api.Class.extend(/** @lends wp.customize.Notification.prototype */{ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
833 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
834 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
835 |
* Template function for rendering the notification. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
836 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
837 |
* This will be populated with template option or else it will be populated with template from the ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
838 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
839 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
840 |
* @var {Function} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
841 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
842 |
template: null, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
843 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
844 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
845 |
* ID for the template to render the notification. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
846 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
847 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
848 |
* @var {string} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
849 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
850 |
templateId: 'customize-notification', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
851 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
852 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
853 |
* Additional class names to add to the notification container. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
854 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
855 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
856 |
* @var {string} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
857 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
858 |
containerClasses: '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
859 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
860 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
861 |
* Initialize notification. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
862 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
863 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
864 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
865 |
* @param {string} code - Notification code. |
16 | 866 |
* @param {Object} params - Notification parameters. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
867 |
* @param {string} params.message - Message. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
868 |
* @param {string} [params.type=error] - Type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
869 |
* @param {string} [params.setting] - Related setting ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
870 |
* @param {Function} [params.template] - Function for rendering template. If not provided, this will come from templateId. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
871 |
* @param {string} [params.templateId] - ID for template to render the notification. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
872 |
* @param {string} [params.containerClasses] - Additional class names to add to the notification container. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
873 |
* @param {boolean} [params.dismissible] - Whether the notification can be dismissed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
874 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
875 |
initialize: function( code, params ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
876 |
var _params; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
877 |
this.code = code; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
878 |
_params = _.extend( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
879 |
{ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
880 |
message: null, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
881 |
type: 'error', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
882 |
fromServer: false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
883 |
data: null, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
884 |
setting: null, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
885 |
template: null, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
886 |
dismissible: false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
887 |
containerClasses: '' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
888 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
889 |
params |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
890 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
891 |
delete _params.code; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
892 |
_.extend( this, _params ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
893 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
894 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
895 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
896 |
* Render the notification. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
897 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
898 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
899 |
* |
16 | 900 |
* @return {jQuery} Notification container element. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
901 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
902 |
render: function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
903 |
var notification = this, container, data; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
904 |
if ( ! notification.template ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
905 |
notification.template = wp.template( notification.templateId ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
906 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
907 |
data = _.extend( {}, notification, { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
908 |
alt: notification.parent && notification.parent.alt |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
909 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
910 |
container = $( notification.template( data ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
911 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
912 |
if ( notification.dismissible ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
913 |
container.find( '.notice-dismiss' ).on( 'click keydown', function( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
914 |
if ( 'keydown' === event.type && 13 !== event.which ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
915 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
916 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
917 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
918 |
if ( notification.parent ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
919 |
notification.parent.remove( notification.code ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
920 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
921 |
container.remove(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
922 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
923 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
924 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
925 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
926 |
return container; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
927 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
928 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
929 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
930 |
// The main API object is also a collection of all customizer settings. |
0 | 931 |
api = $.extend( new api.Values(), api ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
932 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
933 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
934 |
* Get all customize settings. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
935 |
* |
9 | 936 |
* @alias wp.customize.get |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
937 |
* |
16 | 938 |
* @return {Object} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
939 |
*/ |
0 | 940 |
api.get = function() { |
941 |
var result = {}; |
|
942 |
||
943 |
this.each( function( obj, key ) { |
|
944 |
result[ key ] = obj.get(); |
|
945 |
}); |
|
946 |
||
947 |
return result; |
|
948 |
}; |
|
949 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
950 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
951 |
* Utility function namespace |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
952 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
953 |
* @namespace wp.customize.utils |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
954 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
955 |
api.utils = {}; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
956 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
957 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
958 |
* Parse query string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
959 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
960 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
961 |
* @access public |
9 | 962 |
* |
963 |
* @alias wp.customize.utils.parseQueryString |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
964 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
965 |
* @param {string} queryString Query string. |
16 | 966 |
* @return {Object} Parsed query string. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
967 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
968 |
api.utils.parseQueryString = function parseQueryString( queryString ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
969 |
var queryParams = {}; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
970 |
_.each( queryString.split( '&' ), function( pair ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
971 |
var parts, key, value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
972 |
parts = pair.split( '=', 2 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
973 |
if ( ! parts[0] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
974 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
975 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
976 |
key = decodeURIComponent( parts[0].replace( /\+/g, ' ' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
977 |
key = key.replace( / /g, '_' ); // What PHP does. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
978 |
if ( _.isUndefined( parts[1] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
979 |
value = null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
980 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
981 |
value = decodeURIComponent( parts[1].replace( /\+/g, ' ' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
982 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
983 |
queryParams[ key ] = value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
984 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
985 |
return queryParams; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
986 |
}; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
987 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
988 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
989 |
* Expose the API publicly on window.wp.customize |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
990 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
991 |
* @namespace wp.customize |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
992 |
*/ |
0 | 993 |
exports.customize = api; |
994 |
})( wp, jQuery ); |