| 1 |
define(["./_base/lang", "./sniff", "./_base/window", "./dom", "./dom-geometry", "./dom-style", "./dom-construct"], |
| 2 |
function(lang, has, baseWindow, dom, geom, style, domConstruct){ |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
has.add("rtl-adjust-position-for-verticalScrollBar", function(win, doc){ |
| 27 |
var body = baseWindow.body(doc), |
| 28 |
scrollable = domConstruct.create('div', { |
| 29 |
style: {overflow:'scroll', overflowX:'visible', direction:'rtl', visibility:'hidden', position:'absolute', left:'0', top:'0', width:'64px', height:'64px'} |
| 30 |
}, body, "last"), |
| 31 |
div = domConstruct.create('div', { |
| 32 |
style: {overflow:'hidden', direction:'ltr'} |
| 33 |
}, scrollable, "last"), |
| 34 |
ret = geom.position(div).x != 0; |
| 35 |
scrollable.removeChild(div); |
| 36 |
body.removeChild(scrollable); |
| 37 |
return ret; |
| 38 |
}); |
| 39 |
|
| 40 |
has.add("position-fixed-support", function(win, doc){ |
| 41 |
|
| 42 |
var body = baseWindow.body(doc), |
| 43 |
outer = domConstruct.create('span', { |
| 44 |
style: {visibility:'hidden', position:'fixed', left:'1px', top:'1px'} |
| 45 |
}, body, "last"), |
| 46 |
inner = domConstruct.create('span', { |
| 47 |
style: {position:'fixed', left:'0', top:'0'} |
| 48 |
}, outer, "last"), |
| 49 |
ret = geom.position(inner).x != geom.position(outer).x; |
| 50 |
outer.removeChild(inner); |
| 51 |
body.removeChild(outer); |
| 52 |
return ret; |
| 53 |
}); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
var window = { |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
getBox: function( doc){ |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
doc = doc || baseWindow.doc; |
| 67 |
|
| 68 |
var |
| 69 |
scrollRoot = (doc.compatMode == 'BackCompat') ? baseWindow.body(doc) : doc.documentElement, |
| 70 |
|
| 71 |
scroll = geom.docScroll(doc), |
| 72 |
w, h; |
| 73 |
|
| 74 |
if(has("touch")){ |
| 75 |
var uiWindow = window.get(doc); |
| 76 |
|
| 77 |
w = uiWindow.innerWidth || scrollRoot.clientWidth; |
| 78 |
h = uiWindow.innerHeight || scrollRoot.clientHeight; |
| 79 |
}else{ |
| 80 |
|
| 81 |
|
| 82 |
w = scrollRoot.clientWidth; |
| 83 |
h = scrollRoot.clientHeight; |
| 84 |
} |
| 85 |
return { |
| 86 |
l: scroll.x, |
| 87 |
t: scroll.y, |
| 88 |
w: w, |
| 89 |
h: h |
| 90 |
}; |
| 91 |
}, |
| 92 |
|
| 93 |
get: function( doc){ |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
if(has("ie") && window !== document.parentWindow){ |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
doc.parentWindow.execScript("document._parentWindow = window;", "Javascript"); |
| 109 |
|
| 110 |
|
| 111 |
var win = doc._parentWindow; |
| 112 |
doc._parentWindow = null; |
| 113 |
return win; |
| 114 |
} |
| 115 |
|
| 116 |
return doc.parentWindow || doc.defaultView; |
| 117 |
}, |
| 118 |
|
| 119 |
scrollIntoView: function( node, pos){ |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
try{ |
| 129 |
node = dom.byId(node); |
| 130 |
var doc = node.ownerDocument || baseWindow.doc, |
| 131 |
body = baseWindow.body(doc), |
| 132 |
html = doc.documentElement || body.parentNode, |
| 133 |
isIE = has("ie") || has("trident"), |
| 134 |
isWK = has("webkit"); |
| 135 |
|
| 136 |
if(node == body || node == html){ return; } |
| 137 |
if(!(has("mozilla") || isIE || isWK || has("opera") || has("trident") || has("edge")) |
| 138 |
&& ("scrollIntoView" in node)){ |
| 139 |
node.scrollIntoView(false); |
| 140 |
return; |
| 141 |
} |
| 142 |
var backCompat = doc.compatMode == 'BackCompat', |
| 143 |
rootWidth = Math.min(body.clientWidth || html.clientWidth, html.clientWidth || body.clientWidth), |
| 144 |
rootHeight = Math.min(body.clientHeight || html.clientHeight, html.clientHeight || body.clientHeight), |
| 145 |
scrollRoot = (isWK || backCompat) ? body : html, |
| 146 |
nodePos = pos || geom.position(node), |
| 147 |
el = node.parentNode, |
| 148 |
isFixed = function(el){ |
| 149 |
return (isIE <= 6 || (isIE == 7 && backCompat)) |
| 150 |
? false |
| 151 |
: (has("position-fixed-support") && (style.get(el, 'position').toLowerCase() == "fixed")); |
| 152 |
}, |
| 153 |
self = this, |
| 154 |
scrollElementBy = function(el, x, y){ |
| 155 |
if(el.tagName == "BODY" || el.tagName == "HTML"){ |
| 156 |
self.get(el.ownerDocument).scrollBy(x, y); |
| 157 |
}else{ |
| 158 |
x && (el.scrollLeft += x); |
| 159 |
y && (el.scrollTop += y); |
| 160 |
} |
| 161 |
}; |
| 162 |
if(isFixed(node)){ return; } |
| 163 |
while(el){ |
| 164 |
if(el == body){ el = scrollRoot; } |
| 165 |
var elPos = geom.position(el), |
| 166 |
fixedPos = isFixed(el), |
| 167 |
rtl = style.getComputedStyle(el).direction.toLowerCase() == "rtl"; |
| 168 |
|
| 169 |
if(el == scrollRoot){ |
| 170 |
elPos.w = rootWidth; elPos.h = rootHeight; |
| 171 |
if(scrollRoot == html && (isIE || has("trident")) && rtl){ |
| 172 |
elPos.x += scrollRoot.offsetWidth-elPos.w; |
| 173 |
} |
| 174 |
elPos.x = 0; |
| 175 |
elPos.y = 0; |
| 176 |
}else{ |
| 177 |
var pb = geom.getPadBorderExtents(el); |
| 178 |
elPos.w -= pb.w; elPos.h -= pb.h; elPos.x += pb.l; elPos.y += pb.t; |
| 179 |
var clientSize = el.clientWidth, |
| 180 |
scrollBarSize = elPos.w - clientSize; |
| 181 |
if(clientSize > 0 && scrollBarSize > 0){ |
| 182 |
if(rtl && has("rtl-adjust-position-for-verticalScrollBar")){ |
| 183 |
elPos.x += scrollBarSize; |
| 184 |
} |
| 185 |
elPos.w = clientSize; |
| 186 |
} |
| 187 |
clientSize = el.clientHeight; |
| 188 |
scrollBarSize = elPos.h - clientSize; |
| 189 |
if(clientSize > 0 && scrollBarSize > 0){ |
| 190 |
elPos.h = clientSize; |
| 191 |
} |
| 192 |
} |
| 193 |
if(fixedPos){ |
| 194 |
if(elPos.y < 0){ |
| 195 |
elPos.h += elPos.y; elPos.y = 0; |
| 196 |
} |
| 197 |
if(elPos.x < 0){ |
| 198 |
elPos.w += elPos.x; elPos.x = 0; |
| 199 |
} |
| 200 |
if(elPos.y + elPos.h > rootHeight){ |
| 201 |
elPos.h = rootHeight - elPos.y; |
| 202 |
} |
| 203 |
if(elPos.x + elPos.w > rootWidth){ |
| 204 |
elPos.w = rootWidth - elPos.x; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
var l = nodePos.x - elPos.x, |
| 209 |
|
| 210 |
t = nodePos.y - elPos.y, |
| 211 |
r = l + nodePos.w - elPos.w, |
| 212 |
bot = t + nodePos.h - elPos.h; |
| 213 |
var s, old; |
| 214 |
if(r * l > 0 && (!!el.scrollLeft || el == scrollRoot || el.scrollWidth > el.offsetHeight)){ |
| 215 |
s = Math[l < 0? "max" : "min"](l, r); |
| 216 |
if(rtl && ((isIE == 8 && !backCompat) || has("trident") >= 5)){ s = -s; } |
| 217 |
old = el.scrollLeft; |
| 218 |
scrollElementBy(el, s, 0); |
| 219 |
s = el.scrollLeft - old; |
| 220 |
nodePos.x -= s; |
| 221 |
} |
| 222 |
if(bot * t > 0 && (!!el.scrollTop || el == scrollRoot || el.scrollHeight > el.offsetHeight)){ |
| 223 |
s = Math.ceil(Math[t < 0? "max" : "min"](t, bot)); |
| 224 |
old = el.scrollTop; |
| 225 |
scrollElementBy(el, 0, s); |
| 226 |
s = el.scrollTop - old; |
| 227 |
nodePos.y -= s; |
| 228 |
} |
| 229 |
el = (el != scrollRoot) && !fixedPos && el.parentNode; |
| 230 |
} |
| 231 |
}catch(error){ |
| 232 |
console.error('scrollIntoView: ' + error); |
| 233 |
node.scrollIntoView(false); |
| 234 |
} |
| 235 |
} |
| 236 |
}; |
| 237 |
|
| 238 |
has("extend-dojo") && lang.setObject("dojo.window", window); |
| 239 |
|
| 240 |
return window; |
| 241 |
}); |