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