pagination
Version:
Pagination for javascript/nodejs
1 lines • 4.72 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,u=s%2===0?1:0,p={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 p;if(r=Math.ceil(a/i),p.pageCount=r,2>r)return p.fromResult=1,p.toResult=a,p;for(l>r&&(l=r,p.current=l),o=Math.floor(s/2),t=l-o,e=l+o-u,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++)p.range.push(n);return l>1&&(p.first=1,p.previous=l-1),r>l&&(p.last=r,p.next=l+1),p.fromResult=(l-1)*i+1,p.toResult=l===r?a:p.fromResult+i-1,p},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){"use strict";var e={},r=function(t,e){var r,n,o,a,s,i,l,u,e=e||{},p=e.open||"<%",c=e.close||"%>",h=["var buf = [];","\nwith (paginationData) {","\n buf.push('"];for(o=0,s=t.length;s>o;++o)if(t.slice(o,p.length+o)===p){switch(o+=p.length,t.substr(o,1)){case"=":r="', escape(",n="), '",++o;break;case"-":r="', (",n="), '",++o;break;default:r="');",n="; buf.push('"}for(a=t.indexOf(c,o),i=t.substring(o,a),l=o,u=0;~(u=i.indexOf("\n",u));)u++;h.push(r,i,n),o+=a-l+c.length-1}else h.push("\\"===t.substr(o,1)?"\\\\":"'"===t.substr(o,1)?"\\'":"\r"===t.substr(o,1)?" ":"\n"===t.substr(o,1)?"\\n":t.substr(o,1));return h.push("');\n}\nreturn buf.join('');"),h.join("")},n=function(t){return String(t).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")},o=function(t,e){var o=new Function("paginationData, escape",r(t,e));return function(t){return o.call(this,t,n)}},a=function(t,r){var n,r=r||{};if(r.cache){if(!r.id)throw new Error('"cache" option requires "id"');n=e[r.id]||(e[r.id]=o(t,r))}else n=o(t,r);return n};t.TemplateEngine={parse:r,compile:a}},exports.module(pagination,pagination.util),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);