API Docs for: 3.10.3
Show:

File: widget/js/WidgetHTMLParser.js

  1. /**
  2. * Adds HTML Parser support to the base Widget class
  3. *
  4. * @module widget
  5. * @submodule widget-htmlparser
  6. * @for Widget
  7. */
  8.  
  9. var Widget = Y.Widget,
  10. Node = Y.Node,
  11. Lang = Y.Lang,
  12.  
  13. SRC_NODE = "srcNode",
  14. CONTENT_BOX = "contentBox";
  15.  
  16. /**
  17. * Object hash, defining how attribute values are to be parsed from
  18. * markup contained in the widget's content box. e.g.:
  19. * <pre>
  20. * {
  21. * // Set single Node references using selector syntax
  22. * // (selector is run through node.one)
  23. * titleNode: "span.yui-title",
  24. * // Set NodeList references using selector syntax
  25. * // (array indicates selector is to be run through node.all)
  26. * listNodes: ["li.yui-item"],
  27. * // Set other attribute types, using a parse function.
  28. * // Context is set to the widget instance.
  29. * label: function(contentBox) {
  30. * return contentBox.one("span.title").get("innerHTML");
  31. * }
  32. * }
  33. * </pre>
  34. *
  35. * @property HTML_PARSER
  36. * @type Object
  37. * @static
  38. */
  39. Widget.HTML_PARSER = {};
  40.  
  41. /**
  42. * The build configuration for the Widget class.
  43. * <p>
  44. * Defines the static fields which need to be aggregated,
  45. * when this class is used as the main class passed to
  46. * the <a href="Base.html#method_build">Base.build</a> method.
  47. * </p>
  48. * @property _buildCfg
  49. * @type Object
  50. * @static
  51. * @final
  52. * @private
  53. */
  54. Widget._buildCfg = {
  55. aggregates : ["HTML_PARSER"]
  56. };
  57.  
  58. /**
  59. * The DOM node to parse for configuration values, passed to the Widget's HTML_PARSER definition
  60. *
  61. * @attribute srcNode
  62. * @type String | Node
  63. * @writeOnce
  64. */
  65. Widget.ATTRS[SRC_NODE] = {
  66. value: null,
  67. setter: Node.one,
  68. getter: "_getSrcNode",
  69. writeOnce: true
  70. };
  71.  
  72. Y.mix(Widget.prototype, {
  73.  
  74. /**
  75. * @method _getSrcNode
  76. * @protected
  77. * @return {Node} The Node to apply HTML_PARSER to
  78. */
  79. _getSrcNode : function(val) {
  80. return val || this.get(CONTENT_BOX);
  81. },
  82.  
  83. /**
  84. * @method _applyParsedConfig
  85. * @protected
  86. * @return {Object} The merged configuration literal
  87. */
  88. _applyParsedConfig : function(node, cfg, parsedCfg) {
  89. return (parsedCfg) ? Y.mix(cfg, parsedCfg, false) : cfg;
  90. },
  91.  
  92. /**
  93. * Utility method used to apply the <code>HTML_PARSER</code> configuration for the
  94. * instance, to retrieve config data values.
  95. *
  96. * @method _applyParser
  97. * @protected
  98. * @param config {Object} User configuration object (will be populated with values from Node)
  99. */
  100. _applyParser : function(config) {
  101.  
  102. var widget = this,
  103. srcNode = this._getNodeToParse(),
  104. schema = widget._getHtmlParser(),
  105. parsedConfig,
  106. val;
  107.  
  108. if (schema && srcNode) {
  109. Y.Object.each(schema, function(v, k, o) {
  110. val = null;
  111.  
  112. if (Lang.isFunction(v)) {
  113. val = v.call(widget, srcNode);
  114. } else {
  115. if (Lang.isArray(v)) {
  116. val = srcNode.all(v[0]);
  117. if (val.isEmpty()) {
  118. val = null;
  119. }
  120. } else {
  121. val = srcNode.one(v);
  122. }
  123. }
  124.  
  125. if (val !== null && val !== undefined) {
  126. parsedConfig = parsedConfig || {};
  127. parsedConfig[k] = val;
  128. }
  129. });
  130. }
  131. config = widget._applyParsedConfig(srcNode, config, parsedConfig);
  132. },
  133.  
  134. /**
  135. * Determines whether we have a node reference which we should try and parse.
  136. *
  137. * The current implementation does not parse nodes generated from CONTENT_TEMPLATE,
  138. * only explicitly set srcNode, or contentBox attributes.
  139. *
  140. * @method _getNodeToParse
  141. * @return {Node} The node reference to apply HTML_PARSER to.
  142. * @private
  143. */
  144. _getNodeToParse : function() {
  145. var srcNode = this.get("srcNode");
  146. return (!this._cbFromTemplate) ? srcNode : null;
  147. },
  148.  
  149. /**
  150. * Gets the HTML_PARSER definition for this instance, by merging HTML_PARSER
  151. * definitions across the class hierarchy.
  152. *
  153. * @private
  154. * @method _getHtmlParser
  155. * @return {Object} HTML_PARSER definition for this instance
  156. */
  157. _getHtmlParser : function() {
  158. // Removed caching for kweight. This is a private method
  159. // and only called once so don't need to cache HTML_PARSER
  160. var classes = this._getClasses(),
  161. parser = {},
  162. i, p;
  163.  
  164. for (i = classes.length - 1; i >= 0; i--) {
  165. p = classes[i].HTML_PARSER;
  166. if (p) {
  167. Y.mix(parser, p, true);
  168. }
  169. }
  170. return parser;
  171. }
  172. });
  173.