window.js 4.4 KB

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