]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/attribute/attribute-complex.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / attribute / attribute-complex.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
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.value,
85                 valFn = cfg.valueFn,
86                 simple,
87                 complex,
88                 i,
89                 l,
90                 path,
91                 subval,
92                 subvals;
93
94             if (valFn) {
95                 if (!valFn.call) {
96                     valFn = this[valFn];
97                 }
98                 if (valFn) {
99                     val = valFn.call(this);
100                 }
101             }
102
103             if (!cfg.readOnly && initValues) {
104
105                 // Simple Attributes
106                 simple = initValues.simple;
107                 if (simple && simple.hasOwnProperty(attr)) {
108                     val = simple[attr];
109                 }
110
111                 // Complex Attributes (complex values applied, after simple, incase both are set)
112                 complex = initValues.complex;
113                 if (complex && complex.hasOwnProperty(attr)) {
114                     subvals = complex[attr];
115                     for (i = 0, l = subvals.length; i < l; ++i) {
116                         path = subvals[i].path;
117                         subval = subvals[i].value;
118                         O.setValue(val, path, subval);
119                     }
120                 }
121             }
122
123             return val;
124         }
125     };
126
127     Y.mix(Y.Attribute, Y.Attribute.Complex, true, null, 1);
128
129
130 }, '3.3.0' ,{requires:['attribute-base']});