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