]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojo/_base/_loader/hostenv_browser.js
Comment class stub
[eow] / static / dojo-release-1.1.1 / dojo / _base / _loader / hostenv_browser.js
1 /*=====
2 dojo.isBrowser = {
3         //      example:
4         //      |       if(dojo.isBrowser){ ... }
5 };
6
7 dojo.isFF = {
8         //      example:
9         //      |       if(dojo.isFF > 1){ ... }
10 };
11
12 dojo.isIE = {
13         // example:
14         //      |       if(dojo.isIE > 6){
15         //      |               // we are IE7
16         //      |       }
17 };
18
19 dojo.isSafari = {
20         //      example:
21         //      |       if(dojo.isSafari){ ... }
22         //      example: 
23         //              Detect iPhone:
24         //      |       if(dojo.isSafari && (navigator.userAgent.indexOf("iPhone") < 0)){ 
25         //      |               // we are iPhone. iPod touch reports "iPod" above
26         //      |       }
27 };
28
29 dojo = {
30         // isBrowser: Boolean
31         //              True if the client is a web-browser
32         isBrowser: true,
33         //      isFF: Number
34         //              Greater than zero if client is FireFox. 0 otherwise. Corresponds to
35         //              major detected FireFox version (1.5, 2, 3, etc.)
36         isFF: 2,
37         //      isIE: Number
38         //              Greater than zero if client is MSIE(PC). 0 otherwise. Corresponds to
39         //              major detected IE version (6, 7, 8, etc.)
40         isIE: 6,
41         //      isKhtml: Number
42         //              Greater than zero if client is a KTHML-derived browser (Konqueror,
43         //              Safari, etc.). 0 otherwise. Corresponds to major detected version.
44         isKhtml: 0,
45         //      isMozilla: Number
46         //              Greater than zero if client is a Mozilla-based browser (Firefox,
47         //              SeaMonkey). 0 otherwise. Corresponds to major detected version.
48         isMozilla: 0,
49         //      isOpera: Number
50         //              Greater than zero if client is Opera. 0 otherwise. Corresponds to
51         //              major detected version.
52         isOpera: 0,
53         //      isSafari: Number
54         //              Greater than zero if client is Safari or iPhone. 0 otherwise.
55         isSafari: 0
56 }
57 =====*/
58
59 if(typeof window != 'undefined'){
60         dojo.isBrowser = true;
61         dojo._name = "browser";
62
63
64         // attempt to figure out the path to dojo if it isn't set in the config
65         (function(){
66                 var d = dojo;
67                 // this is a scope protection closure. We set browser versions and grab
68                 // the URL we were loaded from here.
69
70                 // grab the node we were loaded from
71                 if(document && document.getElementsByTagName){
72                         var scripts = document.getElementsByTagName("script");
73                         var rePkg = /dojo(\.xd)?\.js(\W|$)/i;
74                         for(var i = 0; i < scripts.length; i++){
75                                 var src = scripts[i].getAttribute("src");
76                                 if(!src){ continue; }
77                                 var m = src.match(rePkg);
78                                 if(m){
79                                         // find out where we came from
80                                         if(!d.config.baseUrl){
81                                                 d.config.baseUrl = src.substring(0, m.index);
82                                         }
83                                         // and find out if we need to modify our behavior
84                                         var cfg = scripts[i].getAttribute("djConfig");
85                                         if(cfg){
86                                                 var cfgo = eval("({ "+cfg+" })");
87                                                 for(var x in cfgo){
88                                                         dojo.config[x] = cfgo[x];
89                                                 }
90                                         }
91                                         break; // "first Dojo wins"
92                                 }
93                         }
94                 }
95                 d.baseUrl = d.config.baseUrl;
96
97                 // fill in the rendering support information in dojo.render.*
98                 var n = navigator;
99                 var dua = n.userAgent;
100                 var dav = n.appVersion;
101                 var tv = parseFloat(dav);
102
103                 d.isOpera = (dua.indexOf("Opera") >= 0) ? tv : 0;
104                 // safari detection derived from:
105                 //              http://developer.apple.com/internet/safari/faq.html#anchor2
106                 //              http://developer.apple.com/internet/safari/uamatrix.html
107                 var idx = Math.max(dav.indexOf("WebKit"), dav.indexOf("Safari"), 0);
108                 if(idx){
109                         // try to grab the explicit Safari version first. If we don't get
110                         // one, look for 419.3+ as the indication that we're on something
111                         // "Safari 3-ish". Lastly, default to "Safari 2" handling.
112                         d.isSafari = parseFloat(dav.split("Version/")[1]) || ( ( parseFloat(dav.substr(idx+7)) >= 419.3 ) ? 3 : 2 ) || 2;
113                 }
114                 d.isAIR = (dua.indexOf("AdobeAIR") >= 0) ? 1 : 0;
115                 d.isKhtml = (dav.indexOf("Konqueror") >= 0 || d.isSafari) ? tv : 0;
116                 d.isMozilla = d.isMoz = (dua.indexOf("Gecko") >= 0 && !d.isKhtml) ? tv : 0;
117                 d.isFF = d.isIE = 0;
118                 if(d.isMoz){
119                         d.isFF = parseFloat(dua.split("Firefox/")[1]) || 0;
120                 }
121                 if(document.all && !d.isOpera){
122                         d.isIE = parseFloat(dav.split("MSIE ")[1]) || 0;
123                 }
124
125                 //Workaround to get local file loads of dojo to work on IE 7
126                 //by forcing to not use native xhr.
127                 if(dojo.isIE && window.location.protocol === "file:"){
128                         dojo.config.ieForceActiveXXhr=true;
129                 }
130
131                 var cm = document.compatMode;
132                 d.isQuirks = cm == "BackCompat" || cm == "QuirksMode" || d.isIE < 6;
133
134                 // TODO: is the HTML LANG attribute relevant?
135                 d.locale = dojo.config.locale || (d.isIE ? n.userLanguage : n.language).toLowerCase();
136
137                 // These are in order of decreasing likelihood; this will change in time.
138                 d._XMLHTTP_PROGIDS = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
139
140                 d._xhrObj = function(){
141                         // summary: 
142                         //              does the work of portably generating a new XMLHTTPRequest
143                         //              object.
144                         var http = null;
145                         var last_e = null;
146                         if(!dojo.isIE || !dojo.config.ieForceActiveXXhr){
147                                 try{ http = new XMLHttpRequest(); }catch(e){}
148                         }
149                         if(!http){
150                                 for(var i=0; i<3; ++i){
151                                         var progid = d._XMLHTTP_PROGIDS[i];
152                                         try{
153                                                 http = new ActiveXObject(progid);
154                                         }catch(e){
155                                                 last_e = e;
156                                         }
157
158                                         if(http){
159                                                 d._XMLHTTP_PROGIDS = [progid];  // so faster next time
160                                                 break;
161                                         }
162                                 }
163                         }
164
165                         if(!http){
166                                 throw new Error("XMLHTTP not available: "+last_e);
167                         }
168
169                         return http; // XMLHTTPRequest instance
170                 }
171
172                 d._isDocumentOk = function(http){
173                         var stat = http.status || 0;
174                         return (stat >= 200 && stat < 300) ||   // Boolean
175                                 stat == 304 ||                                          // allow any 2XX response code
176                                 stat == 1223 ||                                                 // get it out of the cache
177                                 (!stat && (location.protocol=="file:" || location.protocol=="chrome:") ); // Internet Explorer mangled the status code
178                 }
179
180                 //See if base tag is in use.
181                 //This is to fix http://trac.dojotoolkit.org/ticket/3973,
182                 //but really, we need to find out how to get rid of the dojo._Url reference
183                 //below and still have DOH work with the dojo.i18n test following some other
184                 //test that uses the test frame to load a document (trac #2757).
185                 //Opera still has problems, but perhaps a larger issue of base tag support
186                 //with XHR requests (hasBase is true, but the request is still made to document
187                 //path, not base path).
188                 var owloc = window.location+"";
189                 var base = document.getElementsByTagName("base");
190                 var hasBase = (base && base.length > 0);
191
192                 d._getText = function(/*URI*/ uri, /*Boolean*/ fail_ok){
193                         // summary: Read the contents of the specified uri and return those contents.
194                         // uri:
195                         //              A relative or absolute uri. If absolute, it still must be in
196                         //              the same "domain" as we are.
197                         // fail_ok:
198                         //              Default false. If fail_ok and loading fails, return null
199                         //              instead of throwing.
200                         // returns: The response text. null is returned when there is a
201                         //              failure and failure is okay (an exception otherwise)
202
203                         // alert("_getText: " + uri);
204
205                         // NOTE: must be declared before scope switches ie. this._xhrObj()
206                         var http = this._xhrObj();
207
208                         if(!hasBase && dojo._Url){
209                                 uri = (new dojo._Url(owloc, uri)).toString();
210                         }
211                         /*
212                         console.debug("_getText:", uri);
213                         console.debug(window.location+"");
214                         alert(uri);
215                         */
216
217                         if(d.config.cacheBust){
218                                 uri += (uri.indexOf("?") == -1 ? "?" : "&") + String(d.config.cacheBust).replace(/\W+/g,"");
219                         }
220
221                         http.open('GET', uri, false);
222                         try{
223                                 http.send(null);
224                                 // alert(http);
225                                 if(!d._isDocumentOk(http)){
226                                         var err = Error("Unable to load "+uri+" status:"+ http.status);
227                                         err.status = http.status;
228                                         err.responseText = http.responseText;
229                                         throw err;
230                                 }
231                         }catch(e){
232                                 if(fail_ok){ return null; } // null
233                                 // rethrow the exception
234                                 throw e;
235                         }
236                         return http.responseText; // String
237                 }
238         })();
239
240         dojo._initFired = false;
241         //      BEGIN DOMContentLoaded, from Dean Edwards (http://dean.edwards.name/weblog/2006/06/again/)
242         dojo._loadInit = function(e){
243                 dojo._initFired = true;
244                 // allow multiple calls, only first one will take effect
245                 // A bug in khtml calls events callbacks for document for event which isnt supported
246                 // for example a created contextmenu event calls DOMContentLoaded, workaround
247                 var type = (e && e.type) ? e.type.toLowerCase() : "load";
248                 if(arguments.callee.initialized || (type != "domcontentloaded" && type != "load")){ return; }
249                 arguments.callee.initialized = true;
250                 if("_khtmlTimer" in dojo){
251                         clearInterval(dojo._khtmlTimer);
252                         delete dojo._khtmlTimer;
253                 }
254
255                 if(dojo._inFlightCount == 0){
256                         dojo._modulesLoaded();
257                 }
258         }
259
260         dojo._fakeLoadInit = function(){
261                 dojo._loadInit({type: "load"});
262         }
263
264         if(!dojo.config.afterOnLoad){
265                 //      START DOMContentLoaded
266                 // Mozilla and Opera 9 expose the event we could use
267                 if(document.addEventListener){
268                         // NOTE: 
269                         //              due to a threading issue in Firefox 2.0, we can't enable
270                         //              DOMContentLoaded on that platform. For more information, see:
271                         //              http://trac.dojotoolkit.org/ticket/1704
272                         if(dojo.isOpera || dojo.isFF >= 3 || (dojo.isMoz && dojo.config.enableMozDomContentLoaded === true)){
273                                 document.addEventListener("DOMContentLoaded", dojo._loadInit, null);
274                         }
275         
276                         //      mainly for Opera 8.5, won't be fired if DOMContentLoaded fired already.
277                         //  also used for Mozilla because of trac #1640
278                         window.addEventListener("load", dojo._loadInit, null);
279                 }
280         
281                 if(dojo.isAIR){
282                         window.addEventListener("load", dojo._loadInit, null);
283                 }else if(/(WebKit|khtml)/i.test(navigator.userAgent)){ // sniff
284                         dojo._khtmlTimer = setInterval(function(){
285                                 if(/loaded|complete/.test(document.readyState)){
286                                         dojo._loadInit(); // call the onload handler
287                                 }
288                         }, 10);
289                 }
290                 //      END DOMContentLoaded
291         }
292
293         (function(){
294                 var _w = window;
295                 var _handleNodeEvent = function(/*String*/evtName, /*Function*/fp){
296                         // summary:
297                         //              non-destructively adds the specified function to the node's
298                         //              evtName handler.
299                         // evtName: should be in the form "onclick" for "onclick" handlers.
300                         // Make sure you pass in the "on" part.
301                         var oldHandler = _w[evtName] || function(){};
302                         _w[evtName] = function(){
303                                 fp.apply(_w, arguments);
304                                 oldHandler.apply(_w, arguments);
305                         };
306                 };
307
308                 if(dojo.isIE){
309                         //      for Internet Explorer. readyState will not be achieved on init
310                         //      call, but dojo doesn't need it however, we'll include it
311                         //      because we don't know if there are other functions added that
312                         //      might.  Note that this has changed because the build process
313                         //      strips all comments -- including conditional ones.
314                         if(!dojo.config.afterOnLoad){
315                                 document.write('<scr'+'ipt defer src="//:" '
316                                         + 'onreadystatechange="if(this.readyState==\'complete\'){' + dojo._scopeName + '._loadInit();}">'
317                                         + '</scr'+'ipt>'
318                                 );
319                         }
320
321                         // IE WebControl hosted in an application can fire "beforeunload" and "unload"
322                         // events when control visibility changes, causing Dojo to unload too soon. The
323                         // following code fixes the problem
324                         // Reference: http://support.microsoft.com/default.aspx?scid=kb;en-us;199155
325                         var _unloading = true;
326                         _handleNodeEvent("onbeforeunload", function(){
327                                 _w.setTimeout(function(){ _unloading = false; }, 0);
328                         });
329                         _handleNodeEvent("onunload", function(){
330                                 if(_unloading){ dojo.unloaded(); }
331                         });
332
333                         try{
334                                 document.namespaces.add("v","urn:schemas-microsoft-com:vml");
335                                 document.createStyleSheet().addRule("v\\:*", "behavior:url(#default#VML)");
336                         }catch(e){}
337                 }else{
338                         // FIXME: dojo.unloaded requires dojo scope, so using anon function wrapper.
339                         _handleNodeEvent("onbeforeunload", function() { dojo.unloaded(); });
340                 }
341
342         })();
343
344         /*
345         OpenAjax.subscribe("OpenAjax", "onload", function(){
346                 if(dojo._inFlightCount == 0){
347                         dojo._modulesLoaded();
348                 }
349         });
350
351         OpenAjax.subscribe("OpenAjax", "onunload", function(){
352                 dojo.unloaded();
353         });
354         */
355 } //if (typeof window != 'undefined')
356
357 //Register any module paths set up in djConfig. Need to do this
358 //in the hostenvs since hostenv_browser can read djConfig from a
359 //script tag's attribute.
360 (function(){
361         var mp = dojo.config["modulePaths"];
362         if(mp){
363                 for(var param in mp){
364                         dojo.registerModulePath(param, mp[param]);
365                 }
366         }
367 })();
368
369 //Load debug code if necessary.
370 if(dojo.config.isDebug){
371         dojo.require("dojo._firebug.firebug");
372 }
373
374 if(dojo.config.debugAtAllCosts){
375         dojo.config.useXDomain = true;
376         dojo.require("dojo._base._loader.loader_xd");
377         dojo.require("dojo._base._loader.loader_debug");
378         dojo.require("dojo.i18n");
379 }