1/** 2 * This jQuery plugin displays pagination links inside the selected elements. 3 * 4 * @author Gabriel Birke (birke *at* d-scribe *dot* de) 5 * @version 1.2 6 * @param {int} maxentries Number of entries to paginate 7 * @param {Object} opts Several options (see README for documentation) 8 * @return {Object} jQuery Object 9 */ 10jQuery.fn.pagination = function(maxentries, opts){ 11 opts = jQuery.extend({ 12 items_per_page:8, 13 num_display_entries:8, 14 current_page:0, 15 num_edge_entries:0, 16 link_to:"JavaScript:void(0)", 17 prev_text:"Prev", 18 next_text:"Next", 19 ellipse_text:"...", 20 prev_show_always:false, 21 next_show_always:false, 22 callback:function(){return false;} 23 },opts||{}); 24 25 return this.each(function() { 26 /** 27 * Calculate the maximum number of pages 28 */ 29 function numPages() { 30 return Math.ceil(maxentries/opts.items_per_page); 31 } 32 33 /** 34 * Calculate start and end point of pagination links depending on 35 * current_page and num_display_entries. 36 * @return {Array} 37 */ 38 function getInterval() { 39 var ne_half = Math.ceil(opts.num_display_entries/2); 40 var np = numPages(); 41 var upper_limit = np-opts.num_display_entries; 42 var start = current_page>ne_half?Math.max(Math.min(current_page-ne_half, upper_limit), 0):0; 43 var end = current_page>ne_half?Math.min(current_page+ne_half, np):Math.min(opts.num_display_entries, np); 44 return [start,end]; 45 } 46 47 /** 48 * This is the event handling function for the pagination links. 49 * @param {int} page_id The new page number 50 */ 51 function pageSelected(page_id, evt){ 52 current_page = page_id; 53 drawLinks(); 54 var continuePropagation = opts.callback(page_id, panel); 55 if (!continuePropagation) { 56 if (evt.stopPropagation) { 57 evt.stopPropagation(); 58 } 59 else { 60 evt.cancelBubble = true; 61 } 62 } 63 return continuePropagation; 64 } 65 66 /** 67 * This function inserts the pagination links into the container element 68 */ 69 function drawLinks() { 70 panel.empty(); 71 var interval = getInterval(); 72 var np = numPages(); 73 // This helper function returns a handler function that calls pageSelected with the right page_id 74 var getClickHandler = function(page_id) { 75 return function(evt){ return pageSelected(page_id,evt); } 76 } 77 // Helper function for generating a single link (or a span tag if it's the current page) 78 var appendItem = function(page_id, appendopts){ 79 page_id = page_id<0?0:(page_id<np?page_id:np-1); // Normalize page id to sane value 80 appendopts = jQuery.extend({text:page_id+1, classes:""}, appendopts||{}); 81 if(page_id == current_page){ 82 var lnk = jQuery("<span class='current'>"+(appendopts.text)+"</span>"); 83 } 84 else 85 { 86 var lnk = jQuery("<a>"+(appendopts.text)+"</a>") 87 .bind("click", getClickHandler(page_id)) 88 .attr('href', opts.link_to.replace(/__id__/,page_id)); 89 90 91 } 92 if(appendopts.classes){lnk.addClass(appendopts.classes);} 93 panel.append(lnk); 94 } 95 // Generate "Previous"-Link 96 if(opts.prev_text && (current_page > 0 || opts.prev_show_always)){ 97 appendItem(current_page-1,{text:opts.prev_text, classes:"prev"}); 98 } 99 // Generate starting points 100 if (interval[0] > 0 && opts.num_edge_entries > 0) 101 { 102 var end = Math.min(opts.num_edge_entries, interval[0]); 103 for(var i=0; i<end; i++) { 104 appendItem(i); 105 } 106 if(opts.num_edge_entries < interval[0] && opts.ellipse_text) 107 { 108 jQuery("<span>"+opts.ellipse_text+"</span>").appendTo(panel); 109 } 110 } 111 // Generate interval links 112 for(var i=interval[0]; i<interval[1]; i++) { 113 appendItem(i); 114 } 115 // Generate ending points 116 if (interval[1] < np && opts.num_edge_entries > 0) 117 { 118 if(np-opts.num_edge_entries > interval[1]&& opts.ellipse_text) 119 { 120 jQuery("<span>"+opts.ellipse_text+"</span>").appendTo(panel); 121 } 122 var begin = Math.max(np-opts.num_edge_entries, interval[1]); 123 for(var i=begin; i<np; i++) { 124 appendItem(i); 125 } 126 127 } 128 // Generate "Next"-Link 129 if(opts.next_text && (current_page < np-1 || opts.next_show_always)){ 130 appendItem(current_page+1,{text:opts.next_text, classes:"next"}); 131 } 132 } 133 134 // Extract current_page from options 135 var current_page = opts.current_page; 136 // Create a sane value for maxentries and items_per_page 137 maxentries = (!maxentries || maxentries < 0)?1:maxentries; 138 opts.items_per_page = (!opts.items_per_page || opts.items_per_page < 0)?1:opts.items_per_page; 139 // Store DOM element for easy access from all inner functions 140 var panel = jQuery(this); 141 // Attach control functions to the DOM element 142 this.selectPage = function(page_id){ pageSelected(page_id);} 143 this.prevPage = function(){ 144 if (current_page > 0) { 145 pageSelected(current_page - 1); 146 return true; 147 } 148 else { 149 return false;
150 } 151 } 152 this.nextPage = function(){ 153 if(current_page < numPages()-1) { 154 pageSelected(current_page+1); 155 return true; 156 } 157 else { 158 return false; 159 } 160 } 161 // When all initialisation is done, draw the links 162 drawLinks(); 163 // call callback function 164 opts.callback(current_page, this); 165 }); 166}
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.