API Docs for: 3.10.3
Show:

File: attribute/js/AttributeExtras.js

  1. /**
  2. * The attribute module provides an augmentable Attribute implementation, which
  3. * adds configurable attributes and attribute change events to the class being
  4. * augmented. It also provides a State class, which is used internally by Attribute,
  5. * but can also be used independently to provide a name/property/value data structure to
  6. * store state.
  7. *
  8. * @module attribute
  9. */
  10.  
  11. /**
  12. * The attribute-extras submodule provides less commonly used attribute methods, and can
  13. * be augmented/mixed into an implemention which used attribute-core.
  14. *
  15. * @module attribute
  16. * @submodule attribute-extras
  17. */
  18. var BROADCAST = "broadcast",
  19. PUBLISHED = "published",
  20. INIT_VALUE = "initValue",
  21.  
  22. MODIFIABLE = {
  23. readOnly:1,
  24. writeOnce:1,
  25. getter:1,
  26. broadcast:1
  27. };
  28.  
  29. /**
  30. * A augmentable implementation for AttributeCore, providing less frequently used
  31. * methods for Attribute management such as modifyAttrs(), removeAttr and reset()
  32. *
  33. * @class AttributeExtras
  34. * @extensionfor AttributeCore
  35. */
  36. function AttributeExtras() {}
  37.  
  38. AttributeExtras.prototype = {
  39.  
  40. /**
  41. * Updates the configuration of an attribute which has already been added.
  42. * <p>
  43. * The properties which can be modified through this interface are limited
  44. * to the following subset of attributes, which can be safely modified
  45. * after a value has already been set on the attribute: readOnly, writeOnce,
  46. * broadcast and getter.
  47. * </p>
  48. * @method modifyAttr
  49. * @param {String} name The name of the attribute whose configuration is to be updated.
  50. * @param {Object} config An object with configuration property/value pairs, specifying the configuration properties to modify.
  51. */
  52. modifyAttr: function(name, config) {
  53. var host = this, // help compression
  54. prop, state;
  55.  
  56. if (host.attrAdded(name)) {
  57.  
  58. if (host._isLazyAttr(name)) {
  59. host._addLazyAttr(name);
  60. }
  61.  
  62. state = host._state;
  63. for (prop in config) {
  64. if (MODIFIABLE[prop] && config.hasOwnProperty(prop)) {
  65. state.add(name, prop, config[prop]);
  66.  
  67. // If we reconfigured broadcast, need to republish
  68. if (prop === BROADCAST) {
  69. state.remove(name, PUBLISHED);
  70. }
  71. }
  72. }
  73. }
  74. /*jshint maxlen:200*/
  75. if (!host.attrAdded(name)) {Y.log('Attribute modifyAttr:' + name + ' has not been added. Use addAttr to add the attribute', 'warn', 'attribute');}
  76. /*jshint maxlen:150 */
  77. },
  78.  
  79. /**
  80. * Removes an attribute from the host object
  81. *
  82. * @method removeAttr
  83. * @param {String} name The name of the attribute to be removed.
  84. */
  85. removeAttr: function(name) {
  86. this._state.removeAll(name);
  87. },
  88.  
  89. /**
  90. * Resets the attribute (or all attributes) to its initial value, as long as
  91. * the attribute is not readOnly, or writeOnce.
  92. *
  93. * @method reset
  94. * @param {String} name Optional. The name of the attribute to reset. If omitted, all attributes are reset.
  95. * @return {Object} A reference to the host object.
  96. * @chainable
  97. */
  98. reset : function(name) {
  99. var host = this; // help compression
  100.  
  101. if (name) {
  102. if (host._isLazyAttr(name)) {
  103. host._addLazyAttr(name);
  104. }
  105. host.set(name, host._state.get(name, INIT_VALUE));
  106. } else {
  107. Y.each(host._state.data, function(v, n) {
  108. host.reset(n);
  109. });
  110. }
  111. return host;
  112. },
  113.  
  114. /**
  115. * Returns an object with the configuration properties (and value)
  116. * for the given attribute. If attrName is not provided, returns the
  117. * configuration properties for all attributes.
  118. *
  119. * @method _getAttrCfg
  120. * @protected
  121. * @param {String} name Optional. The attribute name. If not provided, the method will return the configuration for all attributes.
  122. * @return {Object} The configuration properties for the given attribute, or all attributes.
  123. */
  124. _getAttrCfg : function(name) {
  125. var o,
  126. state = this._state;
  127.  
  128. if (name) {
  129. o = state.getAll(name) || {};
  130. } else {
  131. o = {};
  132. Y.each(state.data, function(v, n) {
  133. o[n] = state.getAll(n);
  134. });
  135. }
  136.  
  137. return o;
  138. }
  139. };
  140.  
  141. Y.AttributeExtras = AttributeExtras;
  142.