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.1 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: 10, 13 num_display_entries: 10, 14 current_page: 0, 15 num_edge_entries: 0, 16 link_to: "#", 17 prev_text: "Prev", 18 next_text: "Next", 19 ellipse_text: "...", 20 prev_show_always: true, 21 next_show_always: true, 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 = $("<li class='active'><a href='javascript:;'>" + (appendopts.text) + "</a></li>"); 83 } 84 else { 85 var lnk = $("<li><a href='javascript:;'>" + (appendopts.text) + "</a></li>") 86 .bind("click", getClickHandler(page_id)) 87 .attr('href', opts.link_to.replace(/__id__/, page_id)); 88 89 90 } 91 if (appendopts.classes) { lnk.removeAttr('class'); lnk.addClass(appendopts.classes); } 92 panel.append(lnk); 93 } 94 // Generate "Previous"-Link 95 if (opts.prev_text && (current_page > 0 || opts.prev_show_always)) { 96 if (current_page == 0) { 97 appendItem(current_page - 1, { text: opts.prev_text, classes: "disabled" }); 98 } 99 else { 100 appendItem(current_page - 1, { text: opts.prev_text, classes: "" }); 101 } 102 } 103 // Generate starting points 104 if (interval[0] >
104 0 && opts.num_edge_entries > 0) { 105 var end = Math.min(opts.num_edge_entries, interval[0]); 106 for (var i = 0; i < end; i++) { 107 appendItem(i); 108 } 109 if (opts.num_edge_entries < interval[0] && opts.ellipse_text) { 110 jQuery("<li><a>" + opts.ellipse_text + "</a></li>").appendTo(panel); 111 } 112 } 113 // Generate interval links 114 for (var i = interval[0]; i < interval[1]; i++) { 115 appendItem(i); 116 } 117 // Generate ending points 118 if (interval[1] < np && opts.num_edge_entries > 0) { 119 if (np - opts.num_edge_entries > interval[1] && opts.ellipse_text) { 120 jQuery("<li><a href='javascript:;'>" + opts.ellipse_text + "</li>").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 if (current_page == np - 1) { 131 appendItem(current_page + 1, { text: opts.next_text, classes: "disabled" }); 132 } 133 else { 134 appendItem(current_page + 1, { text: opts.next_text, classes: "" }); 135 } 136 } 137 } 138 139 // Extract current_page from options 140 var current_page = opts.current_page; 141 // Create a sane value for maxentries and items_per_page 142 maxentries = (!maxentries || maxentries < 0) ? 1 : maxentries; 143 opts.items_per_page = (!opts.items_per_page || opts.items_per_page < 0) ? 1 : opts.items_per_page; 144 // Store DOM element for easy access from all inner functions 145 var panel = jQuery(this); 146 // Attach control functions to the DOM element 147 this.selectPage = function (page_id) { pageSelected(page_id); } 148 this.prevPage = function () { 149 if (current_page > 0) { 150 pageSelected(current_page - 1); 151 return true; 152 } 153 else { 154 return false; 155 } 156 } 157 this.nextPage = function () { 158 if (current_page < numPages() - 1) { 159 pageSelected(current_page + 1); 160 return true; 161 } 162 else { 163 return false; 164 } 165 } 166 // When all initialisation is done, draw the links 167 drawLinks(); 168 }); 169} 170 171
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.