'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var core = require('@vue-layout/core'); var vue = require('vue'); function buildPaginationOptions(options) { const { buildOrFail, build } = core.createOptionBuilder('pagination'); return { ...options, busy: options.busy ?? false, tag: buildOrFail({ key: 'tag', value: options.tag, alt: 'ul' }), class: buildOrFail({ key: 'class', value: options.class, alt: 'pagination' }), itemTag: buildOrFail({ key: 'itemTag', value: options.itemTag, alt: 'li' }), itemClass: buildOrFail({ key: 'itemClass', value: options.itemClass, alt: 'page-item' }), linkClass: buildOrFail({ key: 'linkClass', value: options.linkClass, alt: 'page-link' }), linkActiveClass: buildOrFail({ key: 'linkActiveClass', value: options.linkActiveClass, alt: 'active' }), prevTag: buildOrFail({ key: 'prevTag', value: options.prevTag, alt: 'i' }), prevClass: build({ key: 'prevClass', value: options.prevClass, alt: [] }), prevContent: build({ key: 'prevContent', value: options.prevContent }), nextTag: buildOrFail({ key: 'nextTag', value: options.nextTag, alt: 'i' }), nextClass: build({ key: 'nextClass', value: options.nextClass, alt: [] }), nextContent: build({ key: 'nextContent', value: options.nextContent }) }; } function buildPagination(input) { const options = buildPaginationOptions(input); let totalPages = 1; let currentPage = 1; if (options.total > 0 && options.limit > 0) { totalPages = Math.max(Math.ceil(options.total / options.limit), 1); currentPage = Math.floor(options.offset / options.limit) + 1; } const betweenPages = []; for(let i = currentPage - 2; i < currentPage + 2; i++){ if (i > 0 && i <= totalPages) { betweenPages.push(i); } } const busy = vue.unref(options.busy); const load = (page)=>{ if (busy || page === currentPage) { return undefined; } if (vue.isRef(options.busy)) { options.busy.value = true; } const data = { page, offset: (page - 1) * options.limit, limit: options.limit, total: totalPages }; const output = options.load(data); if (core.isPromise(output)) { return output.finally(()=>{ if (vue.isRef(options.busy)) { options.busy.value = false; } }); } if (vue.isRef(options.busy)) { options.busy.value = false; } return output; }; const renderPrevPage = ()=>{ let content; if (options.prevClass || options.prevContent) { content = vue.h(options.prevTag, { class: options.prevClass }, options.prevContent); } else { content = [ currentPage - 1 ]; } let prevPage = []; if (currentPage > 1) { prevPage = [ vue.h(options.itemTag, { class: options.itemClass }, [ vue.h('button', { class: options.linkClass, disabled: options.busy, onClick ($event) { $event.preventDefault(); return load(currentPage - 1); } }, [ content ]) ]) ]; } return prevPage; }; const renderBetweenPages = ()=>{ const children = []; for(let i = 0; i < betweenPages.length; i++){ const node = vue.h(options.itemTag, { class: options.itemClass }, [ vue.h('button', { ...vue.mergeProps({ ...betweenPages[i] === currentPage ? { class: options.linkActiveClass } : {} }, { class: options.linkClass }), disabled: options.busy, onClick ($event) { $event.preventDefault(); // eslint-disable-next-line prefer-rest-params return load(betweenPages[i]); } }, [ betweenPages[i] ]) ]); children.push(node); } return children; }; const renderNextPage = ()=>{ let nextPage = []; let content; if (options.nextClass || options.nextContent) { content = vue.h(options.nextTag, { class: options.nextClass }, options.nextContent); } else { content = [ currentPage + 1 ]; } if (currentPage < totalPages) { nextPage = [ vue.h(options.itemTag, { class: options.itemClass }, [ vue.h('button', { class: options.linkClass, disabled: options.busy, onClick ($event) { $event.preventDefault(); return load(currentPage + 1); } }, [ content ]) ]) ]; } return nextPage; }; return vue.h(options.tag, { class: options.class }, [ renderPrevPage(), renderBetweenPages(), renderNextPage() ]); } const VCPagination = vue.defineComponent({ props: { total: { type: Number, default: 0 }, limit: { type: Number, default: 0 }, offset: { type: Number, default: 0 }, busy: { type: Boolean, default: false }, meta: { type: Object, default: undefined } }, emits: [ 'load' ], setup (props, { emit }) { const meta = vue.computed(()=>{ if (typeof props.meta === 'undefined') { return {}; } return props.meta; }); return ()=>buildPagination({ total: meta.value.total ?? props.total, limit: meta.value.limit ?? props.limit, offset: meta.value.offset ?? props.offset, busy: meta.value.busy ?? props.busy, load: (data)=>emit('load', data) }); } }); function install(instance, options) { options ?? (options = {}); core.applyPluginBaseOptions(instance, options); instance.component('VCPagination', VCPagination); } var index = { install }; exports.VCPagination = VCPagination; exports.buildPagination = buildPagination; exports.buildPaginationOptions = buildPaginationOptions; exports.default = index; exports.install = install; module.exports = Object.assign(exports.default, exports); //# sourceMappingURL=index.cjs.map