-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
paginate.ts
134 lines (105 loc) Β· 3.41 KB
/
paginate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Composables
import { useProxiedModel } from '@/composables/proxiedModel'
// Utilities
import { computed, inject, provide, watch, watchEffect } from 'vue'
import { clamp, getCurrentInstance, propsFactory } from '@/util'
// Types
import type { InjectionKey, Ref } from 'vue'
import type { Group } from './group'
import type { EventProp } from '@/util'
export const makeDataTablePaginateProps = propsFactory({
page: {
type: [Number, String],
default: 1,
},
itemsPerPage: {
type: [Number, String],
default: 10,
},
}, 'DataTable-paginate')
const VDataTablePaginationSymbol: InjectionKey<{
page: Ref<number>
itemsPerPage: Ref<number>
startIndex: Ref<number>
stopIndex: Ref<number>
pageCount: Ref<number>
itemsLength: Ref<number>
prevPage: () => void
nextPage: () => void
setPage: (value: number) => void
setItemsPerPage: (value: number) => void
}> = Symbol.for('vuetify:data-table-pagination')
type PaginationProps = {
page: number | string
'onUpdate:page': EventProp | undefined
itemsPerPage: number | string
'onUpdate:itemsPerPage': EventProp | undefined
itemsLength?: number | string
}
export function createPagination (props: PaginationProps) {
const page = useProxiedModel(props, 'page', undefined, value => +(value ?? 1))
const itemsPerPage = useProxiedModel(props, 'itemsPerPage', undefined, value => +(value ?? 10))
return { page, itemsPerPage }
}
export function providePagination (options: {
page: Ref<number>
itemsPerPage: Ref<number>
itemsLength: Ref<number>
}) {
const { page, itemsPerPage, itemsLength } = options
const startIndex = computed(() => {
if (itemsPerPage.value === -1) return 0
return itemsPerPage.value * (page.value - 1)
})
const stopIndex = computed(() => {
if (itemsPerPage.value === -1) return itemsLength.value
return Math.min(itemsLength.value, startIndex.value + itemsPerPage.value)
})
const pageCount = computed(() => {
if (itemsPerPage.value === -1 || itemsLength.value === 0) return 1
return Math.ceil(itemsLength.value / itemsPerPage.value)
})
watchEffect(() => {
if (page.value > pageCount.value) {
page.value = pageCount.value
}
})
function setItemsPerPage (value: number) {
itemsPerPage.value = value
page.value = 1
}
function nextPage () {
page.value = clamp(page.value + 1, 1, pageCount.value)
}
function prevPage () {
page.value = clamp(page.value - 1, 1, pageCount.value)
}
function setPage (value: number) {
page.value = clamp(value, 1, pageCount.value)
}
const data = { page, itemsPerPage, startIndex, stopIndex, pageCount, itemsLength, nextPage, prevPage, setPage, setItemsPerPage }
provide(VDataTablePaginationSymbol, data)
return data
}
export function usePagination () {
const data = inject(VDataTablePaginationSymbol)
if (!data) throw new Error('Missing pagination!')
return data
}
export function usePaginatedItems <T> (options: {
items: Ref<readonly (T | Group<T>)[]>
startIndex: Ref<number>
stopIndex: Ref<number>
itemsPerPage: Ref<number>
}) {
const vm = getCurrentInstance('usePaginatedItems')
const { items, startIndex, stopIndex, itemsPerPage } = options
const paginatedItems = computed(() => {
if (itemsPerPage.value <= 0) return items.value
return items.value.slice(startIndex.value, stopIndex.value)
})
watch(paginatedItems, val => {
vm.emit('update:currentItems', val)
})
return { paginatedItems }
}