source: Dev/branches/rest-dojo-ui/client/util/doh/_parseURLargs.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: 6.6 KB
Line 
1(function(){
2        var
3                boot=
4                        // zero to many scripts to load a configuration and/or loader.
5                        // i.e. path-to-util/doh/runner.html?boots=path-to/config.js,path-to/require.js
6                        ["../../dojo/dojo.js"],
7
8                standardDojoBoot= boot,
9
10                test=
11                        // zero to many AMD modules and/or URLs to load; provided by csv URL query parameter="test"
12                        // For example, the URL...
13                        //
14                        //               path-to-util/doh/runner.html?test=doh/selfTest,my/path/test.js
15                        //
16                        // ...will load...
17                        //
18                        //       * the AMD module doh/selfTest
19                        //       * the plain old Javascript resource my/path/test.js
20                        //
21                        ["dojo/tests/module"],
22
23                paths =
24                        // zero to many path items to pass to the AMD loader; provided by semicolon separated values
25                        // for URL query parameter="paths"; each path item has the form <from-path>,<to-path>
26                        // i.e. path-to-util/doh/runner.html?paths=my/from/path,my/to/path;my/from/path2,my/to/path2
27                        {},
28                       
29                dohPlugins =
30                        // Semicolon separated list of files to load before the tests.
31                        // Idea is to override aspects of DOH for reporting purposes.
32                        "",
33
34                breakOnError =
35                        // boolean; instructs doh to call the debugger upon a test failures; this can be helpful when
36                        // trying to isolate exactly where the test failed
37                        false,
38
39                async =
40                        // boolean; config require.asyc==true before loading boot; this will have the effect of making
41                        // version 1.7+ dojo bootstrap/loader operating in async mode
42                        false,
43
44                sandbox =
45                        // boolean; use a loader configuration that sandboxes the dojo and dojox objects used by doh
46                        false,
47
48                trim= function(text){
49                        if(text instanceof Array){
50                                for (var result= [], i= 0; i<text.length; i++) {
51                                        result.push(trim(text[i]));
52                                }
53                                return result;
54                        }else{
55                                return text.match(/[^\s]*/)[0];
56                        }
57                }
58
59                qstr = window.location.search.substr(1);
60
61        if(qstr.length){
62                for(var qparts= qstr.split("&"), x= 0; x<qparts.length; x++){
63                        var tp = qparts[x].split("="), name=tp[0], value=(tp[1]||"").replace(/[<>"':\(\)]/g, ""); // replace() to avoid XSS attack
64                        //Avoid URLs that use the same protocol but on other domains, for security reasons.
65                        if (value.indexOf("//") === 0 || value.indexOf("\\\\") === 0) {
66                                throw "Insupported URL";
67                        }
68                        switch(name){
69                                // Note:
70                                //       * dojoUrl is deprecated, and is a synonym for boot
71                                //       * testUrl is deprecated, and is a synonym for test
72                                //       * testModule is deprecated, and is a synonym for test (dots are automatically replaced with slashes)
73                                //       * registerModulePath is deprecated, and is a synonym for paths
74                                case "boot":
75                                case "dojoUrl":
76                                        boot= trim(value.split(","));
77                                        break;
78
79                                case "test":
80                                case "testUrl":
81                                        test= trim(value.split(","));
82                                        break;
83
84                                case "testModule":
85                                        test= trim(value.replace(/\./g, "/").split(","));
86                                        break;
87
88                                // registerModulePath is deprecated; use "paths"
89                                case "registerModulePath":
90                                case "paths":
91                                        for(var path, modules = value.split(";"), i= 0; i<modules.length; i++){
92                                                path= modules[i].split(",");
93                                                paths[trim(path[0])]= trim(path[1]);
94                                        }
95                                        break;
96
97                                case "breakOnError":
98                                        breakOnError= true;
99                                        break;
100
101                                case "sandbox":
102                                        sandbox= true;
103                                        break;
104
105                                case "async":
106                                        async= true;
107                                        break;
108                                case "dohPlugins":
109                                        dohPlugins=value.split(";");
110                                        break;
111                        }
112                }
113        }
114
115        function fixHeight(dojo){
116                // IE9 doesn't give test iframe height because no nodes have an explicit pixel height!
117                // Give outer table a pixel height.
118                if(dojo.isIE){
119                        var headerHeight=0;
120                        var rows=dojo.query('#testLayout > tbody > tr');
121                        for(var i=0; i<rows.length-1; i++){
122                                headerHeight+=dojo.position(rows[i]).h;
123                        }
124                        try{
125                                // we subtract the headerHeight from the window height because the table row containing the tests is height:100% so they will stretch the table to the intended height.
126                                dojo.byId('testLayout').style.height=(dojo.window.getBox().h-headerHeight)+"px";
127                        }catch(e){
128                                // An obscure race condition when you load the runner in IE from the command line causes the window reported height to be 0.
129                                // Try to recover after the window finishes rendering.
130                                setTimeout(function(){ fixHeight(dojo); },0);
131                        }
132                }
133        }
134
135        var config;
136        if(sandbox){
137                // configure the loader assuming the dojo loader; of course the injected boot(s) can override this config
138                config= {
139                        paths: paths,
140                        // this config uses the dojo loader's scoping features to sandbox the version of dojo used by doh
141                        packages:[{
142                                name:'doh',
143                                location:'../util/doh',
144                                // here's the magic...everytime doh asks for a "dojo" module, it gets mapped to a "dohDojo"
145                                // module; same goes for dojox/dohDojox since doh uses dojox
146                                packageMap:{dojo:"dohDojo", dojox:"dohDojox"}
147                        },{
148                                // now define the dohDojo package...
149                                name:'dohDojo',
150                                location:'../dojo',
151                                packageMap:{dojo:"dohDojo", dojox:"dohDojox"}
152                        },{
153                                // and the dohDojox package...
154                                name:'dohDojox',
155                                location:'../dojox',
156                                // and dojox uses dojo...that is, dohDojox...which must be mapped to dohDojo in the context of dohDojox
157                                packageMap:{dojo:"dohDojo", dojox:"dohDojox"}
158                        }],
159                       
160                        // next, we need to preposition a special configuration for dohDojo
161                        cache:{
162                                "dohDojo*_base/config":function(){
163                                        define([], {
164                                                // this configuration keeps dojo, dijit, and dojox out of the global space
165                                                scopeMap:[["dojo", "dohDojo"], ["dijit", "dohDijit"], ["dojox", "dohDojox"]],
166                                                isDebug:true,
167                                                noGlobals:true
168                                        });
169                                }
170                        },
171
172                        // control the loader; don't boot global dojo, doh will ask for dojo itself
173                        has:{
174                                "dojo-sniff":0,
175                                "dojo-loader":1,
176                                "dojo-boot":0,
177                                "dojo-test-sniff":1
178                        },
179
180                        // no sniffing; therefore, set the baseUrl
181                        baseUrl:"../../dojo",
182
183                        deps:["dohDojo", "doh", "dohDojo/window"],
184
185                        callback:function(dohDojo, doh){
186                                dohDojo.ready(function(){
187                                        fixHeight(dohDojo);
188                                        doh.breakOnError= breakOnError;
189                                        require(test);
190                                        dohDojo.ready(doh, "run");
191                                });
192                        },
193
194                        async:async
195                };
196        }else{
197                config= {
198                        paths: paths,
199                        deps:["dojo", "doh", "dojo/window"],
200                        callback:function(dojo, doh){
201                                dojo.ready(function(){
202                                        fixHeight(dojo);
203                                        doh.breakOnError= breakOnError;
204                                        require(test);
205                                        dojo.ready(doh, "run");
206                                });
207                        },
208                        async:async,
209                        isDebug:1
210                };
211        }
212       
213        // load all of the dohPlugins
214        if(dohPlugins){
215                var i=0;
216                for(i=0; i<dohPlugins.length; i++){
217                        config.deps.push(dohPlugins[i]);
218                }
219        }
220       
221        require= config;
222
223        // now script inject any boots
224        for(var e, i= 0; i<boot.length; i++) {
225                if(boot[i]){
226                        e= document.createElement("script");
227                        e.type= "text/javascript";
228                        e.src= boot[i];
229                        e.charset= "utf-8";
230                        document.getElementsByTagName("head")[0].appendChild(e);
231                }
232        }
233})()
Note: See TracBrowser for help on using the repository browser.