]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/widget/widget-htmlparser.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / widget / widget-htmlparser.js
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.3.0
6 build: 3167
7 */
8 YUI.add('widget-htmlparser', function(Y) {
9
10 /**
11  * Adds HTML Parser support to the base Widget class
12  *
13  * @module widget
14  * @submodule widget-htmlparser
15  * @for Widget
16  */
17
18
19 var Widget = Y.Widget,
20     Node = Y.Node,
21     Lang = Y.Lang,
22
23     SRC_NODE = "srcNode",
24     CONTENT_BOX = "contentBox";
25
26 /**
27  * Object hash, defining how attribute values are to be parsed from
28  * markup contained in the widget's content box. e.g.:
29  * <pre>
30  *   {
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");
41  *       }
42  *   }
43  * </pre>
44  * 
45  * @property Widget.HTML_PARSER
46  * @type Object
47  * @static
48  */
49 Widget.HTML_PARSER = {};
50
51 /**
52  * The build configuration for the Widget class.
53  * <p>
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.
57  * </p>
58  * @property _buildCfg
59  * @type Object
60  * @static
61  * @final
62  * @private
63  */
64 Widget._buildCfg = {
65     aggregates : ["HTML_PARSER"]
66 };
67
68 /**
69  * The DOM node to parse for configuration values, passed to the Widget's HTML_PARSER definition
70  *
71  * @attribute srcNode
72  * @type String | Node
73  * @writeOnce
74  */
75 Widget.ATTRS[SRC_NODE] = {
76     value: null,
77     setter: Node.one,
78     getter: "_getSrcNode",
79     writeOnce: true
80 };
81
82 Y.mix(Widget.prototype, {
83
84     /**
85      * @method _getSrcNode
86      * @protected
87      * @return {Node} The Node to apply HTML_PARSER to
88      */
89     _getSrcNode : function(val) {
90         return val || this.get(CONTENT_BOX);
91     },
92
93     /**
94      * @method _applyParsedConfig
95      * @protected
96      * @return {Object} The merged configuration literal
97      */
98     _applyParsedConfig : function(node, cfg, parsedCfg) {
99         return (parsedCfg) ? Y.mix(cfg, parsedCfg, false) : cfg;
100     },
101
102     /**
103      * Utilitity method used to apply the <code>HTML_PARSER</code> configuration for the 
104      * instance, to retrieve config data values.
105      *
106      * @method _applyParser
107      * @protected
108      * @param config {Object} User configuration object (will be populated with values from Node) 
109      */
110     _applyParser : function(config) {
111
112         var widget = this,
113             srcNode = widget.get(SRC_NODE),
114             schema = widget._getHtmlParser(),
115             parsedConfig,
116             val;
117
118         if (schema && srcNode) {
119             Y.Object.each(schema, function(v, k, o) {
120                 val = null;
121
122                 if (Lang.isFunction(v)) {
123                     val = v.call(widget, srcNode);
124                 } else {
125                     if (Lang.isArray(v)) {
126                         val = srcNode.all(v[0]);
127                         if (val.isEmpty()) {
128                             val = null;
129                         }
130                     } else {
131                         val = srcNode.one(v);
132                     }
133                 }
134
135                 if (val !== null && val !== undefined) {
136                     parsedConfig = parsedConfig || {};
137                     parsedConfig[k] = val;
138                 }
139             });
140         }
141         config = widget._applyParsedConfig(srcNode, config, parsedConfig);
142     },
143
144     /**
145      * Gets the HTML_PARSER definition for this instance, by merging HTML_PARSER
146      * definitions across the class hierarchy.
147      *
148      * @private
149      * @method _getHtmlParser
150      * @return {Object} HTML_PARSER definition for this instance
151      */
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(),
156             parser = {},
157             i, p;
158
159         for (i = classes.length - 1; i >= 0; i--) {
160             p = classes[i].HTML_PARSER;
161             if (p) {
162                 Y.mix(parser, p, true);
163             }
164         }
165         return parser;
166     }
167 });
168
169
170 }, '3.3.0' ,{requires:['widget-base']});