forked from josecebe/twbs-pagination
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.twbsPagination.min.js
1 lines (1 loc) · 4.19 KB
/
jquery.twbsPagination.min.js
1
(function(e,d,a,f){var b=e.fn.twbsPagination;var c=function(i,g){this.$element=e(i);this.options=e.extend({},e.fn.twbsPagination.defaults,g);if(this.options.startPage<1||this.options.startPage>this.options.totalPages){throw new Error("Start page option is incorrect")}if(this.options.totalPages<=0){throw new Error("Total pages option cannot be less 1 (one)!")}if(this.options.totalPages<this.options.visiblePages){this.options.visiblePages=this.options.totalPages}if(this.options.onPageClick instanceof Function){this.$element.first().bind("page",this.options.onPageClick)}var h=(typeof this.$element.prop==="function")?this.$element.prop("tagName"):this.$element.attr("tagName");if(h==="UL"){this.$listContainer=this.$element}else{this.$listContainer=e("<ul></ul>")}this.$listContainer.addClass(this.options.paginationClass);if(h!=="UL"){this.$element.append(this.$listContainer)}this.render(this.getPages(this.options.startPage));this.setupEvents();return this};c.prototype={constructor:c,destroy:function(){this.$element.empty();this.$element.removeData("twbs-pagination");this.$element.unbind("page");return this},show:function(g){if(g<1||g>this.options.totalPages){throw new Error("Page is incorrect.")}this.render(this.getPages(g));this.setupEvents();this.$element.trigger("page",g);return this},buildListItems:function(g){var j=e();if(this.options.first){j=j.add(this.buildItem("first",1))}if(this.options.prev){var l=g.currentPage>1?g.currentPage-1:1;j=j.add(this.buildItem("prev",l))}for(var h=0;h<g.numeric.length;h++){j=j.add(this.buildItem("page",g.numeric[h]))}if(this.options.next){var k=g.currentPage>=this.options.totalPages?this.options.totalPages:g.currentPage+1;j=j.add(this.buildItem("next",k))}if(this.options.last){j=j.add(this.buildItem("last",this.options.totalPages))}return j},buildItem:function(i,j){var h=e("<li></li>"),k=e("<a></a>"),g=null;h.addClass(i);h.data("page",j);switch(i){case"page":g=j;break;case"first":g=this.options.first;break;case"prev":g=this.options.prev;break;case"next":g=this.options.next;break;case"last":g=this.options.last;break;default:break}h.append(k.attr("href",this.href(j)).html(g));return h},getPages:function(j){var g=[];var k=Math.floor(this.options.visiblePages/2);var l=j-k+1-this.options.visiblePages%2;var h=j+k;if(l<=0){l=1;h=this.options.visiblePages}if(h>this.options.totalPages){l=this.options.totalPages-this.options.visiblePages+1;h=this.options.totalPages}var i=l;while(i<=h){g.push(i);i++}return{currentPage:j,numeric:g}},render:function(g){this.$listContainer.children().remove();this.$listContainer.append(this.buildListItems(g));this.$listContainer.find(".page").removeClass("active");this.$listContainer.find(".page").filter(function(){return e(this).data("page")===g.currentPage}).addClass("active");if(g.currentPage===1){this.$listContainer.find(".prev a,.first a").attr("href","javascript:void(0);")}if(g.currentPage===this.options.totalPages){this.$listContainer.find(".next a,.last a").attr("href","javascript:void(0);")}this.$listContainer.find(".first").toggleClass("disabled",g.currentPage===1);this.$listContainer.find(".last").toggleClass("disabled",g.currentPage===this.options.totalPages);this.$listContainer.find(".prev").toggleClass("disabled",g.currentPage===1);this.$listContainer.find(".next").toggleClass("disabled",g.currentPage===this.options.totalPages)},setupEvents:function(){var g=this;this.$listContainer.find("li").each(function(){var h=e(this);h.off();if(h.hasClass("disabled")||h.hasClass("active")){return}h.click(function(){g.show(parseInt(h.data("page"),10))})})},href:function(g){return this.options.href.replace(this.options.hrefVariable,g)}};e.fn.twbsPagination=function(i){var h=Array.prototype.slice.call(arguments,1);var k;var l=e(this);var j=l.data("twbs-pagination");var g=typeof i==="object"&&i;if(!j){l.data("twbs-pagination",(j=new c(this,g)))}if(typeof i==="string"){k=j[i].apply(j,h)}return(k===f)?l:k};e.fn.twbsPagination.defaults={totalPages:0,startPage:1,visiblePages:5,href:"javascript:void(0);",hrefVariable:"{{number}}",first:"First",prev:"Previous",next:"Next",last:"Last",paginationClass:"pagination",onPageClick:null};e.fn.twbsPagination.Constructor=c;e.fn.twbsPagination.noConflict=function(){e.fn.twbsPagination=b;return this}})(jQuery,window,document);