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.

widget-htmlparser.js 4.4KB

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