-
Notifications
You must be signed in to change notification settings - Fork 1
/
pagiator.min.js
1 lines (1 loc) · 2.32 KB
/
pagiator.min.js
1
(function(){function a(a,b){if(!a)throw new Error("newPage is missing page number as parameter!");return{value:a,choosing:!!b}}function b(b,c,d){if(!b||!c||!d)throw new Error("makeRange is missing required parameters!");if("number"!=typeof b||"number"!=typeof c)throw new Error("parameters must is a number!");for(var e=[],f=b;f<=c;f++)e.push(a(f,f===d));return e}function c(a,b,c){var d=document.createElement(a);if(b.split(" ").forEach(function(a){d.classList.add(a)}),c){var e=document.createTextNode(c);d.appendChild(e)}return d}function d(a,b){this.totalPage=a||1,this.neighbor=b||2,this.currentPage=1}d.prototype={firstPage:1,baseUrl:"",textContent:{leftDirection:"<",rightDirection:">"},HTMLClasses:{container:"pagination",page:"page",currentPage:"page choosing",direction:"direction"},setup:function(a){return a.baseUrl&&(this.baseUrl=a.baseUrl),a.HTMLClasses&&(this.HTMLClasses=a.HTMLClasses),a.textContent&&(this.textContent=a.textContent),this},choose:function(a){return this.currentPage=a,this},ArrayRender:function(){var c=this.totalPage,d=this.currentPage,e=this.firstPage,f=this.neighbor,g=2*this.neighbor+5;if(!d)throw new Error("Choosing page number is required!");if(1>d)throw new Error(`Can't choose negative number as page`);if(d>c)throw new Error(`You can't choose the page greater than last page`);if(c<=g)return b(1,c,d);var h=d-f,i=d+f;return 3>h?[a(e,d===e),...b(e+1,2*f+3,d),a("RIGHT"),a(c)]:i>=c-1?[a(e,d===e),a("LEFT"),...b(c-2-2*f,c-1,d),a(c,c===d)]:[a(1),a("LEFT"),...b(h,i,d),a("RIGHT"),a(c)]},HTMLRender:function(a){var b=this.ArrayRender(this.currentPage),d=this.currentPage,e=this.HTMLClasses,f=this.baseUrl||"",g=this.textContent,h=c("div",this.HTMLClasses.container||"pagination");if(!document)return void console.log(`This enviroment doesn't have document Object (DOM)`);if(!a)throw new Error("Selector is required in HTMLRender!");b.forEach(function(a){switch(a.value){case"LEFT":var b=c("a",e.direction||"direction",g.leftDirection);b.href=f+"/"+(d-1),h.appendChild(b);break;case"RIGHT":var i=c("a",e.direction||"direction",g.rightDirection);i.href=f+"/"+(d+1),h.appendChild(i);break;case d:var j=c("a",e.currentPage||"page choosing",a.value);j.href=f+"/"+a.value,h.appendChild(j);break;default:var k=c("a",e.page||"page",a.value);k.href=f+"/"+a.value,h.appendChild(k);}}),document.querySelectorAll(a)[0].appendChild(h)}},this.Pg=d})(this);