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