source: Dev/trunk/src/client/dojox/mobile/parser.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 5.1 KB
Line 
1define([
2        "dojo/_base/kernel",
3        "dojo/_base/array",
4        "dojo/_base/config",
5        "dojo/_base/lang",
6        "dojo/_base/window",
7        "dojo/ready"
8], function(dojo, array, config, lang, win, ready){
9
10        // module:
11        //              dojox/mobile/parser
12
13        var dm = lang.getObject("dojox.mobile", true);
14
15        var Parser = function(){
16                // summary:
17                //              A lightweight parser.
18                // description:
19                //              dojox/mobile/parser is an extremely small subset of dojo/parser.
20                //              It has no additional features over dojo/parser, so there is no
21                //              benefit in terms of features by using dojox/mobile/parser instead
22                //              of dojo/parser. However, if dojox/mobile/parser's capabilities are
23                //              enough for your application, using it could reduce the total code size.
24
25                var _ctorMap = {};
26                var getCtor = function(type, mixins){
27                        if(typeof(mixins) === "string"){
28                                var t = type + ":" + mixins.replace(/ /g, "");
29                                return _ctorMap[t] ||
30                                        (_ctorMap[t] = getCtor(type).createSubclass(array.map(mixins.split(/, */), getCtor)));
31                        }
32                        return _ctorMap[type] || (_ctorMap[type] = lang.getObject(type) || require(type));
33                };
34                var _eval = function(js){ return eval(js); };
35
36                this.instantiate = function(/* DomNode[] */nodes, /* Object? */mixin, /* Object? */options){
37                        // summary:
38                        //              Function for instantiating a list of widget nodes.
39                        // nodes:
40                        //              The list of DomNodes to walk and instantiate widgets on.
41                        mixin = mixin || {};
42                        options = options || {};
43                        var i, ws = [];
44                        if(nodes){
45                                for(i = 0; i < nodes.length; i++){
46                                        var n = nodes[i],
47                                                type = n._type,
48                                                ctor = getCtor(type, n.getAttribute("data-dojo-mixins")),
49                                                proto = ctor.prototype,
50                                                params = {}, prop, v, t;
51                                        lang.mixin(params, _eval.call(options.propsThis, '({'+(n.getAttribute("data-dojo-props")||"")+'})'));
52                                        lang.mixin(params, options.defaults);
53                                        lang.mixin(params, mixin);
54                                        for(prop in proto){
55                                                v = n.getAttributeNode(prop);
56                                                v = v && v.nodeValue;
57                                                t = typeof proto[prop];
58                                                if(!v && (t !== "boolean" || v !== "")){ continue; }
59                                                if(lang.isArray(proto[prop])){
60                                                        params[prop] = v.split(/\s*,\s*/);
61                                                }else if(t === "string"){
62                                                        params[prop] = v;
63                                                }else if(t === "number"){
64                                                        params[prop] = v - 0;
65                                                }else if(t === "boolean"){
66                                                        params[prop] = (v !== "false");
67                                                }else if(t === "object"){
68                                                        params[prop] = eval("(" + v + ")");
69                                                }else if(t === "function"){
70                                                        params[prop] = lang.getObject(v, false) || new Function(v);
71                                                        n.removeAttribute(prop);
72                                                }
73                                        }
74                                        params["class"] = n.className;
75                                        if(!params.style){ params.style = n.style.cssText; }
76                                        v = n.getAttribute("data-dojo-attach-point");
77                                        if(v){ params.dojoAttachPoint = v; }
78                                        v = n.getAttribute("data-dojo-attach-event");
79                                        if(v){ params.dojoAttachEvent = v; }
80                                        var instance = new ctor(params, n);
81                                        ws.push(instance);
82                                        var jsId = n.getAttribute("jsId") || n.getAttribute("data-dojo-id");
83                                        if(jsId){
84                                                lang.setObject(jsId, instance);
85                                        }
86                                }
87                                for(i = 0; i < ws.length; i++){
88                                        var w = ws[i];
89                                        !options.noStart && w.startup && !w._started && w.startup();
90                                }
91                        }
92                        return ws;
93                };
94
95                this.parse = function(/* DomNode */ rootNode, /* Object? */ options){
96                        // summary:
97                        //              Function to handle parsing for widgets in the current document.
98                        //              It is not as powerful as the full parser, but it will handle basic
99                        //              use cases fine.
100                        // rootNode:
101                        //              The root node in the document to parse from
102                        if(!rootNode){
103                                rootNode = win.body();
104                        }else if(!options && rootNode.rootNode){
105                                // Case where 'rootNode' is really a params object.
106                                options = rootNode;
107                                rootNode = rootNode.rootNode;
108                        }
109
110                        var nodes = rootNode.getElementsByTagName("*");
111                        var i, j, list = [];
112                        for(i = 0; i < nodes.length; i++){
113                                var n = nodes[i],
114                                        type = (n._type = n.getAttribute("dojoType") || n.getAttribute("data-dojo-type"));
115                                if(type){
116                                        if(n._skip){
117                                                n._skip = "";
118                                                continue;
119                                        }
120                                        if(getCtor(type).prototype.stopParser && !(options && options.template)){
121                                                var arr = n.getElementsByTagName("*");
122                                                for(j = 0; j < arr.length; j++){
123                                                        arr[j]._skip = "1";
124                                                }
125                                        }
126                                        list.push(n);
127                                }
128                        }
129                        var mixin = options && options.template ? {template: true} : null;
130                        return this.instantiate(list, mixin, options);
131                };
132        };
133
134        // Singleton.   (TODO: replace parser class and singleton w/a simple hash of functions)
135        var parser = new Parser();
136
137        if(config.parseOnLoad){
138                ready(100, function(){
139                        // Now that all the modules are loaded, check if the app loaded dojo/parser too.
140                        // If it did, let dojo/parser handle the parseOnLoad flag instead of me.
141                        try{
142                                if(!require("dojo/parser")){
143                                        // IE6 takes this path when dojo/parser unavailable, rather than catch() block below,
144                                        // due to http://support.microsoft.com/kb/944397
145                                        parser.parse();
146                                }
147                        }catch(e){
148                                // Other browsers (and later versions of IE) take this path when dojo/parser unavailable
149                                parser.parse();
150                        }
151                });
152        }
153        dm.parser = parser; // for backward compatibility
154        dojo.parser = dojo.parser || parser; // in case user application calls dojo.parser
155
156        return parser;
157});
Note: See TracBrowser for help on using the repository browser.