1// ------------------------------------------------------------------- 2// Virtual Pagination Script- By Dynamic Drive, available at: http://www.dynamicdrive.com 3// Updated: Nov 21st, 2008 to v2.0 4// ** Adds ability to define multiple pagination DIVs (the secondary DIVs mirror primary DIV's contents) 5// ** Last viewed page persistence, so last viewed page can be remembered/ recalled within browser session. 6// ** Improvements to instance.navigate() to select a page using an arbitrary link or inside another script. 7// ** Ability to select a page using a URL parameter (ie: target.htm?virtualpiececlass=index). 8 9// Updated: Oct 19th, 2009 to v2.1 10// ** New wraparound:true/false option added, which when false disables moving back/forth beyond first and last content, respectively 11// 12// PUBLIC: virtualpaginate() 13// Main Virtual Paginate Object function. 14// ------------------------------------------------------------------- 15 16document.write('<style type="text/css">' //write out CSS for class ".hidepeice" that hides pieces of contents within pages 17 +'.hidepiece{display:none}\n' 18 +'@media print{.hidepiece{display:block !important;}}\n' 19 +'</style>') 20 21function virtualpaginate(config){ //config: {piececlass:, piececontainer:, pieces_per_page:, defaultpage:, wraparound:, persist} 22 this.piececlass=config.piececlass 23 var elementType=(typeof config.piececontainer=="undefined")? "div" : config.piececontainer //The type of element used to divide up content into pieces. Defaults to "div" 24 this.pieces=virtualpaginate.collectElementbyClass(config.piececlass, elementType) //get total number of divs matching class name 25 //Set this.chunksize: 1 if "chunksize" param is undefined, "chunksize" if it's less than total pieces available, or simply total pieces avail (show all) 26 this.chunksize=(typeof config.pieces_per_page=="undefined")? 1 : (config.pieces_per_page>0 && config.pieces_per_page<this.pieces.length)? config.pieces_per_page : this.pieces.length 27 this.pagecount=Math.ceil(this.pieces.length/this.chunksize) //calculate number of "pages" needed to show the divs 28 this.wraparound=config.wraparound || false 29 this.paginatediv=[], this.flatviewlinks=[], this.cpspan=[], this.selectmenu=[], this.prevlinks=[], this.nextlinks=[] 30 this.persist=config.persist 31 var persistedpage=virtualpaginate.getCookie("dd_"+this.piececlass) || 0 32 var urlselectedpage=virtualpaginate.urlparamselect(this.piececlass) //returns null or index from: mypage.htm?piececlass=index 33 this.currentpage=(typeof urlselectedpage=="number")? urlselectedpage : ((this.persist)? persistedpage : config.defaultpage) 34 this.currentpage=(this.currentpage<this.pagecount)? parseInt(this.currentpage) : 0 //ensure currentpage is within range of available pages 35 this.showpage(this.currentpage) //Show selected page 36} 37 38// ------------------------------------------------------------------- 39// PUBLIC: navigate(keyword)- Calls this.showpage() based on parameter passed (0=page1, 1=page2 etc, "next", "first", or "last") 40// ------------------------------------------------------------------- 41 42virtualpaginate.prototype.navigate=function(keyword){ 43 if ((!this.wraparound && keyword=="previous" && this.currentpage==0) || (!this.wraparound && keyword=="next" && this.currentpage==this.pagecount-1)) 44 return //exit immediately if wraparound is disabled and prev link is clicked when on 1st content or last link is clicked when on final content 45 var prevlinkindex=this.currentpage //Get index of last clicked on page 46 if (keyword=="previous") 47 this.currentpage=(this.currentpage>0)? this.currentpage-1 : (this.currentpage==0)? this.pagecount-1 : 0 48 else if (keyword=="next") 49 this.currentpage=(this.currentpage<this.pagecount-1)? this.currentpage+1 : 0 50 else if (keyword=="first") 51 this.currentpage=0 52 else if (keyword=="last") 53 this.currentpage=this.pagecount-1 //last page number 54 else 55 this.currentpage=parseInt(keyword) 56 this.currentpage=(this.currentpage<this.pagecount)? this.currentpage : 0 //ensure pagenumber is within range of available pages 57 this.showpage(this.currentpage) 58 for (var p=0; p<this.paginatediv.length; p++){ //loop through all pagination DIVs 59 if (this.flatviewpresent) 60 this.flatviewlinks[p][prevlinkindex].className="" //"Unhighlight" previous page (before this.currentpage increments) 61 if (this.selectmenupresent) 62 this.selectmenu[p].selectedIndex=this.currentpage 63 if (this.flatviewpresent) 64 this.flatviewlinks[p][this.currentpage].className="selected" //"Highlight" current page 65 } 66 if (!this.wraparound){ 67 for (var i=0; i<this.prevlinks.length; i++) //add or remove "disable" class from prev links depending on current page number 68 virtualpaginate.setcssclass(this.prevlinks[i], "disabled", (this.currentpage==0)? "add" : "remove") 69 for (var i=0; i<this.nextlinks.length; i++) //add or remove "disable" class from next links depending on current page number 70 virtualpaginate.setcssclass(this.nextlinks[i], "disabled", (this.currentpage==(this.pagecount-1))? "add" : "remove") 71 } 72} 73 74 75// ------------------------------------------------------------------- 76// PUBLIC: buildpagination()- Create pagination interface by calling one or more of the paginate_build_() functions 77// ------------------------------------------------------------------- 78 79virtualpaginate.prototype.buildpagination=function(divids, optnavtext){
80 var divids=(typeof divids=="string")? [divids] : divids //force divids to be an array of ids 81 var primarypaginatediv=divids.shift() //get first id within divids[] 82 var paginaterawHTML=document.getElementById(primarypaginatediv).innerHTML 83 this.paginate_build(primarypaginatediv, 0, optnavtext) 84 for (var i=0; i<divids.length; i++){ 85 document.getElementById(divids[i]).innerHTML=paginaterawHTML 86 this.paginate_build(divids[i], i+1, optnavtext) 87 } 88} 89 90// ------------------------------------------------------------------- 91// PRIVATE utility functions 92// ------------------------------------------------------------------- 93 94virtualpaginate.collectElementbyClass=function(classname, element){ //Returns an array containing DIVs with specified classname. Requires setcssclass() 95 if (document.querySelectorAll){ 96 var pieces=document.querySelectorAll(element+"."+classname) //return pieces as HTMLCollection 97 } 98 else{ 99 var pieces=[] 100 var alltags=document.getElementsByTagName(element) 101 for (var i=0; i<alltags.length; i++){ 102 if (virtualpaginate.setcssclass(alltags[i], classname, "check")) //if element carries class name in question 103 pieces[pieces.length]=alltags[i] //return pieces as array 104 } 105 } 106 return pieces 107} 108 109// ------------------------------------------------------------------- 110// PRIVATE: setcssclass() method- Checks, Add, or Removes a class from an element 111// ------------------------------------------------------------------- 112 113virtualpaginate.setcssclass=function(el, targetclass, action){ 114 var needle=new RegExp("(^|\\s+)"+targetclass+"($|\\s+)", "ig") 115 if (action=="check") 116 return needle.test(el.className) 117 else if (action=="remove") 118 el.className=el.className.replace(needle, "") 119 else if (action=="add" && !needle.test(el.className)) 120 el.className+=" "+targetclass 121} 122 123virtualpaginate.urlparamselect=function(vpclass){ 124 var result=window.location.search.match(new RegExp(vpclass+"=(\\d+)", "i")) //check for "?piececlass=2" in URL 125 return (result==null)? null : parseInt(RegExp.$1) //returns null or index, where index (int) is the selected virtual page's index 126} 127 128virtualpaginate.getCookie=function(Name){ 129 var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair 130 if (document.cookie.match(re)) //if cookie found 131 return document.cookie.match(re)[0].split("=")[1] //return its value 132 return null 133} 134 135virtualpaginate.setCookie=function(name, value){ 136 document.cookie = name+"="+value 137} 138 139// ------------------------------------------------------------------- 140// PRIVATE: showpage(pagenumber)- Shows a page based on parameter passed (0=page1, 1=page2 etc) 141// ------------------------------------------------------------------- 142 143virtualpaginate.prototype.showpage=function(pagenumber){ 144 var totalitems=this.pieces.length //total number of broken up divs 145 var showstartindex=pagenumber*this.chunksize //array index of div to start showing per pagenumber setting 146 var showendindex=showstartindex+this.chunksize-1 //array index of div to stop showing after per pagenumber setting 147 for (var i=0; i<totalitems; i++){ 148 if (i>=showstartindex && i<=showendindex) 149 this.pieces[i].style.display="block" 150 else 151 this.pieces[i].style.display="none" 152 } 153 if (this.persist){ //if persistence enabled 154 virtualpaginate.setCookie("dd_"+this.piececlass, this.currentpage) 155 } 156 if (this.cpspan.length>0){ //if <span class="paginateinfo> element is present, update it with the most current info (ie: Page 3/4) 157 for (var p=0; p<this.cpspan.length; p++) 158 this.cpspan[p].innerHTML='Page '+(this.currentpage+1)+'/'+this.pagecount 159 } 160} 161 162// ------------------------------------------------------------------- 163// PRIVATE: build() methods- Various methods to create pagination interfaces 164// paginate_paginate_build()- Main build() paginate function 165// paginate_output_flatview()- Accepts <span class="flatview"> element and populates it with sequential pagination links 166// paginate_paginate_build_flatview()- Parses the modified <span class="flatview"> element and assigns click behavior to the pagination links 167// paginate_build_selectmenu(paginatedropdown)- Accepts an empty SELECT element and turns it into pagination menu 168// paginate_build_regularlinks(paginatelinks)- Accepts a collection of links and screens out/ creates pagination out of ones with specific "rel" attr 169// paginate_build_cpinfo(cpspan)- Accepts <span class="paginateinfo"> element and displays current page info (ie: Page 1/4) 170// ------------------------------------------------------------------- 171 172virtualpaginate.prototype.paginate_build=function(divid, divpos, optnavtext){ 173 var instanceOfBox=this
174 var paginatediv=document.getElementById(divid) 175 if (this.chunksize==this.pieces.length){ //if user has set to display all pieces at once, no point in creating pagination div 176 paginatediv.style.display="none" 177 return 178 } 179 var paginationcode=paginatediv.innerHTML //Get user defined, "unprocessed" HTML within paginate div 180 if (paginatediv.getElementsByTagName("select").length>0) //if there's a select menu in div 181 this.paginate_build_selectmenu(paginatediv.getElementsByTagName("select")[0], divpos, optnavtext) 182 if (paginatediv.getElementsByTagName("a").length>0) //if there are links defined in div 183 this.paginate_build_regularlinks(paginatediv.getElementsByTagName("a")) 184 var allspans=paginatediv.getElementsByTagName("span") //Look for span tags within passed div 185 for (var i=0; i<allspans.length; i++){ 186 if (allspans[i].className=="flatview") 187 this.paginate_output_flatview(allspans[i], divpos, optnavtext) 188 else if (allspans[i].className=="paginateinfo") 189 this.paginate_build_cpinfo(allspans[i], divpos) 190 } 191 this.paginatediv[divpos]=paginatediv 192} 193 194virtualpaginate.prototype.paginate_output_flatview=function(flatviewcontainer, divpos, anchortext){ 195 var flatviewhtml="" 196 var anchortext=anchortext || new Array() 197 for (var i=0; i<this.pagecount; i++){ 198 if (typeof anchortext[i]!="undefined") //if custom anchor text for this link exists 199 flatviewhtml+='<a href="#flatview" rel="'+i+'">'+anchortext[i]+'</a> ' //build pagination link using custom anchor text 200 else 201 flatviewhtml+='<a href="#flatview" rel="'+i+'">'+(i+1)+'</a> ' //build pagination link using auto incremented sequential number instead 202 } 203 flatviewcontainer.innerHTML=flatviewhtml 204 this.paginate_build_flatview(flatviewcontainer, divpos, anchortext) 205} 206 207virtualpaginate.prototype.paginate_build_flatview=function(flatviewcontainer, divpos, anchortext){ 208 var instanceOfBox=this 209 var flatviewhtml="" 210 this.flatviewlinks[divpos]=flatviewcontainer.getElementsByTagName("a") 211 for (var i=0; i<this.flatviewlinks[divpos].length; i++){ 212 this.flatviewlinks[divpos][i].onclick=function(){ 213 var prevlinkindex=instanceOfBox.currentpage //Get index of last clicked on flatview link 214 var curlinkindex=parseInt(this.getAttribute("rel")) 215 instanceOfBox.navigate(curlinkindex) 216 return false 217 } 218 } 219 this.flatviewlinks[divpos][this.currentpage].className="selected" //"Highlight" current flatview link 220 this.flatviewpresent=true //indicate flat view links are present 221} 222 223virtualpaginate.prototype.paginate_build_selectmenu=function(paginatedropdown, divpos, anchortext){ 224 var instanceOfBox=this 225 var anchortext=anchortext || new Array() 226 this.selectmenupresent=1 227 for (var i=0; i<this.pagecount; i++){ 228 if (typeof anchortext[i]!="undefined") //if custom anchor text for this link exists, use anchor text as each OPTION's text 229 paginatedropdown.options[i]=new Option(anchortext[i], i) 230 else //else, use auto incremented, sequential numbers 231 paginatedropdown.options[i]=new Option("Page "+(i+1)+" of "+this.pagecount, i) 232 } 233 paginatedropdown.selectedIndex=this.currentpage 234 setTimeout(function(){paginatedropdown.selectedIndex=instanceOfBox.currentpage}, 500) //refresh currently selected option (for IE's sake) 235 paginatedropdown.onchange=function(){ 236 instanceOfBox.navigate(this.selectedIndex) 237 } 238 this.selectmenu[divpos]=paginatedropdown 239 this.selectmenu[divpos].selectedIndex=this.currentpage //"Select" current page's corresponding option 240} 241 242virtualpaginate.prototype.paginate_build_regularlinks=function(paginatelinks){ 243 var instanceOfBox=this 244 for (var i=0; i<paginatelinks.length; i++){ 245 var currentpagerel=paginatelinks[i].getAttribute("rel") 246 if (/^(previous)|(next)|(first)|(last)$/.test(currentpagerel)){ //screen for these "rel" values 247 paginatelinks[i].onclick=function(){ 248 instanceOfBox.navigate(this.getAttribute("rel")) 249 return false 250 } 251 } 252 if (currentpagerel=="previous" || paginatelinks[i].href.indexOf("previous")!=-1){ //check if this is a "previous" link 253 if (!this.wraparound && this.currentpage==0) //if current page is first page, disable "prev" link 254 virtualpaginate.setcssclass(paginatelinks[i], "disabled", "add") 255 this.prevlinks.push(paginatelinks[i]) 256 } 257 else if (currentpagerel=="next" || paginatelinks[i].href.indexOf("next")!=-1){ //check if this is a "next" link 258 if (!this.wraparound && this.currentpage==this.pagecount-1) //if current page is last page, disable "next" link 259 virtualpaginate.setcssclass(paginatelinks[i], "disabled", "add") 260 this.nextlinks.push(paginatelinks[i]) 261 } 262 263 } 264} 265
266virtualpaginate.prototype.paginate_build_cpinfo=function(cpspan, divpos){ 267 this.cpspan[divpos]=cpspan 268 cpspan.innerHTML='Page '+(this.currentpage+1)+'/'+this.pagecount 269} 270 271 272
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.