|
| 1 | +import { createElement, computed } from '@vue/composition-api' |
| 2 | + |
| 3 | +export default { |
| 4 | + name: 'FeathersVuexPagination', |
| 5 | + props: { |
| 6 | + /** |
| 7 | + * An object containing { $limit, and $skip } |
| 8 | + */ |
| 9 | + value: { |
| 10 | + type: Object, |
| 11 | + // eslint-disable-next-line @typescript-eslint/explicit-function-return-type |
| 12 | + default: () => null |
| 13 | + }, |
| 14 | + /** |
| 15 | + * The `latestQuery` object from the useFind data |
| 16 | + */ |
| 17 | + latestQuery: { |
| 18 | + type: Object, |
| 19 | + // eslint-disable-next-line @typescript-eslint/explicit-function-return-type |
| 20 | + default: () => null |
| 21 | + } |
| 22 | + }, |
| 23 | + // eslint-disable-next-line |
| 24 | + setup(props, context) { |
| 25 | + // Total |
| 26 | + const pageCount = computed(() => { |
| 27 | + const q = props.latestQuery |
| 28 | + if (q && q.response) { |
| 29 | + return Math.ceil(q.response.total / props.value.$limit) |
| 30 | + } else { |
| 31 | + return 1 |
| 32 | + } |
| 33 | + }) |
| 34 | + // Current Page |
| 35 | + const currentPage = computed({ |
| 36 | + set(pageNumber: number) { |
| 37 | + if (pageNumber < 1) { |
| 38 | + pageNumber = 1 |
| 39 | + } else if (pageNumber > pageCount.value) { |
| 40 | + pageNumber = pageCount.value |
| 41 | + } |
| 42 | + const $limit = props.value.$limit |
| 43 | + const $skip = $limit * (pageNumber - 1) |
| 44 | + |
| 45 | + context.emit('input', { $limit, $skip }) |
| 46 | + }, |
| 47 | + get() { |
| 48 | + const params = props.value |
| 49 | + if (params) { |
| 50 | + return params.$skip / params.$limit + 1 |
| 51 | + } else { |
| 52 | + return 1 |
| 53 | + } |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + const canPrev = computed(() => { |
| 58 | + return currentPage.value - 1 > 0 |
| 59 | + }) |
| 60 | + const canNext = computed(() => { |
| 61 | + return currentPage.value + 1 < pageCount.value |
| 62 | + }) |
| 63 | + |
| 64 | + function toStart(): void { |
| 65 | + currentPage.value = 1 |
| 66 | + } |
| 67 | + function toEnd(): void { |
| 68 | + currentPage.value = pageCount.value |
| 69 | + } |
| 70 | + |
| 71 | + function next(): void { |
| 72 | + currentPage.value++ |
| 73 | + } |
| 74 | + function prev(): void { |
| 75 | + currentPage.value-- |
| 76 | + } |
| 77 | + |
| 78 | + // eslint-disable-next-line @typescript-eslint/explicit-function-return-type |
| 79 | + return () => { |
| 80 | + if (context.slots.default) { |
| 81 | + return context.slots.default({ |
| 82 | + currentPage: currentPage.value, |
| 83 | + pageCount: pageCount.value, |
| 84 | + canPrev: canPrev.value, |
| 85 | + canNext: canNext.value, |
| 86 | + toStart, |
| 87 | + toEnd, |
| 88 | + prev, |
| 89 | + next |
| 90 | + }) |
| 91 | + } else { |
| 92 | + return createElement('div', {}, [ |
| 93 | + createElement('p', `FeathersVuexPagination uses the default slot:`), |
| 94 | + createElement('p', `#default="{ currentPage, pageCount }"`) |
| 95 | + ]) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments