source: Dev/branches/rest-dojo-ui/client/dojox/mobile/parser.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 3.5 KB
Line 
1define([
2        "dojo/_base/kernel",
3        "dojo/_base/config",
4        "dojo/_base/lang",
5        "dojo/_base/window",
6        "dojo/ready"
7], function(dojo, config, lang, win, ready){
8
9        // module:
10        //              dojox/mobile/parser
11        // summary:
12        //              A lightweight parser.
13
14        var dm = lang.getObject("dojox.mobile", true);
15
16        var parser = new function(){
17                // summary:
18                //              A lightweight parser.
19                // description:
20                //              dojox.mobile.parser is an extremely small subset of
21                //              dojo.parser. It has no extended features over dojo.parser, so
22                //              there is no reason you have to use dojox.mobile.parser instead
23                //              of dojo.parser. However, if dojox.mobile.parser's capability is
24                //              enough for your application, use of it could reduce the total
25                //              code size.
26
27                this.instantiate = function(/* Array */nodes, /* Object? */mixin, /* Object? */args){
28                        // summary:
29                        //              Function for instantiating a list of widget nodes.
30                        // nodes:
31                        //              The list of DOMNodes to walk and instantiate widgets on.
32                        mixin = mixin || {};
33                        args = args || {};
34                        var i, ws = [];
35                        if(nodes){
36                                for(i = 0; i < nodes.length; i++){
37                                        var n = nodes[i];
38                                        var cls = lang.getObject(n.getAttribute("dojoType") || n.getAttribute("data-dojo-type"));
39                                        var proto = cls.prototype;
40                                        var params = {}, prop, v, t;
41                                        lang.mixin(params, eval('({'+(n.getAttribute("data-dojo-props")||"")+'})'));
42                                        lang.mixin(params, args.defaults);
43                                        lang.mixin(params, mixin);
44                                        for(prop in proto){
45                                                v = n.getAttributeNode(prop);
46                                                v = v && v.nodeValue;
47                                                t = typeof proto[prop];
48                                                if(!v && (t !== "boolean" || v !== "")){ continue; }
49                                                if(t === "string"){
50                                                        params[prop] = v;
51                                                }else if(t === "number"){
52                                                        params[prop] = v - 0;
53                                                }else if(t === "boolean"){
54                                                        params[prop] = (v !== "false");
55                                                }else if(t === "object"){
56                                                        params[prop] = eval("(" + v + ")");
57                                                }
58                                        }
59                                        params["class"] = n.className;
60                                        params.style = n.style && n.style.cssText;
61                                        v = n.getAttribute("data-dojo-attach-point");
62                                        if(v){ params.dojoAttachPoint = v; }
63                                        v = n.getAttribute("data-dojo-attach-event");
64                                        if(v){ params.dojoAttachEvent = v; }
65                                        var instance = new cls(params, n);
66                                        ws.push(instance);
67                                        var jsId = n.getAttribute("jsId") || n.getAttribute("data-dojo-id");
68                                        if(jsId){
69                                                lang.setObject(jsId, instance);
70                                        }
71                                }
72                                for(i = 0; i < ws.length; i++){
73                                        var w = ws[i];
74                                        !args.noStart && w.startup && !w._started && w.startup();
75                                }
76                        }
77                        return ws;
78                };
79
80                this.parse = function(rootNode, args){
81                        // summary:
82                        //              Function to handle parsing for widgets in the current document.
83                        //              It is not as powerful as the full parser, but it will handle basic
84                        //              use cases fine.
85                        // rootNode:
86                        //              The root node in the document to parse from
87                        if(!rootNode){
88                                rootNode = win.body();
89                        }else if(!args && rootNode.rootNode){
90                                // Case where 'rootNode' is really a params object.
91                                args = rootNode;
92                                rootNode = rootNode.rootNode;
93                        }
94
95                        var nodes = rootNode.getElementsByTagName("*");
96                        var i, list = [];
97                        for(i = 0; i < nodes.length; i++){
98                                var n = nodes[i];
99                                if(n.getAttribute("dojoType") || n.getAttribute("data-dojo-type")){
100                                        list.push(n);
101                                }
102                        }
103                        var mixin = args && args.template ? {template: true} : null;
104                        return this.instantiate(list, mixin, args);
105                };
106        }();
107        if(config.parseOnLoad){
108                ready(100, parser, "parse");
109        }
110        dm.parser = parser; // for backward compatibility
111        dojo.parser = parser; // in case user application calls dojo.parser
112        return parser;
113});
Note: See TracBrowser for help on using the repository browser.