2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('widget-htmlparser', function(Y) {
11 * Adds HTML Parser support to the base Widget class
14 * @submodule widget-htmlparser
19 var Widget = Y.Widget,
24 CONTENT_BOX = "contentBox";
27 * Object hash, defining how attribute values are to be parsed from
28 * markup contained in the widget's content box. e.g.:
31 * // Set single Node references using selector syntax
32 * // (selector is run through node.one)
33 * titleNode: "span.yui-title",
34 * // Set NodeList references using selector syntax
35 * // (array indicates selector is to be run through node.all)
36 * listNodes: ["li.yui-item"],
37 * // Set other attribute types, using a parse function.
38 * // Context is set to the widget instance.
39 * label: function(contentBox) {
40 * return contentBox.one("span.title").get("innerHTML");
45 * @property Widget.HTML_PARSER
49 Widget.HTML_PARSER = {};
52 * The build configuration for the Widget class.
54 * Defines the static fields which need to be aggregated,
55 * when this class is used as the main class passed to
56 * the <a href="Base.html#method_build">Base.build</a> method.
65 aggregates : ["HTML_PARSER"]
69 * The DOM node to parse for configuration values, passed to the Widget's HTML_PARSER definition
75 Widget.ATTRS[SRC_NODE] = {
78 getter: "_getSrcNode",
82 Y.mix(Widget.prototype, {
87 * @return {Node} The Node to apply HTML_PARSER to
89 _getSrcNode : function(val) {
90 return val || this.get(CONTENT_BOX);
94 * @method _applyParsedConfig
96 * @return {Object} The merged configuration literal
98 _applyParsedConfig : function(node, cfg, parsedCfg) {
99 return (parsedCfg) ? Y.mix(cfg, parsedCfg, false) : cfg;
103 * Utilitity method used to apply the <code>HTML_PARSER</code> configuration for the
104 * instance, to retrieve config data values.
106 * @method _applyParser
108 * @param config {Object} User configuration object (will be populated with values from Node)
110 _applyParser : function(config) {
113 srcNode = widget.get(SRC_NODE),
114 schema = widget._getHtmlParser(),
118 if (schema && srcNode) {
119 Y.Object.each(schema, function(v, k, o) {
122 if (Lang.isFunction(v)) {
123 val = v.call(widget, srcNode);
125 if (Lang.isArray(v)) {
126 val = srcNode.all(v[0]);
131 val = srcNode.one(v);
135 if (val !== null && val !== undefined) {
136 parsedConfig = parsedConfig || {};
137 parsedConfig[k] = val;
141 config = widget._applyParsedConfig(srcNode, config, parsedConfig);
145 * Gets the HTML_PARSER definition for this instance, by merging HTML_PARSER
146 * definitions across the class hierarchy.
149 * @method _getHtmlParser
150 * @return {Object} HTML_PARSER definition for this instance
152 _getHtmlParser : function() {
153 // Removed caching for kweight. This is a private method
154 // and only called once so don't need to cache HTML_PARSER
155 var classes = this._getClasses(),
159 for (i = classes.length - 1; i >= 0; i--) {
160 p = classes[i].HTML_PARSER;
162 Y.mix(parser, p, true);
170 }, '3.3.0' ,{requires:['widget-base']});