Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

attribute-complex.js 4.2KB

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