AutoScroll.js 5.4 KB

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