function virtualpaginate(className, chunksize, elementType) { var elementType=(typeof elementType=="undefined")? "div" : elementType this.pieces=virtualpaginate.collectElementbyClass(className, elementType) this.chunksize=(typeof chunksize=="undefined")? 1 : (chunksize>0 && chunksize < this.pieces.length)? chunksize : this.pieces.length this.pagecount=Math.ceil(this.pieces.length/this.chunksize) this.showpage(-1) this.currentpage=0 this.showpage(this.currentpage) } virtualpaginate.collectElementbyClass=function(classname, element){ var classnameRE=new RegExp("(^|\\s+)"+classname+"($|\\s+)", "i") var pieces=[] var alltags=document.getElementsByTagName(element) for (var i=0; i < alltags.length; i++){ if (typeof alltags[i].className=="string" && alltags[i].className.search(classnameRE)!=-1) pieces[pieces.length]=alltags[i] } return pieces } virtualpaginate.prototype.showpage=function(pagenumber) { var totalitems=this.pieces.length var showstartindex=pagenumber*this.chunksize var showendindex=showstartindex+this.chunksize-1 for (var i=0; i < totalitems; i++) { if (i>=showstartindex && i<=showendindex) this.pieces[i].style.display="block" else this.pieces[i].style.display="none" } this.currentpage=parseInt(pagenumber) //this.check_page_number(this.currentpage); if (this.cpspan) this.cpspan.innerHTML='Pagina '+(this.currentpage+1)+' di ' + this.pagecount } virtualpaginate.prototype.check_page_number = function (page_number) { var instanceOfBox=this // alert(page_number + " : " + this.pagecount); if(page_number == 0) { this.previous_btn.style.visibility="hidden" this.first_btn.style.visibility="hidden" this.next_btn.style.visibility="visible" this.last_btn.style.visibility="visible" } else if(page_number == (this.pagecount-1)) { this.previous_btn.style.visibility="visible" this.first_btn.style.visibility="visible" this.next_btn.style.visibility="hidden" this.last_btn.style.visibility="hidden" } else { this.previous_btn.style.visibility="visible" this.first_btn.style.visibility="visible" this.next_btn.style.visibility="visible" this.last_btn.style.visibility="visible" } } virtualpaginate.prototype.paginate_build_selectmenu=function(paginatedropdown){ var instanceOfBox=this this.selectmenupresent=1 for (var i=0; i < this.pagecount; i++) paginatedropdown.options[i] = new Option("Pagina "+(i+1)+" di "+this.pagecount, i) paginatedropdown.selectedIndex=this.currentpage paginatedropdown.onchange=function(){ instanceOfBox.showpage(this.selectedIndex) } } /** * Assegna i metodi ai pulsanti di navigazione principali * * @paginatelinks array che contiene i pulsanti di navigazione */ virtualpaginate.prototype.paginate_build_regularlinks=function(paginatelinks){ var instanceOfBox=this this.parse_navigation_link(paginatelinks) for (var i=0; i < paginatelinks.length; i++){ var currentpagerel = paginatelinks[i].getAttribute("rel") if (currentpagerel=="previous" || currentpagerel=="next" || currentpagerel=="first" || currentpagerel=="last") paginatelinks[i].onclick=function(){ instanceOfBox.navigate(this.getAttribute("rel")) return false } } } virtualpaginate.prototype.parse_navigation_link = function(paginatelinks) { //this.paginate_build_regularlinks(paginatelinks); for (var i=0; i < paginatelinks.length; i++) { var currentpagerel = paginatelinks[i].getAttribute("rel") switch (paginatelinks[i].getAttribute("rel")) { case "previous": this.previous_btn = paginatelinks[i]; break; case "next": this.next_btn = paginatelinks[i]; break; case "first": this.first_btn = paginatelinks[i]; break; case "last": this.last_btn = paginatelinks[i]; break; } } } virtualpaginate.prototype.paginate_build_flatview=function(flatviewcontainer){ var instanceOfBox=this var flatviewhtml="" for (var i=0; i < this.pagecount; i++) { flatviewhtml+=''+(i+1)+'' if ( i < (this.pagecount-1)) { flatviewhtml+=' | ' } } flatviewcontainer.innerHTML=flatviewhtml this.flatviewlinks = flatviewcontainer.getElementsByTagName("a") for (var i=0; i < this.flatviewlinks.length; i++){ this.flatviewlinks[i].onclick = function() { instanceOfBox.flatviewlinks[instanceOfBox.currentpage].className="" this.className="selected" instanceOfBox.check_page_number(this.getAttribute("rel")) instanceOfBox.showpage(this.getAttribute("rel")) return false } } //alert("virtualpaginate.prototype.paginate_build_flatview") this.flatviewlinks[this.currentpage].className="selected" this.flatviewpresent=true } virtualpaginate.prototype.paginate_build_cpinfo=function(cpspan){ this.cpspan=cpspan cpspan.innerHTML='Pagina '+(this.currentpage+1)+' di '+this.pagecount } /** * Crea la paginazione * * @divid Div che contiene la paginazione */ virtualpaginate.prototype.buildpagination=function(divid){ var instanceOfBox=this var paginatediv=document.getElementById(divid) if (this.chunksize==this.pieces.length){ paginatediv.style.display="none" return } var paginationcode = paginatediv.innerHTML if (paginatediv.getElementsByTagName("select").length>0) this.paginate_build_selectmenu(paginatediv.getElementsByTagName("select")[0]) if (paginatediv.getElementsByTagName("a").length>0) this.paginate_build_regularlinks(paginatediv.getElementsByTagName("a")) var allspans=paginatediv.getElementsByTagName("span") for (var i=0; i < allspans.length; i++){ if (allspans[i].className=="flatview") this.paginate_build_flatview(allspans[i]) else if (allspans[i].className=="paginateinfo") this.paginate_build_cpinfo(allspans[i]) } this.paginatediv=paginatediv } virtualpaginate.prototype.navigate=function(keyword){ if (this.flatviewpresent) this.flatviewlinks[this.currentpage].className="" if (keyword=="previous") this.currentpage=(this.currentpage>0)? this.currentpage-1 : (this.currentpage==0)? this.pagecount-1 : 0 else if (keyword=="next") this.currentpage=(this.currentpage < (this.pagecount-1))? this.currentpage+1 : 0 else if (keyword=="first") this.currentpage=0 else if (keyword=="last") this.currentpage=this.pieces.length-1 this.showpage(this.currentpage) this.check_page_number(this.currentpage) if (this.selectmenupresent) this.paginatediv.getElementsByTagName("select")[0].selectedIndex=this.currentpage if (this.flatviewpresent) this.flatviewlinks[this.currentpage].className="selected" }