ScrollManager.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. /**
  7. * @class Ext.dd.ScrollManager Provides automatic scrolling of overflow regions
  8. * in the page during drag operations.<br>
  9. * <br>
  10. * <b>Note: This class uses "Point Mode" and is untested in "Intersect
  11. * Mode".</b>
  12. * @singleton
  13. */
  14. Ext.dd.ScrollManager = function() {
  15. var ddm = Ext.dd.DragDropMgr;
  16. var els = {};
  17. var dragEl = null;
  18. var proc = {};
  19. var onStop = function(e) {
  20. dragEl = null;
  21. clearProc();
  22. };
  23. var triggerRefresh = function() {
  24. if (ddm.dragCurrent) {
  25. ddm.refreshCache(ddm.dragCurrent.groups);
  26. }
  27. };
  28. var doScroll = function() {
  29. if (ddm.dragCurrent) {
  30. var dds = Ext.dd.ScrollManager;
  31. var inc = proc.el.ddScrollConfig
  32. ? proc.el.ddScrollConfig.increment
  33. : dds.increment;
  34. if (!dds.animate) {
  35. if (proc.el.scroll(proc.dir, inc)) {
  36. triggerRefresh();
  37. }
  38. } else {
  39. proc.el.scroll(proc.dir, inc, true, dds.animDuration,
  40. triggerRefresh);
  41. }
  42. }
  43. };
  44. var clearProc = function() {
  45. if (proc.id) {
  46. clearInterval(proc.id);
  47. }
  48. proc.id = 0;
  49. proc.el = null;
  50. proc.dir = "";
  51. };
  52. var startProc = function(el, dir) {
  53. clearProc();
  54. proc.el = el;
  55. proc.dir = dir;
  56. proc.id = setInterval(doScroll, Ext.dd.ScrollManager.frequency);
  57. };
  58. var onFire = function(e, isDrop) {
  59. if (isDrop || !ddm.dragCurrent) {
  60. return;
  61. }
  62. var dds = Ext.dd.ScrollManager;
  63. if (!dragEl || dragEl != ddm.dragCurrent) {
  64. dragEl = ddm.dragCurrent;
  65. // refresh regions on drag start
  66. dds.refreshCache();
  67. }
  68. var xy = Ext.lib.Event.getXY(e);
  69. var pt = new Ext.lib.Point(xy[0], xy[1]);
  70. for (var id in els) {
  71. var el = els[id], r = el._region;
  72. var c = el.ddScrollConfig ? el.ddScrollConfig : dds;
  73. if (r && r.contains(pt) && el.isScrollable()) {
  74. if (r.bottom - pt.y <= c.vthresh) {
  75. if (proc.el != el) {
  76. startProc(el, "down");
  77. }
  78. return;
  79. } else if (r.right - pt.x <= c.hthresh) {
  80. if (proc.el != el) {
  81. startProc(el, "left");
  82. }
  83. return;
  84. } else if (pt.y - r.top <= c.vthresh) {
  85. if (proc.el != el) {
  86. startProc(el, "up");
  87. }
  88. return;
  89. } else if (pt.x - r.left <= c.hthresh) {
  90. if (proc.el != el) {
  91. startProc(el, "right");
  92. }
  93. return;
  94. }
  95. }
  96. }
  97. clearProc();
  98. };
  99. ddm.fireEvents = ddm.fireEvents.createSequence(onFire, ddm);
  100. ddm.stopDrag = ddm.stopDrag.createSequence(onStop, ddm);
  101. return {
  102. /**
  103. * Registers new overflow element(s) to auto scroll
  104. *
  105. * @param {Mixed/Array}
  106. * el The id of or the element to be scrolled or an array of
  107. * either
  108. */
  109. register : function(el) {
  110. if (el instanceof Array) {
  111. for (var i = 0, len = el.length; i < len; i++) {
  112. this.register(el[i]);
  113. }
  114. } else {
  115. el = Ext.get(el);
  116. els[el.id] = el;
  117. }
  118. },
  119. /**
  120. * Unregisters overflow element(s) so they are no longer scrolled
  121. *
  122. * @param {Mixed/Array}
  123. * el The id of or the element to be removed or an array of
  124. * either
  125. */
  126. unregister : function(el) {
  127. if (el instanceof Array) {
  128. for (var i = 0, len = el.length; i < len; i++) {
  129. this.unregister(el[i]);
  130. }
  131. } else {
  132. el = Ext.get(el);
  133. delete els[el.id];
  134. }
  135. },
  136. /**
  137. * The number of pixels from the top or bottom edge of a container the
  138. * pointer needs to be to trigger scrolling (defaults to 25)
  139. *
  140. * @type Number
  141. */
  142. vthresh : 25,
  143. /**
  144. * The number of pixels from the right or left edge of a container the
  145. * pointer needs to be to trigger scrolling (defaults to 25)
  146. *
  147. * @type Number
  148. */
  149. hthresh : 25,
  150. /**
  151. * The number of pixels to scroll in each scroll increment (defaults to
  152. * 50)
  153. *
  154. * @type Number
  155. */
  156. increment : 100,
  157. /**
  158. * The frequency of scrolls in milliseconds (defaults to 500)
  159. *
  160. * @type Number
  161. */
  162. frequency : 500,
  163. /**
  164. * True to animate the scroll (defaults to true)
  165. *
  166. * @type Boolean
  167. */
  168. animate : true,
  169. /**
  170. * The animation duration in seconds - MUST BE less than
  171. * Ext.dd.ScrollManager.frequency! (defaults to .4)
  172. *
  173. * @type Number
  174. */
  175. animDuration : .4,
  176. /**
  177. * Manually trigger a cache refresh.
  178. */
  179. refreshCache : function() {
  180. for (var id in els) {
  181. if (typeof els[id] == 'object') { // for people extending the
  182. // object prototype
  183. els[id]._region = els[id].getRegion();
  184. }
  185. }
  186. }
  187. };
  188. }();