source: Dev/branches/rest-dojo-ui/client/dojo/_base/window.js @ 263

Last change on this file since 263 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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