window.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. define("dojo/window", ["./_base/lang", "./_base/sniff", "./_base/window", "./dom", "./dom-geometry", "./dom-style", "./dom-construct"],
  2. function(lang, has, baseWindow, dom, geom, style, domConstruct) {
  3. // feature detection
  4. /* not needed but included here for future reference
  5. has.add("rtl-innerVerticalScrollBar-on-left", function(win, doc){
  6. var body = baseWindow.body(doc),
  7. scrollable = domConstruct.create('div', {
  8. style: {overflow:'scroll', overflowX:'hidden', direction:'rtl', visibility:'hidden', position:'absolute', left:'0', width:'64px', height:'64px'}
  9. }, body, "last"),
  10. center = domConstruct.create('center', {
  11. style: {overflow:'hidden', direction:'ltr'}
  12. }, scrollable, "last"),
  13. inner = domConstruct.create('div', {
  14. style: {overflow:'visible', display:'inline' }
  15. }, center, "last");
  16. inner.innerHTML=" ";
  17. var midPoint = Math.max(inner.offsetLeft, geom.position(inner).x);
  18. var ret = midPoint >= 32;
  19. center.removeChild(inner);
  20. scrollable.removeChild(center);
  21. body.removeChild(scrollable);
  22. return ret;
  23. });
  24. */
  25. has.add("rtl-adjust-position-for-verticalScrollBar", function(win, doc){
  26. var body = baseWindow.body(doc),
  27. scrollable = domConstruct.create('div', {
  28. style: {overflow:'scroll', overflowX:'visible', direction:'rtl', visibility:'hidden', position:'absolute', left:'0', top:'0', width:'64px', height:'64px'}
  29. }, body, "last"),
  30. div = domConstruct.create('div', {
  31. style: {overflow:'hidden', direction:'ltr'}
  32. }, scrollable, "last"),
  33. ret = geom.position(div).x != 0;
  34. scrollable.removeChild(div);
  35. body.removeChild(scrollable);
  36. return ret;
  37. });
  38. has.add("position-fixed-support", function(win, doc){
  39. // IE6, IE7+quirks, and some older mobile browsers don't support position:fixed
  40. var body = baseWindow.body(doc),
  41. outer = domConstruct.create('span', {
  42. style: {visibility:'hidden', position:'fixed', left:'1px', top:'1px'}
  43. }, body, "last"),
  44. inner = domConstruct.create('span', {
  45. style: {position:'fixed', left:'0', top:'0'}
  46. }, outer, "last"),
  47. ret = geom.position(inner).x != geom.position(outer).x;
  48. outer.removeChild(inner);
  49. body.removeChild(outer);
  50. return ret;
  51. });
  52. // module:
  53. // dojo/window
  54. // summary:
  55. // TODOC
  56. var window = lang.getObject("dojo.window", true);
  57. /*=====
  58. dojo.window = {
  59. // summary:
  60. // TODO
  61. };
  62. window = dojo.window;
  63. =====*/
  64. window.getBox = function(){
  65. // summary:
  66. // Returns the dimensions and scroll position of the viewable area of a browser window
  67. var
  68. scrollRoot = (baseWindow.doc.compatMode == 'BackCompat') ? baseWindow.body() : baseWindow.doc.documentElement,
  69. // get scroll position
  70. scroll = geom.docScroll(), // scrollRoot.scrollTop/Left should work
  71. w, h;
  72. if(has("touch")){ // if(scrollbars not supported)
  73. 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
  74. // on mobile, scrollRoot.clientHeight <= uiWindow.innerHeight <= scrollRoot.offsetHeight, return uiWindow.innerHeight
  75. w = uiWindow.innerWidth || scrollRoot.clientWidth; // || scrollRoot.clientXXX probably never evaluated
  76. h = uiWindow.innerHeight || scrollRoot.clientHeight;
  77. }else{
  78. // on desktops, scrollRoot.clientHeight <= scrollRoot.offsetHeight <= uiWindow.innerHeight, return scrollRoot.clientHeight
  79. // uiWindow.innerWidth/Height includes the scrollbar and cannot be used
  80. w = scrollRoot.clientWidth;
  81. h = scrollRoot.clientHeight;
  82. }
  83. return {
  84. l: scroll.x,
  85. t: scroll.y,
  86. w: w,
  87. h: h
  88. };
  89. };
  90. window.get = function(doc){
  91. // summary:
  92. // Get window object associated with document doc
  93. // In some IE versions (at least 6.0), document.parentWindow does not return a
  94. // reference to the real window object (maybe a copy), so we must fix it as well
  95. // We use IE specific execScript to attach the real window reference to
  96. // document._parentWindow for later use
  97. if(has("ie") < 9 && window !== document.parentWindow){
  98. /*
  99. In IE 6, only the variable "window" can be used to connect events (others
  100. may be only copies).
  101. */
  102. doc.parentWindow.execScript("document._parentWindow = window;", "Javascript");
  103. //to prevent memory leak, unset it after use
  104. //another possibility is to add an onUnload handler which seems overkill to me (liucougar)
  105. var win = doc._parentWindow;
  106. doc._parentWindow = null;
  107. return win; // Window
  108. }
  109. return doc.parentWindow || doc.defaultView; // Window
  110. };
  111. window.scrollIntoView = function(/*DomNode*/ node, /*Object?*/ pos){
  112. // summary:
  113. // Scroll the passed node into view using minimal movement, if it is not already.
  114. // Don't rely on node.scrollIntoView working just because the function is there since
  115. // it forces the node to the page's bottom or top (and left or right in IE) without consideration for the minimal movement.
  116. // WebKit's node.scrollIntoViewIfNeeded doesn't work either for inner scrollbars in right-to-left mode
  117. // and when there's a fixed position scrollable element
  118. try{ // catch unexpected/unrecreatable errors (#7808) since we can recover using a semi-acceptable native method
  119. node = dom.byId(node);
  120. var doc = node.ownerDocument || baseWindow.doc, // TODO: why baseWindow.doc? Isn't node.ownerDocument always defined?
  121. body = baseWindow.body(doc),
  122. html = doc.documentElement || body.parentNode,
  123. isIE = has("ie"),
  124. isWK = has("webkit");
  125. // if an untested browser, then use the native method
  126. if(node == body || node == html){ return; }
  127. if(!(has("mozilla") || isIE || isWK || has("opera") || has("trident")) && ("scrollIntoView" in node)){
  128. node.scrollIntoView(false); // short-circuit to native if possible
  129. return;
  130. }
  131. var backCompat = doc.compatMode == 'BackCompat',
  132. rootWidth = Math.min(body.clientWidth || html.clientWidth, html.clientWidth || body.clientWidth),
  133. rootHeight = Math.min(body.clientHeight || html.clientHeight, html.clientHeight || body.clientHeight),
  134. scrollRoot = (isWK || backCompat) ? body : html,
  135. nodePos = pos || geom.position(node),
  136. el = node.parentNode,
  137. isFixed = function(el){
  138. return (isIE <= 6 || (isIE == 7 && backCompat))
  139. ? false
  140. : (has("position-fixed-support") && (style.get(el, 'position').toLowerCase() == "fixed"));
  141. },
  142. self = this,
  143. scrollElementBy = function(el, x, y){
  144. if(el.tagName == "BODY" || el.tagName == "HTML"){
  145. self.get(el.ownerDocument).scrollBy(x, y);
  146. }else{
  147. x && (el.scrollLeft += x);
  148. y && (el.scrollTop += y);
  149. }
  150. };
  151. if(isFixed(node)){ return; } // nothing to do
  152. while(el){
  153. if(el == body){ el = scrollRoot; }
  154. var elPos = geom.position(el),
  155. fixedPos = isFixed(el),
  156. rtl = style.getComputedStyle(el).direction.toLowerCase() == "rtl";
  157. if(el == scrollRoot){
  158. elPos.w = rootWidth; elPos.h = rootHeight;
  159. if(scrollRoot == html && (isIE || has("trident")) && rtl){ elPos.x += scrollRoot.offsetWidth-elPos.w; } // IE workaround where scrollbar causes negative x
  160. if(elPos.x < 0 || !isIE || isIE >= 9 || has("trident")){ elPos.x = 0; } // older IE can have values > 0
  161. if(elPos.y < 0 || !isIE || isIE >= 9 || has("trident")){ elPos.y = 0; }
  162. }else{
  163. var pb = geom.getPadBorderExtents(el);
  164. elPos.w -= pb.w; elPos.h -= pb.h; elPos.x += pb.l; elPos.y += pb.t;
  165. var clientSize = el.clientWidth,
  166. scrollBarSize = elPos.w - clientSize;
  167. if(clientSize > 0 && scrollBarSize > 0){
  168. if(rtl && has("rtl-adjust-position-for-verticalScrollBar")){
  169. elPos.x += scrollBarSize;
  170. }
  171. elPos.w = clientSize;
  172. }
  173. clientSize = el.clientHeight;
  174. scrollBarSize = elPos.h - clientSize;
  175. if(clientSize > 0 && scrollBarSize > 0){
  176. elPos.h = clientSize;
  177. }
  178. }
  179. if(fixedPos){ // bounded by viewport, not parents
  180. if(elPos.y < 0){
  181. elPos.h += elPos.y; elPos.y = 0;
  182. }
  183. if(elPos.x < 0){
  184. elPos.w += elPos.x; elPos.x = 0;
  185. }
  186. if(elPos.y + elPos.h > rootHeight){
  187. elPos.h = rootHeight - elPos.y;
  188. }
  189. if(elPos.x + elPos.w > rootWidth){
  190. elPos.w = rootWidth - elPos.x;
  191. }
  192. }
  193. // calculate overflow in all 4 directions
  194. var l = nodePos.x - elPos.x, // beyond left: < 0
  195. // t = nodePos.y - Math.max(elPos.y, 0), // beyond top: < 0
  196. t = nodePos.y - elPos.y, // beyond top: < 0
  197. r = l + nodePos.w - elPos.w, // beyond right: > 0
  198. bot = t + nodePos.h - elPos.h; // beyond bottom: > 0
  199. var s, old;
  200. if(r * l > 0 && (!!el.scrollLeft || el == scrollRoot || el.scrollWidth > el.offsetHeight)){
  201. s = Math[l < 0? "max" : "min"](l, r);
  202. if(rtl && ((isIE == 8 && !backCompat) || isIE >= 9 || has("trident"))){ s = -s; }
  203. old = el.scrollLeft;
  204. scrollElementBy(el, s, 0);
  205. s = el.scrollLeft - old;
  206. nodePos.x -= s;
  207. }
  208. if(bot * t > 0 && (!!el.scrollTop || el == scrollRoot || el.scrollHeight > el.offsetHeight)){
  209. s = Math.ceil(Math[t < 0? "max" : "min"](t, bot));
  210. old = el.scrollTop;
  211. scrollElementBy(el, 0, s);
  212. s = el.scrollTop - old;
  213. nodePos.y -= s;
  214. }
  215. el = (el != scrollRoot) && !fixedPos && el.parentNode;
  216. }
  217. }catch(error){
  218. console.error('scrollIntoView: ' + error);
  219. node.scrollIntoView(false);
  220. }
  221. };
  222. return window;
  223. });