~launchpad-pqm/lazr-js/toolchain

« back to all changes in this revision

Viewing changes to src-js/lazrjs/yui/widget/widget-htmlparser-debug.js

  • Committer: Sidnei da Silva
  • Date: 2009-11-16 00:51:29 UTC
  • mto: This revision was merged to the branch mainline in revision 154.
  • Revision ID: sidnei.da.silva@canonical.com-20091116005129-8ibwjlboa38glaw5
- Improved generation of skin modules and revamped combo service to make it more twisty.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.2.0
6
 
build: 2676
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
 
                    } else {
128
 
                        val = srcNode.one(v);
129
 
                    }
130
 
                }
131
 
 
132
 
                if (val !== null && val !== undefined) {
133
 
                    parsedConfig = parsedConfig || {};
134
 
                    parsedConfig[k] = val;
135
 
                }
136
 
            });
137
 
        }
138
 
        config = widget._applyParsedConfig(srcNode, config, parsedConfig);
139
 
    },
140
 
 
141
 
    /**
142
 
     * Gets the HTML_PARSER definition for this instance, by merging HTML_PARSER
143
 
     * definitions across the class hierarchy.
144
 
     *
145
 
     * @private
146
 
     * @method _getHtmlParser
147
 
     * @return {Object} HTML_PARSER definition for this instance
148
 
     */
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(),
153
 
            parser = {},
154
 
            i, p;
155
 
 
156
 
        for (i = classes.length - 1; i >= 0; i--) {
157
 
            p = classes[i].HTML_PARSER;
158
 
            if (p) {
159
 
                Y.mix(parser, p, true);
160
 
            }
161
 
        }
162
 
        return parser;
163
 
    }
164
 
});
165
 
 
166
 
 
167
 
}, '3.2.0' ,{requires:['widget-base']});