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]);
128
val = srcNode.one(v);
132
if (val !== null && val !== undefined) {
133
parsedConfig = parsedConfig || {};
134
parsedConfig[k] = val;
138
config = widget._applyParsedConfig(srcNode, config, parsedConfig);
142
* Gets the HTML_PARSER definition for this instance, by merging HTML_PARSER
143
* definitions across the class hierarchy.
146
* @method _getHtmlParser
147
* @return {Object} HTML_PARSER definition for this instance
149
_getHtmlParser : function() {
150
// Removed caching for kweight. This is a private method
151
// and only called once so don't need to cache HTML_PARSER
152
var classes = this._getClasses(),
156
for (i = classes.length - 1; i >= 0; i--) {
157
p = classes[i].HTML_PARSER;
159
Y.mix(parser, p, true);
167
}, '3.2.0' ,{requires:['widget-base']});