AutoScroll.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.grid.enhanced.plugins.AutoScroll"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.grid.enhanced.plugins.AutoScroll"] = true;
  8. dojo.provide("dojox.grid.enhanced.plugins.AutoScroll");
  9. dojo.require("dojox.grid.enhanced._Plugin");
  10. dojo.require("dojox.grid._RowSelector");
  11. dojo.declare("dojox.grid.enhanced.plugins.AutoScroll", dojox.grid.enhanced._Plugin, {
  12. // summary:
  13. // Provides horizontal and vertical auto-scroll for grid.
  14. // name: String
  15. // Plugin name
  16. name: "autoScroll",
  17. // autoScrollInterval: Integer
  18. // The time interval (in miliseconds) between 2 scrolling.
  19. autoScrollInterval: 1000,
  20. // autoScrollMargin: Integer
  21. // The width (in pixel) of the margin area where autoscroll can be triggered.
  22. autoScrollMargin: 30,
  23. constructor: function(grid, args){
  24. this.grid = grid;
  25. this.readyForAutoScroll = false;
  26. this._scrolling = false;
  27. args = dojo.isObject(args) ? args : {};
  28. if("interval" in args){
  29. this.autoScrollInterval = args.interval;
  30. }
  31. if("margin" in args){
  32. this.autoScrollMargin = args.margin;
  33. }
  34. this._initEvents();
  35. this._mixinGrid();
  36. },
  37. _initEvents: function(){
  38. var g = this.grid;
  39. this.connect(g, "onCellMouseDown", function(){
  40. this.readyForAutoScroll = true;
  41. });
  42. this.connect(g, "onHeaderCellMouseDown", function(){
  43. this.readyForAutoScroll = true;
  44. });
  45. this.connect(g, "onRowSelectorMouseDown", function(){
  46. this.readyForAutoScroll = true;
  47. });
  48. this.connect(dojo.doc, "onmouseup", function(evt){
  49. this._manageAutoScroll(true);
  50. this.readyForAutoScroll = false;
  51. });
  52. this.connect(dojo.doc, "onmousemove", function(evt){
  53. if(this.readyForAutoScroll){
  54. this._event = evt;
  55. var gridPos = dojo.position(g.domNode),
  56. hh = g._getHeaderHeight(),
  57. margin = this.autoScrollMargin,
  58. ey = evt.clientY, ex = evt.clientX,
  59. gy = gridPos.y, gx = gridPos.x,
  60. gh = gridPos.h, gw = gridPos.w;
  61. if(ex >= gx && ex <= gx + gw){
  62. if(ey >= gy + hh && ey < gy + hh + margin){
  63. this._manageAutoScroll(false, true, false);
  64. return;
  65. }else if(ey > gy + gh - margin && ey <= gy + gh){
  66. this._manageAutoScroll(false, true, true);
  67. return;
  68. }else if(ey >= gy && ey <= gy + gh){
  69. var withinSomeview = dojo.some(g.views.views, function(view, i){
  70. if(view instanceof dojox.grid._RowSelector){
  71. return false;
  72. }
  73. var viewPos = dojo.position(view.domNode);
  74. if(ex < viewPos.x + margin && ex >= viewPos.x){
  75. this._manageAutoScroll(false, false, false, view);
  76. return true;
  77. }else if(ex > viewPos.x + viewPos.w - margin && ex < viewPos.x + viewPos.w){
  78. this._manageAutoScroll(false, false, true, view);
  79. return true;
  80. }
  81. return false;
  82. }, this);
  83. if(withinSomeview){
  84. return;
  85. }
  86. }
  87. }
  88. //stop autoscroll.
  89. this._manageAutoScroll(true);
  90. }
  91. });
  92. },
  93. _mixinGrid: function(){
  94. var g = this.grid;
  95. g.onStartAutoScroll = function(/*isVertical, isForward*/){};
  96. g.onEndAutoScroll = function(/*isVertical, isForward, view, scrollToRowIndex, event*/){};
  97. },
  98. _fireEvent: function(eventName, args){
  99. var g = this.grid;
  100. switch(eventName){
  101. case "start":
  102. g.onStartAutoScroll.apply(g, args);
  103. break;
  104. case "end":
  105. g.onEndAutoScroll.apply(g, args);
  106. break;
  107. }
  108. },
  109. _manageAutoScroll: function(toStop, isVertical, isForward, view){
  110. if(toStop){
  111. this._scrolling = false;
  112. clearInterval(this._handler);
  113. }else if(!this._scrolling){
  114. this._scrolling = true;
  115. this._fireEvent("start", [isVertical, isForward, view]);
  116. this._autoScroll(isVertical, isForward, view);
  117. this._handler = setInterval(dojo.hitch(this, "_autoScroll", isVertical, isForward, view), this.autoScrollInterval);
  118. }
  119. },
  120. _autoScroll: function(isVertical, isForward, view){
  121. var g = this.grid,
  122. target = null;
  123. if(isVertical){
  124. var targetRow = g.scroller.firstVisibleRow + (isForward ? 1 : -1);
  125. if(targetRow >= 0 && targetRow < g.rowCount){
  126. g.scrollToRow(targetRow);
  127. target = targetRow;
  128. }
  129. }else{
  130. target = this._scrollColumn(isForward, view);
  131. }
  132. if(target !== null){
  133. this._fireEvent("end", [isVertical, isForward, view, target, this._event]);
  134. }
  135. },
  136. _scrollColumn: function(isForward, view){
  137. var node = view.scrollboxNode,
  138. target = null;
  139. if(node.clientWidth < node.scrollWidth){
  140. var cells = dojo.filter(this.grid.layout.cells, function(cell){
  141. return !cell.hidden;
  142. });
  143. var viewPos = dojo.position(view.domNode);
  144. var limit, edge, headerPos, i;
  145. if(isForward){
  146. limit = node.clientWidth;
  147. for(i = 0; i < cells.length; ++i){
  148. headerPos = dojo.position(cells[i].getHeaderNode());
  149. edge = headerPos.x - viewPos.x + headerPos.w;
  150. if(edge > limit){
  151. target = cells[i].index;
  152. node.scrollLeft += edge - limit + 10;
  153. break;
  154. }
  155. }
  156. }else{
  157. limit = 0;
  158. for(i = cells.length - 1; i >= 0; --i){
  159. headerPos = dojo.position(cells[i].getHeaderNode());
  160. edge = headerPos.x - viewPos.x;
  161. if(edge < limit){
  162. target = cells[i].index;
  163. node.scrollLeft += edge - limit - 10;
  164. break;
  165. }
  166. }
  167. }
  168. }
  169. return target;
  170. }
  171. });
  172. dojox.grid.EnhancedGrid.registerPlugin(dojox.grid.enhanced.plugins.AutoScroll/*name:'autoScroll'*/);
  173. }