paulwalko.github.io/_site/javascripts/pagination.js

11 lines
12 KiB
JavaScript
Executable File

/*
* pagination.js 2.0.5
* A jQuery plugin to provide simple yet fully customisable pagination
* https://github.com/superRaytin/paginationjs
* Homepage: http://paginationjs.com
*
* Copyright 2014-2100, superRaytin
* Released under the MIT license.
*/
!function(a,b){function c(a){throw new Error("Pagination: "+a)}function d(a){a.dataSource||c('"dataSource" is required.'),"string"==typeof a.dataSource?"undefined"==typeof a.totalNumber?c('"totalNumber" is required.'):b.isNumeric(a.totalNumber)||c('"totalNumber" is incorrect. (Number)'):i.isObject(a.dataSource)&&("undefined"==typeof a.locator?c('"dataSource" is a Object, please specify "locator".'):"string"==typeof a.locator||b.isFunction(a.locator)||c(""+a.locator+" is incorrect. (String | Function)"))}function e(a){var b;return("object"==(b=typeof a)?null==a&&"null"||Object.prototype.toString.call(a).slice(8,-1):b).toLowerCase()}"undefined"==typeof b&&c("Pagination requires jQuery.");var f="pagination",g="addHook",h="__pagination-";b.fn.pagination&&(f="pagination2"),b.fn[f]=function(e){if("undefined"==typeof e)return this;var f=b(this),g={initialize:function(){var a=this;if(f.data("pagination")||f.data("pagination",{}),a.callHook("beforeInit")!==!1){f.data("pagination").initialized&&b(".paginationjs",f).remove();var c=a.model={pageRange:k.pageRange,pageSize:k.pageSize};a.parseDataSource(k.dataSource,function(b){if(a.sync=i.isArray(b),a.sync&&(c.totalNumber=k.totalNumber=b.length),c.totalPage=a.getTotalPage(),!(k.hideWhenLessThanOnePage&&c.totalPage<=1)){var d=a.render(!0);k.className&&d.addClass(k.className),c.el=d,f["bottom"===k.position?"append":"prepend"](d),a.observer(),f.data("pagination").initialized=!0,a.callHook("afterInit",d)}})}},render:function(a){var c=this,d=c.model,e=d.el||b('<div class="paginationjs"></div>'),f=a!==!0;c.callHook("beforeRender",f);var g=d.pageNumber||k.pageNumber,h=k.pageRange,i=d.totalPage,j=g-h,l=g+h;return l>i&&(l=i,j=i-2*h,j=1>j?1:j),1>=j&&(j=1,l=Math.min(2*h+1,i)),e.html(c.createTemplate({currentPage:g,pageRange:h,totalPage:i,rangeStart:j,rangeEnd:l})),c.callHook("afterRender",f),e},createTemplate:function(a){var c,d,e=this,f=a.currentPage,g=a.totalPage,h=a.rangeStart,i=a.rangeEnd,j=k.totalNumber,l=k.showPrevious,m=k.showNext,n=k.showPageNumbers,o=k.showNavigator,p=k.showGoInput,q=k.showGoButton,r=k.pageLink,s=k.prevText,t=k.nextText,u=k.ellipsisText,v=k.goButtonText,w=k.classPrefix,x=k.activeClassName,y=k.disableClassName,z=k.ulClassName,A=b.isFunction(k.formatNavigator)?k.formatNavigator():k.formatNavigator,B=b.isFunction(k.formatGoInput)?k.formatGoInput():k.formatGoInput,C=b.isFunction(k.formatGoButton)?k.formatGoButton():k.formatGoButton,D=b.isFunction(k.autoHidePrevious)?k.autoHidePrevious():k.autoHidePrevious,E=b.isFunction(k.autoHideNext)?k.autoHideNext():k.autoHideNext,F=b.isFunction(k.header)?k.header():k.header,G=b.isFunction(k.footer)?k.footer():k.footer,H="",I='<input type="text" class="J-paginationjs-go-pagenumber">',J='<input type="button" class="J-paginationjs-go-button" value="'+v+'">';if(F&&(c=e.replaceVariables(F,{currentPage:f,totalPage:g,totalNumber:j}),H+=c),l||n||m){if(H+='<div class="paginationjs-pages">',H+=z?'<ul class="'+z+'">':"<ul>",l&&(1===f?D||(H+='<li class="'+w+"-prev "+y+'"><a href="#">'+s+"</a></li>"):H+='<li class="'+w+'-prev J-paginationjs-previous" data-num="'+(f-1)+'" title="Previous page"><a href="'+r+'">'+s+"</a></li>"),n){if(3>=h)for(d=1;h>d;d++)H+=d==f?'<li class="'+w+"-page J-paginationjs-page "+x+'" data-num="'+d+'"><a href="#">'+d+"</a></li>":'<li class="'+w+'-page J-paginationjs-page" data-num="'+d+'"><a href="'+r+'">'+d+"</a></li>";else k.showFirstOnEllipsisShow&&(H+='<li class="'+w+"-page "+w+'-first J-paginationjs-page" data-num="1"><a href="'+r+'">1</a></li>'),H+='<li class="'+w+"-ellipsis "+y+'"><a href="#">'+u+"</a></li>";for(d=h;i>=d;d++)H+=d==f?'<li class="'+w+"-page J-paginationjs-page "+x+'" data-num="'+d+'"><a href="#">'+d+"</a></li>":'<li class="'+w+'-page J-paginationjs-page" data-num="'+d+'"><a href="'+r+'">'+d+"</a></li>";if(i>=g-2)for(d=i+1;g>=d;d++)H+='<li class="'+w+'-page J-paginationjs-page" data-num="'+d+'"><a href="'+r+'">'+d+"</a></li>";else H+='<li class="'+w+"-ellipsis "+y+'"><a href="#">'+u+"</a></li>",k.showLastOnEllipsisShow&&(H+='<li class="'+w+"-page "+w+'-last J-paginationjs-page" data-num="'+g+'"><a href="'+r+'">'+g+"</a></li>")}m&&(f==g?E||(H+='<li class="'+w+"-next "+y+'"><a href="#">'+t+"</a></li>"):H+='<li class="'+w+'-next J-paginationjs-next" data-num="'+(f+1)+'" title="Next page"><a href="'+r+'">'+t+"</a></li>"),H+="</ul></div>"}return o&&A&&(c=e.replaceVariables(A,{currentPage:f,totalPage:g,totalNumber:j}),H+='<div class="'+w+'-nav J-paginationjs-nav">'+c+"</div>"),p&&B&&(c=e.replaceVariables(B,{currentPage:f,totalPage:g,totalNumber:j,input:I}),H+='<div class="'+w+'-go-input">'+c+"</div>"),q&&C&&(c=e.replaceVariables(C,{currentPage:f,totalPage:g,totalNumber:j,button:J}),H+='<div class="'+w+'-go-button">'+c+"</div>"),G&&(c=e.replaceVariables(G,{currentPage:f,totalPage:g,totalNumber:j}),H+=c),H},go:function(a,c){function d(a){if(g.direction="undefined"==typeof g.pageNumber?0:h>g.pageNumber?1:-1,g.pageNumber=h,e.render(),e.disabled&&!e.sync&&e.enable(),f.data("pagination").model=g,b.isFunction(k.formatResult)){var d=b.extend(!0,[],a);i.isArray(a=k.formatResult(d))||(a=d)}f.data("pagination").currentPageData=a,e.callHook("beforePaging"),e.doCallback(a,c),e.callHook("afterPaging"),1==h&&e.callHook("afterIsFirstPage"),h==g.totalPage&&e.callHook("afterIsLastPage")}var e=this,g=e.model;if(!e.disabled){var h=a,j=k.pageSize,l=g.totalPage;if(h=parseInt(h),!(!h||1>h||h>l)){if(e.sync)return void d(e.getDataSegment(h));var m={},n=k.alias||{};m[n.pageSize?n.pageSize:"pageSize"]=j,m[n.pageNumber?n.pageNumber:"pageNumber"]=h;var o={type:"get",cache:!1,data:{},contentType:"application/x-www-form-urlencoded; charset=UTF-8",dataType:"json",async:!0};b.extend(!0,o,k.ajax),b.extend(o.data||{},m),o.url=k.dataSource,o.success=function(a){d(e.filterDataByLocator(a))},o.error=function(a,b,c){k.formatAjaxError&&k.formatAjaxError(a,b,c),e.enable()},e.disable(),b.ajax(o)}}},doCallback:function(a,c){var d=this,e=d.model;b.isFunction(c)?c(a,e):b.isFunction(k.callback)&&k.callback(a,e)},destroy:function(){this.callHook("beforeDestroy")!==!1&&(this.model.el.remove(),f.off(),b("#paginationjs-style").remove(),this.callHook("afterDestroy"))},previous:function(a){this.go(this.model.pageNumber-1,a)},next:function(a){this.go(this.model.pageNumber+1,a)},disable:function(){var a=this,b=a.sync?"sync":"async";a.callHook("beforeDisable",b)!==!1&&(a.disabled=!0,a.model.disabled=!0,a.callHook("afterDisable",b))},enable:function(){var a=this,b=a.sync?"sync":"async";a.callHook("beforeEnable",b)!==!1&&(a.disabled=!1,a.model.disabled=!1,a.callHook("afterEnable",b))},show:function(){var a=this;a.model.el.is(":visible")||a.model.el.show()},hide:function(){var a=this;a.model.el.is(":visible")&&a.model.el.hide()},replaceVariables:function(a,b){var c;for(var d in b){var e=b[d],f=new RegExp("<%=\\s*"+d+"\\s*%>","img");c=(c||a).replace(f,e)}return c},getDataSegment:function(a){var b=k.pageSize,c=k.dataSource,d=k.totalNumber,e=b*(a-1)+1,f=Math.min(a*b,d);return c.slice(e-1,f)},getTotalPage:function(){return Math.ceil(k.totalNumber/k.pageSize)},getLocator:function(a){var d;return"string"==typeof a?d=a:b.isFunction(a)?d=a():c('"locator" is incorrect. (String | Function)'),d},filterDataByLocator:function(a){var d,e=this.getLocator(k.locator);if(i.isObject(a)){try{b.each(e.split("."),function(b,c){d=(d?d:a)[c]})}catch(f){}d?i.isArray(d)||c("dataSource."+e+" must be an Array."):c("dataSource."+e+" is undefined.")}return d||a},parseDataSource:function(a,d){var e=this,f=arguments;i.isObject(a)?d(k.dataSource=e.filterDataByLocator(a)):i.isArray(a)?d(k.dataSource=a):b.isFunction(a)?k.dataSource(function(a){b.isFunction(a)&&c('Unexpect parameter of the "done" Function.'),f.callee.call(e,a,d)}):"string"==typeof a?(/^https?|file:/.test(a)&&(k.ajaxDataType="jsonp"),d(a)):c('Unexpect data type of the "dataSource".')},callHook:function(c){var d,e=f.data("pagination"),g=Array.prototype.slice.apply(arguments);return g.shift(),k[c]&&b.isFunction(k[c])&&k[c].apply(a,g)===!1&&(d=!1),e.hooks&&e.hooks[c]&&b.each(e.hooks[c],function(b,c){c.apply(a,g)===!1&&(d=!1)}),d!==!1},observer:function(){var a=this,d=a.model.el;f.on(h+"go",function(d,e,f){e=parseInt(b.trim(e)),e&&(b.isNumeric(e)||c('"pageNumber" is incorrect. (Number)'),a.go(e,f))}),d.delegate(".J-paginationjs-page","click",function(c){var d=b(c.currentTarget),e=b.trim(d.attr("data-num"));return!e||d.hasClass(k.disableClassName)||d.hasClass(k.activeClassName)?void 0:a.callHook("beforePageOnClick",c)===!1?!1:(a.go(e),a.callHook("afterPageOnClick",c),k.pageLink?void 0:!1)}),d.delegate(".J-paginationjs-previous","click",function(c){var d=b(c.currentTarget),e=b.trim(d.attr("data-num"));return e&&!d.hasClass(k.disableClassName)?a.callHook("beforePreviousOnClick",c)===!1?!1:(a.go(e),a.callHook("afterPreviousOnClick",c),k.pageLink?void 0:!1):void 0}),d.delegate(".J-paginationjs-next","click",function(c){var d=b(c.currentTarget),e=b.trim(d.attr("data-num"));return e&&!d.hasClass(k.disableClassName)?a.callHook("beforeNextOnClick",c)===!1?!1:(a.go(e),a.callHook("afterNextOnClick",c),k.pageLink?void 0:!1):void 0}),d.delegate(".J-paginationjs-go-button","click",function(){var c=b(".J-paginationjs-go-pagenumber",d).val();return a.callHook("beforeGoButtonOnClick",event,c)===!1?!1:(f.trigger(h+"go",c),void a.callHook("afterGoButtonOnClick",event,c))}),d.delegate(".J-paginationjs-go-pagenumber","keyup",function(c){if(13===c.which){var e=b(c.currentTarget).val();if(a.callHook("beforeGoInputOnEnter",c,e)===!1)return!1;f.trigger(h+"go",e),b(".J-paginationjs-go-pagenumber",d).focus(),a.callHook("afterGoInputOnEnter",c,e)}}),f.on(h+"previous",function(b,c){a.previous(c)}),f.on(h+"next",function(b,c){a.next(c)}),f.on(h+"disable",function(){a.disable()}),f.on(h+"enable",function(){a.enable()}),f.on(h+"show",function(){a.show()}),f.on(h+"hide",function(){a.hide()}),f.on(h+"destroy",function(){a.destroy()}),(g.sync||k.triggerPagingOnInit)&&f.trigger(h+"go",Math.min(k.pageNumber,a.model.totalPage))}};if(f.data("pagination")&&f.data("pagination").initialized===!0){if(b.isNumeric(e))return f.trigger.call(this,h+"go",e,arguments[1]),this;if("string"==typeof e){var j=Array.prototype.slice.apply(arguments);switch(j[0]=h+j[0],e){case"previous":case"next":case"go":case"disable":case"enable":case"show":case"hide":case"destroy":f.trigger.apply(this,j);break;case"getSelectedPageNum":return f.data("pagination").model?f.data("pagination").model.pageNumber:f.data("pagination").attributes.pageNumber;case"getTotalPage":return f.data("pagination").model.totalPage;case"getSelectedPageData":return f.data("pagination").currentPageData;case"isDisabled":return f.data("pagination").model.disabled===!0;default:c("Pagination do not provide action: "+e)}return this}}else i.isObject(e)||c("options is illegal");var k=b.extend({},arguments.callee.defaults,e);return d(k),g.initialize(),this},b.fn[f].defaults={totalNumber:1,pageNumber:1,pageSize:10,pageRange:2,showPrevious:!0,showNext:!0,showPageNumbers:!0,showNavigator:!1,showGoInput:!1,showGoButton:!1,pageLink:"",prevText:"&laquo;",nextText:"&raquo;",ellipsisText:"...",goButtonText:"Go",classPrefix:"paginationjs",activeClassName:"active",disableClassName:"disabled",inlineStyle:!0,formatNavigator:"<%= currentPage %> / <%= totalPage %>",formatGoInput:"<%= input %>",formatGoButton:"<%= button %>",position:"bottom",autoHidePrevious:!1,autoHideNext:!1,triggerPagingOnInit:!0,hideWhenLessThanOnePage:!1,showFirstOnEllipsisShow:!0,showLastOnEllipsisShow:!0,callback:function(){}},b.fn[g]=function(a,d){arguments.length<2&&c("Missing argument."),b.isFunction(d)||c("callback must be a function.");var e=b(this),f=e.data("pagination");f||(e.data("pagination",{}),f=e.data("pagination")),!f.hooks&&(f.hooks={}),f.hooks[a]=f.hooks[a]||[],f.hooks[a].push(d)},b[f]=function(a,d){arguments.length<2&&c("Requires two parameters.");var e;return e="string"!=typeof a&&a instanceof jQuery?a:b(a),e.length?(e.pagination(d),e):void 0};var i={};b.each(["Object","Array"],function(a,b){i["is"+b]=function(a){return e(a)===b.toLowerCase()}}),"function"==typeof define&&(define.amd||define.cmd)&&define(function(){return b})}(this,window.jQuery);