~smagoun/whoopsie/whoopsie-lp1017637

« back to all changes in this revision

Viewing changes to backend/stats/static/js/yui/build/attribute-core/attribute-core.js

  • Committer: Evan Dandrea
  • Date: 2012-05-09 05:53:45 UTC
  • Revision ID: evan.dandrea@canonical.com-20120509055345-z2j41tmcbf4as5uf
The backend now lives in lp:daisy and the website (errors.ubuntu.com) now lives in lp:errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
YUI 3.5.0 (build 5089)
3
 
Copyright 2012 Yahoo! Inc. All rights reserved.
4
 
Licensed under the BSD License.
5
 
http://yuilibrary.com/license/
6
 
*/
7
 
YUI.add('attribute-core', function(Y) {
8
 
 
9
 
    /**
10
 
     * The State class maintains state for a collection of named items, with 
11
 
     * a varying number of properties defined.
12
 
     *
13
 
     * It avoids the need to create a separate class for the item, and separate instances 
14
 
     * of these classes for each item, by storing the state in a 2 level hash table, 
15
 
     * improving performance when the number of items is likely to be large.
16
 
     *
17
 
     * @constructor
18
 
     * @class State
19
 
     */
20
 
    Y.State = function() {
21
 
        /**
22
 
         * Hash of attributes
23
 
         * @property data
24
 
         */
25
 
        this.data = {};
26
 
    };
27
 
 
28
 
    Y.State.prototype = {
29
 
 
30
 
        /**
31
 
         * Adds a property to an item.
32
 
         *
33
 
         * @method add
34
 
         * @param name {String} The name of the item.
35
 
         * @param key {String} The name of the property.
36
 
         * @param val {Any} The value of the property.
37
 
         */
38
 
        add : function(name, key, val) {
39
 
            var d = this.data;
40
 
            d[name] = d[name] || {};
41
 
            d[name][key] = val;
42
 
        },
43
 
 
44
 
        /**
45
 
         * Adds multiple properties to an item.
46
 
         *
47
 
         * @method addAll
48
 
         * @param name {String} The name of the item.
49
 
         * @param o {Object} A hash of property/value pairs.
50
 
         */
51
 
        addAll: function(name, o) {
52
 
            var key;
53
 
 
54
 
            for (key in o) {
55
 
                if (o.hasOwnProperty(key)) {
56
 
                    this.add(name, key, o[key]);
57
 
                }
58
 
            }
59
 
        },
60
 
 
61
 
        /**
62
 
         * Removes a property from an item.
63
 
         *
64
 
         * @method remove
65
 
         * @param name {String} The name of the item.
66
 
         * @param key {String} The property to remove.
67
 
         */
68
 
        remove: function(name, key) {
69
 
            var d = this.data;
70
 
            if (d[name]) {
71
 
                delete d[name][key];
72
 
            }
73
 
        },
74
 
 
75
 
        /**
76
 
         * Removes multiple properties from an item, or remove the item completely.
77
 
         *
78
 
         * @method removeAll
79
 
         * @param name {String} The name of the item.
80
 
         * @param o {Object|Array} Collection of properties to delete. If not provided, the entire item is removed.
81
 
         */
82
 
        removeAll: function(name, o) {
83
 
            var d = this.data;
84
 
 
85
 
            if (!o) {
86
 
                if (d[name]) {
87
 
                    delete d[name];
88
 
                }
89
 
            } else {
90
 
                Y.each(o, function(v, k) {
91
 
                    if(Y.Lang.isString(k)) {
92
 
                        this.remove(name, k);
93
 
                    } else {
94
 
                        this.remove(name, v);
95
 
                    }
96
 
                }, this);
97
 
            }
98
 
        },
99
 
 
100
 
        /**
101
 
         * For a given item, returns the value of the property requested, or undefined if not found.
102
 
         *
103
 
         * @method get
104
 
         * @param name {String} The name of the item
105
 
         * @param key {String} Optional. The property value to retrieve.
106
 
         * @return {Any} The value of the supplied property.
107
 
         */
108
 
        get: function(name, key) {
109
 
            var d = this.data;
110
 
            return (d[name]) ? d[name][key] : undefined;
111
 
        },
112
 
 
113
 
        /**
114
 
         * For the given item, returns an object with all of the
115
 
         * item's property/value pairs. By default the object returned
116
 
         * is a shallow copy of the stored data, but passing in true
117
 
         * as the second parameter will return a reference to the stored
118
 
         * data.
119
 
         *
120
 
         * @method getAll
121
 
         * @param name {String} The name of the item
122
 
         * @param reference {boolean} true, if you want a reference to the stored
123
 
         * object 
124
 
         * @return {Object} An object with property/value pairs for the item.
125
 
         */
126
 
        getAll : function(name, reference) {
127
 
            var d = this.data, o;
128
 
 
129
 
            if (!reference) {
130
 
                Y.each(d[name], function(v, k) {
131
 
                        o = o || {};
132
 
                        o[k] = v;
133
 
                });
134
 
            } else {
135
 
                o = d[name];
136
 
            }
137
 
 
138
 
            return o;
139
 
        }
140
 
    };
141
 
    /**
142
 
     * The attribute module provides an augmentable Attribute implementation, which 
143
 
     * adds configurable attributes and attribute change events to the class being 
144
 
     * augmented. It also provides a State class, which is used internally by Attribute,
145
 
     * but can also be used independently to provide a name/property/value data structure to
146
 
     * store state.
147
 
     *
148
 
     * @module attribute
149
 
     */
150
 
 
151
 
    /**
152
 
     * The attribute-core submodule provides the lightest level of attribute handling support 
153
 
     * without Attribute change events, or lesser used methods such as reset(), modifyAttrs(),
154
 
     * and removeAttr().
155
 
     *
156
 
     * @module attribute
157
 
     * @submodule attribute-core
158
 
     */
159
 
    var O = Y.Object,
160
 
        Lang = Y.Lang,
161
 
 
162
 
        DOT = ".",
163
 
 
164
 
        // Externally configurable props
165
 
        GETTER = "getter",
166
 
        SETTER = "setter",
167
 
        READ_ONLY = "readOnly",
168
 
        WRITE_ONCE = "writeOnce",
169
 
        INIT_ONLY = "initOnly",
170
 
        VALIDATOR = "validator",
171
 
        VALUE = "value",
172
 
        VALUE_FN = "valueFn",
173
 
        LAZY_ADD = "lazyAdd",
174
 
 
175
 
        // Used for internal state management
176
 
        ADDED = "added",
177
 
        BYPASS_PROXY = "_bypassProxy",
178
 
        INITIALIZING = "initializing",
179
 
        INIT_VALUE = "initValue",
180
 
        LAZY = "lazy",
181
 
        IS_LAZY_ADD = "isLazyAdd",
182
 
 
183
 
        INVALID_VALUE;
184
 
 
185
 
    /**
186
 
     * <p>
187
 
     * AttributeCore provides the lightest level of configurable attribute support. It is designed to be 
188
 
     * augmented on to a host class, and provides the host with the ability to configure 
189
 
     * attributes to store and retrieve state, <strong>but without support for attribute change events</strong>.
190
 
     * </p>
191
 
     * <p>For example, attributes added to the host can be configured:</p>
192
 
     * <ul>
193
 
     *     <li>As read only.</li>
194
 
     *     <li>As write once.</li>
195
 
     *     <li>With a setter function, which can be used to manipulate
196
 
     *     values passed to Attribute's <a href="#method_set">set</a> method, before they are stored.</li>
197
 
     *     <li>With a getter function, which can be used to manipulate stored values,
198
 
     *     before they are returned by Attribute's <a href="#method_get">get</a> method.</li>
199
 
     *     <li>With a validator function, to validate values before they are stored.</li>
200
 
     * </ul>
201
 
     *
202
 
     * <p>See the <a href="#method_addAttr">addAttr</a> method, for the complete set of configuration
203
 
     * options available for attributes.</p>
204
 
     * 
205
 
     * <p>Object/Classes based on AttributeCore can augment <a href="AttributeEvents.html">AttributeEvents</a> 
206
 
     * (with true for overwrite) and <a href="AttributeExtras.html">AttributeExtras</a> to add attribute event and 
207
 
     * additional, less commonly used attribute methods, such as `modifyAttr`, `removeAttr` and `reset`.</p>   
208
 
     *
209
 
     * @class AttributeCore
210
 
     * @param attrs {Object} The attributes to add during construction (passed through to <a href="#method_addAttrs">addAttrs</a>). These can also be defined on the constructor being augmented with Attribute by defining the ATTRS property on the constructor.
211
 
     * @param values {Object} The initial attribute values to apply (passed through to <a href="#method_addAttrs">addAttrs</a>). These are not merged/cloned. The caller is responsible for isolating user provided values if required.
212
 
     * @param lazy {boolean} Whether or not to add attributes lazily (passed through to <a href="#method_addAttrs">addAttrs</a>).
213
 
     */
214
 
    function AttributeCore(attrs, values, lazy) {
215
 
        this._initAttrHost(attrs, values, lazy);            
216
 
    }
217
 
 
218
 
    /**
219
 
     * <p>The value to return from an attribute setter in order to prevent the set from going through.</p>
220
 
     *
221
 
     * <p>You can return this value from your setter if you wish to combine validator and setter 
222
 
     * functionality into a single setter function, which either returns the massaged value to be stored or 
223
 
     * AttributeCore.INVALID_VALUE to prevent invalid values from being stored.</p>
224
 
     *
225
 
     * @property INVALID_VALUE
226
 
     * @type Object
227
 
     * @static
228
 
     * @final
229
 
     */
230
 
    AttributeCore.INVALID_VALUE = {};
231
 
    INVALID_VALUE = AttributeCore.INVALID_VALUE;
232
 
 
233
 
    /**
234
 
     * The list of properties which can be configured for 
235
 
     * each attribute (e.g. setter, getter, writeOnce etc.).
236
 
     *
237
 
     * This property is used internally as a whitelist for faster
238
 
     * Y.mix operations.
239
 
     *
240
 
     * @property _ATTR_CFG
241
 
     * @type Array
242
 
     * @static
243
 
     * @protected
244
 
     */
245
 
    AttributeCore._ATTR_CFG = [SETTER, GETTER, VALIDATOR, VALUE, VALUE_FN, WRITE_ONCE, READ_ONLY, LAZY_ADD, BYPASS_PROXY];
246
 
 
247
 
    AttributeCore.prototype = {
248
 
 
249
 
        /**
250
 
         * Constructor logic for attributes. Initializes the host state, and sets up the inital attributes passed to the 
251
 
         * constructor.
252
 
         *
253
 
         * @method _initAttrHost
254
 
         * @param attrs {Object} The attributes to add during construction (passed through to <a href="#method_addAttrs">addAttrs</a>). These can also be defined on the constructor being augmented with Attribute by defining the ATTRS property on the constructor.
255
 
         * @param values {Object} The initial attribute values to apply (passed through to <a href="#method_addAttrs">addAttrs</a>). These are not merged/cloned. The caller is responsible for isolating user provided values if required.
256
 
         * @param lazy {boolean} Whether or not to add attributes lazily (passed through to <a href="#method_addAttrs">addAttrs</a>).
257
 
         * @private
258
 
         */
259
 
        _initAttrHost : function(attrs, values, lazy) {
260
 
            this._state = new Y.State();
261
 
            this._initAttrs(attrs, values, lazy);
262
 
        },
263
 
 
264
 
        /**
265
 
         * <p>
266
 
         * Adds an attribute with the provided configuration to the host object.
267
 
         * </p>
268
 
         * <p>
269
 
         * The config argument object supports the following properties:
270
 
         * </p>
271
 
         * 
272
 
         * <dl>
273
 
         *    <dt>value &#60;Any&#62;</dt>
274
 
         *    <dd>The initial value to set on the attribute</dd>
275
 
         *
276
 
         *    <dt>valueFn &#60;Function | String&#62;</dt>
277
 
         *    <dd>
278
 
         *    <p>A function, which will return the initial value to set on the attribute. This is useful
279
 
         *    for cases where the attribute configuration is defined statically, but needs to 
280
 
         *    reference the host instance ("this") to obtain an initial value. If both the value and valueFn properties are defined, 
281
 
         *    the value returned by the valueFn has precedence over the value property, unless it returns undefined, in which 
282
 
         *    case the value property is used.</p>
283
 
         *
284
 
         *    <p>valueFn can also be set to a string, representing the name of the instance method to be used to retrieve the value.</p>
285
 
         *    </dd>
286
 
         *
287
 
         *    <dt>readOnly &#60;boolean&#62;</dt>
288
 
         *    <dd>Whether or not the attribute is read only. Attributes having readOnly set to true
289
 
         *        cannot be modified by invoking the set method.</dd>
290
 
         *
291
 
         *    <dt>writeOnce &#60;boolean&#62; or &#60;string&#62;</dt>
292
 
         *    <dd>
293
 
         *        Whether or not the attribute is "write once". Attributes having writeOnce set to true, 
294
 
         *        can only have their values set once, be it through the default configuration, 
295
 
         *        constructor configuration arguments, or by invoking set.
296
 
         *        <p>The writeOnce attribute can also be set to the string "initOnly", in which case the attribute can only be set during initialization
297
 
         *        (when used with Base, this means it can only be set during construction)</p>
298
 
         *    </dd>
299
 
         *
300
 
         *    <dt>setter &#60;Function | String&#62;</dt>
301
 
         *    <dd>
302
 
         *    <p>The setter function used to massage or normalize the value passed to the set method for the attribute. 
303
 
         *    The value returned by the setter will be the final stored value. Returning
304
 
         *    <a href="#property_Attribute.INVALID_VALUE">Attribute.INVALID_VALUE</a>, from the setter will prevent
305
 
         *    the value from being stored.
306
 
         *    </p>
307
 
         *    
308
 
         *    <p>setter can also be set to a string, representing the name of the instance method to be used as the setter function.</p>
309
 
         *    </dd>
310
 
         *      
311
 
         *    <dt>getter &#60;Function | String&#62;</dt>
312
 
         *    <dd>
313
 
         *    <p>
314
 
         *    The getter function used to massage or normalize the value returned by the get method for the attribute.
315
 
         *    The value returned by the getter function is the value which will be returned to the user when they 
316
 
         *    invoke get.
317
 
         *    </p>
318
 
         *
319
 
         *    <p>getter can also be set to a string, representing the name of the instance method to be used as the getter function.</p>
320
 
         *    </dd>
321
 
         *
322
 
         *    <dt>validator &#60;Function | String&#62;</dt>
323
 
         *    <dd>
324
 
         *    <p>
325
 
         *    The validator function invoked prior to setting the stored value. Returning
326
 
         *    false from the validator function will prevent the value from being stored.
327
 
         *    </p>
328
 
         *    
329
 
         *    <p>validator can also be set to a string, representing the name of the instance method to be used as the validator function.</p>
330
 
         *    </dd>
331
 
         *
332
 
         *    <dt>lazyAdd &#60;boolean&#62;</dt>
333
 
         *    <dd>Whether or not to delay initialization of the attribute until the first call to get/set it. 
334
 
         *    This flag can be used to over-ride lazy initialization on a per attribute basis, when adding multiple attributes through 
335
 
         *    the <a href="#method_addAttrs">addAttrs</a> method.</dd>
336
 
         *
337
 
         * </dl>
338
 
         *
339
 
         * <p>The setter, getter and validator are invoked with the value and name passed in as the first and second arguments, and with
340
 
         * the context ("this") set to the host object.</p>
341
 
         *
342
 
         * <p>Configuration properties outside of the list mentioned above are considered private properties used internally by attribute, 
343
 
         * and are not intended for public use.</p>
344
 
         * 
345
 
         * @method addAttr
346
 
         *
347
 
         * @param {String} name The name of the attribute.
348
 
         * @param {Object} config An object with attribute configuration property/value pairs, specifying the configuration for the attribute.
349
 
         *
350
 
         * <p>
351
 
         * <strong>NOTE:</strong> The configuration object is modified when adding an attribute, so if you need 
352
 
         * to protect the original values, you will need to merge the object.
353
 
         * </p>
354
 
         *
355
 
         * @param {boolean} lazy (optional) Whether or not to add this attribute lazily (on the first call to get/set). 
356
 
         *
357
 
         * @return {Object} A reference to the host object.
358
 
         *
359
 
         * @chainable
360
 
         */
361
 
        addAttr: function(name, config, lazy) {
362
 
 
363
 
 
364
 
            var host = this, // help compression
365
 
                state = host._state,
366
 
                value,
367
 
                hasValue;
368
 
 
369
 
            config = config || {};
370
 
 
371
 
            lazy = (LAZY_ADD in config) ? config[LAZY_ADD] : lazy;
372
 
 
373
 
            if (lazy && !host.attrAdded(name)) {
374
 
                state.addAll(name, {
375
 
                    lazy : config,
376
 
                    added : true
377
 
                });
378
 
            } else {
379
 
 
380
 
 
381
 
                if (!host.attrAdded(name) || state.get(name, IS_LAZY_ADD)) {
382
 
 
383
 
                    hasValue = (VALUE in config);
384
 
 
385
 
 
386
 
                    if (hasValue) {
387
 
                        // We'll go through set, don't want to set value in config directly
388
 
                        value = config.value;
389
 
                        delete config.value;
390
 
                    }
391
 
 
392
 
                    config.added = true;
393
 
                    config.initializing = true;
394
 
 
395
 
                    state.addAll(name, config);
396
 
 
397
 
                    if (hasValue) {
398
 
                        // Go through set, so that raw values get normalized/validated
399
 
                        host.set(name, value);
400
 
                    }
401
 
 
402
 
                    state.remove(name, INITIALIZING);
403
 
                }
404
 
            }
405
 
 
406
 
            return host;
407
 
        },
408
 
 
409
 
        /**
410
 
         * Checks if the given attribute has been added to the host
411
 
         *
412
 
         * @method attrAdded
413
 
         * @param {String} name The name of the attribute to check.
414
 
         * @return {boolean} true if an attribute with the given name has been added, false if it hasn't. This method will return true for lazily added attributes.
415
 
         */
416
 
        attrAdded: function(name) {
417
 
            return !!this._state.get(name, ADDED);
418
 
        },
419
 
 
420
 
        /**
421
 
         * Returns the current value of the attribute. If the attribute
422
 
         * has been configured with a 'getter' function, this method will delegate
423
 
         * to the 'getter' to obtain the value of the attribute.
424
 
         *
425
 
         * @method get
426
 
         *
427
 
         * @param {String} name The name of the attribute. If the value of the attribute is an Object, 
428
 
         * dot notation can be used to obtain the value of a property of the object (e.g. <code>get("x.y.z")</code>)
429
 
         *
430
 
         * @return {Any} The value of the attribute
431
 
         */
432
 
        get : function(name) {
433
 
            return this._getAttr(name);
434
 
        },
435
 
 
436
 
        /**
437
 
         * Checks whether or not the attribute is one which has been
438
 
         * added lazily and still requires initialization.
439
 
         *
440
 
         * @method _isLazyAttr
441
 
         * @private
442
 
         * @param {String} name The name of the attribute
443
 
         * @return {boolean} true if it's a lazily added attribute, false otherwise.
444
 
         */
445
 
        _isLazyAttr: function(name) {
446
 
            return this._state.get(name, LAZY);
447
 
        },
448
 
 
449
 
        /**
450
 
         * Finishes initializing an attribute which has been lazily added.
451
 
         *
452
 
         * @method _addLazyAttr
453
 
         * @private
454
 
         * @param {Object} name The name of the attribute
455
 
         */
456
 
        _addLazyAttr: function(name, cfg) {
457
 
            var state = this._state,
458
 
                lazyCfg = state.get(name, LAZY);
459
 
 
460
 
            state.add(name, IS_LAZY_ADD, true);
461
 
            state.remove(name, LAZY);
462
 
            this.addAttr(name, lazyCfg);
463
 
        },
464
 
 
465
 
        /**
466
 
         * Sets the value of an attribute.
467
 
         *
468
 
         * @method set
469
 
         * @chainable
470
 
         *
471
 
         * @param {String} name The name of the attribute. If the 
472
 
         * current value of the attribute is an Object, dot notation can be used
473
 
         * to set the value of a property within the object (e.g. <code>set("x.y.z", 5)</code>).
474
 
         *
475
 
         * @param {Any} value The value to set the attribute to.
476
 
         *
477
 
         * @return {Object} A reference to the host object.
478
 
         */
479
 
        set : function(name, val) {
480
 
            return this._setAttr(name, val);
481
 
        },
482
 
 
483
 
        /**
484
 
         * Allows setting of readOnly/writeOnce attributes. See <a href="#method_set">set</a> for argument details.
485
 
         *
486
 
         * @method _set
487
 
         * @protected
488
 
         * @chainable
489
 
         * 
490
 
         * @param {String} name The name of the attribute.
491
 
         * @param {Any} val The value to set the attribute to.
492
 
         * @return {Object} A reference to the host object.
493
 
         */
494
 
        _set : function(name, val) {
495
 
            return this._setAttr(name, val, null, true);
496
 
        },
497
 
 
498
 
        /**
499
 
         * Provides the common implementation for the public set and protected _set methods.
500
 
         *
501
 
         * See <a href="#method_set">set</a> for argument details.
502
 
         *
503
 
         * @method _setAttr
504
 
         * @protected
505
 
         * @chainable
506
 
         *
507
 
         * @param {String} name The name of the attribute.
508
 
         * @param {Any} value The value to set the attribute to.
509
 
         * @param {Object} opts (Optional) Optional event data to be mixed into
510
 
         * the event facade passed to subscribers of the attribute's change event.
511
 
         * This is currently a hack. There's no real need for the AttributeCore implementation
512
 
         * to support this parameter, but breaking it out into AttributeEvents, results in
513
 
         * additional function hops for the critical path. May change in 3.5.0 PR3.
514
 
         * @param {boolean} force If true, allows the caller to set values for 
515
 
         * readOnly or writeOnce attributes which have already been set.
516
 
         *
517
 
         * @return {Object} A reference to the host object.
518
 
         */
519
 
        _setAttr : function(name, val, opts, force)  {
520
 
            
521
 
            // HACK - no real reason core needs to know about opts, but 
522
 
            // it adds fn hops if we want to break it out. 
523
 
            // Not sure it's worth it for this critical path
524
 
            
525
 
            var allowSet = true,
526
 
                state = this._state,
527
 
                stateProxy = this._stateProxy,
528
 
                cfg,
529
 
                initialSet,
530
 
                strPath,
531
 
                path,
532
 
                currVal,
533
 
                writeOnce,
534
 
                initializing;
535
 
 
536
 
            if (name.indexOf(DOT) !== -1) {
537
 
                strPath = name;
538
 
                path = name.split(DOT);
539
 
                name = path.shift();
540
 
            }
541
 
 
542
 
            if (this._isLazyAttr(name)) {
543
 
                this._addLazyAttr(name);
544
 
            }
545
 
 
546
 
            cfg = state.getAll(name, true) || {}; 
547
 
 
548
 
            initialSet = (!(VALUE in cfg));
549
 
 
550
 
            if (stateProxy && name in stateProxy && !cfg._bypassProxy) {
551
 
                // TODO: Value is always set for proxy. Can we do any better? Maybe take a snapshot as the initial value for the first call to set? 
552
 
                initialSet = false;
553
 
            }
554
 
 
555
 
            writeOnce = cfg.writeOnce;
556
 
            initializing = cfg.initializing;
557
 
 
558
 
            if (!initialSet && !force) {
559
 
 
560
 
                if (writeOnce) {
561
 
                    allowSet = false;
562
 
                }
563
 
 
564
 
                if (cfg.readOnly) {
565
 
                    allowSet = false;
566
 
                }
567
 
            }
568
 
 
569
 
            if (!initializing && !force && writeOnce === INIT_ONLY) {
570
 
                allowSet = false;
571
 
            }
572
 
 
573
 
            if (allowSet) {
574
 
                // Don't need currVal if initialSet (might fail in custom getter if it always expects a non-undefined/non-null value)
575
 
                if (!initialSet) {
576
 
                    currVal =  this.get(name);
577
 
                }
578
 
 
579
 
                if (path) {
580
 
                   val = O.setValue(Y.clone(currVal), path, val);
581
 
 
582
 
                   if (val === undefined) {
583
 
                       allowSet = false;
584
 
                   }
585
 
                }
586
 
 
587
 
                if (allowSet) {
588
 
                    if (!this._fireAttrChange || initializing) {
589
 
                        this._setAttrVal(name, strPath, currVal, val);
590
 
                    } else {
591
 
                        // HACK - no real reason core needs to know about _fireAttrChange, but 
592
 
                        // it adds fn hops if we want to break it out. Not sure it's worth it for this critical path
593
 
                        this._fireAttrChange(name, strPath, currVal, val, opts);
594
 
                    }
595
 
                }
596
 
            }
597
 
 
598
 
            return this;
599
 
        },
600
 
 
601
 
        /**
602
 
         * Provides the common implementation for the public get method,
603
 
         * allowing Attribute hosts to over-ride either method.
604
 
         *
605
 
         * See <a href="#method_get">get</a> for argument details.
606
 
         *
607
 
         * @method _getAttr
608
 
         * @protected
609
 
         * @chainable
610
 
         *
611
 
         * @param {String} name The name of the attribute.
612
 
         * @return {Any} The value of the attribute.
613
 
         */
614
 
        _getAttr : function(name) {
615
 
            var host = this, // help compression
616
 
                fullName = name,
617
 
                state = host._state,
618
 
                path,
619
 
                getter,
620
 
                val,
621
 
                cfg;
622
 
 
623
 
            if (name.indexOf(DOT) !== -1) {
624
 
                path = name.split(DOT);
625
 
                name = path.shift();
626
 
            }
627
 
 
628
 
            // On Demand - Should be rare - handles out of order valueFn references
629
 
            if (host._tCfgs && host._tCfgs[name]) {
630
 
                cfg = {};
631
 
                cfg[name] = host._tCfgs[name];
632
 
                delete host._tCfgs[name];
633
 
                host._addAttrs(cfg, host._tVals);
634
 
            }
635
 
            
636
 
            // Lazy Init
637
 
            if (host._isLazyAttr(name)) {
638
 
                host._addLazyAttr(name);
639
 
            }
640
 
 
641
 
            val = host._getStateVal(name);
642
 
                        
643
 
            getter = state.get(name, GETTER);
644
 
 
645
 
            if (getter && !getter.call) {
646
 
                getter = this[getter];
647
 
            }
648
 
 
649
 
            val = (getter) ? getter.call(host, val, fullName) : val;
650
 
            val = (path) ? O.getValue(val, path) : val;
651
 
 
652
 
            return val;
653
 
        },
654
 
 
655
 
        /**
656
 
         * Gets the stored value for the attribute, from either the 
657
 
         * internal state object, or the state proxy if it exits
658
 
         * 
659
 
         * @method _getStateVal
660
 
         * @private
661
 
         * @param {String} name The name of the attribute
662
 
         * @return {Any} The stored value of the attribute
663
 
         */
664
 
        _getStateVal : function(name) {
665
 
            var stateProxy = this._stateProxy;
666
 
            return stateProxy && (name in stateProxy) && !this._state.get(name, BYPASS_PROXY) ? stateProxy[name] : this._state.get(name, VALUE);
667
 
        },
668
 
 
669
 
        /**
670
 
         * Sets the stored value for the attribute, in either the 
671
 
         * internal state object, or the state proxy if it exits
672
 
         *
673
 
         * @method _setStateVal
674
 
         * @private
675
 
         * @param {String} name The name of the attribute
676
 
         * @param {Any} value The value of the attribute
677
 
         */
678
 
        _setStateVal : function(name, value) {
679
 
            var stateProxy = this._stateProxy;
680
 
            if (stateProxy && (name in stateProxy) && !this._state.get(name, BYPASS_PROXY)) {
681
 
                stateProxy[name] = value;
682
 
            } else {
683
 
                this._state.add(name, VALUE, value);
684
 
            }
685
 
        },
686
 
 
687
 
        /**
688
 
         * Updates the stored value of the attribute in the privately held State object,
689
 
         * if validation and setter passes.
690
 
         *
691
 
         * @method _setAttrVal
692
 
         * @private
693
 
         * @param {String} attrName The attribute name.
694
 
         * @param {String} subAttrName The sub-attribute name, if setting a sub-attribute property ("x.y.z").
695
 
         * @param {Any} prevVal The currently stored value of the attribute.
696
 
         * @param {Any} newVal The value which is going to be stored.
697
 
         * 
698
 
         * @return {booolean} true if the new attribute value was stored, false if not.
699
 
         */
700
 
        _setAttrVal : function(attrName, subAttrName, prevVal, newVal) {
701
 
 
702
 
            var host = this,
703
 
                allowSet = true,
704
 
                cfg = this._state.getAll(attrName, true) || {},
705
 
                validator = cfg.validator,
706
 
                setter = cfg.setter,
707
 
                initializing = cfg.initializing,
708
 
                prevRawVal = this._getStateVal(attrName),
709
 
                name = subAttrName || attrName,
710
 
                retVal,
711
 
                valid;
712
 
 
713
 
            if (validator) {
714
 
                if (!validator.call) { 
715
 
                    // Assume string - trying to keep critical path tight, so avoiding Lang check
716
 
                    validator = this[validator];
717
 
                }
718
 
                if (validator) {
719
 
                    valid = validator.call(host, newVal, name);
720
 
 
721
 
                    if (!valid && initializing) {
722
 
                        newVal = cfg.defaultValue;
723
 
                        valid = true; // Assume it's valid, for perf.
724
 
                    }
725
 
                }
726
 
            }
727
 
 
728
 
            if (!validator || valid) {
729
 
                if (setter) {
730
 
                    if (!setter.call) {
731
 
                        // Assume string - trying to keep critical path tight, so avoiding Lang check
732
 
                        setter = this[setter];
733
 
                    }
734
 
                    if (setter) {
735
 
                        retVal = setter.call(host, newVal, name);
736
 
 
737
 
                        if (retVal === INVALID_VALUE) {
738
 
                            allowSet = false;
739
 
                        } else if (retVal !== undefined){
740
 
                            newVal = retVal;
741
 
                        }
742
 
                    }
743
 
                }
744
 
 
745
 
                if (allowSet) {
746
 
                    if(!subAttrName && (newVal === prevRawVal) && !Lang.isObject(newVal)) {
747
 
                        allowSet = false;
748
 
                    } else {
749
 
                        // Store value
750
 
                        if (!(INIT_VALUE in cfg)) {
751
 
                            cfg.initValue = newVal;
752
 
                        }
753
 
                        host._setStateVal(attrName, newVal);
754
 
                    }
755
 
                }
756
 
 
757
 
            } else {
758
 
                allowSet = false;
759
 
            }
760
 
 
761
 
            return allowSet;
762
 
        },
763
 
 
764
 
        /**
765
 
         * Sets multiple attribute values.
766
 
         *
767
 
         * @method setAttrs
768
 
         * @param {Object} attrs  An object with attributes name/value pairs.
769
 
         * @return {Object} A reference to the host object.
770
 
         * @chainable
771
 
         */
772
 
        setAttrs : function(attrs) {
773
 
            return this._setAttrs(attrs);
774
 
        },
775
 
 
776
 
        /**
777
 
         * Implementation behind the public setAttrs method, to set multiple attribute values.
778
 
         *
779
 
         * @method _setAttrs
780
 
         * @protected
781
 
         * @param {Object} attrs  An object with attributes name/value pairs.
782
 
         * @return {Object} A reference to the host object.
783
 
         * @chainable
784
 
         */
785
 
        _setAttrs : function(attrs) {
786
 
            for (var attr in attrs) {
787
 
                if ( attrs.hasOwnProperty(attr) ) {
788
 
                    this.set(attr, attrs[attr]);
789
 
                }
790
 
            }
791
 
            return this;
792
 
        },
793
 
 
794
 
        /**
795
 
         * Gets multiple attribute values.
796
 
         *
797
 
         * @method getAttrs
798
 
         * @param {Array | boolean} attrs Optional. An array of attribute names. If omitted, all attribute values are
799
 
         * returned. If set to true, all attributes modified from their initial values are returned.
800
 
         * @return {Object} An object with attribute name/value pairs.
801
 
         */
802
 
        getAttrs : function(attrs) {
803
 
            return this._getAttrs(attrs);
804
 
        },
805
 
 
806
 
        /**
807
 
         * Implementation behind the public getAttrs method, to get multiple attribute values.
808
 
         *
809
 
         * @method _getAttrs
810
 
         * @protected
811
 
         * @param {Array | boolean} attrs Optional. An array of attribute names. If omitted, all attribute values are
812
 
         * returned. If set to true, all attributes modified from their initial values are returned.
813
 
         * @return {Object} An object with attribute name/value pairs.
814
 
         */
815
 
        _getAttrs : function(attrs) {
816
 
            var host = this,
817
 
                o = {}, 
818
 
                i, l, attr, val,
819
 
                modifiedOnly = (attrs === true);
820
 
 
821
 
            // TODO - figure out how to get all "added"
822
 
            attrs = (attrs && !modifiedOnly) ? attrs : O.keys(host._state.data);
823
 
 
824
 
            for (i = 0, l = attrs.length; i < l; i++) {
825
 
                // Go through get, to honor cloning/normalization
826
 
                attr = attrs[i];
827
 
                val = host.get(attr);
828
 
 
829
 
                if (!modifiedOnly || host._getStateVal(attr) != host._state.get(attr, INIT_VALUE)) {
830
 
                    o[attr] = host.get(attr); 
831
 
                }
832
 
            }
833
 
 
834
 
            return o;
835
 
        },
836
 
 
837
 
        /**
838
 
         * Configures a group of attributes, and sets initial values.
839
 
         *
840
 
         * <p>
841
 
         * <strong>NOTE:</strong> This method does not isolate the configuration object by merging/cloning. 
842
 
         * The caller is responsible for merging/cloning the configuration object if required.
843
 
         * </p>
844
 
         *
845
 
         * @method addAttrs
846
 
         * @chainable
847
 
         *
848
 
         * @param {Object} cfgs An object with attribute name/configuration pairs.
849
 
         * @param {Object} values An object with attribute name/value pairs, defining the initial values to apply.
850
 
         * Values defined in the cfgs argument will be over-written by values in this argument unless defined as read only.
851
 
         * @param {boolean} lazy Whether or not to delay the intialization of these attributes until the first call to get/set.
852
 
         * Individual attributes can over-ride this behavior by defining a lazyAdd configuration property in their configuration.
853
 
         * See <a href="#method_addAttr">addAttr</a>.
854
 
         * 
855
 
         * @return {Object} A reference to the host object.
856
 
         */
857
 
        addAttrs : function(cfgs, values, lazy) {
858
 
            var host = this; // help compression
859
 
            if (cfgs) {
860
 
                host._tCfgs = cfgs;
861
 
                host._tVals = host._normAttrVals(values);
862
 
                host._addAttrs(cfgs, host._tVals, lazy);
863
 
                host._tCfgs = host._tVals = null;
864
 
            }
865
 
 
866
 
            return host;
867
 
        },
868
 
 
869
 
        /**
870
 
         * Implementation behind the public addAttrs method. 
871
 
         * 
872
 
         * This method is invoked directly by get if it encounters a scenario 
873
 
         * in which an attribute's valueFn attempts to obtain the 
874
 
         * value an attribute in the same group of attributes, which has not yet 
875
 
         * been added (on demand initialization).
876
 
         *
877
 
         * @method _addAttrs
878
 
         * @private
879
 
         * @param {Object} cfgs An object with attribute name/configuration pairs.
880
 
         * @param {Object} values An object with attribute name/value pairs, defining the initial values to apply.
881
 
         * Values defined in the cfgs argument will be over-written by values in this argument unless defined as read only.
882
 
         * @param {boolean} lazy Whether or not to delay the intialization of these attributes until the first call to get/set.
883
 
         * Individual attributes can over-ride this behavior by defining a lazyAdd configuration property in their configuration.
884
 
         * See <a href="#method_addAttr">addAttr</a>.
885
 
         */
886
 
        _addAttrs : function(cfgs, values, lazy) {
887
 
            var host = this, // help compression
888
 
                attr,
889
 
                attrCfg,
890
 
                value;
891
 
 
892
 
            for (attr in cfgs) {
893
 
                if (cfgs.hasOwnProperty(attr)) {
894
 
 
895
 
                    // Not Merging. Caller is responsible for isolating configs
896
 
                    attrCfg = cfgs[attr];
897
 
                    attrCfg.defaultValue = attrCfg.value;
898
 
 
899
 
                    // Handle simple, complex and user values, accounting for read-only
900
 
                    value = host._getAttrInitVal(attr, attrCfg, host._tVals);
901
 
 
902
 
                    if (value !== undefined) {
903
 
                        attrCfg.value = value;
904
 
                    }
905
 
 
906
 
                    if (host._tCfgs[attr]) {
907
 
                        delete host._tCfgs[attr];
908
 
                    }
909
 
 
910
 
                    host.addAttr(attr, attrCfg, lazy);
911
 
                }
912
 
            }
913
 
        },
914
 
 
915
 
        /**
916
 
         * Utility method to protect an attribute configuration
917
 
         * hash, by merging the entire object and the individual 
918
 
         * attr config objects. 
919
 
         *
920
 
         * @method _protectAttrs
921
 
         * @protected
922
 
         * @param {Object} attrs A hash of attribute to configuration object pairs.
923
 
         * @return {Object} A protected version of the attrs argument.
924
 
         */
925
 
        _protectAttrs : function(attrs) {
926
 
            if (attrs) {
927
 
                attrs = Y.merge(attrs);
928
 
                for (var attr in attrs) {
929
 
                    if (attrs.hasOwnProperty(attr)) {
930
 
                        attrs[attr] = Y.merge(attrs[attr]);
931
 
                    }
932
 
                }
933
 
            }
934
 
            return attrs;
935
 
        },
936
 
 
937
 
        /**
938
 
         * Utility method to normalize attribute values. The base implementation 
939
 
         * simply merges the hash to protect the original.
940
 
         *
941
 
         * @method _normAttrVals
942
 
         * @param {Object} valueHash An object with attribute name/value pairs
943
 
         *
944
 
         * @return {Object}
945
 
         *
946
 
         * @private
947
 
         */
948
 
        _normAttrVals : function(valueHash) {
949
 
            return (valueHash) ? Y.merge(valueHash) : null;
950
 
        },
951
 
 
952
 
        /**
953
 
         * Returns the initial value of the given attribute from
954
 
         * either the default configuration provided, or the 
955
 
         * over-ridden value if it exists in the set of initValues 
956
 
         * provided and the attribute is not read-only.
957
 
         *
958
 
         * @param {String} attr The name of the attribute
959
 
         * @param {Object} cfg The attribute configuration object
960
 
         * @param {Object} initValues The object with simple and complex attribute name/value pairs returned from _normAttrVals
961
 
         *
962
 
         * @return {Any} The initial value of the attribute.
963
 
         *
964
 
         * @method _getAttrInitVal
965
 
         * @private
966
 
         */
967
 
        _getAttrInitVal : function(attr, cfg, initValues) {
968
 
            var val, valFn;
969
 
            // init value is provided by the user if it exists, else, provided by the config
970
 
            if (!cfg.readOnly && initValues && initValues.hasOwnProperty(attr)) {
971
 
                val = initValues[attr];
972
 
            } else {
973
 
                val = cfg.value;
974
 
                valFn = cfg.valueFn;
975
 
 
976
 
                if (valFn) {
977
 
                    if (!valFn.call) {
978
 
                        valFn = this[valFn];
979
 
                    }
980
 
                    if (valFn) {
981
 
                        val = valFn.call(this, attr);
982
 
                    }
983
 
                }
984
 
            }
985
 
 
986
 
 
987
 
            return val;
988
 
        },
989
 
 
990
 
        /**
991
 
         * Utility method to set up initial attributes defined during construction, either through the constructor.ATTRS property, or explicitly passed in.
992
 
         * 
993
 
         * @method _initAttrs
994
 
         * @protected
995
 
         * @param attrs {Object} The attributes to add during construction (passed through to <a href="#method_addAttrs">addAttrs</a>). These can also be defined on the constructor being augmented with Attribute by defining the ATTRS property on the constructor.
996
 
         * @param values {Object} The initial attribute values to apply (passed through to <a href="#method_addAttrs">addAttrs</a>). These are not merged/cloned. The caller is responsible for isolating user provided values if required.
997
 
         * @param lazy {boolean} Whether or not to add attributes lazily (passed through to <a href="#method_addAttrs">addAttrs</a>).
998
 
         */
999
 
        _initAttrs : function(attrs, values, lazy) {
1000
 
            // ATTRS support for Node, which is not Base based
1001
 
            attrs = attrs || this.constructor.ATTRS;
1002
 
    
1003
 
            var Base = Y.Base,
1004
 
                BaseCore = Y.BaseCore,
1005
 
                baseInst = (Base && Y.instanceOf(this, Base)),
1006
 
                baseCoreInst = (!baseInst && BaseCore && Y.instanceOf(this, BaseCore));
1007
 
 
1008
 
            if ( attrs && !baseInst && !baseCoreInst) {
1009
 
                this.addAttrs(this._protectAttrs(attrs), values, lazy);
1010
 
            }
1011
 
        }
1012
 
    };
1013
 
 
1014
 
    Y.AttributeCore = AttributeCore;
1015
 
 
1016
 
 
1017
 
}, '3.5.0' );