]> git.wh0rd.org Git - tt-rss.git/blob - lib/dojo/lib/backCompat.js
remove call-by-reference to comply with php 5.4
[tt-rss.git] / lib / dojo / lib / backCompat.js
1 /*
2         Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
3         Available via Academic Free License >= 2.1 OR the modified BSD license.
4         see: http://dojotoolkit.org/license for details
5 */
6
7
8 // AMD module id = dojo/lib/backCompat
9 //
10 // This module defines those dojo properties/methods that are defined by
11 // dojo/_base/_loader/loader and are still needed when loading with and
12 // AMD loader (when loading with an AMD loader, dojo/_base/_loader/loader
13 // is never loaded).
14 //
15 // note: this module is relevant only when loading dojo with an AMD loader;
16 // it is never evaluated otherwise.
17
18 define(["require", "dojo/_base/_loader/bootstrap"], function(require, dojo){
19         // the following dojo properties do not exist in the AMD-loaded version of dojo 1.x:
20         var names= [
21                 "_moduleHasPrefix",
22                 "_loadPath",
23                 "_loadUri",
24                 "_loadUriAndCheck",
25                 "loaded",
26                 "_callLoaded",
27                 "_getModuleSymbols",
28                 "_loadModule",
29                 "require",
30                 "provide",
31                 "platformRequire",
32                 "requireIf",
33                 "requireAfterIf",
34                 "registerModulePath"
35         ], i, name;
36         for(i = 0; i<names.length;){
37                 name = names[i++];
38                 dojo[name] = (function(name) {
39                         return function(){
40                                 console.warn("dojo." + name + " not available when using an AMD loader.");
41                         };
42                 })(name);
43         }
44
45         // define dojo.addOnLoad in terms of the DOMContentLoaded detection available from the AMD loaders
46         // (requirejs and bdBuild). Note that the behavior of this feature is slightly different compared to the dojo
47         // v1.x sync loader. There, the onload queue is fired upon detecting both DOMContentLoaded *and* all
48         // demanded modules have arrived. It is impossible to simulate this behavior with requirejs since it does
49         // not publish its internal status (it is possible with bdLoad).
50         // TODO: consider taking ownership of this API back from the loader.
51         // TODO: consider requesting requirejs publish more enough internal state to determine if all demanded
52         // modules have been defined.
53         var
54                 argsToArray = function(args) {
55                         var result = [], i;
56                         for(i = 0; i<args.length;){
57                                 result.push(args[i++]);
58                         }
59                         return result;
60                 },
61
62                 simpleHitch = function(context, callback){
63                         if(callback){
64                                 return (typeof callback=="string") ?
65                                         function(){context[callback]();} :
66                                         function(){callback.call(context);};
67                         }else{
68                                 return context;
69                         }
70                 };
71         dojo.ready = dojo.addOnLoad = function(context, callback){
72                 require.ready(callback ? simpleHitch(context, callback) : context);
73         };
74         dojo.addOnLoad(function() {
75                 dojo.postLoad = dojo.config.afterOnLoad = true;
76         });
77         var dca = dojo.config.addOnLoad;
78         if(dca){
79                 dojo.addOnLoad[(dca instanceof Array ? "apply" : "call")](dojo, dca);
80         }
81
82         // TODO: in the dojo 1.x sync loader the array dojo._loaders holds the queue of callbacks to be executed
83         // upon DOMContentLoaded. This queue is manipulated directly by dojo/uacss, dojo/parser, dijit/_base/wia
84         // and others (at least in dojox). This is also impossible to simulate universally across all AMD loaders.
85         // The following will at least accept client code accessing dojo._loaders , dojo._loaders.unshift, and
86         // dojo._loaders.splice--which is all that exists in the current dojo/dijit code stacks.
87         var
88                 loaders = dojo._loaders = [],
89                 runLoaders = function(){
90                         var temp= loaders.slice(0);
91                         Array.prototype.splice.apply(loaders, [0, loaders.length]);
92                         while(temp.length){
93                                 temp.shift().call();
94                         };
95                 };
96         loaders.unshift = function() {
97                 Array.prototype.unshift.apply(loaders, argsToArray(arguments));
98                 require.ready(runLoaders);
99         };
100         loaders.splice = function() {
101                 Array.prototype.splice.apply(loaders, argsToArray(arguments));
102                 require.ready(runLoaders);
103         };
104
105         //TODO: put unload handling in a separate module
106         var unloaders = dojo._unloaders = [];
107         dojo.unloaded = function(){
108                 while(unloaders.length){
109                         unloaders.pop().call();
110                 }
111         };
112
113         //TODO: kill this low-value function when it is exorcised from dojo
114         dojo._onto = function(arr, obj, fn){
115                 arr.push(fn ? simpleHitch(obj, fn) : obj);
116         };
117
118         //TODO: kill this when the bootstrap is rewritten to not include DOMContentLoaded detection
119         // (it should probably be just a module) for now, just sink the detection; leverage the
120         // AMD loaders to handle DOMContentLoaded detection
121         dojo._modulesLoaded = function(){};
122
123         //TODO: kill this when we understand its purpose relative to AMD
124         dojo.loadInit = function(init){
125                 init();
126         };
127
128         var amdModuleName= function(moduleName){
129                 return moduleName.replace(/\./g, "/");
130         };
131
132         dojo.getL10nName = function(moduleName, bundleName, locale){
133                 locale = locale ? locale.toLowerCase() : dojo.locale;
134                 moduleName = "i18n!" + amdModuleName(moduleName);
135                 return (/root/i.test(locale)) ?
136                         (moduleName + "/nls/" + bundleName) :
137                         (moduleName + "/nls/"    + locale + "/" + bundleName);
138         };
139
140         dojo.requireLocalization = function(moduleName, bundleName, locale){
141                 // NOTE: locales other than the locale specified in dojo.locale need to be specifically
142                 // declared as a module dependency when using AMD.
143                 if(require.vendor!="altoviso.com"){
144                         locale = !locale || locale.toLowerCase() === dojo.locale ? "root" :  locale;
145                 }
146                 return require(dojo.getL10nName(moduleName, bundleName, locale));
147         };
148
149         dojo.i18n= {
150                 getLocalization: dojo.requireLocalization,
151                 normalizeLocale: function(locale){
152                         var result = locale ? locale.toLowerCase() : dojo.locale;
153                         if(result == "root"){
154                                 result = "ROOT";
155                         }
156                         return result;
157                 }
158         };
159
160         //TODO: dojo._Url seems rarely used and long to be part of the boostrap; consider moving
161         //note: this routine cut and paste from dojo/_base/_loader/loader
162         var
163                 ore = new RegExp("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?$"),
164                 ire = new RegExp("^((([^\\[:]+):)?([^@]+)@)?(\\[([^\\]]+)\\]|([^\\[:]*))(:([0-9]+))?$");
165         dojo._Url = function(){
166                 var n = null,
167                         _a = arguments,
168                         uri = [_a[0]];
169                 // resolve uri components relative to each other
170                 for(var i = 1; i<_a.length; i++){
171                         if(!_a[i]){ continue; }
172
173                         // Safari doesn't support this.constructor so we have to be explicit
174                         // FIXME: Tracked (and fixed) in Webkit bug 3537.
175                         //              http://bugs.webkit.org/show_bug.cgi?id=3537
176                         var relobj = new dojo._Url(_a[i]+""),
177                                 uriobj = new dojo._Url(uri[0]+"");
178
179                         if(
180                                 relobj.path == "" &&
181                                 !relobj.scheme &&
182                                 !relobj.authority &&
183                                 !relobj.query
184                         ){
185                                 if(relobj.fragment != n){
186                                         uriobj.fragment = relobj.fragment;
187                                 }
188                                 relobj = uriobj;
189                         }else if(!relobj.scheme){
190                                 relobj.scheme = uriobj.scheme;
191
192                                 if(!relobj.authority){
193                                         relobj.authority = uriobj.authority;
194
195                                         if(relobj.path.charAt(0) != "/"){
196                                                 var path = uriobj.path.substring(0,
197                                                         uriobj.path.lastIndexOf("/") + 1) + relobj.path;
198
199                                                 var segs = path.split("/");
200                                                 for(var j = 0; j < segs.length; j++){
201                                                         if(segs[j] == "."){
202                                                                 // flatten "./" references
203                                                                 if(j == segs.length - 1){
204                                                                         segs[j] = "";
205                                                                 }else{
206                                                                         segs.splice(j, 1);
207                                                                         j--;
208                                                                 }
209                                                         }else if(j > 0 && !(j == 1 && segs[0] == "") &&
210                                                                 segs[j] == ".." && segs[j-1] != ".."){
211                                                                 // flatten "../" references
212                                                                 if(j == (segs.length - 1)){
213                                                                         segs.splice(j, 1);
214                                                                         segs[j - 1] = "";
215                                                                 }else{
216                                                                         segs.splice(j - 1, 2);
217                                                                         j -= 2;
218                                                                 }
219                                                         }
220                                                 }
221                                                 relobj.path = segs.join("/");
222                                         }
223                                 }
224                         }
225
226                         uri = [];
227                         if(relobj.scheme){
228                                 uri.push(relobj.scheme, ":");
229                         }
230                         if(relobj.authority){
231                                 uri.push("//", relobj.authority);
232                         }
233                         uri.push(relobj.path);
234                         if(relobj.query){
235                                 uri.push("?", relobj.query);
236                         }
237                         if(relobj.fragment){
238                                 uri.push("#", relobj.fragment);
239                         }
240                 }
241
242                 this.uri = uri.join("");
243
244                 // break the uri into its main components
245                 var r = this.uri.match(ore);
246
247                 this.scheme = r[2] || (r[1] ? "" : n);
248                 this.authority = r[4] || (r[3] ? "" : n);
249                 this.path = r[5]; // can never be undefined
250                 this.query = r[7] || (r[6] ? "" : n);
251                 this.fragment    = r[9] || (r[8] ? "" : n);
252
253                 if(this.authority != n){
254                         // server based naming authority
255                         r = this.authority.match(ire);
256
257                         this.user = r[3] || n;
258                         this.password = r[4] || n;
259                         this.host = r[6] || r[7]; // ipv6 || ipv4
260                         this.port = r[9] || n;
261                 }
262         };
263
264         dojo._Url.prototype.toString = function(){ return this.uri; };
265
266         dojo.moduleUrl = function(module, url){
267                 if(!module){
268                  //TODO: don't understand why this would ever be so, but that's the logic in loader
269                  return null;
270                 }
271                 module = amdModuleName(module) + (url ? ("/" + url) : "");
272                 var
273                         type= "",
274                         match= module.match(/(.+)(\.[^\/]*)$/);
275                 if (match) {
276                         module= match[1];
277                         type= match[2];
278                 }
279                 return new dojo._Url(require.nameToUrl(module, type)); // dojo._Url
280         };
281
282         return dojo;
283 });