1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
if(!dojo._hasResource['dojox.grid._grid.scroller']){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource['dojox.grid._grid.scroller'] = true;
dojo.provide('dojox.grid._grid.scroller');
dojo.declare('dojox.grid.scroller.base', null, {
// summary:
// virtual scrollbox, abstract class
// Content must in /rows/
// Rows are managed in contiguous sets called /pages/
// There are a fixed # of rows per page
// The minimum rendered unit is a page
constructor: function(){
this.pageHeights = [];
this.stack = [];
},
// specified
rowCount: 0, // total number of rows to manage
defaultRowHeight: 10, // default height of a row
keepRows: 100, // maximum number of rows that should exist at one time
contentNode: null, // node to contain pages
scrollboxNode: null, // node that controls scrolling
// calculated
defaultPageHeight: 0, // default height of a page
keepPages: 10, // maximum number of pages that should exists at one time
pageCount: 0,
windowHeight: 0,
firstVisibleRow: 0,
lastVisibleRow: 0,
// private
page: 0,
pageTop: 0,
// init
init: function(inRowCount, inKeepRows, inRowsPerPage){
switch(arguments.length){
case 3: this.rowsPerPage = inRowsPerPage;
case 2: this.keepRows = inKeepRows;
case 1: this.rowCount = inRowCount;
}
this.defaultPageHeight = this.defaultRowHeight * this.rowsPerPage;
//this.defaultPageHeight = this.defaultRowHeight * Math.min(this.rowsPerPage, this.rowCount);
this.pageCount = Math.ceil(this.rowCount / this.rowsPerPage);
this.setKeepInfo(this.keepRows);
this.invalidate();
if(this.scrollboxNode){
this.scrollboxNode.scrollTop = 0;
this.scroll(0);
this.scrollboxNode.onscroll = dojo.hitch(this, 'onscroll');
}
},
setKeepInfo: function(inKeepRows){
this.keepRows = inKeepRows;
this.keepPages = !this.keepRows ? this.keepRows : Math.max(Math.ceil(this.keepRows / this.rowsPerPage), 2);
},
// updating
invalidate: function(){
this.invalidateNodes();
this.pageHeights = [];
this.height = (this.pageCount ? (this.pageCount - 1)* this.defaultPageHeight + this.calcLastPageHeight() : 0);
this.resize();
},
updateRowCount: function(inRowCount){
this.invalidateNodes();
this.rowCount = inRowCount;
// update page count, adjust document height
oldPageCount = this.pageCount;
this.pageCount = Math.ceil(this.rowCount / this.rowsPerPage);
if(this.pageCount < oldPageCount){
for(var i=oldPageCount-1; i>=this.pageCount; i--){
this.height -= this.getPageHeight(i);
delete this.pageHeights[i]
}
}else if(this.pageCount > oldPageCount){
this.height += this.defaultPageHeight * (this.pageCount - oldPageCount - 1) + this.calcLastPageHeight();
}
this.resize();
},
// abstract interface
pageExists: function(inPageIndex){
},
measurePage: function(inPageIndex){
},
positionPage: function(inPageIndex, inPos){
},
repositionPages: function(inPageIndex){
},
installPage: function(inPageIndex){
},
preparePage: function(inPageIndex, inPos, inReuseNode){
},
renderPage: function(inPageIndex){
},
removePage: function(inPageIndex){
},
pacify: function(inShouldPacify){
},
// pacification
pacifying: false,
pacifyTicks: 200,
setPacifying: function(inPacifying){
if(this.pacifying != inPacifying){
this.pacifying = inPacifying;
this.pacify(this.pacifying);
}
},
startPacify: function(){
this.startPacifyTicks = new Date().getTime();
},
doPacify: function(){
var result = (new Date().getTime() - this.startPacifyTicks) > this.pacifyTicks;
this.setPacifying(true);
this.startPacify();
return result;
},
endPacify: function(){
this.setPacifying(false);
},
// default sizing implementation
resize: function(){
if(this.scrollboxNode){
this.windowHeight = this.scrollboxNode.clientHeight;
}
dojox.grid.setStyleHeightPx(this.contentNode, this.height);
},
calcLastPageHeight: function(){
if(!this.pageCount){
return 0;
}
var lastPage = this.pageCount - 1;
var lastPageHeight = ((this.rowCount % this.rowsPerPage)||(this.rowsPerPage)) * this.defaultRowHeight;
this.pageHeights[lastPage] = lastPageHeight;
return lastPageHeight;
},
updateContentHeight: function(inDh){
this.height += inDh;
this.resize();
},
updatePageHeight: function(inPageIndex){
if(this.pageExists(inPageIndex)){
var oh = this.getPageHeight(inPageIndex);
var h = (this.measurePage(inPageIndex))||(oh);
this.pageHeights[inPageIndex] = h;
if((h)&&(oh != h)){
this.updateContentHeight(h - oh)
this.repositionPages(inPageIndex);
}
}
},
rowHeightChanged: function(inRowIndex){
this.updatePageHeight(Math.floor(inRowIndex / this.rowsPerPage));
},
// scroller core
invalidateNodes: function(){
while(this.stack.length){
this.destroyPage(this.popPage());
}
},
createPageNode: function(){
var p = document.createElement('div');
p.style.position = 'absolute';
//p.style.width = '100%';
p.style[dojo._isBodyLtr() ? "left" : "right"] = '0';
return p;
},
getPageHeight: function(inPageIndex){
var ph = this.pageHeights[inPageIndex];
return (ph !== undefined ? ph : this.defaultPageHeight);
},
// FIXME: this is not a stack, it's a FIFO list
pushPage: function(inPageIndex){
return this.stack.push(inPageIndex);
},
popPage: function(){
return this.stack.shift();
},
findPage: function(inTop){
var i = 0, h = 0;
for(var ph = 0; i<this.pageCount; i++, h += ph){
ph = this.getPageHeight(i);
if(h + ph >= inTop){
break;
}
}
this.page = i;
this.pageTop = h;
},
buildPage: function(inPageIndex, inReuseNode, inPos){
this.preparePage(inPageIndex, inReuseNode);
this.positionPage(inPageIndex, inPos);
// order of operations is key below
this.installPage(inPageIndex);
this.renderPage(inPageIndex);
// order of operations is key above
this.pushPage(inPageIndex);
},
needPage: function(inPageIndex, inPos){
var h = this.getPageHeight(inPageIndex), oh = h;
if(!this.pageExists(inPageIndex)){
this.buildPage(inPageIndex, this.keepPages&&(this.stack.length >= this.keepPages), inPos);
h = this.measurePage(inPageIndex) || h;
this.pageHeights[inPageIndex] = h;
if(h && (oh != h)){
this.updateContentHeight(h - oh)
}
}else{
this.positionPage(inPageIndex, inPos);
}
return h;
},
onscroll: function(){
this.scroll(this.scrollboxNode.scrollTop);
},
scroll: function(inTop){
this.startPacify();
this.findPage(inTop);
var h = this.height;
var b = this.getScrollBottom(inTop);
for(var p=this.page, y=this.pageTop; (p<this.pageCount)&&((b<0)||(y<b)); p++){
y += this.needPage(p, y);
}
this.firstVisibleRow = this.getFirstVisibleRow(this.page, this.pageTop, inTop);
this.lastVisibleRow = this.getLastVisibleRow(p - 1, y, b);
// indicates some page size has been updated
if(h != this.height){
this.repositionPages(p-1);
}
this.endPacify();
},
getScrollBottom: function(inTop){
return (this.windowHeight >= 0 ? inTop + this.windowHeight : -1);
},
// events
processNodeEvent: function(e, inNode){
var t = e.target;
while(t && (t != inNode) && t.parentNode && (t.parentNode.parentNode != inNode)){
t = t.parentNode;
}
if(!t || !t.parentNode || (t.parentNode.parentNode != inNode)){
return false;
}
var page = t.parentNode;
e.topRowIndex = page.pageIndex * this.rowsPerPage;
e.rowIndex = e.topRowIndex + dojox.grid.indexInParent(t);
e.rowTarget = t;
return true;
},
processEvent: function(e){
return this.processNodeEvent(e, this.contentNode);
},
dummy: 0
});
dojo.declare('dojox.grid.scroller', dojox.grid.scroller.base, {
// summary:
// virtual scroller class, makes no assumption about shape of items being scrolled
constructor: function(){
this.pageNodes = [];
},
// virtual rendering interface
renderRow: function(inRowIndex, inPageNode){
},
removeRow: function(inRowIndex){
},
// page node operations
getDefaultNodes: function(){
return this.pageNodes;
},
getDefaultPageNode: function(inPageIndex){
return this.getDefaultNodes()[inPageIndex];
},
positionPageNode: function(inNode, inPos){
inNode.style.top = inPos + 'px';
},
getPageNodePosition: function(inNode){
return inNode.offsetTop;
},
repositionPageNodes: function(inPageIndex, inNodes){
var last = 0;
for(var i=0; i<this.stack.length; i++){
last = Math.max(this.stack[i], last);
}
//
var n = inNodes[inPageIndex];
var y = (n ? this.getPageNodePosition(n) + this.getPageHeight(inPageIndex) : 0);
//console.log('detected height change, repositioning from #%d (%d) @ %d ', inPageIndex + 1, last, y, this.pageHeights[0]);
//
for(var p=inPageIndex+1; p<=last; p++){
n = inNodes[p];
if(n){
//console.log('#%d @ %d', inPageIndex, y, this.getPageNodePosition(n));
if(this.getPageNodePosition(n) == y){
return;
}
//console.log('placing page %d at %d', p, y);
this.positionPage(p, y);
}
y += this.getPageHeight(p);
}
},
invalidatePageNode: function(inPageIndex, inNodes){
var p = inNodes[inPageIndex];
if(p){
delete inNodes[inPageIndex];
this.removePage(inPageIndex, p);
dojox.grid.cleanNode(p);
p.innerHTML = '';
}
return p;
},
preparePageNode: function(inPageIndex, inReusePageIndex, inNodes){
var p = (inReusePageIndex === null ? this.createPageNode() : this.invalidatePageNode(inReusePageIndex, inNodes));
p.pageIndex = inPageIndex;
p.id = (this._pageIdPrefix || "") + 'page-' + inPageIndex;
inNodes[inPageIndex] = p;
},
// implementation for page manager
pageExists: function(inPageIndex){
return Boolean(this.getDefaultPageNode(inPageIndex));
},
measurePage: function(inPageIndex){
return this.getDefaultPageNode(inPageIndex).offsetHeight;
},
positionPage: function(inPageIndex, inPos){
this.positionPageNode(this.getDefaultPageNode(inPageIndex), inPos);
},
repositionPages: function(inPageIndex){
this.repositionPageNodes(inPageIndex, this.getDefaultNodes());
},
preparePage: function(inPageIndex, inReuseNode){
this.preparePageNode(inPageIndex, (inReuseNode ? this.popPage() : null), this.getDefaultNodes());
},
installPage: function(inPageIndex){
this.contentNode.appendChild(this.getDefaultPageNode(inPageIndex));
},
destroyPage: function(inPageIndex){
var p = this.invalidatePageNode(inPageIndex, this.getDefaultNodes());
dojox.grid.removeNode(p);
},
// rendering implementation
renderPage: function(inPageIndex){
var node = this.pageNodes[inPageIndex];
for(var i=0, j=inPageIndex*this.rowsPerPage; (i<this.rowsPerPage)&&(j<this.rowCount); i++, j++){
this.renderRow(j, node);
}
},
removePage: function(inPageIndex){
for(var i=0, j=inPageIndex*this.rowsPerPage; i<this.rowsPerPage; i++, j++){
this.removeRow(j);
}
},
// scroll control
getPageRow: function(inPage){
return inPage * this.rowsPerPage;
},
getLastPageRow: function(inPage){
return Math.min(this.rowCount, this.getPageRow(inPage + 1)) - 1;
},
getFirstVisibleRowNodes: function(inPage, inPageTop, inScrollTop, inNodes){
var row = this.getPageRow(inPage);
var rows = dojox.grid.divkids(inNodes[inPage]);
for(var i=0,l=rows.length; i<l && inPageTop<inScrollTop; i++, row++){
inPageTop += rows[i].offsetHeight;
}
return (row ? row - 1 : row);
},
getFirstVisibleRow: function(inPage, inPageTop, inScrollTop){
if(!this.pageExists(inPage)){
return 0;
}
return this.getFirstVisibleRowNodes(inPage, inPageTop, inScrollTop, this.getDefaultNodes());
},
getLastVisibleRowNodes: function(inPage, inBottom, inScrollBottom, inNodes){
var row = this.getLastPageRow(inPage);
var rows = dojox.grid.divkids(inNodes[inPage]);
for(var i=rows.length-1; i>=0 && inBottom>inScrollBottom; i--, row--){
inBottom -= rows[i].offsetHeight;
}
return row + 1;
},
getLastVisibleRow: function(inPage, inBottom, inScrollBottom){
if(!this.pageExists(inPage)){
return 0;
}
return this.getLastVisibleRowNodes(inPage, inBottom, inScrollBottom, this.getDefaultNodes());
},
findTopRowForNodes: function(inScrollTop, inNodes){
var rows = dojox.grid.divkids(inNodes[this.page]);
for(var i=0,l=rows.length,t=this.pageTop,h; i<l; i++){
h = rows[i].offsetHeight;
t += h;
if(t >= inScrollTop){
this.offset = h - (t - inScrollTop);
return i + this.page * this.rowsPerPage;
}
}
return -1;
},
findScrollTopForNodes: function(inRow, inNodes){
var rowPage = Math.floor(inRow / this.rowsPerPage);
var t = 0;
for(var i=0; i<rowPage; i++){
t += this.getPageHeight(i);
}
this.pageTop = t;
this.needPage(rowPage, this.pageTop);
var rows = dojox.grid.divkids(inNodes[rowPage]);
var r = inRow - this.rowsPerPage * rowPage;
for(var i=0,l=rows.length; i<l && i<r; i++){
t += rows[i].offsetHeight;
}
return t;
},
findTopRow: function(inScrollTop){
return this.findTopRowForNodes(inScrollTop, this.getDefaultNodes());
},
findScrollTop: function(inRow){
return this.findScrollTopForNodes(inRow, this.getDefaultNodes());
},
dummy: 0
});
dojo.declare('dojox.grid.scroller.columns', dojox.grid.scroller, {
// summary:
// Virtual scroller class that scrolls list of columns. Owned by grid and used internally
// for virtual scrolling.
constructor: function(inContentNodes){
this.setContentNodes(inContentNodes);
},
// nodes
setContentNodes: function(inNodes){
this.contentNodes = inNodes;
this.colCount = (this.contentNodes ? this.contentNodes.length : 0);
this.pageNodes = [];
for(var i=0; i<this.colCount; i++){
this.pageNodes[i] = [];
}
},
getDefaultNodes: function(){
return this.pageNodes[0] || [];
},
scroll: function(inTop) {
if(this.colCount){
dojox.grid.scroller.prototype.scroll.call(this, inTop);
}
},
// resize
resize: function(){
if(this.scrollboxNode){
this.windowHeight = this.scrollboxNode.clientHeight;
}
for(var i=0; i<this.colCount; i++){
dojox.grid.setStyleHeightPx(this.contentNodes[i], this.height);
}
},
// implementation for page manager
positionPage: function(inPageIndex, inPos){
for(var i=0; i<this.colCount; i++){
this.positionPageNode(this.pageNodes[i][inPageIndex], inPos);
}
},
preparePage: function(inPageIndex, inReuseNode){
var p = (inReuseNode ? this.popPage() : null);
for(var i=0; i<this.colCount; i++){
this.preparePageNode(inPageIndex, p, this.pageNodes[i]);
}
},
installPage: function(inPageIndex){
for(var i=0; i<this.colCount; i++){
this.contentNodes[i].appendChild(this.pageNodes[i][inPageIndex]);
}
},
destroyPage: function(inPageIndex){
for(var i=0; i<this.colCount; i++){
dojox.grid.removeNode(this.invalidatePageNode(inPageIndex, this.pageNodes[i]));
}
},
// rendering implementation
renderPage: function(inPageIndex){
var nodes = [];
for(var i=0; i<this.colCount; i++){
nodes[i] = this.pageNodes[i][inPageIndex];
}
//this.renderRows(inPageIndex*this.rowsPerPage, this.rowsPerPage, nodes);
for(var i=0, j=inPageIndex*this.rowsPerPage; (i<this.rowsPerPage)&&(j<this.rowCount); i++, j++){
this.renderRow(j, nodes);
}
}
});
}
|