window.js 4.1 KB

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