UNPKG

pagination

Version:
1 lines 4.81 kB
window.pagination={},window.exports=pagination,window.pagination.util={inherits:function(t,r){if(!r||!t)throw new Error("extend failed, please check that all dependencies are included.");var e=function(){};e.prototype=r.prototype,t.prototype=new e,t.prototype.constructor=t,t.superclass=r.prototype,r.prototype.constructor===Object.prototype.constructor&&(r.prototype.constructor=r)}},Object.keys||(Object.keys=function(t){var r,e=[],a=Object.prototype.hasOwnProperty;if(t!==Object(t))throw new TypeError("Object.keys called on a non-object");for(r in t)a.call(t,r)&&e.push(r);return e}),function(t){"use strict";var r={},e={NEXT:"Next",PREVIOUS:"Previous",FIRST:"First",LAST:"Last",CURRENT_PAGE_REPORT:"Results {FromResult} - {ToResult} of {TotalResult}"},a=(t.translationKeys=Object.keys(e),function(t){return e[t]}),s=function(t){var r,e,s;for(this.options={totalResult:0,prelink:"",rowsPerPage:10,pageLinks:5,current:1,translator:a,translationCache:!1,translationCacheKey:"en",pageParamName:"page",slashSeparator:!1},r=Object.keys(t),e=0,s=r.length;s>e;e++)this.set(r[e],t[r[e]]);this._result=null};t.Paginator=s,s.prototype={getPaginationData:function(){return this._result||(this._result=this.calc()),this._result},calc:function(){var t,r,e,a,s,o=this.options.totalResult,n=this.options.pageLinks,i=this.options.rowsPerPage,l=this.options.current,p=n%2===0?1:0,u={prelink:this.options.prelink,current:l,previous:null,next:null,first:null,last:null,range:[],fromResult:null,toResult:null,totalResult:o,pageCount:null};if(0>=i)return u;if(e=Math.ceil(o/i),u.pageCount=e,2>e)return u.fromResult=1,u.toResult=o,u;for(l>e&&(l=e,u.current=l),s=Math.floor(n/2),t=l-s,r=l+s-p,1>t&&(t=1,r=t+n-1,r>e&&(r=e)),r>e&&(r=e,t=r-n+1,1>t&&(t=1)),a=t;r>=a;a++)u.range.push(a);return l>1&&(u.first=1,u.previous=l-1),e>l&&(u.last=e,u.next=l+1),u.fromResult=(l-1)*i+1,u.toResult=l===e?o:u.fromResult+i-1,u},set:function(t,r){if(this.options.hasOwnProperty(t)){switch(t){case"current":case"totalResult":case"pageLinks":case"rowsPerPage":if(r=parseInt(r,10),isNaN(r))throw new Error('Invalid value for "'+t+'", expected an integer');break;case"translator":if(!(r&&r.constructor&&r.call&&r.apply))throw new Error("Translator must be a function");break;case"translationCacheKey":case"pageParamName":case"prelink":r=String(r)}this.options[t]=r,this._result&&(this._result=null)}},preparePreLink:function(t){return this.options.slashSeparator?("/"!==t[t.length-1]&&(t+="/"),t+this.options.pageParamName+"/"):(-1!==t.indexOf("?")?"?"!==t[t.length-1]&&"&"!==t[t.length-1]&&(t+="&"):t+="?",t+this.options.pageParamName+"=")},render:function(){throw new Error("Implement")}},t.registerFactory=function(t,e){if(r.hasOwnProperty(t))throw new Error(t+" already exists");r[t]=e},t.create=function(t,e){if(r.hasOwnProperty(t))return new r[t](e);throw new Error("Paginator type"+t+" not found in register")}}(exports),exports.module=function(t,r){"use strict";var e={CURRENT_PAGE_REPORT:{}},a=t.ItemPaginator=function(r){t.Paginator.call(this,r),this.set("pageLinks",1)};r.inherits(a,t.Paginator),a.prototype.renderCurrentPageReport=function(t,r,a){var s;return this.options.translationCache?(e.CURRENT_PAGE_REPORT.hasOwnProperty(this.options.translationCacheKey)||(s="return '"+this.options.translator("CURRENT_PAGE_REPORT").replace("'","'").replace("{FromResult}","' + fromResult + '").replace("{ToResult}","' + toResult + '").replace("{TotalResult}","' + totalResult + '")+"';",e.CURRENT_PAGE_REPORT[this.options.translationCacheKey]=new Function("fromResult, toResult, totalResult",s)),e.CURRENT_PAGE_REPORT[this.options.translationCacheKey](t,r,a)):this.options.translator("CURRENT_PAGE_REPORT").replace("{FromResult}",t).replace("{ToResult}",r).replace("{TotalResult}",a)},a.prototype.render=function(){var t=this.getPaginationData(),r=this.preparePreLink(t.prelink),e='<div class="paginator">';return e+='<span class="paginator-current-report">',e+=this.renderCurrentPageReport(t.fromResult,t.toResult,t.totalResult),e+="</span>",e+=t.first?'<a href="'+r+t.first+'" class="paginator-first">'+this.options.translator("FIRST")+"</a>":'<span class="paginator-first">'+this.options.translator("FIRST")+"</span>",e+=t.previous?'<a href="'+r+t.previous+'" class="paginator-previous">'+this.options.translator("PREVIOUS")+"</a>":'<span class="paginator-previous">'+this.options.translator("PREVIOUS")+"</span>",e+=t.next?'<a href="'+r+t.next+'" class="paginator-next">'+this.options.translator("NEXT")+"</a>":'<span class="paginator-next">'+this.options.translator("NEXT")+"</span>",e+=t.last?'<a href="'+r+t.last+'" class="paginator-last">'+this.options.translator("LAST")+"</a>":'<span class="paginator-last">'+this.options.translator("LAST")+"</span>",e+="</div>"},t.registerFactory("item",a)},exports.module(pagination,pagination.util);