1 | /* file: pageNavigator.js - v1.0.0 | minified on 2011/01/27 with http://jscompress.com/ */ |
---|
2 | function pagesNavigator(container) |
---|
3 | {var itemsId={first:'iNavFirst',last:'iNavLast',previous:'iNavPrevious',next:'iNavNext',pageNumber:'iNavPage',morePrevious:'iNavPrevMore',moreNext:'iNavNextMore',container:container,},pages={num:0,current:1,},options={numberItem:0,itemPerPage:25,defaultPage:0,displayNumPage:7,displayFirst:true,displayLast:true,displayPrevious:true,displayNext:true,hideMode:'1,<',textFirst:'<<',textLast:'>>',textPrevious:'<',textNext:'>',textMore:'...',onPageChange:null,classActive:'',classInactive:'',classDisabled:'',};this.doAction=function(fct) |
---|
4 | {switch(fct) |
---|
5 | {case'setOptions':if(arguments.length==2) |
---|
6 | {setOptions(arguments[1]);} |
---|
7 | break;}};var calcNumPages=function(numItems,numPerPage) |
---|
8 | {return(Math.ceil(numItems/numPerPage));};var setOptions=function(optionsToSet) |
---|
9 | {if(typeof optionsToSet=='object') |
---|
10 | {options=jQuery.extend(options,optionsToSet);if(options.numberItem<=0)options.numberItem=0;if(options.itemPerPage<=0)options.itemPerPage=25;if(options.displayNumPage<=2)options.displayNumPage=8;pages.num=calcNumPages(options.numberItem,options.itemPerPage);if(options.defaultPage>0&&options.defaultPage<=pages.num)pages.current=options.defaultPage;build();}};var build=function() |
---|
11 | {re=/1/;if((re.exec(options.hideMode)==null)==false&&pages.num==1) |
---|
12 | {$('#'+itemsId.container).html('');return('');} |
---|
13 | var content="<ul id='"+container+"PageNavigator'>";styleLI=" list-style:none;float:left; ";re=/</;if((re.exec(options.hideMode)==null)==false&&pages.num<=options.displayNumPage) |
---|
14 | {hideFPNL='display:none;';} |
---|
15 | else |
---|
16 | {hideFPNL='';} |
---|
17 | pnClass="class='cPnInactive "+options.classInactive+"'";if(options.displayFirst)content+="<li style='"+styleLI+hideFPNL+"' id='"+itemsId.first+"' "+pnClass+">"+options.textFirst+"</li>";if(options.displayPrevious)content+="<li style='"+styleLI+hideFPNL+"' id='"+itemsId.previous+"' "+pnClass+">"+options.textPrevious+"</li>";content+="<li style='"+styleLI+"display:none;' id='"+itemsId.morePrevious+"' class='cPnDisabled "+options.classDisabled+"'>"+options.textMore+"</li>";for(i=1;i<=pages.num;i++) |
---|
18 | {content+="<li style='"+styleLI+"display:none;' id='"+itemsId.pageNumber+i+"' "+pnClass+">"+i+"</li>";} |
---|
19 | content+="<li style='"+styleLI+";display:none;' id='"+itemsId.moreNext+"' class='cPnDisabled "+options.classDisabled+"'>"+options.textMore+"</li>";if(options.displayNext)content+="<li style='"+styleLI+hideFPNL+"' id='"+itemsId.next+"' "+pnClass+">"+options.textNext+"</li>";if(options.displayLast)content+="<li style='"+styleLI+hideFPNL+"' id='"+itemsId.last+"' "+pnClass+">"+options.textLast+"</li>";content+="</ul>";$('#'+itemsId.container).css('visibility','hidden').html(content);$('.cPnInactive').bind('click',onChangePage);displayNav();$('#'+itemsId.container).css('visibility','visible');};var displayNav=function() |
---|
20 | {prev=Math.ceil((options.displayNumPage-1)/2);if(pages.current-prev<=0) |
---|
21 | {prev=pages.current-1;} |
---|
22 | next=options.displayNumPage-1-prev;if(pages.current+next>=pages.num) |
---|
23 | {prev+=(pages.current+next-pages.num);next=pages.num-pages.current;} |
---|
24 | prev=pages.current-prev;next=pages.current+next;if(prev>1) |
---|
25 | {$('#'+itemsId.morePrevious).css('display','block');} |
---|
26 | else |
---|
27 | {$('#'+itemsId.morePrevious).css('display','none');} |
---|
28 | if(next<pages.num) |
---|
29 | {$('#'+itemsId.moreNext).css('display','block');} |
---|
30 | else |
---|
31 | {$('#'+itemsId.moreNext).css('display','none');} |
---|
32 | $('#'+itemsId.container+' ul li').each(function() |
---|
33 | {id=-1;if(!(this.id==itemsId.first||this.id==itemsId.previous||this.id==itemsId.next||this.id==itemsId.last||this.id==itemsId.morePrevious||this.id==itemsId.moreNext)) |
---|
34 | {re=/[0-9]*$/i;id=re.exec(this.id)[0];if(id>=prev&&id<=next) |
---|
35 | {$(this).css('display','block');} |
---|
36 | else |
---|
37 | {$(this).css('display','none');} |
---|
38 | if(id==pages.current) |
---|
39 | {$(this).addClass('cPnActive '+options.classActive).removeClass('cPnInactive '+options.classInactive);} |
---|
40 | else |
---|
41 | {$(this).addClass('cPnInactive '+options.classInactive).removeClass('cPnActive '+options.classActive);}} |
---|
42 | if(((this.id==itemsId.first||this.id==itemsId.previous)&&pages.current==1)||((this.id==itemsId.last||this.id==itemsId.next)&&pages.current==pages.num)||(this.id==itemsId.morePrevious||this.id==itemsId.moreNext)) |
---|
43 | {$(this).addClass('cPnDisabled '+options.classDisabled).removeClass('cPnInactive '+options.classInactive);} |
---|
44 | else |
---|
45 | {$(this).addClass('cPnInactive '+options.classInactive).removeClass('cPnDisabled '+options.classDisabled);}});};var onChangePage=function(event) |
---|
46 | {if(event.target.id==itemsId.first) |
---|
47 | {pages.current=1;} |
---|
48 | else if(event.target.id==itemsId.previous) |
---|
49 | {pages.current--;} |
---|
50 | else if(event.target.id==itemsId.next) |
---|
51 | {pages.current++;} |
---|
52 | else if(event.target.id==itemsId.last) |
---|
53 | {pages.current=pages.num;} |
---|
54 | else |
---|
55 | {re=/[0-9]*$/i;page=re.exec(event.target.id)[0];if(page==pages.current)return(false);pages.current=eval(page);} |
---|
56 | if(pages.current<=0)pages.current=1;if(pages.current>=pages.num)pages.current=pages.num;displayNav();if(options.onPageChange!=null&&jQuery.isFunction(options.onPageChange))options.onPageChange(pages.current);};if(arguments.length==2) |
---|
57 | {setOptions(arguments[1]);}} |
---|