-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheasyPaginate.min.js
30 lines (30 loc) · 4.84 KB
/
easyPaginate.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(function($,window){var EasyPaginate;EasyPaginate=(function(){EasyPaginate.prototype.defaults={pageClassNameBase:"easyPaginatePage_",nextButtonId:"next-button",prevButtonId:"prev-button",pageNumberClass:"page-number",itemClassName:"",pageNumberId:"page-number",numberOfItemsAccross:1,numberOfItemsDown:1,fixedPagination:true,startingPageNumber:1,controlModifier:"visibility",afterChangePageCallBack:function(){},};function EasyPaginate(element,options){this.$element=$(element);this.options=$.extend({},this.defaults,options);this.controlModifierOn=(this.options.controlModifier=='display')?"inline":"visible"
this.controlModifierOff=(this.options.controlModifier=='display')?"none":"hidden"
this.nextButton=$("#"+this.options.nextButtonId)
this.prevButton=$("#"+this.options.prevButtonId)
this.pageNumbers=$("."+this.options.pageNumberClass)
this.pageNumber=(this.options.pageNumberId!="")?$("#"+this.options.pageNumberId):""
this.items=this.$element.find('.'+this.options.itemClassName)
this.currentPageNumber=this.options.startingPageNumber
this.maxPageArea=0
this.maxItemsPerPage=0
this.totalPages=0
this.paginate();};EasyPaginate.prototype.paginate=function(){this.setPageAreaOrMaxItems();this.$element.css('display','block');this.options.fixedPagination?this.fixedPaginate():this.dynamicPaginate();this.nextButton.unbind("click")
this.prevButton.unbind("click")
this.nextButton.click((function(_this){return function(){if(_this.currentPageNumber<_this.totalPages){_this.changePage(++_this.currentPageNumber);}
return false;};})(this));this.prevButton.click((function(_this){return function(){if(_this.currentPageNumber>1){_this.changePage(--_this.currentPageNumber);}
return false;}})(this));this.pageNumbers.click((function(_this){return function(event){pageNumber=$(event.target).find("a").text()
_this.changePage(pageNumber);return false;}})(this));this.changePage(this.currentPageNumber)
return this;};EasyPaginate.prototype.setPageAreaOrMaxItems=function(){if((this.options.numberOfItemsAccross>0)&&(this.options.numberOfItemsDown>0)){this.maxItemsPerPage=this.options.numberOfItemsAccross*this.options.numberOfItemsDown;}
else{this.maxPageArea=this.$element.css("height").match(/\d*/)*this.$element.css("width").match(/\d*/);}
return this;};EasyPaginate.prototype.fixedPaginate=function(){var pageNumber=1;var totalItemsOnCurrentPage=1;if(this.items.length>0){this.totalPages=1;}
$(this.items).removeClass((function(_this){return function(index,css){regex=new RegExp("("+_this.options.pageClassNameBase+"\\d*)","g")
return(css.match(regex)||[]).join(" ");};})(this));this.items.each((function(_this){return function(index,item){item=$(item);if(totalItemsOnCurrentPage>_this.maxItemsPerPage){pageNumber++
totalItemsOnCurrentPage=1;_this.totalPages++;}
className=_this.options.pageClassNameBase+pageNumber;if(!item.hasClass(className)){item.addClass(className);}
totalItemsOnCurrentPage++;};})(this));return this;};EasyPaginate.prototype.dynamicPaginate=function(){var pageNumber=1;var newContentArea=0;var className;this.items.each((function(_this){return function(){newContentArea+=_this.$element.fullArea();className=_this.$element.attr("class");if(newContentArea<=this.maxPageArea){className=className+" "+_this.options.pageClassNameBase+pageNumber;}
else{className=className+" "+_this.options.pageClassNameBase+(++pageNumber);newContentArea=$(this).fullArea();this.totalPages++;}
_this.$element.attr("class",className);};})(this));return this;};EasyPaginate.prototype.changePage=function(pageNumber){var newContentGroup=$('#'+this.$element.attr("id")+' .'+this.options.pageClassNameBase+pageNumber);if(newContentGroup!=null){this.displayNoItems();newContentGroup.each(function(index){$(this).css('display','block');});this.currentPageNumber=pageNumber;}
this.options.afterChangePageCallBack(this,this.currentPageNumber,this.totalPages);return this;};EasyPaginate.prototype.displayNoItems=function(){this.items.each((function(_this){return function(index,item){item=$(item)
item.css('display','none');};})(this));return this;};return EasyPaginate;})();$.fn.fullArea=function(){var element=$(this);$.extend(element,{fullHeight:function(){var totalHeight=0;$.each(["border","padding","margin"],function(){totalHeight+=element.convertToInt(element.css(this+"-top"));totalHeight+=element.convertToInt(element.css(this+"-bottom"));});return(totalHeight+=element.height());},fullWidth:function(){var totalWidth=0;$.each(["border","padding","margin"],function(){totalWidth+=element.convertToInt(element.css(this+"-right"));totalWidth+=element.convertToInt(element.css(this+"-left"));});return(totalWidth+=element.width());},convertToInt:function(value){value=value.replace("px","");return value==""?0:parseInt(value);}});return element.fullHeight()*element.fullWidth();};return $.fn.extend({easyPaginate:function(options){return this.each(function(){$this=$(this)
$this.data("EasyPaginate",new EasyPaginate(this,options));});}});})(window.jQuery,window);