/*
* jquery pager plugin
* version 1.0 (11/20/2010)
*/
(function($) {
$.fn.pager = function(options) {
var opts = $.extend({}, $.fn.pager.defaults, options);
return this.each(function() {
// empty out the destination element and then render out the pager with the supplied options
$(this).empty().append(renderpager(parseint(options.pagenumber), parseint(options.pagecount), options.buttonclickcallback));
// specify correct cursor activity
$('.pages li').mouseover(function() { document.body.style.cursor = "pointer"; }).mouseout(function() { document.body.style.cursor = "auto"; });
});
};
// render and return the pager with the supplied options
function renderpager(pagenumber, pagecount, buttonclickcallback) {
// setup $pager to hold render
var $pager = $('
');
// add in the previous and next buttons
$pager.append(renderbutton('<<', pagenumber, pagecount, buttonclickcallback)).append(renderbutton('<', pagenumber, pagecount, buttonclickcallback));
// pager currently only handles 10 viewable pages ( could be easily parameterized, maybe in next version ) so handle edge cases
var startpoint = 1;
var endpoint = 5;
if (pagenumber >= 5) {
startpoint = pagenumber - 2;
endpoint = pagenumber + 2;
}
if (endpoint > pagecount) {
startpoint = pagecount - 4;
endpoint = pagecount;
}
if (startpoint < 1) {
startpoint = 1;
}
// loop thru visible pages and render buttons
for (var page = startpoint; page <= endpoint; page++) {
var currentbutton = $('' + (page) + '');
page == pagenumber ? currentbutton.addclass('pgcurrent') : currentbutton.click(function() { buttonclickcallback(this.firstchild.data); });
currentbutton.appendto($pager);
}
// render in the next and 末页 buttons before returning the whole rendered control back.
$pager.append(renderbutton('>', pagenumber, pagecount, buttonclickcallback)).append(renderbutton('>>', pagenumber, pagecount, buttonclickcallback));
return $pager;
}
// renders and returns a 'specialized' button, ie 'next', 'previous' etc. rather than a page number button
function renderbutton(buttonlabel, pagenumber, pagecount, buttonclickcallback) {
var $button = $('' + buttonlabel + '');
var destpage = 1;
// work out destination page for required button type
switch (buttonlabel) {
case "<<":
destpage = 1;
break;
case "<":
destpage = pagenumber - 1;
break;
case ">":
destpage = pagenumber + 1;
break;
case ">>":
destpage = pagecount;
break;
}
// disable and 'grey' out buttons if not needed.
if (buttonlabel == "<<" || buttonlabel == "<") {
pagenumber <= 1 ? $button.addclass('pgempty') : $button.click(function() { buttonclickcallback(destpage); });
}
else {
pagenumber >= pagecount ? $button.addclass('pgempty') : $button.click(function() { buttonclickcallback(destpage); });
}
return $button;
}
// pager defaults. hardly worth bothering with in this case but used as placeholder for expansion in the next version
$.fn.pager.defaults = {
pagenumber: 1,
pagecount: 1
};
})(jquery);