|
1 /* |
|
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
3 Code licensed under the BSD License: |
|
4 http://developer.yahoo.net/yui/license.txt |
|
5 version: 3.0.0 |
|
6 build: 1549 |
|
7 */ |
|
8 YUI.add('attribute-complex', function(Y) { |
|
9 |
|
10 /** |
|
11 * Adds support for attribute providers to handle complex attributes in the constructor |
|
12 * |
|
13 * @module attribute |
|
14 * @submodule attribute-complex |
|
15 * @for Attribute |
|
16 */ |
|
17 |
|
18 var O = Y.Object, |
|
19 DOT = "."; |
|
20 |
|
21 Y.Attribute.Complex = function() {}; |
|
22 Y.Attribute.Complex.prototype = { |
|
23 |
|
24 /** |
|
25 * Utility method to split out simple attribute name/value pairs ("x") |
|
26 * from complex attribute name/value pairs ("x.y.z"), so that complex |
|
27 * attributes can be keyed by the top level attribute name. |
|
28 * |
|
29 * @method _normAttrVals |
|
30 * @param {Object} valueHash An object with attribute name/value pairs |
|
31 * |
|
32 * @return {Object} An object literal with 2 properties - "simple" and "complex", |
|
33 * containing simple and complex attribute values respectively keyed |
|
34 * by the top level attribute name, or null, if valueHash is falsey. |
|
35 * |
|
36 * @private |
|
37 */ |
|
38 _normAttrVals : function(valueHash) { |
|
39 var vals = {}, |
|
40 subvals = {}, |
|
41 path, |
|
42 attr, |
|
43 v, k; |
|
44 |
|
45 if (valueHash) { |
|
46 for (k in valueHash) { |
|
47 if (valueHash.hasOwnProperty(k)) { |
|
48 if (k.indexOf(DOT) !== -1) { |
|
49 path = k.split(DOT); |
|
50 attr = path.shift(); |
|
51 v = subvals[attr] = subvals[attr] || []; |
|
52 v[v.length] = { |
|
53 path : path, |
|
54 value: valueHash[k] |
|
55 }; |
|
56 } else { |
|
57 vals[k] = valueHash[k]; |
|
58 } |
|
59 } |
|
60 } |
|
61 return { simple:vals, complex:subvals }; |
|
62 } else { |
|
63 return null; |
|
64 } |
|
65 }, |
|
66 |
|
67 /** |
|
68 * Returns the initial value of the given attribute from |
|
69 * either the default configuration provided, or the |
|
70 * over-ridden value if it exists in the set of initValues |
|
71 * provided and the attribute is not read-only. |
|
72 * |
|
73 * @param {String} attr The name of the attribute |
|
74 * @param {Object} cfg The attribute configuration object |
|
75 * @param {Object} initValues The object with simple and complex attribute name/value pairs returned from _normAttrVals |
|
76 * |
|
77 * @return {Any} The initial value of the attribute. |
|
78 * |
|
79 * @method _getAttrInitVal |
|
80 * @private |
|
81 */ |
|
82 _getAttrInitVal : function(attr, cfg, initValues) { |
|
83 |
|
84 var val = (cfg.valueFn) ? cfg.valueFn.call(this) : cfg.value, |
|
85 simple, |
|
86 complex, |
|
87 i, |
|
88 l, |
|
89 path, |
|
90 subval, |
|
91 subvals; |
|
92 |
|
93 if (!cfg.readOnly && initValues) { |
|
94 |
|
95 // Simple Attributes |
|
96 simple = initValues.simple; |
|
97 if (simple && simple.hasOwnProperty(attr)) { |
|
98 val = simple[attr]; |
|
99 } |
|
100 |
|
101 // Complex Attributes (complex values applied, after simple, incase both are set) |
|
102 complex = initValues.complex; |
|
103 if (complex && complex.hasOwnProperty(attr)) { |
|
104 subvals = complex[attr]; |
|
105 for (i = 0, l = subvals.length; i < l; ++i) { |
|
106 path = subvals[i].path; |
|
107 subval = subvals[i].value; |
|
108 O.setValue(val, path, subval); |
|
109 } |
|
110 } |
|
111 } |
|
112 |
|
113 return val; |
|
114 } |
|
115 }; |
|
116 |
|
117 Y.mix(Y.Attribute, Y.Attribute.Complex, true, null, 1); |
|
118 |
|
119 |
|
120 }, '3.0.0' ,{requires:['attribute-base']}); |