(function ($) { 'use strict'; $.jqpaginator = function (el, options) { if(!(this instanceof $.jqpaginator)){ return new $.jqpaginator(el, options); } var self = this; self.$container = $(el); self.$container.data('jqpaginator', self); self.init = function () { if (options.first || options.prev || options.next || options.last || options.page) { options = $.extend({}, { first: '', prev: '', next: '', last: '', page: '' }, options); } self.options = $.extend({}, $.jqpaginator.defaultoptions, options); self.verify(); self.extendjquery(); self.render(); self.fireevent(this.options.currentpage, 'init'); }; self.verify = function () { var opts = self.options; if (!self.isnumber(opts.totalpages)) { throw new error('[jqpaginator] type error: totalpages'); } if (!self.isnumber(opts.totalcounts)) { throw new error('[jqpaginator] type error: totalcounts'); } if (!self.isnumber(opts.pagesize)) { throw new error('[jqpaginator] type error: pagesize'); } if (!self.isnumber(opts.currentpage)) { throw new error('[jqpaginator] type error: currentpage'); } if (!self.isnumber(opts.visiblepages)) { throw new error('[jqpaginator] type error: visiblepages'); } if (!opts.totalpages && !opts.totalcounts) { throw new error('[jqpaginator] totalcounts or totalpages is required'); } if (!opts.totalpages && !opts.totalcounts) { throw new error('[jqpaginator] totalcounts or totalpages is required'); } if (!opts.totalpages && opts.totalcounts && !opts.pagesize) { throw new error('[jqpaginator] pagesize is required'); } if (opts.totalcounts && opts.pagesize) { opts.totalpages = math.ceil(opts.totalcounts / opts.pagesize); } if (opts.currentpage < 1 || opts.currentpage > opts.totalpages) { throw new error('[jqpaginator] currentpage is incorrect'); } if (opts.totalpages < 1) { throw new error('[jqpaginator] totalpages cannot be less currentpage'); } }; self.extendjquery = function () { $.fn.jqpaginatorhtml = function (s) { return s ? this.before(s).remove() : $('

').append(this.eq(0).clone()).html(); }; }; self.render = function () { self.renderhtml(); self.setstatus(); self.bindevents(); }; self.renderhtml = function () { var html = []; var pages = self.getpages(); for (var i = 0, j = pages.length; i < j; i++) { html.push(self.builditem('page', pages[i])); } self.isenable('prev') && html.unshift(self.builditem('prev', self.options.currentpage - 1)); self.isenable('first') && html.unshift(self.builditem('first', 1)); self.isenable('statistics') && html.unshift(self.builditem('statistics')); self.isenable('next') && html.push(self.builditem('next', self.options.currentpage + 1)); self.isenable('last') && html.push(self.builditem('last', self.options.totalpages)); if (self.options.wrapper) { self.$container.html($(self.options.wrapper).html(html.join('')).jqpaginatorhtml()); } else { self.$container.html(html.join('')); } }; self.builditem = function (type, pagedata) { var html = self.options[type] .replace(/{{page}}/g, pagedata) .replace(/{{totalpages}}/g, self.options.totalpages) .replace(/{{totalcounts}}/g, self.options.totalcounts); return $(html).attr({ 'jp-role': type, 'jp-data': pagedata }).jqpaginatorhtml(); }; self.setstatus = function () { var options = self.options; if (!self.isenable('first') || options.currentpage === 1) { $('[jp-role=first]', self.$container).addclass(options.disableclass); } if (!self.isenable('prev') || options.currentpage === 1) { $('[jp-role=prev]', self.$container).addclass(options.disableclass); } if (!self.isenable('next') || options.currentpage >= options.totalpages) { $('[jp-role=next]', self.$container).addclass(options.disableclass); } if (!self.isenable('last') || options.currentpage >= options.totalpages) { $('[jp-role=last]', self.$container).addclass(options.disableclass); } $('[jp-role=page]', self.$container).removeclass(options.activeclass); $('[jp-role=page][jp-data=' + options.currentpage + ']', self.$container).addclass(options.activeclass); }; self.getpages = function () { var pages = [], visiblepages = self.options.visiblepages, currentpage = self.options.currentpage, totalpages = self.options.totalpages; if (visiblepages > totalpages) { visiblepages = totalpages; } var half = math.floor(visiblepages / 2); var start = currentpage - half + 1 - visiblepages % 2; var end = currentpage + half; if (start < 1) { start = 1; end = visiblepages; } if (end > totalpages) { end = totalpages; start = 1 + totalpages - visiblepages; } var itpage = start; while (itpage <= end) { pages.push(itpage); itpage++; } return pages; }; self.isnumber = function (value) { var type = typeof value; return type === 'number' || type === 'undefined'; }; self.isenable = function (type) { return self.options[type] && typeof self.options[type] === 'string'; }; self.switchpage = function (pageindex) { self.options.currentpage = pageindex; self.render(); }; self.fireevent = function (pageindex, type) { return (typeof self.options.onpagechange !== 'function') || (self.options.onpagechange(pageindex, type) !== false); }; self.callmethod = function (method, options) { switch (method) { case 'option': self.options = $.extend({}, self.options, options); self.verify(); self.render(); break; case 'destroy': self.$container.empty(); self.$container.removedata('jqpaginator'); break; default : throw new error('[jqpaginator] method "' + method + '" does not exist'); } return self.$container; }; self.bindevents = function () { var opts = self.options; self.$container.off(); self.$container.on('click', '[jp-role]', function () { var $el = $(this); if ($el.hasclass(opts.disableclass) || $el.hasclass(opts.activeclass)) { return; } var pageindex = +$el.attr('jp-data'); if (self.fireevent(pageindex, 'change')) { self.switchpage(pageindex); } }); }; self.init(); return self.$container; }; $.jqpaginator.defaultoptions = { wrapper: '', first: '

  • first
  • ', prev: '', next: '', last: '
  • last
  • ', page: '
  • {{page}}
  • ', totalpages: 0, totalcounts: 0, pagesize: 0, currentpage: 1, visiblepages: 7, disableclass: 'disabled', activeclass: 'active', onpagechange: null }; $.fn.jqpaginator = function () { var self = this, args = array.prototype.slice.call(arguments); if (typeof args[0] === 'string') { var $instance = $(self).data('jqpaginator'); if (!$instance) { throw new error('[jqpaginator] the element is not instantiated'); } else { return $instance.callmethod(args[0], args[1]); } } else { return new $.jqpaginator(this, args[0]); } }; })(jquery);