Debian Codegraph @

dojox/widget/Pager.js

raw symbols SCIP
1 define(["dojo/aspect", "dojo/_base/array", "dojo/_base/declare", "dojo/dom", "dojo/dom-attr", "dojo/dom-class", "dojo/dom-construct", "dojo/dom-geometry", "dojo/dom-style", "dojo/fx", "dojo/_base/kernel", "dojo/keys",
2 "dojo/_base/lang", "dojo/on", "dijit/_WidgetBase", "dijit/_TemplatedMixin", "./PagerItem", "dojo/text!./Pager/Pager.html"],
3 function(aspect, array, declare, dom, attr, domClass, domConstruct, geometry, style, fx, kernel, keys, lang, on, _WidgetBase, _TemplatedMixin, PagerItem, template){
4
5 kernel.experimental("dojox.widget.Pager");
6
7 return declare("dojox.widget.Pager",
8 [_WidgetBase, _TemplatedMixin],
9 {
10 // summary:
11 // A Pager, displaying a list of sized nodes
12
13
14 templateString: template,
15
16
17 // iconPrevious: String?
18 // The url of the previous page icon
19 iconPrevious: "",
20
21 // iconNext: String?
22 // The url of the next page icon
23 iconNext: "",
24
25 iconPage: require.toUrl("dojox/widget/Pager/images/pageInactive.png"),
26 iconPageActive: require.toUrl("dojox/widget/Pager/images/pageActive.png"),
27
28 // store: Object
29 // A dojo.data Data store
30 store: null, // data store for items
31
32 // orientation: String
33 // Either "horizontal or "vertical" to define the direction the pages will slide
34 orientation: "horizontal", // or vertical
35
36 // statusPos: String
37 // A string describing where to put the Pager "current page" indicator. Options are
38 // "leading" or "trailing". In the case of horiztonal orientation, "leading" indicates
39 // positioned above the PageItems. In the case of vertical, "leading" indicates "before".
40 statusPos: "leading",
41
42 // pagerPos: String
43 // TODOC
44 pagerPos: "center",
45
46 // duration: Integer
47 // Time in milliseconds to transition the pages
48 duration: 500,
49
50 // itemSpace: Integer
51 // Spacing between items? TODOC
52 itemSpace: 2,
53
54 // resizeChildren: Boolean
55 // TODOC
56 resizeChildren: true,
57
58 // itemClass: String|Widget
59 // The full dotted named of a Class or Widget constructor to use for the internal Pager Items.
60 itemClass: PagerItem,
61
62 // itemsPage: Integer
63 // The numbers of items to display in each "Page"
64 itemsPage: 3,
65
66 postMixInProperties: function(){
67 var h = (this.orientation == "horizontal");
68 lang.mixin(this,{
69 _totalPages:0,
70 _currentPage:1,
71 dirClass: "pager" + (h ? "Horizontal" : "Vertical"),
72 iconNext: require.toUrl("dojox/widget/Pager/images/" + (h ? "h" : "v") + "Next.png"),
73 iconPrevious: require.toUrl("dojox/widget/Pager/images/" + (h ? "h" : "v") + "Previous.png")
74 });
75 },
76
77 // Bidi Support
78 _next: function(){
79 // summary:
80 // Handle next behavior depends on widget direction
81 if(!this.isLeftToRight()){
82 this._pagerPrevious();
83 }else{
84 this._pagerNext();
85 }
86 },
87
88 // Bidi Support
89 _previous: function(){
90 // summary:
91 // Handle previous behavior depends on widget direction
92 if(!this.isLeftToRight()){
93 this._pagerNext();
94 }else{
95 this._pagerPrevious();
96 }
97 },
98
99 postCreate: function(){
100 this.inherited(arguments);
101 this.store.fetch({
102 onComplete: lang.hitch(this, "_init")
103 });
104
105 },
106
107 _a11yStyle: function(e){
108 // summary:
109 // top level onfocus/onblur listen to set a class "pagerFocus" on some node
110 // and remove it onblur
111 domClass.toggle(e.target, "pagerFocus", (e.type == "focus"));
112 },
113
114 _handleKey: function(e){
115 // summary:
116 // Handle keyboard navigation internally
117
118 var key = (e.charCode == keys.SPACE ? keys.SPACE : e.keyCode);
119 switch(key){
120
121 case keys.UP_ARROW:
122 case keys.RIGHT_ARROW:
123 case 110:
124 case 78: // key "n"
125 e.preventDefault();
126 this._next();
127 break;
128 case keys.DOWN_ARROW:
129 case keys.LEFT_ARROW:
130 case 112:
131 case 80: // key "p"
132 e.preventDefault();
133 this._previous();
134 break;
135 case keys.ENTER:
136 switch(e.target){
137 case this.pagerNext : this._next(); break;
138 case this.pagerPrevious : this._previous(); break;
139 }
140 break;
141 }
142 },
143
144 _init: function(items) {
145 this.items = items;
146 this._renderPages();
147 this._renderStatus();
148 this._renderPager();
149 },
150
151 generatePagerItem: function(/*item */ item, /* Number */ cnt){
152 // summary:
153 // this method instantiates pagerItems to be used by the pager, it is overridable to allow
154 // customizing these items
155
156 // only use getObject if its not the default _PagerItem value
157 var itemClass = this.itemClass,
158 pagerItem = (typeof itemClass == "string" ? lang.getObject(itemClass) : itemClass);
159
160 var contentContainer = domConstruct.create('div', {
161 innerHTML: item.content
162 });
163
164 return new pagerItem({
165 id: this.id + '-item-' + (cnt + 1)
166 }, contentContainer);
167 },
168
169 _renderPages: function(){
170 var pcv = this.pagerContainerView,
171 _h = (this.orientation == "horizontal");
172
173 if(_h){
174 var pagerH = geometry.getMarginBox(this.pagerContainerPager).h,
175 statusH = geometry.getMarginBox(this.pagerContainerStatus).h;
176
177 if (this.pagerPos != 'center'){
178 var addonHeight = pagerH+statusH;
179 }else{
180 var addonHeight = statusH;
181
182 var widthSub = this.pagerIconNext.width,
183 containerWidth = style.get(pcv, 'width'),
184 newWidth = containerWidth-(2*widthSub);
185
186 style.set(pcv, {
187 width: newWidth+'px',
188 marginLeft: this.pagerIconNext.width+'px',
189 marginRight: this.pagerIconNext.width+'px'
190 });
191 }
192 var totalH = style.get(this.pagerContainer, 'height') - addonHeight;
193 style.set(this.pagerContainerView, 'height', totalH+'px');
194
195 var itemSpace = Math.floor(style.get(pcv, 'width') / this.itemsPage);
196 if(this.statusPos == 'trailing'){
197 if(this.pagerPos != 'center'){
198 style.set(pcv, 'marginTop', pagerH+'px');
199 }
200 style.set(pcv, 'marginBottom', statusH+'px');
201 }else{
202 style.set(pcv, 'marginTop', statusH+'px');
203 if (this.pagerPos != 'center'){
204 style.set(pcv, 'marginTop', pagerH+'px');
205 }
206 }
207 }else{
208 var pagerW = geometry.getMarginBox(this.pagerContainerPager).w,
209 statusW = geometry.getMarginBox(this.pagerContainerStatus).w,
210 containerW = style.get(this.pagerContainer, 'width');
211
212 if(this.pagerPos != 'center'){
213 var addonWidth = pagerW + statusW;
214 }else{
215 var addonWidth = statusW,
216 heightSub = this.pagerIconNext.height,
217 containerHeight = style.get(pcv, 'height'),
218 newHeight = containerHeight - (2 * heightSub);
219
220 style.set(pcv,{
221 height: newHeight+'px',
222 marginTop: this.pagerIconNext.height+'px',
223 marginBottom: this.pagerIconNext.height+'px'
224 });
225 }
226 var totalW = style.get(this.pagerContainer, 'width') - addonWidth;
227 style.set(pcv, 'width', totalW+'px');
228
229 var itemSpace = Math.floor(style.get(pcv, 'height') / this.itemsPage);
230 if(this.statusPos == 'trailing'){
231 if (this.pagerPos != 'center'){
232 style.set(pcv, 'marginLeft', pagerW + 'px');
233 }
234 style.set(pcv, 'marginRight', statusW + 'px');
235 }else{
236 style.set(pcv, 'marginLeft', statusW + 'px');
237 if(this.pagerPos != 'center'){
238 style.set(pcv, 'marginRight', pagerW+'px');
239 }
240 }
241 }
242 var paddingLead = "padding" + (_h ? "Left" : "Top"),
243 paddingTrail = "padding" + (_h ? "Right" : "Bottom");
244
245 array.forEach(this.items, function(item, cnt){
246 var pagerItem = this.generatePagerItem(item, cnt),
247 containerProps = {};
248
249 this.pagerItems.appendChild(pagerItem.domNode);
250
251 containerProps[(_h ? "width" : "height")] = (itemSpace - this.itemSpace) + "px";
252 var p = (_h ? "height" : "width");
253 containerProps[p] = style.get(pcv, p) + "px";
254 style.set(pagerItem.containerNode, containerProps);
255
256 if(this.resizeChildren){
257 pagerItem.resizeChildren();
258 }
259 pagerItem.parseChildren();
260
261 // only display amount of items as defined in itemsPage
262 style.set(pagerItem.domNode, "position", "absolute");
263
264 if (cnt < this.itemsPage){
265 var pos = (cnt) * itemSpace,
266 trailingDir = (_h ? "left" : "top"),
267 dir = (_h ? "top" : "left");
268
269 style.set(pagerItem.domNode, dir, "0px");
270 style.set(pagerItem.domNode, trailingDir, pos+"px");
271 }else{
272 style.set(pagerItem.domNode, "top", "-1000px");
273 style.set(pagerItem.domNode, "left", "-1000px");
274 }
275
276 style.set(pagerItem.domNode, paddingTrail, (this.itemSpace/2)+"px");
277 style.set(pagerItem.domNode, paddingLead, (this.itemSpace/2)+"px");
278
279 }, this);
280 },
281
282 _renderPager: function() {
283 var tcp = this.pagerContainerPager,
284 zero = "0px",
285 _h = (this.orientation == "horizontal");
286
287 if(_h){
288
289 if(this.statusPos == 'center'){
290
291 }else if (this.statusPos == 'trailing'){
292 style.set(tcp, 'top', zero);
293 }else{
294 style.set(tcp, 'bottom', zero);
295 }
296 style.set(this.pagerNext, 'right', zero);
297 style.set(this.pagerPrevious, 'left', zero);
298
299 }else{
300
301 if (this.statusPos == 'trailing'){
302 style.set(tcp, 'left', zero);
303 }else{
304 style.set(tcp, 'right', zero);
305 }
306 style.set(this.pagerNext, 'bottom', zero);
307 style.set(this.pagerPrevious, 'top', zero);
308 }
309
310 },
311
312 _renderStatus: function() {
313 this._totalPages = Math.ceil(this.items.length / this.itemsPage);
314 // FIXME!!
315 this.iconWidth = 0;
316 this.iconHeight = 0;
317 this.iconsLoaded = 0;
318 this._iconConnects = [];
319
320 for (var i = 1; i <= this._totalPages; i++){
321 var icon = new Image();
322
323 var pointer = i;
324 on(icon, 'click', lang.hitch(this, "_pagerSkip", pointer));
325
326 this._iconConnects[pointer] = on(icon, 'load', lang.hitch(this, function(pointer){
327 this.iconWidth += icon.width;
328 this.iconHeight += icon.height;
329 this.iconsLoaded++;
330
331 if (this._totalPages == this.iconsLoaded){
332 if (this.orientation == "horizontal"){
333 if (this.statusPos == 'trailing'){
334 if (this.pagerPos == 'center'){
335 var containerHeight = style.get(this.pagerContainer, 'height'),
336 statusHeight = style.get(this.pagerContainerStatus, 'height');
337
338 style.set(this.pagerContainerPager, 'top', ((containerHeight/2)-(statusHeight/2))+'px');
339 }
340 style.set(this.pagerContainerStatus, 'bottom', '0px');
341 }else{
342 if (this.pagerPos == 'center'){
343 var containerHeight = style.get(this.pagerContainer, 'height'),
344 statusHeight = style.get(this.pagerContainerStatus, 'height');
345
346 style.set(this.pagerContainerPager, 'bottom', ((containerHeight/2)-(statusHeight/2))+'px');
347 }
348 style.set(this.pagerContainerStatus, 'top', '0px');
349 }
350
351 var position = (style.get(this.pagerContainer, 'width')/2)-(this.iconWidth/2);
352 // Bidi Support
353 style.set(this.pagerContainerStatus, this.isLeftToRight()?'paddingLeft':'paddingRight', position+'px');
354 }else{
355 if (this.statusPos == 'trailing'){
356 if (this.pagerPos == 'center'){
357 var containerWidth = style.get(this.pagerContainer, 'width'),
358 statusWidth = style.get(this.pagerContainerStatus, 'width');
359
360 style.set(this.pagerContainerPager, 'left', ((containerWidth/2)-(statusWidth/2))+'px');
361 }
362 style.set(this.pagerContainerStatus, 'right', '0px');
363 }else{
364 if (this.pagerPos == 'center'){
365 var containerWidth = style.get(this.pagerContainer, 'width'),
366 statusWidth = style.get(this.pagerContainerStatus, 'width');
367
368 style.set(this.pagerContainerPager, 'right', ((containerWidth/2)-(statusWidth/2))+'px');
369 }
370 style.set(this.pagerContainerStatus, 'left', '0px');
371 }
372 var position = (style.get(this.pagerContainer, 'height')/2)-(this.iconHeight/2);
373 style.set(this.pagerContainerStatus, 'paddingTop', position+'px');
374 }
375 }
376 this._iconConnects[pointer].remove();
377 }, pointer));
378
379 if (i==this._currentPage){
380 icon.src=this.iconPageActive;
381 }else{
382 icon.src=this.iconPage;
383 }
384 var pointer = i;
385
386 domClass.add(icon, this.orientation+'PagerIcon');
387 attr.set(icon, 'id', this.id+'-status-'+i);
388 this.pagerContainerStatus.appendChild(icon);
389
390 if (this.orientation == "vertical"){
391 style.set(icon, 'display', 'block');
392 }
393 }
394 },
395
396 _pagerSkip: function(page){
397 if (this._currentPage == page){
398 return;
399 }else{
400 // calculate whether to go left or right, take shortest way
401 var distanceP; var distanceN;
402 if (page < this._currentPage){
403 distanceP = this._currentPage - page;
404 distanceN = (this._totalPages + page) - this._currentPage;
405 }else{
406 distanceP = (this._totalPages + this._currentPage) - page;
407 distanceN = page - this._currentPage;
408 }
409
410 var b = (distanceN > distanceP);
411 this._toScroll = (b ? distanceP : distanceN);
412 var cmd = (b ? "_pagerPrevious" : "_pagerNext"),
413 handle = aspect.after(this, "onScrollEnd", lang.hitch(this, function(){
414 this._toScroll--;
415 if(this._toScroll < 1){
416 handle.remove();
417 }else{
418 this[cmd]();
419 }
420 }), true);
421
422 this[cmd]();
423 }
424 },
425
426 _pagerNext: function(){
427 if(this._anim) return;
428
429 /**
430 * fade slide out current items
431 * make sure that next items are ligned up nicely before sliding them in
432 */
433 var _anims = [];
434 for (var i = this._currentPage * this.itemsPage; i > (this._currentPage - 1) * this.itemsPage; i--){
435 if (!dom.byId(this.id+'-item-'+i)) continue;
436
437 var currentItem = dom.byId(this.id+'-item-'+i);
438 var marginBox = geometry.getMarginBox(currentItem);
439 if (this.orientation == "horizontal") {
440 var move = marginBox.l - (this.itemsPage * marginBox.w);
441 _anims.push(fx.slideTo({node: currentItem, left: move, duration: this.duration}));
442 }else{
443 var move = marginBox.t - (this.itemsPage * marginBox.h);
444 _anims.push(fx.slideTo({node: currentItem, top: move, duration: this.duration}));
445 }
446
447 }
448 var previousPage = this._currentPage;
449 if (this._currentPage == this._totalPages){
450 this._currentPage = 1;
451 }else{
452 this._currentPage++;
453 }
454
455 var cnt = this.itemsPage;
456 for (var i=this._currentPage*this.itemsPage; i>(this._currentPage-1)*this.itemsPage; i--){
457 if (dom.byId(this.id+'-item-'+i)){
458 var currentItem = dom.byId(this.id+'-item-'+i);
459 var marginBox = geometry.getMarginBox(currentItem);
460 if (this.orientation == "horizontal") {
461 var newPos = (style.get(this.pagerContainerView, 'width')+((cnt-1)*marginBox.w))-1;
462 style.set(currentItem, 'left', newPos+'px');
463 style.set(currentItem, 'top', '0px');
464
465 var move = newPos-(this.itemsPage*marginBox.w);
466 _anims.push(fx.slideTo({node: currentItem, left: move, duration: this.duration}));
467 }else{
468 newPos = (style.get(this.pagerContainerView, 'height')+((cnt-1)*marginBox.h))-1;
469 style.set(currentItem, 'top', newPos+'px');
470 style.set(currentItem, 'left', '0px');
471
472 var move = newPos-(this.itemsPage*marginBox.h);
473 _anims.push(fx.slideTo({ node: currentItem, top: move, duration: this.duration}));
474 }
475 }
476 cnt--;
477 }
478
479 this._anim = fx.combine(_anims);
480 var animHandle = aspect.after(this._anim, "onEnd", lang.hitch(this, function(){
481 delete this._anim;
482 this.onScrollEnd();
483 animHandle.remove();
484 }), true);
485
486 this._anim.play();
487
488 // set pager icons
489 dom.byId(this.id+'-status-'+previousPage).src = this.iconPage;
490 dom.byId(this.id+'-status-'+this._currentPage).src = this.iconPageActive;
491 },
492
493 _pagerPrevious: function(){
494 if(this._anim) return;
495
496 var _anims = [];
497 for (var i=this._currentPage*this.itemsPage; i>(this._currentPage-1)*this.itemsPage; i--){
498 if (!dom.byId(this.id+'-item-'+i)) continue;
499
500 var currentItem = dom.byId(this.id+'-item-'+i);
501 var marginBox = geometry.getMarginBox(currentItem);
502 if (this.orientation == "horizontal") {
503 var move = style.get(currentItem, 'left')+(this.itemsPage*marginBox.w);
504 _anims.push(fx.slideTo({node: currentItem, left: move, duration: this.duration}));
505 }else{
506 var move = style.get(currentItem, 'top')+(this.itemsPage*marginBox.h);
507 _anims.push(fx.slideTo({node: currentItem, top: move, duration: this.duration}));
508 }
509 }
510
511 var previousPage = this._currentPage;
512 if (this._currentPage == 1){
513 this._currentPage = this._totalPages;
514 }else{
515 this._currentPage--;
516 }
517
518 var cnt = this.itemsPage;
519 var j=1;
520 for (var i=this._currentPage*this.itemsPage; i>(this._currentPage-1)*this.itemsPage; i--){
521 if(dom.byId(this.id+'-item-'+i)){
522 var currentItem = dom.byId(this.id+'-item-'+i),
523 marginBox = geometry.getMarginBox(currentItem);
524
525 if (this.orientation == "horizontal") {
526 var newPos = -(j * marginBox.w) + 1;
527 style.set(currentItem, 'left', newPos+'px');
528 style.set(currentItem, 'top', '0px');
529
530 var move = ((cnt - 1) * marginBox.w);
531 _anims.push(fx.slideTo({node: currentItem, left: move, duration: this.duration}));
532
533 var move = newPos+(this.itemsPage * marginBox.w);
534 _anims.push(fx.slideTo({node: currentItem, left: move, duration: this.duration}));
535 }else{
536 newPos = -((j * marginBox.h) + 1);
537 style.set(currentItem, 'top', newPos+'px');
538 style.set(currentItem, 'left', '0px');
539
540 var move = ((cnt - 1) * marginBox.h);
541 _anims.push(fx.slideTo({node: currentItem, top: move, duration: this.duration}));
542 }
543
544 }
545 cnt--;
546 j++;
547 }
548
549 this._anim = fx.combine(_anims);
550 var animHandle = aspect.after(this._anim, "onEnd", lang.hitch(this, function(){
551 delete this._anim;
552 this.onScrollEnd();
553 animHandle.remove();
554 }), true);
555
556 this._anim.play();
557
558 // set pager icons
559 dom.byId(this.id + '-status-' + previousPage).src = this.iconPage;
560 dom.byId(this.id + '-status-' + this._currentPage).src = this.iconPageActive;
561
562 },
563
564 onScrollEnd: function(){
565 // summary:
566 // Stub Function. Fired after the slide is complete. Override or connect.
567 }
568
569 });
570
571 });