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

Last change on this file since 256 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: 5.5 KB
RevLine 
[256]1define(["./_base/kernel", "./_base/lang", "./_base/sniff", "./_base/window", "./dom", "./dom-geometry", "./dom-style"], function(dojo, lang, has, baseWindow, dom, geom, style) {
2        // module:
3        //              dojo/window
4        // summary:
5        //              TODOC
6
7lang.getObject("window", true, dojo);
8
9dojo.window.getBox = function(){
10        // summary:
11        //              Returns the dimensions and scroll position of the viewable area of a browser window
12
13        var scrollRoot = (baseWindow.doc.compatMode == 'BackCompat') ? baseWindow.body() : baseWindow.doc.documentElement;
14
15        // get scroll position
16        var scroll = geom.docScroll(); // scrollRoot.scrollTop/Left should work
17
18        var uiWindow = baseWindow.doc.parentWindow || baseWindow.doc.defaultView;   // use UI window, not dojo.global window
19        // dojo.global.innerWidth||dojo.global.innerHeight is for mobile
20        return {
21                l: scroll.x,
22                t: scroll.y,
23                w: uiWindow.innerWidth || scrollRoot.clientWidth,
24                h: uiWindow.innerHeight || scrollRoot.clientHeight
25        };
26};
27
28dojo.window.get = function(doc){
29        // summary:
30        //              Get window object associated with document doc
31
32        // In some IE versions (at least 6.0), document.parentWindow does not return a
33        // reference to the real window object (maybe a copy), so we must fix it as well
34        // We use IE specific execScript to attach the real window reference to
35        // document._parentWindow for later use
36        if(has("ie") && window !== document.parentWindow){
37                /*
38                In IE 6, only the variable "window" can be used to connect events (others
39                may be only copies).
40                */
41                doc.parentWindow.execScript("document._parentWindow = window;", "Javascript");
42                //to prevent memory leak, unset it after use
43                //another possibility is to add an onUnload handler which seems overkill to me (liucougar)
44                var win = doc._parentWindow;
45                doc._parentWindow = null;
46                return win;     //      Window
47        }
48
49        return doc.parentWindow || doc.defaultView;     //      Window
50};
51
52dojo.window.scrollIntoView = function(/*DomNode*/ node, /*Object?*/ pos){
53        // summary:
54        //              Scroll the passed node into view, if it is not already.
55
56        // don't rely on node.scrollIntoView working just because the function is there
57
58        try{ // catch unexpected/unrecreatable errors (#7808) since we can recover using a semi-acceptable native method
59                node = dom.byId(node);
60                var doc = node.ownerDocument || baseWindow.doc,
61                        body = doc.body || baseWindow.body(),
62                        html = doc.documentElement || body.parentNode,
63                        isIE = has("ie"), isWK = has("webkit");
64                // if an untested browser, then use the native method
65                if((!(has("mozilla") || isIE || isWK || has("opera")) || node == body || node == html) && (typeof node.scrollIntoView != "undefined")){
66                        node.scrollIntoView(false); // short-circuit to native if possible
67                        return;
68                }
69                var backCompat = doc.compatMode == 'BackCompat',
70                        clientAreaRoot = (isIE >= 9 && node.ownerDocument.parentWindow.frameElement)
71                                ? ((html.clientHeight > 0 && html.clientWidth > 0 && (body.clientHeight == 0 || body.clientWidth == 0 || body.clientHeight > html.clientHeight || body.clientWidth > html.clientWidth)) ? html : body)
72                                : (backCompat ? body : html),
73                        scrollRoot = isWK ? body : clientAreaRoot,
74                        rootWidth = clientAreaRoot.clientWidth,
75                        rootHeight = clientAreaRoot.clientHeight,
76                        rtl = !geom.isBodyLtr(),
77                        nodePos = pos || geom.position(node),
78                        el = node.parentNode,
79                        isFixed = function(el){
80                                return ((isIE <= 6 || (isIE && backCompat))? false : (style.get(el, 'position').toLowerCase() == "fixed"));
81                        };
82                if(isFixed(node)){ return; } // nothing to do
83
84                while(el){
85                        if(el == body){ el = scrollRoot; }
86                        var elPos = geom.position(el),
87                                fixedPos = isFixed(el);
88
89                        if(el == scrollRoot){
90                                elPos.w = rootWidth; elPos.h = rootHeight;
91                                if(scrollRoot == html && isIE && rtl){ elPos.x += scrollRoot.offsetWidth-elPos.w; } // IE workaround where scrollbar causes negative x
92                                if(elPos.x < 0 || !isIE){ elPos.x = 0; } // IE can have values > 0
93                                if(elPos.y < 0 || !isIE){ elPos.y = 0; }
94                        }else{
95                                var pb = geom.getPadBorderExtents(el);
96                                elPos.w -= pb.w; elPos.h -= pb.h; elPos.x += pb.l; elPos.y += pb.t;
97                                var clientSize = el.clientWidth,
98                                        scrollBarSize = elPos.w - clientSize;
99                                if(clientSize > 0 && scrollBarSize > 0){
100                                        elPos.w = clientSize;
101                                        elPos.x += (rtl && (isIE || el.clientLeft > pb.l/*Chrome*/)) ? scrollBarSize : 0;
102                                }
103                                clientSize = el.clientHeight;
104                                scrollBarSize = elPos.h - clientSize;
105                                if(clientSize > 0 && scrollBarSize > 0){
106                                        elPos.h = clientSize;
107                                }
108                        }
109                        if(fixedPos){ // bounded by viewport, not parents
110                                if(elPos.y < 0){
111                                        elPos.h += elPos.y; elPos.y = 0;
112                                }
113                                if(elPos.x < 0){
114                                        elPos.w += elPos.x; elPos.x = 0;
115                                }
116                                if(elPos.y + elPos.h > rootHeight){
117                                        elPos.h = rootHeight - elPos.y;
118                                }
119                                if(elPos.x + elPos.w > rootWidth){
120                                        elPos.w = rootWidth - elPos.x;
121                                }
122                        }
123                        // calculate overflow in all 4 directions
124                        var l = nodePos.x - elPos.x, // beyond left: < 0
125                                t = nodePos.y - Math.max(elPos.y, 0), // beyond top: < 0
126                                r = l + nodePos.w - elPos.w, // beyond right: > 0
127                                bot = t + nodePos.h - elPos.h; // beyond bottom: > 0
128                        if(r * l > 0){
129                                var s = Math[l < 0? "max" : "min"](l, r);
130                                if(rtl && ((isIE == 8 && !backCompat) || isIE >= 9)){ s = -s; }
131                                nodePos.x += el.scrollLeft;
132                                el.scrollLeft += s;
133                                nodePos.x -= el.scrollLeft;
134                        }
135                        if(bot * t > 0){
136                                nodePos.y += el.scrollTop;
137                                el.scrollTop += Math[t < 0? "max" : "min"](t, bot);
138                                nodePos.y -= el.scrollTop;
139                        }
140                        el = (el != scrollRoot) && !fixedPos && el.parentNode;
141                }
142        }catch(error){
143                console.error('scrollIntoView: ' + error);
144                node.scrollIntoView(false);
145        }
146};
147
148return dojo.window;
149});
Note: See TracBrowser for help on using the repository browser.