root / trunk / web / dojo / dojox / grid / _Scroller.js @ 12
History | View | Annotate | Download (10.1 KB)
1 |
/*
|
---|---|
2 |
Copyright (c) 2004-2010, 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 |
|
7 |
|
8 |
if(!dojo._hasResource["dojox.grid._Scroller"]){ |
9 |
dojo._hasResource["dojox.grid._Scroller"]=true; |
10 |
dojo.provide("dojox.grid._Scroller");
|
11 |
(function(){
|
12 |
var _1=function(_2){ |
13 |
var i=0,n,p=_2.parentNode; |
14 |
while((n=p.childNodes[i++])){
|
15 |
if(n==_2){
|
16 |
return i-1; |
17 |
} |
18 |
} |
19 |
return -1; |
20 |
}; |
21 |
var _3=function(_4){ |
22 |
if(!_4){
|
23 |
return;
|
24 |
} |
25 |
var _5=function(_6){ |
26 |
return _6.domNode&&dojo.isDescendant(_6.domNode,_4,true); |
27 |
}; |
28 |
var ws=dijit.registry.filter(_5);
|
29 |
for(var i=0,w;(w=ws[i]);i++){ |
30 |
w.destroy(); |
31 |
} |
32 |
delete ws;
|
33 |
}; |
34 |
var _7=function(_8){ |
35 |
var _9=dojo.byId(_8);
|
36 |
return (_9&&_9.tagName?_9.tagName.toLowerCase():""); |
37 |
}; |
38 |
var _a=function(_b,_c){ |
39 |
var _d=[];
|
40 |
var i=0,n; |
41 |
while((n=_b.childNodes[i])){
|
42 |
i++; |
43 |
if(_7(n)==_c){
|
44 |
_d.push(n); |
45 |
} |
46 |
} |
47 |
return _d;
|
48 |
}; |
49 |
var _e=function(_f){ |
50 |
return _a(_f,"div"); |
51 |
}; |
52 |
dojo.declare("dojox.grid._Scroller",null,{constructor:function(_10){ |
53 |
this.setContentNodes(_10);
|
54 |
this.pageHeights=[];
|
55 |
this.pageNodes=[];
|
56 |
this.stack=[];
|
57 |
},rowCount:0,defaultRowHeight:32,keepRows:100,contentNode:null,scrollboxNode:null,defaultPageHeight:0,keepPages:10,pageCount:0,windowHeight:0,firstVisibleRow:0,lastVisibleRow:0,averageRowHeight:0,page:0,pageTop:0,init:function(_11,_12,_13){ |
58 |
switch(arguments.length){ |
59 |
case 3: |
60 |
this.rowsPerPage=_13;
|
61 |
case 2: |
62 |
this.keepRows=_12;
|
63 |
case 1: |
64 |
this.rowCount=_11;
|
65 |
default:
|
66 |
break;
|
67 |
} |
68 |
this.defaultPageHeight=this.defaultRowHeight*this.rowsPerPage; |
69 |
this.pageCount=this._getPageCount(this.rowCount,this.rowsPerPage); |
70 |
this.setKeepInfo(this.keepRows); |
71 |
this.invalidate();
|
72 |
if(this.scrollboxNode){ |
73 |
this.scrollboxNode.scrollTop=0; |
74 |
this.scroll(0); |
75 |
this.scrollboxNode.onscroll=dojo.hitch(this,"onscroll"); |
76 |
} |
77 |
},_getPageCount:function(_14,_15){ |
78 |
return _14?(Math.ceil(_14/_15)||1):0; |
79 |
},destroy:function(){ |
80 |
this.invalidateNodes();
|
81 |
delete this.contentNodes; |
82 |
delete this.contentNode; |
83 |
delete this.scrollboxNode; |
84 |
},setKeepInfo:function(_16){ |
85 |
this.keepRows=_16;
|
86 |
this.keepPages=!this.keepRows?this.keepPages:Math.max(Math.ceil(this.keepRows/this.rowsPerPage),2); |
87 |
},setContentNodes:function(_17){ |
88 |
this.contentNodes=_17;
|
89 |
this.colCount=(this.contentNodes?this.contentNodes.length:0); |
90 |
this.pageNodes=[];
|
91 |
for(var i=0;i<this.colCount;i++){ |
92 |
this.pageNodes[i]=[];
|
93 |
} |
94 |
},getDefaultNodes:function(){ |
95 |
return this.pageNodes[0]||[]; |
96 |
},invalidate:function(){ |
97 |
this._invalidating=true; |
98 |
this.invalidateNodes();
|
99 |
this.pageHeights=[];
|
100 |
this.height=(this.pageCount?(this.pageCount-1)*this.defaultPageHeight+this.calcLastPageHeight():0); |
101 |
this.resize();
|
102 |
this._invalidating=false; |
103 |
},updateRowCount:function(_18){ |
104 |
this.invalidateNodes();
|
105 |
this.rowCount=_18;
|
106 |
var _19=this.pageCount; |
107 |
if(_19===0){ |
108 |
this.height=1; |
109 |
} |
110 |
this.pageCount=this._getPageCount(this.rowCount,this.rowsPerPage); |
111 |
if(this.pageCount<_19){ |
112 |
for(var i=_19-1;i>=this.pageCount;i--){ |
113 |
this.height-=this.getPageHeight(i); |
114 |
delete this.pageHeights[i]; |
115 |
} |
116 |
}else{
|
117 |
if(this.pageCount>_19){ |
118 |
this.height+=this.defaultPageHeight*(this.pageCount-_19-1)+this.calcLastPageHeight(); |
119 |
} |
120 |
} |
121 |
this.resize();
|
122 |
},pageExists:function(_1a){ |
123 |
return Boolean(this.getDefaultPageNode(_1a)); |
124 |
},measurePage:function(_1b){ |
125 |
if(this.grid.rowHeight){ |
126 |
var _1c=this.grid.rowHeight+1; |
127 |
return ((_1b+1)*this.rowsPerPage>this.rowCount?this.rowCount-_1b*this.rowsPerPage:this.rowsPerPage)*_1c; |
128 |
} |
129 |
var n=this.getDefaultPageNode(_1b); |
130 |
return (n&&n.innerHTML)?n.offsetHeight:undefined; |
131 |
},positionPage:function(_1d,_1e){ |
132 |
for(var i=0;i<this.colCount;i++){ |
133 |
this.pageNodes[i][_1d].style.top=_1e+"px"; |
134 |
} |
135 |
},repositionPages:function(_1f){ |
136 |
var _20=this.getDefaultNodes(); |
137 |
var _21=0; |
138 |
for(var i=0;i<this.stack.length;i++){ |
139 |
_21=Math.max(this.stack[i],_21);
|
140 |
} |
141 |
var n=_20[_1f];
|
142 |
var y=(n?this.getPageNodePosition(n)+this.getPageHeight(_1f):0); |
143 |
for(var p=_1f+1;p<=_21;p++){ |
144 |
n=_20[p]; |
145 |
if(n){
|
146 |
if(this.getPageNodePosition(n)==y){ |
147 |
return;
|
148 |
} |
149 |
this.positionPage(p,y);
|
150 |
} |
151 |
y+=this.getPageHeight(p);
|
152 |
} |
153 |
},installPage:function(_22){ |
154 |
for(var i=0;i<this.colCount;i++){ |
155 |
this.contentNodes[i].appendChild(this.pageNodes[i][_22]); |
156 |
} |
157 |
},preparePage:function(_23,_24){ |
158 |
var p=(_24?this.popPage():null); |
159 |
for(var i=0;i<this.colCount;i++){ |
160 |
var _25=this.pageNodes[i]; |
161 |
var _26=(p===null?this.createPageNode():this.invalidatePageNode(p,_25)); |
162 |
_26.pageIndex=_23; |
163 |
_25[_23]=_26; |
164 |
} |
165 |
},renderPage:function(_27){ |
166 |
var _28=[];
|
167 |
var i,j;
|
168 |
for(i=0;i<this.colCount;i++){ |
169 |
_28[i]=this.pageNodes[i][_27];
|
170 |
} |
171 |
for(i=0,j=_27*this.rowsPerPage;(i<this.rowsPerPage)&&(j<this.rowCount);i++,j++){ |
172 |
this.renderRow(j,_28);
|
173 |
} |
174 |
},removePage:function(_29){ |
175 |
for(var i=0,j=_29*this.rowsPerPage;i<this.rowsPerPage;i++,j++){ |
176 |
this.removeRow(j);
|
177 |
} |
178 |
},destroyPage:function(_2a){ |
179 |
for(var i=0;i<this.colCount;i++){ |
180 |
var n=this.invalidatePageNode(_2a,this.pageNodes[i]); |
181 |
if(n){
|
182 |
dojo.destroy(n); |
183 |
} |
184 |
} |
185 |
},pacify:function(_2b){ |
186 |
},pacifying:false,pacifyTicks:200,setPacifying:function(_2c){ |
187 |
if(this.pacifying!=_2c){ |
188 |
this.pacifying=_2c;
|
189 |
this.pacify(this.pacifying); |
190 |
} |
191 |
},startPacify:function(){ |
192 |
this.startPacifyTicks=new Date().getTime(); |
193 |
},doPacify:function(){ |
194 |
var _2d=(new Date().getTime()-this.startPacifyTicks)>this.pacifyTicks; |
195 |
this.setPacifying(true); |
196 |
this.startPacify();
|
197 |
return _2d;
|
198 |
},endPacify:function(){ |
199 |
this.setPacifying(false); |
200 |
},resize:function(){ |
201 |
if(this.scrollboxNode){ |
202 |
this.windowHeight=this.scrollboxNode.clientHeight; |
203 |
} |
204 |
for(var i=0;i<this.colCount;i++){ |
205 |
dojox.grid.util.setStyleHeightPx(this.contentNodes[i],Math.max(1,this.height)); |
206 |
} |
207 |
var _2e=(!this._invalidating); |
208 |
if(!_2e){
|
209 |
var ah=this.grid.attr("autoHeight"); |
210 |
if(typeof ah=="number"&&ah<=Math.min(this.rowsPerPage,this.rowCount)){ |
211 |
_2e=true;
|
212 |
} |
213 |
} |
214 |
if(_2e){
|
215 |
this.needPage(this.page,this.pageTop); |
216 |
} |
217 |
var _2f=(this.page<this.pageCount-1)?this.rowsPerPage:((this.rowCount%this.rowsPerPage)||this.rowsPerPage); |
218 |
var _30=this.getPageHeight(this.page); |
219 |
this.averageRowHeight=(_30>0&&_2f>0)?(_30/_2f):0; |
220 |
},calcLastPageHeight:function(){ |
221 |
if(!this.pageCount){ |
222 |
return 0; |
223 |
} |
224 |
var _31=this.pageCount-1; |
225 |
var _32=((this.rowCount%this.rowsPerPage)||(this.rowsPerPage))*this.defaultRowHeight; |
226 |
this.pageHeights[_31]=_32;
|
227 |
return _32;
|
228 |
},updateContentHeight:function(_33){ |
229 |
this.height+=_33;
|
230 |
this.resize();
|
231 |
},updatePageHeight:function(_34,_35){ |
232 |
if(this.pageExists(_34)){ |
233 |
var oh=this.getPageHeight(_34); |
234 |
var h=(this.measurePage(_34)); |
235 |
if(h===undefined){ |
236 |
h=oh; |
237 |
} |
238 |
this.pageHeights[_34]=h;
|
239 |
if(oh!=h){
|
240 |
this.updateContentHeight(h-oh);
|
241 |
var ah=this.grid.attr("autoHeight"); |
242 |
if((typeof ah=="number"&&ah>this.rowCount)||(ah===true&&!_35)){ |
243 |
this.grid.sizeChange();
|
244 |
}else{
|
245 |
this.repositionPages(_34);
|
246 |
} |
247 |
} |
248 |
return h;
|
249 |
} |
250 |
return 0; |
251 |
},rowHeightChanged:function(_36){ |
252 |
this.updatePageHeight(Math.floor(_36/this.rowsPerPage),false); |
253 |
},invalidateNodes:function(){ |
254 |
while(this.stack.length){ |
255 |
this.destroyPage(this.popPage()); |
256 |
} |
257 |
},createPageNode:function(){ |
258 |
var p=document.createElement("div"); |
259 |
dojo.attr(p,"role","presentation"); |
260 |
p.style.position="absolute";
|
261 |
p.style[dojo._isBodyLtr()?"left":"right"]="0"; |
262 |
return p;
|
263 |
},getPageHeight:function(_37){ |
264 |
var ph=this.pageHeights[_37]; |
265 |
return (ph!==undefined?ph:this.defaultPageHeight); |
266 |
},pushPage:function(_38){ |
267 |
return this.stack.push(_38); |
268 |
},popPage:function(){ |
269 |
return this.stack.shift(); |
270 |
},findPage:function(_39){ |
271 |
var i=0,h=0; |
272 |
for(var ph=0;i<this.pageCount;i++,h+=ph){ |
273 |
ph=this.getPageHeight(i);
|
274 |
if(h+ph>=_39){
|
275 |
break;
|
276 |
} |
277 |
} |
278 |
this.page=i;
|
279 |
this.pageTop=h;
|
280 |
},buildPage:function(_3a,_3b,_3c){ |
281 |
this.preparePage(_3a,_3b);
|
282 |
this.positionPage(_3a,_3c);
|
283 |
this.installPage(_3a);
|
284 |
this.renderPage(_3a);
|
285 |
this.pushPage(_3a);
|
286 |
},needPage:function(_3d,_3e){ |
287 |
var h=this.getPageHeight(_3d),oh=h; |
288 |
if(!this.pageExists(_3d)){ |
289 |
this.buildPage(_3d,this.keepPages&&(this.stack.length>=this.keepPages),_3e); |
290 |
h=this.updatePageHeight(_3d,true); |
291 |
}else{
|
292 |
this.positionPage(_3d,_3e);
|
293 |
} |
294 |
return h;
|
295 |
},onscroll:function(){ |
296 |
this.scroll(this.scrollboxNode.scrollTop); |
297 |
},scroll:function(_3f){ |
298 |
this.grid.scrollTop=_3f;
|
299 |
if(this.colCount){ |
300 |
this.startPacify();
|
301 |
this.findPage(_3f);
|
302 |
var h=this.height; |
303 |
var b=this.getScrollBottom(_3f); |
304 |
for(var p=this.page,y=this.pageTop;(p<this.pageCount)&&((b<0)||(y<b));p++){ |
305 |
y+=this.needPage(p,y);
|
306 |
} |
307 |
this.firstVisibleRow=this.getFirstVisibleRow(this.page,this.pageTop,_3f); |
308 |
this.lastVisibleRow=this.getLastVisibleRow(p-1,y,b); |
309 |
if(h!=this.height){ |
310 |
this.repositionPages(p-1); |
311 |
} |
312 |
this.endPacify();
|
313 |
} |
314 |
},getScrollBottom:function(_40){ |
315 |
return (this.windowHeight>=0?_40+this.windowHeight:-1); |
316 |
},processNodeEvent:function(e,_41){ |
317 |
var t=e.target;
|
318 |
while(t&&(t!=_41)&&t.parentNode&&(t.parentNode.parentNode!=_41)){
|
319 |
t=t.parentNode; |
320 |
} |
321 |
if(!t||!t.parentNode||(t.parentNode.parentNode!=_41)){
|
322 |
return false; |
323 |
} |
324 |
var _42=t.parentNode;
|
325 |
e.topRowIndex=_42.pageIndex*this.rowsPerPage;
|
326 |
e.rowIndex=e.topRowIndex+_1(t); |
327 |
e.rowTarget=t; |
328 |
return true; |
329 |
},processEvent:function(e){ |
330 |
return this.processNodeEvent(e,this.contentNode); |
331 |
},renderRow:function(_43,_44){ |
332 |
},removeRow:function(_45){ |
333 |
},getDefaultPageNode:function(_46){ |
334 |
return this.getDefaultNodes()[_46]; |
335 |
},positionPageNode:function(_47,_48){ |
336 |
},getPageNodePosition:function(_49){ |
337 |
return _49.offsetTop;
|
338 |
},invalidatePageNode:function(_4a,_4b){ |
339 |
var p=_4b[_4a];
|
340 |
if(p){
|
341 |
delete _4b[_4a];
|
342 |
this.removePage(_4a,p);
|
343 |
_3(p); |
344 |
p.innerHTML="";
|
345 |
} |
346 |
return p;
|
347 |
},getPageRow:function(_4c){ |
348 |
return _4c*this.rowsPerPage; |
349 |
},getLastPageRow:function(_4d){ |
350 |
return Math.min(this.rowCount,this.getPageRow(_4d+1))-1; |
351 |
},getFirstVisibleRow:function(_4e,_4f,_50){ |
352 |
if(!this.pageExists(_4e)){ |
353 |
return 0; |
354 |
} |
355 |
var row=this.getPageRow(_4e); |
356 |
var _51=this.getDefaultNodes(); |
357 |
var _52=_e(_51[_4e]);
|
358 |
for(var i=0,l=_52.length;i<l&&_4f<_50;i++,row++){ |
359 |
_4f+=_52[i].offsetHeight; |
360 |
} |
361 |
return (row?row-1:row); |
362 |
},getLastVisibleRow:function(_53,_54,_55){ |
363 |
if(!this.pageExists(_53)){ |
364 |
return 0; |
365 |
} |
366 |
var _56=this.getDefaultNodes(); |
367 |
var row=this.getLastPageRow(_53); |
368 |
var _57=_e(_56[_53]);
|
369 |
for(var i=_57.length-1;i>=0&&_54>_55;i--,row--){ |
370 |
_54-=_57[i].offsetHeight; |
371 |
} |
372 |
return row+1; |
373 |
},findTopRow:function(_58){ |
374 |
var _59=this.getDefaultNodes(); |
375 |
var _5a=_e(_59[this.page]); |
376 |
for(var i=0,l=_5a.length,t=this.pageTop,h;i<l;i++){ |
377 |
h=_5a[i].offsetHeight; |
378 |
t+=h; |
379 |
if(t>=_58){
|
380 |
this.offset=h-(t-_58);
|
381 |
return i+this.page*this.rowsPerPage; |
382 |
} |
383 |
} |
384 |
return -1; |
385 |
},findScrollTop:function(_5b){ |
386 |
var _5c=Math.floor(_5b/this.rowsPerPage); |
387 |
var t=0; |
388 |
var i,l;
|
389 |
for(i=0;i<_5c;i++){ |
390 |
t+=this.getPageHeight(i);
|
391 |
} |
392 |
this.pageTop=t;
|
393 |
this.needPage(_5c,this.pageTop); |
394 |
var _5d=this.getDefaultNodes(); |
395 |
var _5e=_e(_5d[_5c]);
|
396 |
var r=_5b-this.rowsPerPage*_5c; |
397 |
for(i=0,l=_5e.length;i<l&&i<r;i++){ |
398 |
t+=_5e[i].offsetHeight; |
399 |
} |
400 |
return t;
|
401 |
},dummy:0}); |
402 |
})(); |
403 |
} |