-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimple-bootstrap-paginator.min.js
1 lines (1 loc) · 4.04 KB
/
simple-bootstrap-paginator.min.js
1
!function(t,a,e,n){"use strict";var s="simplePaginator",i={totalPages:7,maxButtonsVisible:5,currentPage:1,nextLabel:"next",prevLabel:"prev",firstLabel:"first",lastLabel:"last",clickCurrentPage:!0,pageChange:function(t){console.log(t)}};function l(a,e){e=o(e),this.element=a,this.options=t.extend({},i,e),this._defaults=i,this._name=s,c(this.options),this.setTotalPages=function(t){if(t<1)throw"Total Pages can't be less than 1";this.options.totalPages=t,c(this.options),g(this)},this.changePage=function(t){if(t<1)throw"Page can't be less than 1";if(t>this.options.totalPages)throw"Page is bigger than total pages";this.options.currentPage=t,this.options.pageChange(t),g(this)},this.hide=function(){t(this.element).empty()},this.setPageChangeFn=function(t,a){if("function"!=typeof t)throw"pageChange is not a function";this.options.pageChange=t,this.options.currentPage=a||1,this.options.pageChange(this.options.currentPage)},this.init()}l.prototype.init=function(){this.options.clickCurrentPage?this.changePage(this.options.currentPage):g(this)};var o=function(t){if(!t.totalPages)throw"totalPages is not defined";if(t.totalPages=r(t.totalPages),!t.pageChange)throw"function pageChange() its not defined";if("function"!=typeof t.pageChange)throw"pageChange is not a function";if(t.maxButtonsVisible&&(t.maxButtonsVisible=r(t.maxButtonsVisible)),t.currentPage&&(t.currentPage=r(t.currentPage)),t.firstLabel&&(t.firstLabel=t.firstLabel.toString()),t.nextLabel&&(t.nextLabel=t.nextLabel.toString()),t.prevLabel&&(t.prevLabel=t.prevLabel.toString()),t.lastLabel&&(t.lastLabel=t.lastLabel.toString()),t.clickCurrentPage&&"boolean"!=typeof t.clickCurrentPage)throw"clickCurrentPage is must be a boolean";return t},r=function(t){if("number"!=typeof t)throw t+" is not a number";if(t<1)throw t+" must be bigger than 0";return Math.floor(t)},c=function(t){t.totalButtons=Math.min(t.totalPages,t.maxButtonsVisible)},g=function(a){var e=a.options;t(a.element).empty();var n='<ul class="pagination">';n=1===e.currentPage?(n=n.concat('<li class="page-item disabled"><a class="page-link">',e.firstLabel,"</a></li>")).concat('<li class="page-item disabled"><a class="page-link">',e.prevLabel,"</a></li>"):(n=n.concat('<li class="page-item"><a class="page-link" style="cursor:pointer;">',e.firstLabel,"</a></li>")).concat('<li class="page-item"><a class="page-link" style="cursor:pointer;">',e.prevLabel,"</a></li>");for(var s=1,i=s+e.totalButtons-1;e.currentPage<s||e.currentPage>i;)e.currentPage>i?(s+=e.totalButtons,(i+=e.totalButtons)>e.totalPages&&(s-=i-e.totalPages,i=e.totalPages)):(s-=e.totalButtons,i-=e.totalButtons,s<0&&(i+=s+e.totalButtons,s=1));e.currentPage===i&&1!=e.totalPages&&(i=(s=e.currentPage-1)+e.totalButtons-1)>=e.totalPages&&(s-=i-e.totalPages,i=e.totalPages),s===e.currentPage&&1!=e.totalPages&&1!=e.currentPage&&(s=(i=e.currentPage+1)-(e.totalButtons-1));for(var l=s;l<=i;l++)n=l===e.currentPage?n.concat('<li class="page-item active"><a class="page-link" style="cursor:pointer;">'+l+"</a></li>"):n.concat('<li class="page-item"><a class="page-link" style="cursor:pointer;">'+l+"</a></li>");n=(n=e.currentPage==e.totalPages?(n=n.concat('<li class="page-item disabled"><a class="page-link">',e.nextLabel,"</a></li>")).concat('<li class="page-item disabled"><a class="page-link">',e.lastLabel,"</a></li>"):(n=n.concat('<li class="page-item"><a class="page-link" style="cursor:pointer;">',e.nextLabel,"</a></li>")).concat('<li class="page-item"><a class="page-link" style="cursor:pointer;">',e.lastLabel,"</a></li>")).concat("</ul>"),t(a.element).append(n),t(a.element).find("ul li").not(".disabled").not(".active").click(function(){var n,s=t(this).find("a").text();switch(s){case e.firstLabel:n=1;break;case e.prevLabel:n=e.currentPage-1;break;case e.nextLabel:n=e.currentPage+1;break;case e.lastLabel:n=e.totalPages;break;default:n=parseInt(s)}a.changePage(n)})};t.fn[s]=function(a){var e=t.makeArray(arguments).slice(1);return this.each(function(){var n=t.data(this,"plugin_"+s);if(!n){var i=new l(this,a);return t.data(this,"plugin_"+s,i),i}if(!n[a])throw"Method "+a+"doesn't exists";n[a].apply(n,e)})}}(jQuery,window,document);