hostenv_browser.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. /*=====
  7. dojo.isBrowser = {
  8. // example:
  9. // | if(dojo.isBrowser){ ... }
  10. };
  11. dojo.isFF = {
  12. // example:
  13. // | if(dojo.isFF > 1){ ... }
  14. };
  15. dojo.isIE = {
  16. // example:
  17. // | if(dojo.isIE > 6){
  18. // | // we are IE7
  19. // | }
  20. };
  21. dojo.isSafari = {
  22. // example:
  23. // | if(dojo.isSafari){ ... }
  24. // example:
  25. // Detect iPhone:
  26. // | if(dojo.isSafari && navigator.userAgent.indexOf("iPhone") != -1){
  27. // | // we are iPhone. Note, iPod touch reports "iPod" above and fails this test.
  28. // | }
  29. };
  30. dojo = {
  31. // isBrowser: Boolean
  32. // True if the client is a web-browser
  33. isBrowser: true,
  34. // isFF: Number | undefined
  35. // Version as a Number if client is FireFox. undefined otherwise. Corresponds to
  36. // major detected FireFox version (1.5, 2, 3, etc.)
  37. isFF: 2,
  38. // isIE: Number | undefined
  39. // Version as a Number if client is MSIE(PC). undefined otherwise. Corresponds to
  40. // major detected IE version (6, 7, 8, etc.)
  41. isIE: 6,
  42. // isKhtml: Number | undefined
  43. // Version as a Number if client is a KHTML browser. undefined otherwise. Corresponds to major
  44. // detected version.
  45. isKhtml: 0,
  46. // isWebKit: Number | undefined
  47. // Version as a Number if client is a WebKit-derived browser (Konqueror,
  48. // Safari, Chrome, etc.). undefined otherwise.
  49. isWebKit: 0,
  50. // isMozilla: Number | undefined
  51. // Version as a Number if client is a Mozilla-based browser (Firefox,
  52. // SeaMonkey). undefined otherwise. Corresponds to major detected version.
  53. isMozilla: 0,
  54. // isOpera: Number | undefined
  55. // Version as a Number if client is Opera. undefined otherwise. Corresponds to
  56. // major detected version.
  57. isOpera: 0,
  58. // isSafari: Number | undefined
  59. // Version as a Number if client is Safari or iPhone. undefined otherwise.
  60. isSafari: 0,
  61. // isChrome: Number | undefined
  62. // Version as a Number if client is Chrome browser. undefined otherwise.
  63. isChrome: 0
  64. // isMac: Boolean
  65. // True if the client runs on Mac
  66. }
  67. =====*/
  68. if(typeof window != 'undefined'){
  69. dojo.isBrowser = true;
  70. dojo._name = "browser";
  71. // attempt to figure out the path to dojo if it isn't set in the config
  72. (function(){
  73. var d = dojo;
  74. // this is a scope protection closure. We set browser versions and grab
  75. // the URL we were loaded from here.
  76. // grab the node we were loaded from
  77. if(document && document.getElementsByTagName){
  78. var scripts = document.getElementsByTagName("script");
  79. var rePkg = /dojo(\.xd)?\.js(\W|$)/i;
  80. for(var i = 0; i < scripts.length; i++){
  81. var src = scripts[i].getAttribute("src");
  82. if(!src){ continue; }
  83. var m = src.match(rePkg);
  84. if(m){
  85. // find out where we came from
  86. if(!d.config.baseUrl){
  87. d.config.baseUrl = src.substring(0, m.index);
  88. }
  89. // and find out if we need to modify our behavior
  90. var cfg = (scripts[i].getAttribute("djConfig") || scripts[i].getAttribute("data-dojo-config"));
  91. if(cfg){
  92. var cfgo = eval("({ "+cfg+" })");
  93. for(var x in cfgo){
  94. dojo.config[x] = cfgo[x];
  95. }
  96. }
  97. break; // "first Dojo wins"
  98. }
  99. }
  100. }
  101. d.baseUrl = d.config.baseUrl;
  102. // fill in the rendering support information in dojo.render.*
  103. var n = navigator;
  104. var dua = n.userAgent,
  105. dav = n.appVersion,
  106. tv = parseFloat(dav);
  107. if(dua.indexOf("Opera") >= 0){ d.isOpera = tv; }
  108. if(dua.indexOf("AdobeAIR") >= 0){ d.isAIR = 1; }
  109. d.isKhtml = (dav.indexOf("Konqueror") >= 0) ? tv : 0;
  110. d.isWebKit = parseFloat(dua.split("WebKit/")[1]) || undefined;
  111. d.isChrome = parseFloat(dua.split("Chrome/")[1]) || undefined;
  112. d.isMac = dav.indexOf("Macintosh") >= 0;
  113. // safari detection derived from:
  114. // http://developer.apple.com/internet/safari/faq.html#anchor2
  115. // http://developer.apple.com/internet/safari/uamatrix.html
  116. var index = Math.max(dav.indexOf("WebKit"), dav.indexOf("Safari"), 0);
  117. if(index && !dojo.isChrome){
  118. // try to grab the explicit Safari version first. If we don't get
  119. // one, look for less than 419.3 as the indication that we're on something
  120. // "Safari 2-ish".
  121. d.isSafari = parseFloat(dav.split("Version/")[1]);
  122. if(!d.isSafari || parseFloat(dav.substr(index + 7)) <= 419.3){
  123. d.isSafari = 2;
  124. }
  125. }
  126. if(dua.indexOf("Gecko") >= 0 && !d.isKhtml && !d.isWebKit){ d.isMozilla = d.isMoz = tv; }
  127. if(d.isMoz){
  128. //We really need to get away from this. Consider a sane isGecko approach for the future.
  129. d.isFF = parseFloat(dua.split("Firefox/")[1] || dua.split("Minefield/")[1]) || undefined;
  130. }
  131. if(document.all && !d.isOpera){
  132. d.isIE = parseFloat(dav.split("MSIE ")[1]) || undefined;
  133. //In cases where the page has an HTTP header or META tag with
  134. //X-UA-Compatible, then it is in emulation mode.
  135. //Make sure isIE reflects the desired version.
  136. //document.documentMode of 5 means quirks mode.
  137. //Only switch the value if documentMode's major version
  138. //is different from isIE's major version.
  139. var mode = document.documentMode;
  140. if(mode && mode != 5 && Math.floor(d.isIE) != mode){
  141. d.isIE = mode;
  142. }
  143. }
  144. //Workaround to get local file loads of dojo to work on IE 7
  145. //by forcing to not use native xhr.
  146. if(dojo.isIE && window.location.protocol === "file:"){
  147. dojo.config.ieForceActiveXXhr=true;
  148. }
  149. d.isQuirks = document.compatMode == "BackCompat";
  150. // TODO: is the HTML LANG attribute relevant?
  151. d.locale = dojo.config.locale || (d.isIE ? n.userLanguage : n.language).toLowerCase();
  152. // These are in order of decreasing likelihood; this will change in time.
  153. d._XMLHTTP_PROGIDS = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
  154. d._xhrObj = function(){
  155. // summary:
  156. // does the work of portably generating a new XMLHTTPRequest object.
  157. var http, last_e;
  158. if(!dojo.isIE || !dojo.config.ieForceActiveXXhr){
  159. try{ http = new XMLHttpRequest(); }catch(e){}
  160. }
  161. if(!http){
  162. for(var i=0; i<3; ++i){
  163. var progid = d._XMLHTTP_PROGIDS[i];
  164. try{
  165. http = new ActiveXObject(progid);
  166. }catch(e){
  167. last_e = e;
  168. }
  169. if(http){
  170. d._XMLHTTP_PROGIDS = [progid]; // so faster next time
  171. break;
  172. }
  173. }
  174. }
  175. if(!http){
  176. throw new Error("XMLHTTP not available: "+last_e);
  177. }
  178. return http; // XMLHTTPRequest instance
  179. }
  180. d._isDocumentOk = function(http){
  181. var stat = http.status || 0,
  182. lp = location.protocol;
  183. return (stat >= 200 && stat < 300) || // Boolean
  184. stat == 304 || // allow any 2XX response code
  185. stat == 1223 || // get it out of the cache
  186. // Internet Explorer mangled the status code
  187. // Internet Explorer mangled the status code OR we're Titanium/browser chrome/chrome extension requesting a local file
  188. (!stat && (lp == "file:" || lp == "chrome:" || lp == "chrome-extension:" || lp == "app:"));
  189. }
  190. //See if base tag is in use.
  191. //This is to fix http://trac.dojotoolkit.org/ticket/3973,
  192. //but really, we need to find out how to get rid of the dojo._Url reference
  193. //below and still have DOH work with the dojo.i18n test following some other
  194. //test that uses the test frame to load a document (trac #2757).
  195. //Opera still has problems, but perhaps a larger issue of base tag support
  196. //with XHR requests (hasBase is true, but the request is still made to document
  197. //path, not base path).
  198. var owloc = window.location+"";
  199. var base = document.getElementsByTagName("base");
  200. var hasBase = (base && base.length > 0);
  201. d._getText = function(/*URI*/ uri, /*Boolean*/ fail_ok){
  202. // summary: Read the contents of the specified uri and return those contents.
  203. // uri:
  204. // A relative or absolute uri. If absolute, it still must be in
  205. // the same "domain" as we are.
  206. // fail_ok:
  207. // Default false. If fail_ok and loading fails, return null
  208. // instead of throwing.
  209. // returns: The response text. null is returned when there is a
  210. // failure and failure is okay (an exception otherwise)
  211. // NOTE: must be declared before scope switches ie. this._xhrObj()
  212. var http = d._xhrObj();
  213. if(!hasBase && dojo._Url){
  214. uri = (new dojo._Url(owloc, uri)).toString();
  215. }
  216. if(d.config.cacheBust){
  217. //Make sure we have a string before string methods are used on uri
  218. uri += "";
  219. uri += (uri.indexOf("?") == -1 ? "?" : "&") + String(d.config.cacheBust).replace(/\W+/g,"");
  220. }
  221. http.open('GET', uri, false);
  222. try{
  223. http.send(null);
  224. if(!d._isDocumentOk(http)){
  225. var err = Error("Unable to load "+uri+" status:"+ http.status);
  226. err.status = http.status;
  227. err.responseText = http.responseText;
  228. throw err;
  229. }
  230. }catch(e){
  231. if(fail_ok){ return null; } // null
  232. // rethrow the exception
  233. throw e;
  234. }
  235. return http.responseText; // String
  236. }
  237. var _w = window;
  238. var _handleNodeEvent = function(/*String*/evtName, /*Function*/fp){
  239. // summary:
  240. // non-destructively adds the specified function to the node's
  241. // evtName handler.
  242. // evtName: should be in the form "onclick" for "onclick" handlers.
  243. // Make sure you pass in the "on" part.
  244. var _a = _w.attachEvent || _w.addEventListener;
  245. evtName = _w.attachEvent ? evtName : evtName.substring(2);
  246. _a(evtName, function(){
  247. fp.apply(_w, arguments);
  248. }, false);
  249. };
  250. d._windowUnloaders = [];
  251. d.windowUnloaded = function(){
  252. // summary:
  253. // signal fired by impending window destruction. You may use
  254. // dojo.addOnWindowUnload() to register a listener for this
  255. // event. NOTE: if you wish to dojo.connect() to this method
  256. // to perform page/application cleanup, be aware that this
  257. // event WILL NOT fire if no handler has been registered with
  258. // dojo.addOnWindowUnload. This behavior started in Dojo 1.3.
  259. // Previous versions always triggered dojo.windowUnloaded. See
  260. // dojo.addOnWindowUnload for more info.
  261. var mll = d._windowUnloaders;
  262. while(mll.length){
  263. (mll.pop())();
  264. }
  265. d = null;
  266. };
  267. var _onWindowUnloadAttached = 0;
  268. d.addOnWindowUnload = function(/*Object?|Function?*/obj, /*String|Function?*/functionName){
  269. // summary:
  270. // registers a function to be triggered when window.onunload
  271. // fires.
  272. // description:
  273. // The first time that addOnWindowUnload is called Dojo
  274. // will register a page listener to trigger your unload
  275. // handler with. Note that registering these handlers may
  276. // destory "fastback" page caching in browsers that support
  277. // it. Be careful trying to modify the DOM or access
  278. // JavaScript properties during this phase of page unloading:
  279. // they may not always be available. Consider
  280. // dojo.addOnUnload() if you need to modify the DOM or do
  281. // heavy JavaScript work since it fires at the eqivalent of
  282. // the page's "onbeforeunload" event.
  283. // example:
  284. // | dojo.addOnWindowUnload(functionPointer)
  285. // | dojo.addOnWindowUnload(object, "functionName");
  286. // | dojo.addOnWindowUnload(object, function(){ /* ... */});
  287. d._onto(d._windowUnloaders, obj, functionName);
  288. if(!_onWindowUnloadAttached){
  289. _onWindowUnloadAttached = 1;
  290. _handleNodeEvent("onunload", d.windowUnloaded);
  291. }
  292. };
  293. var _onUnloadAttached = 0;
  294. d.addOnUnload = function(/*Object?|Function?*/obj, /*String|Function?*/functionName){
  295. // summary:
  296. // registers a function to be triggered when the page unloads.
  297. // description:
  298. // The first time that addOnUnload is called Dojo will
  299. // register a page listener to trigger your unload handler
  300. // with.
  301. //
  302. // In a browser enviroment, the functions will be triggered
  303. // during the window.onbeforeunload event. Be careful of doing
  304. // too much work in an unload handler. onbeforeunload can be
  305. // triggered if a link to download a file is clicked, or if
  306. // the link is a javascript: link. In these cases, the
  307. // onbeforeunload event fires, but the document is not
  308. // actually destroyed. So be careful about doing destructive
  309. // operations in a dojo.addOnUnload callback.
  310. //
  311. // Further note that calling dojo.addOnUnload will prevent
  312. // browsers from using a "fast back" cache to make page
  313. // loading via back button instantaneous.
  314. // example:
  315. // | dojo.addOnUnload(functionPointer)
  316. // | dojo.addOnUnload(object, "functionName")
  317. // | dojo.addOnUnload(object, function(){ /* ... */});
  318. d._onto(d._unloaders, obj, functionName);
  319. if(!_onUnloadAttached){
  320. _onUnloadAttached = 1;
  321. _handleNodeEvent("onbeforeunload", dojo.unloaded);
  322. }
  323. };
  324. })();
  325. //START DOMContentLoaded
  326. dojo._initFired = false;
  327. dojo._loadInit = function(e){
  328. if(dojo._scrollIntervalId){
  329. clearInterval(dojo._scrollIntervalId);
  330. dojo._scrollIntervalId = 0;
  331. }
  332. if(!dojo._initFired){
  333. dojo._initFired = true;
  334. //Help out IE to avoid memory leak.
  335. if(!dojo.config.afterOnLoad && window.detachEvent){
  336. window.detachEvent("onload", dojo._loadInit);
  337. }
  338. if(dojo._inFlightCount == 0){
  339. dojo._modulesLoaded();
  340. }
  341. }
  342. }
  343. if(!dojo.config.afterOnLoad){
  344. if(document.addEventListener){
  345. //Standards. Hooray! Assumption here that if standards based,
  346. //it knows about DOMContentLoaded. It is OK if it does not, the fall through
  347. //to window onload should be good enough.
  348. document.addEventListener("DOMContentLoaded", dojo._loadInit, false);
  349. window.addEventListener("load", dojo._loadInit, false);
  350. }else if(window.attachEvent){
  351. window.attachEvent("onload", dojo._loadInit);
  352. //DOMContentLoaded approximation. Diego Perini found this MSDN article
  353. //that indicates doScroll is available after DOM ready, so do a setTimeout
  354. //to check when it is available.
  355. //http://msdn.microsoft.com/en-us/library/ms531426.aspx
  356. if(!dojo.config.skipIeDomLoaded && self === self.top){
  357. dojo._scrollIntervalId = setInterval(function (){
  358. try{
  359. //When dojo is loaded into an iframe in an IE HTML Application
  360. //(HTA), such as in a selenium test, javascript in the iframe
  361. //can't see anything outside of it, so self===self.top is true,
  362. //but the iframe is not the top window and doScroll will be
  363. //available before document.body is set. Test document.body
  364. //before trying the doScroll trick
  365. if(document.body){
  366. document.documentElement.doScroll("left");
  367. dojo._loadInit();
  368. }
  369. }catch (e){}
  370. }, 30);
  371. }
  372. }
  373. }
  374. if(dojo.isIE){
  375. try{
  376. (function(){
  377. document.namespaces.add("v", "urn:schemas-microsoft-com:vml");
  378. var vmlElems = ["*", "group", "roundrect", "oval", "shape", "rect", "imagedata", "path", "textpath", "text"],
  379. i = 0, l = 1, s = document.createStyleSheet();
  380. if(dojo.isIE >= 8){
  381. i = 1;
  382. l = vmlElems.length;
  383. }
  384. for(; i < l; ++i){
  385. s.addRule("v\\:" + vmlElems[i], "behavior:url(#default#VML); display:inline-block");
  386. }
  387. })();
  388. }catch(e){}
  389. }
  390. //END DOMContentLoaded
  391. /*
  392. OpenAjax.subscribe("OpenAjax", "onload", function(){
  393. if(dojo._inFlightCount == 0){
  394. dojo._modulesLoaded();
  395. }
  396. });
  397. OpenAjax.subscribe("OpenAjax", "onunload", function(){
  398. dojo.unloaded();
  399. });
  400. */
  401. } //if (typeof window != 'undefined')
  402. //Register any module paths set up in djConfig. Need to do this
  403. //in the hostenvs since hostenv_browser can read djConfig from a
  404. //script tag's attribute.
  405. (function(){
  406. var mp = dojo.config["modulePaths"];
  407. if(mp){
  408. for(var param in mp){
  409. dojo.registerModulePath(param, mp[param]);
  410. }
  411. }
  412. })();
  413. //Load debug code if necessary.
  414. if(dojo.config.isDebug){
  415. dojo.require("dojo._firebug.firebug");
  416. }
  417. if(dojo.config.debugAtAllCosts){
  418. // this breaks the new AMD based module loader. The XDomain won't be necessary
  419. // anyway if you switch to the asynchronous loader
  420. //dojo.config.useXDomain = true;
  421. //dojo.require("dojo._base._loader.loader_xd");
  422. dojo.require("dojo._base._loader.loader_debug");
  423. dojo.require("dojo.i18n");
  424. }