]> git.wh0rd.org Git - tt-rss.git/blob - lib/dojo/_base/configRhino.js
lib: Upgrade Dojo and Dijit from 1.8.3 to 1.12.1
[tt-rss.git] / lib / dojo / _base / configRhino.js
1 function rhinoDojoConfig(config, baseUrl, rhinoArgs){
2         // summary:
3         //              This module provides bootstrap configuration for running dojo in rhino.
4
5         // TODO: v1.6 tries to set dojo.doc and dojo.body in rhino; why?
6
7         // get a minimal console up
8         var log = function(hint, args){
9                 print((hint ? hint + ":" : "") + args[0]);
10                 for(var i = 1; i < args.length; i++){
11                         print(", " + args[i]);
12                 }
13         };
14         // intentionally define console in the global namespace
15         console= {
16                 log: function(){ log(0, arguments); },
17                 error: function(){ log("ERROR", arguments); },
18                 warn: function(){ log("WARN", arguments); }
19         };
20
21         // any command line arguments with the load flag are pushed into deps
22         for(var deps = [], i = 0; i < rhinoArgs.length; i++){
23                 var arg = (rhinoArgs[i] + "").split("=");
24                 if(arg[0] == "load"){
25                         deps.push(arg[1]);
26                 }else if(arg[0] == "mapPackage") {
27                         var parts = arg[1].split(":"),
28                                 name = parts[0],
29                                 location=parts[1],
30                                 isPrexisting = false;
31
32                         for (var j = 0; j < config.packages.length; j++) {
33                                 var pkg = config.packages[j];
34                                 if (pkg.name === name) {
35                                         pkg.location = location;
36                                         isPrexisting = true;
37                                         break;
38                                 }
39                         }
40
41                         if (!isPrexisting) {
42                                 config.packages.push({
43                                         name: name,
44                                         location: location
45                                 });
46                         }
47                 }
48         }
49
50         // provides timed callbacks using Java threads
51         if(typeof setTimeout == "undefined" || typeof clearTimeout == "undefined"){
52                 var timeouts = [];
53                 clearTimeout = function(idx){
54                         if(!timeouts[idx]){ return; }
55                         timeouts[idx].stop();
56                 };
57
58                 setTimeout = function(func, delay){
59                         var def = {
60                                 sleepTime:delay,
61                                 hasSlept:false,
62
63                                 run:function(){
64                                         if(!this.hasSlept){
65                                                 this.hasSlept = true;
66                                                 java.lang.Thread.currentThread().sleep(this.sleepTime);
67                                         }
68                                         try{
69                                                 func();
70                                         }catch(e){
71                                                 console.debug("Error running setTimeout thread:" + e);
72                                         }
73                                 }
74                         };
75
76                         var runnable = new java.lang.Runnable(def);
77                         var thread = new java.lang.Thread(runnable);
78                         thread.start();
79                         return timeouts.push(thread) - 1;
80                 };
81         }
82
83         var isLocal = function(url){
84                 return (new java.io.File(url)).exists();
85         };
86
87         // reset the has cache with node-appropriate values;
88         var hasCache = {
89                 "host-rhino":1,
90                 "host-browser":0,
91                 "dom":0,
92                 "dojo-has-api":1,
93                 "dojo-xhr-factory":0,
94                 "dojo-inject-api":1,
95                 "dojo-timeout-api":0,
96                 "dojo-trace-api":1,
97                 "dojo-loader-catches":1,
98                 "dojo-dom-ready-api":0,
99                 "dojo-publish-privates":1,
100                 "dojo-sniff":0,
101                 "dojo-loader":1,
102                 "dojo-test-xd":0,
103                 "dojo-test-sniff":0
104         };
105         for(var p in hasCache){
106                 config.hasCache[p] = hasCache[p];
107         }
108
109         // reset some configuration switches with rhino-appropriate values
110         var rhinoConfig = {
111                 baseUrl:baseUrl,
112                 commandLineArgs:rhinoArgs,
113                 deps:deps,
114                 timeout:0,
115                 locale:String(java.util.Locale.getDefault().toString().replace('_', '-').toLowerCase()),
116
117                 loaderPatch:{
118                         injectUrl: function(url, callback){
119                                 try{
120                                         if(isLocal(url)){
121                                                 load(url);
122                                         }else{
123                                                 require.eval(readUrl(url, "UTF-8"));
124                                         }
125                                         callback();
126                                 }catch(e){
127                                         console.log("failed to load resource (" + url + ")");
128                                         console.log(e);
129                                 }
130                         },
131
132                         getText: function(url, sync, onLoad){
133                                 // TODO: test https://bugzilla.mozilla.org/show_bug.cgi?id=471005; see v1.6 hostenv_rhino
134                                 // note: async mode not supported in rhino
135                                 onLoad(isLocal(url) ? readFile(url, "UTF-8") : readUrl(url, "UTF-8"));
136                         }
137                 }
138         };
139         for(p in rhinoConfig){
140                 config[p] = rhinoConfig[p];
141         }
142 }