UNPKG

vue-paginate

Version:

A simple vue.js plugin to paginate data

6 lines 8.73 kB
/** * vue-paginate v3.6.0 * (c) 2018 Taha Shashtari * @license MIT */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.VuePaginate=e()}(this,function(){"use strict";function t(t,e){var n=t.showStepLinks?[t.stepLinks.prev].concat(t.listOfPages,[t.stepLinks.next]):t.listOfPages;return n.map(function(n){var i={on:{click:function(e){e.preventDefault(),t.currentPage=a(n,t.limit,t.currentPage,t.listOfPages,t.stepLinks)}}},s=r(n,t.currentPage,t.listOfPages.length-1,t.stepLinks),u=n===t.stepLinks.next||n===t.stepLinks.prev?n:n+1;return e("li",{class:s},[e("a",i,u)])})}function e(t,e){var n=new P(t.listOfPages,t.currentPage,t.limit,t.stepLinks).generate();n=t.showStepLinks?[t.stepLinks.prev].concat(n,[t.stepLinks.next]):n;var i=u(n);return n.map(function(n,s){var u={on:{click:function(e){e.preventDefault(),t.currentPage=a(n,t.limit,t.currentPage,t.listOfPages,t.stepLinks,i[s])}}},o=r(n,t.currentPage,t.listOfPages.length-1,t.stepLinks),p=n===parseInt(n,10)?n+1:n;return e("li",{class:o},[e("a",u,p)])})}function n(t,e){var n=t.listOfPages.length-1,i={on:{click:function(e){e.preventDefault(),t.currentPage>0&&(t.currentPage-=1)}}},s={on:{click:function(e){e.preventDefault(),t.currentPage<n&&(t.currentPage+=1)}}},r={class:["next",t.currentPage>=n?"disabled":""]},a={class:["prev",t.currentPage<=0?"disabled":""]},u=e("li",a,[e("a",i,t.simple.prev)]),o=e("li",r,[e("a",s,t.simple.next)]);return[u,o]}function i(t,e){return t.filter(function(t){return"paginate"===t.$vnode.componentOptions.tag}).find(function(t){return t.name===e})}function s(t){return Array.apply(null,{length:t}).map(function(t,e){return e})}function r(t,e,n,i){var s=i.prev,r=i.next,a=[];return t===s?a.push("left-arrow"):t===r?a.push("right-arrow"):t===v?a.push("ellipses"):a.push("number"),t===e&&a.push("active"),t===s&&e<=0?a.push("disabled"):t===r&&e>=n&&a.push("disabled"),a}function a(t,e,n,i,s,r){var a=s.prev,u=s.next;void 0===r&&(r=null);var o=Math.floor(n/e);if(t===a)return n-1<0?0:n-1;if(t===u)return n+1>i.length-1?i.length-1:n+1;if(r&&"right-ellipses"===r)return(o+1)*e;if(r&&"left-ellipses"===r){var p=i.slice(o*e,o*e+e),l=n===i.length-1;return l&&1===p.length&&o--,(o-1)*e+e-1}return t}function u(t){return t.map(function(e,n){return e===v&&0===t[n-1]?"left-ellipses":e===v&&0!==t[n-1]?"right-ellipses":e})}function o(t,e){Object.keys(e).forEach(function(n){if("ul"===n){var i=e.ul;Array.isArray(i)?i.forEach(function(e){return t.classList.add(e)}):t.classList.add(i)}t.querySelectorAll(n).forEach(function(t){var i=e[n];Array.isArray(i)?i.forEach(function(e){return t.classList.add(e)}):t.classList.add(i)})})}function p(t){return void 0===t&&(t=[]),t.reduce(function(t,e){return t[e]={list:[],page:0},t},{})}var l,h=function(){},c="undefined"!=typeof console;h=function(t,e,n){void 0===n&&(n="error"),c&&console[n]("[vue-paginate]: "+t+" "+(e?f(l(e)):""))},l=function(t){if(t.$root===t)return"root instance";var e=t._isVue?t.$options.name||t.$options._componentTag:t.name;return(e?"component <"+e+">":"anonymous component")+(t._isVue&&t.$options.__file?" at "+t.$options.__file:"")};var f=function(t){return"anonymous component"===t&&(t+=' - use the "name" option for better debugging messages.'),"\n(found in "+t+")"},g={name:"paginate",props:{name:{type:String,required:!0},list:{type:Array,required:!0},per:{type:Number,default:3,validator:function(t){return t>0}},tag:{type:String,default:"ul"},container:{type:Object,default:null}},data:function(){return{initialListSize:this.list.length}},computed:{parent:function(){return this.container?this.container:this.$parent},currentPage:{get:function(){if(this.parent.paginate[this.name])return this.parent.paginate[this.name].page},set:function(t){this.parent.paginate[this.name].page=t}},pageItemsCount:function(){var t=this.list.length,e=this.currentPage*this.per+1,n=Math.min(this.currentPage*this.per+this.per,t);return e+"-"+n+" of "+t},lastPage:function(){return Math.ceil(this.list.length/this.per)}},mounted:function(){return this.per<=0&&h('<paginate name="'+this.name+"\"> 'per' prop can't be 0 or less.",this.parent),this.parent.paginate[this.name]?void this.paginateList():void h("'"+this.name+"' is not registered in 'paginate' array.",this.parent)},watch:{currentPage:function(){this.paginateList()},list:function(){this.currentPage>=this.lastPage&&(this.currentPage=this.lastPage-1),this.paginateList()},per:function(){this.currentPage=0,this.paginateList()}},methods:{paginateList:function(){var t=this.currentPage*this.per,e=this.list.slice(t,t+this.per);this.parent.paginate[this.name].list=e},goToPage:function(t){var e=Math.ceil(this.list.length/this.per);return t>e?void h("You cannot go to page "+t+". The last page is "+e+".",this.parent):void(this.currentPage=t-1)}},render:function(t){return t(this.tag,{},this.$slots.default)}},d="«",m="»",v="…",P=function(t,e,n){this.listOfPages=t,this.lastPage=t.length-1,this.currentPage=e===this.lastPage?this.lastPage-1:e,this.limit=n};P.prototype.generate=function(){var t=this._buildFirstHalf(),e=this._buildSecondHalf();return t.concat(e)},P.prototype._buildFirstHalf=function(){var t=this._allPagesButLast().slice(this._currentChunkIndex(),this._currentChunkIndex()+this.limit);return this.currentPage>=this.limit&&(t.unshift(v),t.unshift(0)),this.lastPage-this.limit>this._currentChunkIndex()&&t.push(v),t},P.prototype._buildSecondHalf=function(){var t=[this.lastPage];return t},P.prototype._currentChunkIndex=function(){var t=Math.floor(this.currentPage/this.limit);return t*this.limit},P.prototype._allPagesButLast=function(){var t=this;return this.listOfPages.filter(function(e){return e!==t.lastPage})};var y={name:"paginate-links",props:{for:{type:String,required:!0},limit:{type:Number,default:0},simple:{type:Object,default:null,validator:function(t){return t.prev&&t.next}},stepLinks:{type:Object,default:function(){return{prev:d,next:m}},validator:function(t){return t.prev&&t.next}},showStepLinks:{type:Boolean},hideSinglePage:{type:Boolean},classes:{type:Object,default:null},async:{type:Boolean,default:!1},container:{type:Object,default:null}},data:function(){return{listOfPages:[],numberOfPages:0,target:null}},computed:{parent:function(){return this.container?this.container.el:this.$parent},state:function(){return this.container?this.container.state:this.$parent.paginate[this.for]},currentPage:{get:function(){if(this.state)return this.state.page},set:function(t){this.state.page=t}}},mounted:function(){var t=this;this.simple&&this.limit&&h('<paginate-links for="'+this.for+"\"> 'simple' and 'limit' props can't be used at the same time. In this case, 'simple' will take precedence, and 'limit' will be ignored.",this.parent,"warn"),this.simple&&!this.simple.next&&h('<paginate-links for="'+this.for+"\"> 'simple' prop doesn't contain 'next' value.",this.parent),this.simple&&!this.simple.prev&&h('<paginate-links for="'+this.for+"\"> 'simple' prop doesn't contain 'prev' value.",this.parent),this.stepLinks&&!this.stepLinks.next&&h('<paginate-links for="'+this.for+"\"> 'step-links' prop doesn't contain 'next' value.",this.parent),this.stepLinks&&!this.stepLinks.prev&&h('<paginate-links for="'+this.for+"\"> 'step-links' prop doesn't contain 'prev' value.",this.parent),this.$nextTick(function(){t.updateListOfPages()})},watch:{state:{handler:function(){this.updateListOfPages()},deep:!0},currentPage:function(t,e){this.$emit("change",t+1,e+1)}},methods:{updateListOfPages:function(){if(this.target=i(this.parent.$children,this.for),!this.target){if(this.async)return;return h('<paginate-links for="'+this.for+'"> can\'t be used without its companion <paginate name="'+this.for+'">',this.parent),void h('To fix that issue you may need to use :async="true" on <paginate-links> component to allow for asyncronous rendering',this.parent,"warn")}this.numberOfPages=Math.ceil(this.target.list.length/this.target.per),this.listOfPages=s(this.numberOfPages)}},render:function(i){var s=this;if(!this.target&&this.async)return null;var r=this.simple?n(this,i):this.limit>1?e(this,i):t(this,i);if(this.hideSinglePage&&this.numberOfPages<=1)return null;var a=i("ul",{class:["paginate-links",this.for]},r);return this.classes&&this.$nextTick(function(){o(a.elm,s.classes)}),a}},k={};return k.install=function(t){t.mixin({created:function(){"undefined"!==this.paginate&&this.paginate instanceof Array&&(this.paginate=p(this.paginate))},methods:{paginated:function(t){return this.paginate&&this.paginate[t]?this.paginate[t].list:void h("'"+t+"' is not registered in 'paginate' array.",this)}}}),t.component("paginate",g),t.component("paginate-links",y)},"undefined"!=typeof window&&window.Vue&&window.Vue.use(k),k});