AutoScroll.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.mdnd.AutoScroll"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.mdnd.AutoScroll"] = true;
  8. dojo.provide("dojox.mdnd.AutoScroll");
  9. dojo.declare(
  10. "dojox.mdnd.AutoScroll",
  11. null,
  12. {
  13. // summary:
  14. // Activate scrolling while dragging a widget.
  15. // interval: Integer
  16. // default mouse move offset
  17. interval: 3,
  18. // recursiveTimer: Integer
  19. recursiveTimer: 10,
  20. // marginMouse: Integer
  21. // Default mouse margin
  22. marginMouse: 50,
  23. constructor: function(){
  24. //console.log("dojox.mdnd.AutoScroll ::: constructor ");
  25. this.resizeHandler = dojo.connect(dojo.global,"onresize", this, function(){
  26. this.getViewport();
  27. });
  28. dojo.ready(dojo.hitch(this, "init"));
  29. },
  30. init: function(){
  31. //console.log("dojox.mdnd.AutoScroll ::: init ");
  32. this._html = (dojo.isWebKit) ? dojo.body() : dojo.body().parentNode;
  33. this.getViewport();
  34. },
  35. getViewport:function(){
  36. // summary:
  37. // Set the visible part of the window. Varies accordion to Navigator.
  38. //console.log("dojox.mdnd.AutoScroll ::: getViewport ");
  39. var d = dojo.doc, dd = d.documentElement, w = window, b = dojo.body();
  40. if(dojo.isMozilla){
  41. this._v = { 'w': dd.clientWidth, 'h': w.innerHeight }; // Object
  42. }
  43. else if(!dojo.isOpera && w.innerWidth){
  44. this._v = { 'w': w.innerWidth, 'h': w.innerHeight }; // Object
  45. }
  46. else if(!dojo.isOpera && dd && dd.clientWidth){
  47. this._v = { 'w': dd.clientWidth, 'h': dd.clientHeight }; // Object
  48. }
  49. else if(b.clientWidth){
  50. this._v = { 'w': b.clientWidth, 'h': b.clientHeight }; // Object
  51. }
  52. },
  53. setAutoScrollNode: function(/*Node*/node){
  54. // summary:
  55. // set the node which is dragged
  56. // node:
  57. // node to scroll
  58. //console.log("dojox.mdnd.AutoScroll ::: setAutoScrollNode ");
  59. this._node = node;
  60. },
  61. setAutoScrollMaxPage: function(){
  62. // summary:
  63. // Set the hightest heigh and width authorized scroll.
  64. //console.log("dojox.mdnd.AutoScroll ::: setAutoScrollMaxPage ");
  65. this._yMax = this._html.scrollHeight;
  66. this._xMax = this._html.scrollWidth;
  67. },
  68. checkAutoScroll: function(/*Event*/e){
  69. // summary:
  70. // Check if an autoScroll have to be launched.
  71. //console.log("dojox.mdnd.AutoScroll ::: checkAutoScroll");
  72. if(this._autoScrollActive){
  73. this.stopAutoScroll();
  74. }
  75. this._y = e.pageY;
  76. this._x = e.pageX;
  77. if(e.clientX < this.marginMouse){
  78. this._autoScrollActive = true;
  79. this._autoScrollLeft(e);
  80. }
  81. else if(e.clientX > this._v.w - this.marginMouse){
  82. this._autoScrollActive = true;
  83. this._autoScrollRight(e);
  84. }
  85. if(e.clientY < this.marginMouse){
  86. this._autoScrollActive = true;
  87. this._autoScrollUp(e);
  88. }
  89. else if(e.clientY > this._v.h - this.marginMouse){
  90. this._autoScrollActive = true;
  91. this._autoScrollDown();
  92. }
  93. },
  94. _autoScrollDown: function(){
  95. // summary:
  96. // Manage the down autoscroll.
  97. // tags:
  98. // protected
  99. //console.log("dojox.mdnd.AutoScroll ::: _autoScrollDown ");
  100. if(this._timer){
  101. clearTimeout(this._timer);
  102. }
  103. if(this._autoScrollActive && this._y + this.marginMouse < this._yMax){
  104. this._html.scrollTop += this.interval;
  105. this._node.style.top = (parseInt(this._node.style.top) + this.interval) + "px";
  106. this._y += this.interval;
  107. this._timer = setTimeout(dojo.hitch(this, "_autoScrollDown"), this.recursiveTimer);
  108. }
  109. },
  110. _autoScrollUp: function(){
  111. // summary:
  112. // Manage the up autoscroll.
  113. // tags:
  114. // protected
  115. //console.log("dojox.mdnd.AutoScroll ::: _autoScrollUp ");
  116. if(this._timer){
  117. clearTimeout(this._timer);
  118. }
  119. if(this._autoScrollActive && this._y - this.marginMouse > 0){
  120. this._html.scrollTop -= this.interval;
  121. this._node.style.top = (parseInt(this._node.style.top) - this.interval) + "px";
  122. this._y -= this.interval;
  123. this._timer = setTimeout(dojo.hitch(this, "_autoScrollUp"),this.recursiveTimer);
  124. }
  125. },
  126. _autoScrollRight: function(){
  127. // summary:
  128. // Manage the right autoscroll.
  129. // tags:
  130. // protected
  131. //console.log("dojox.mdnd.AutoScroll ::: _autoScrollRight ");
  132. if(this._timer){
  133. clearTimeout(this._timer);
  134. }
  135. if(this._autoScrollActive && this._x + this.marginMouse < this._xMax){
  136. this._html.scrollLeft += this.interval;
  137. this._node.style.left = (parseInt(this._node.style.left) + this.interval) + "px";
  138. this._x += this.interval;
  139. this._timer = setTimeout(dojo.hitch(this, "_autoScrollRight"), this.recursiveTimer);
  140. }
  141. },
  142. _autoScrollLeft: function(/*Event*/e){
  143. // summary:
  144. // Manage the left autoscroll.
  145. // tags:
  146. // protected
  147. //console.log("dojox.mdnd.AutoScroll ::: _autoScrollLeft ");
  148. if(this._timer){
  149. clearTimeout(this._timer);
  150. }
  151. if(this._autoScrollActive && this._x - this.marginMouse > 0){
  152. this._html.scrollLeft -= this.interval;
  153. this._node.style.left = (parseInt(this._node.style.left) - this.interval) + "px";
  154. this._x -= this.interval;
  155. this._timer = setTimeout(dojo.hitch(this, "_autoScrollLeft"),this.recursiveTimer);
  156. }
  157. },
  158. stopAutoScroll: function(){
  159. // summary:
  160. // Stop the autoscroll.
  161. //console.log("dojox.mdnd.AutoScroll ::: stopAutoScroll ");
  162. if(this._timer){
  163. clearTimeout(this._timer);
  164. }
  165. this._autoScrollActive = false;
  166. },
  167. destroy: function(){
  168. //console.log("dojox.mdnd.AutoScroll ::: destroy ");
  169. dojo.disconnect(this.resizeHandler);
  170. }
  171. });
  172. dojox.mdnd.autoScroll = null;
  173. (function(){
  174. dojox.mdnd.autoScroll = new dojox.mdnd.AutoScroll();
  175. }());
  176. }