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