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