[483] | 1 | define(["./kernel", "./lang", "../sniff"], function(dojo, lang, has){ |
---|
| 2 | // module: |
---|
| 3 | // dojo/_base/window |
---|
| 4 | |
---|
| 5 | var ret = { |
---|
| 6 | // summary: |
---|
| 7 | // API to save/set/restore the global/document scope. |
---|
| 8 | |
---|
| 9 | global: dojo.global, |
---|
| 10 | /*===== |
---|
| 11 | global: { |
---|
| 12 | // summary: |
---|
| 13 | // Alias for the current window. 'global' can be modified |
---|
| 14 | // for temporary context shifting. See also withGlobal(). |
---|
| 15 | // description: |
---|
| 16 | // Use this rather than referring to 'window' to ensure your code runs |
---|
| 17 | // correctly in managed contexts. |
---|
| 18 | }, |
---|
| 19 | =====*/ |
---|
| 20 | |
---|
| 21 | doc: this["document"] || null, |
---|
| 22 | /*===== |
---|
| 23 | doc: { |
---|
| 24 | // summary: |
---|
| 25 | // Alias for the current document. 'doc' can be modified |
---|
| 26 | // for temporary context shifting. See also withDoc(). |
---|
| 27 | // description: |
---|
| 28 | // Use this rather than referring to 'window.document' to ensure your code runs |
---|
| 29 | // correctly in managed contexts. |
---|
| 30 | // example: |
---|
| 31 | // | n.appendChild(dojo.doc.createElement('div')); |
---|
| 32 | }, |
---|
| 33 | =====*/ |
---|
| 34 | |
---|
| 35 | body: function(/*Document?*/ doc){ |
---|
| 36 | // summary: |
---|
| 37 | // Return the body element of the specified document or of dojo/_base/window::doc. |
---|
| 38 | // example: |
---|
| 39 | // | win.body().appendChild(dojo.doc.createElement('div')); |
---|
| 40 | |
---|
| 41 | // Note: document.body is not defined for a strict xhtml document |
---|
| 42 | // Would like to memoize this, but dojo.doc can change vi dojo.withDoc(). |
---|
| 43 | doc = doc || dojo.doc; |
---|
| 44 | return doc.body || doc.getElementsByTagName("body")[0]; // Node |
---|
| 45 | }, |
---|
| 46 | |
---|
| 47 | setContext: function(/*Object*/ globalObject, /*DocumentElement*/ globalDocument){ |
---|
| 48 | // summary: |
---|
| 49 | // changes the behavior of many core Dojo functions that deal with |
---|
| 50 | // namespace and DOM lookup, changing them to work in a new global |
---|
| 51 | // context (e.g., an iframe). The varibles dojo.global and dojo.doc |
---|
| 52 | // are modified as a result of calling this function and the result of |
---|
| 53 | // `dojo.body()` likewise differs. |
---|
| 54 | dojo.global = ret.global = globalObject; |
---|
| 55 | dojo.doc = ret.doc = globalDocument; |
---|
| 56 | }, |
---|
| 57 | |
---|
| 58 | withGlobal: function( /*Object*/ globalObject, |
---|
| 59 | /*Function*/ callback, |
---|
| 60 | /*Object?*/ thisObject, |
---|
| 61 | /*Array?*/ cbArguments){ |
---|
| 62 | // summary: |
---|
| 63 | // Invoke callback with globalObject as dojo.global and |
---|
| 64 | // globalObject.document as dojo.doc. |
---|
| 65 | // description: |
---|
| 66 | // Invoke callback with globalObject as dojo.global and |
---|
| 67 | // globalObject.document as dojo.doc. If provided, globalObject |
---|
| 68 | // will be executed in the context of object thisObject |
---|
| 69 | // When callback() returns or throws an error, the dojo.global |
---|
| 70 | // and dojo.doc will be restored to its previous state. |
---|
| 71 | |
---|
| 72 | var oldGlob = dojo.global; |
---|
| 73 | try{ |
---|
| 74 | dojo.global = ret.global = globalObject; |
---|
| 75 | return ret.withDoc.call(null, globalObject.document, callback, thisObject, cbArguments); |
---|
| 76 | }finally{ |
---|
| 77 | dojo.global = ret.global = oldGlob; |
---|
| 78 | } |
---|
| 79 | }, |
---|
| 80 | |
---|
| 81 | withDoc: function( /*DocumentElement*/ documentObject, |
---|
| 82 | /*Function*/ callback, |
---|
| 83 | /*Object?*/ thisObject, |
---|
| 84 | /*Array?*/ cbArguments){ |
---|
| 85 | // summary: |
---|
| 86 | // Invoke callback with documentObject as dojo/_base/window::doc. |
---|
| 87 | // description: |
---|
| 88 | // Invoke callback with documentObject as dojo/_base/window::doc. If provided, |
---|
| 89 | // callback will be executed in the context of object thisObject |
---|
| 90 | // When callback() returns or throws an error, the dojo/_base/window::doc will |
---|
| 91 | // be restored to its previous state. |
---|
| 92 | |
---|
| 93 | var oldDoc = ret.doc, |
---|
| 94 | oldQ = has("quirks"), |
---|
| 95 | oldIE = has("ie"), isIE, mode, pwin; |
---|
| 96 | |
---|
| 97 | try{ |
---|
| 98 | dojo.doc = ret.doc = documentObject; |
---|
| 99 | // update dojo.isQuirks and the value of the has feature "quirks". |
---|
| 100 | // remove setting dojo.isQuirks and dojo.isIE for 2.0 |
---|
| 101 | dojo.isQuirks = has.add("quirks", dojo.doc.compatMode == "BackCompat", true, true); // no need to check for QuirksMode which was Opera 7 only |
---|
| 102 | |
---|
| 103 | if(has("ie")){ |
---|
| 104 | if((pwin = documentObject.parentWindow) && pwin.navigator){ |
---|
| 105 | // re-run IE detection logic and update dojo.isIE / has("ie") |
---|
| 106 | // (the only time parentWindow/navigator wouldn't exist is if we were not |
---|
| 107 | // passed an actual legitimate document object) |
---|
| 108 | isIE = parseFloat(pwin.navigator.appVersion.split("MSIE ")[1]) || undefined; |
---|
| 109 | mode = documentObject.documentMode; |
---|
| 110 | if(mode && mode != 5 && Math.floor(isIE) != mode){ |
---|
| 111 | isIE = mode; |
---|
| 112 | } |
---|
| 113 | dojo.isIE = has.add("ie", isIE, true, true); |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | if(thisObject && typeof callback == "string"){ |
---|
| 118 | callback = thisObject[callback]; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | return callback.apply(thisObject, cbArguments || []); |
---|
| 122 | }finally{ |
---|
| 123 | dojo.doc = ret.doc = oldDoc; |
---|
| 124 | dojo.isQuirks = has.add("quirks", oldQ, true, true); |
---|
| 125 | dojo.isIE = has.add("ie", oldIE, true, true); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | }; |
---|
| 129 | |
---|
| 130 | has("extend-dojo") && lang.mixin(dojo, ret); |
---|
| 131 | |
---|
| 132 | return ret; |
---|
| 133 | |
---|
| 134 | }); |
---|