backCompat.js 8.4 KB

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