3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('widget-htmlparser', function(Y) {
10
* Adds HTML Parser support to the base Widget class
13
* @submodule widget-htmlparser
18
var Widget = Y.Widget,
23
CONTENT_BOX = "contentBox";
26
* Object hash, defining how attribute values are to be parsed from
27
* markup contained in the widget's content box. e.g.:
30
* // Set single Node references using selector syntax
31
* // (selector is run through node.one)
32
* titleNode: "span.yui-title",
33
* // Set NodeList references using selector syntax
34
* // (array indicates selector is to be run through node.all)
35
* listNodes: ["li.yui-item"],
36
* // Set other attribute types, using a parse function.
37
* // Context is set to the widget instance.
38
* label: function(contentBox) {
39
* return contentBox.one("span.title").get("innerHTML");
44
* @property HTML_PARSER
48
Widget.HTML_PARSER = {};
51
* The build configuration for the Widget class.
53
* Defines the static fields which need to be aggregated,
54
* when this class is used as the main class passed to
55
* the <a href="Base.html#method_build">Base.build</a> method.
64
aggregates : ["HTML_PARSER"]
68
* The DOM node to parse for configuration values, passed to the Widget's HTML_PARSER definition
74
Widget.ATTRS[SRC_NODE] = {
77
getter: "_getSrcNode",
81
Y.mix(Widget.prototype, {
86
* @return {Node} The Node to apply HTML_PARSER to
88
_getSrcNode : function(val) {
89
return val || this.get(CONTENT_BOX);
93
* @method _applyParsedConfig
95
* @return {Object} The merged configuration literal
97
_applyParsedConfig : function(node, cfg, parsedCfg) {
98
return (parsedCfg) ? Y.mix(cfg, parsedCfg, false) : cfg;
102
* Utilitity method used to apply the <code>HTML_PARSER</code> configuration for the
103
* instance, to retrieve config data values.
105
* @method _applyParser
107
* @param config {Object} User configuration object (will be populated with values from Node)
109
_applyParser : function(config) {
112
srcNode = widget.get(SRC_NODE),
113
schema = widget._getHtmlParser(),
117
if (schema && srcNode) {
118
Y.Object.each(schema, function(v, k, o) {
121
if (Lang.isFunction(v)) {
122
val = v.call(widget, srcNode);
124
if (Lang.isArray(v)) {
125
val = srcNode.all(v[0]);
130
val = srcNode.one(v);
134
if (val !== null && val !== undefined) {
135
parsedConfig = parsedConfig || {};
136
parsedConfig[k] = val;
140
config = widget._applyParsedConfig(srcNode, config, parsedConfig);
144
* Gets the HTML_PARSER definition for this instance, by merging HTML_PARSER
145
* definitions across the class hierarchy.
148
* @method _getHtmlParser
149
* @return {Object} HTML_PARSER definition for this instance
151
_getHtmlParser : function() {
152
// Removed caching for kweight. This is a private method
153
// and only called once so don't need to cache HTML_PARSER
154
var classes = this._getClasses(),
158
for (i = classes.length - 1; i >= 0; i--) {
159
p = classes[i].HTML_PARSER;
161
Y.mix(parser, p, true);
169
}, '3.4.1' ,{requires:['widget-base']});