|
| 1 | +<script> |
| 2 | +import { watchEffect, computed, nextTick } from 'vue' |
| 3 | +import PaginationItem from './PaginationItem.vue' |
| 4 | +
|
| 5 | +export default { |
| 6 | + name: 'VPagination', |
| 7 | + components: { |
| 8 | + PaginationItem, |
| 9 | + }, |
| 10 | + props: { |
| 11 | + total: [Number, String], |
| 12 | + perPage: { |
| 13 | + type: [Number, String], |
| 14 | + default: 20, |
| 15 | + }, |
| 16 | + current: { |
| 17 | + type: [Number, String], |
| 18 | + default: 1, |
| 19 | + }, |
| 20 | + rangeBefore: { |
| 21 | + type: [Number, String], |
| 22 | + default: 1, |
| 23 | + }, |
| 24 | + rangeAfter: { |
| 25 | + type: [Number, String], |
| 26 | + default: 1, |
| 27 | + }, |
| 28 | + size: String, |
| 29 | + simple: Boolean, |
| 30 | + rounded: Boolean, |
| 31 | + order: String, |
| 32 | + ariaNextLabel: String, |
| 33 | + ariaPreviousLabel: String, |
| 34 | + ariaPageLabel: String, |
| 35 | + ariaCurrentLabel: String, |
| 36 | + }, |
| 37 | + emits: ['update:current', 'change'], |
| 38 | +
|
| 39 | + setup(props, { emit, slots }) { |
| 40 | + const beforeCurrent = computed(() => { |
| 41 | + return Number.parseInt(props.rangeBefore) |
| 42 | + }) |
| 43 | + const afterCurrent = computed(() => { |
| 44 | + return Number.parseInt(props.rangeAfter) |
| 45 | + }) |
| 46 | +
|
| 47 | + const pageCount = computed(() => { |
| 48 | + return Math.ceil(props.total / props.perPage) |
| 49 | + }) |
| 50 | +
|
| 51 | + const firstItem = computed(() => { |
| 52 | + const _firstItem = props.current * props.perPage - props.perPage + 1 |
| 53 | + return _firstItem >= 0 ? _firstItem : 0 |
| 54 | + }) |
| 55 | +
|
| 56 | + const hasPrev = computed(() => { |
| 57 | + return props.current > 1 |
| 58 | + }) |
| 59 | +
|
| 60 | + const hasFirst = computed(() => { |
| 61 | + return props.current >= 2 + beforeCurrent.value |
| 62 | + }) |
| 63 | +
|
| 64 | + const hasFirstEllipsis = computed(() => { |
| 65 | + return props.current >= beforeCurrent.value + 4 |
| 66 | + }) |
| 67 | +
|
| 68 | + const hasLast = computed(() => { |
| 69 | + return props.current <= pageCount.value - (1 + afterCurrent.value) |
| 70 | + }) |
| 71 | +
|
| 72 | + const hasLastEllipsis = computed(() => { |
| 73 | + return props.current < pageCount.value - (2 + afterCurrent.value) |
| 74 | + }) |
| 75 | +
|
| 76 | + const hasNext = computed(() => { |
| 77 | + return props.current < pageCount.value |
| 78 | + }) |
| 79 | +
|
| 80 | + const pagesInRange = computed(() => { |
| 81 | + if (props.simple) return null |
| 82 | + let left = Math.max(1, props.current - beforeCurrent.value) |
| 83 | + if (left - 1 === 2) { |
| 84 | + left-- |
| 85 | + } |
| 86 | +
|
| 87 | + let right = Math.min(props.current + afterCurrent.value, pageCount.value) |
| 88 | + if (pageCount.value - right === 2) { |
| 89 | + right++ |
| 90 | + } |
| 91 | + const pages = [] |
| 92 | +
|
| 93 | + for (let i = left; i <= right; i++) { |
| 94 | + pages.push(getPage(i)) |
| 95 | + } |
| 96 | +
|
| 97 | + return pages |
| 98 | + }) |
| 99 | +
|
| 100 | + watchEffect(() => { |
| 101 | + if (props.current > pageCount.value) last() |
| 102 | + }) |
| 103 | +
|
| 104 | + function changePage(num, e) { |
| 105 | + if (props.current === num || num < 1 || num > pageCount.value) return |
| 106 | + emit('update:current', num) |
| 107 | + emit('change', num) |
| 108 | + if (e && e.target) { |
| 109 | + nextTick(() => e.target.focus()) |
| 110 | + } |
| 111 | + } |
| 112 | +
|
| 113 | + function last(e) { |
| 114 | + changePage(pageCount.value, e) |
| 115 | + } |
| 116 | + function getPage(num, options = {}) { |
| 117 | + return { |
| 118 | + number: num, |
| 119 | + isCurrent: props.current === num, |
| 120 | + click: e => changePage(num, e), |
| 121 | + disabled: options.disabled || false, |
| 122 | + class: options.class || '', |
| 123 | + 'aria-label': options['aria-label'] || getAriaPageLabel(num, props.current === num), |
| 124 | + } |
| 125 | + } |
| 126 | +
|
| 127 | + function getAriaPageLabel(pageNumber, isCurrent) { |
| 128 | + if (props.ariaPageLabel && (!isCurrent || !props.ariaCurrentLabel)) { |
| 129 | + return props.ariaPageLabel + ' ' + pageNumber + '.' |
| 130 | + } else if (props.ariaPageLabel && isCurrent && props.ariaCurrentLabel) { |
| 131 | + return props.ariaCurrentLabel + ', ' + props.ariaPageLabel + ' ' + pageNumber + '.' |
| 132 | + } |
| 133 | + return null |
| 134 | + } |
| 135 | + return { |
| 136 | + pageCount, |
| 137 | + firstItem, |
| 138 | + hasPrev, |
| 139 | + hasFirst, |
| 140 | + hasFirstEllipsis, |
| 141 | + hasLast, |
| 142 | + hasLastEllipsis, |
| 143 | + hasNext, |
| 144 | + pagesInRange, |
| 145 | + getPage, |
| 146 | + slots, |
| 147 | + } |
| 148 | + }, |
| 149 | +} |
| 150 | +</script> |
| 151 | + |
| 152 | +<template> |
| 153 | + <!-- eslint-disable @pathscale/vue3/v-directive --> |
| 154 | + <nav |
| 155 | + class="pagination" |
| 156 | + :class="[ |
| 157 | + order, |
| 158 | + size, |
| 159 | + { |
| 160 | + 'is-simple': simple, |
| 161 | + 'is-rounded': rounded, |
| 162 | + }, |
| 163 | + ]"> |
| 164 | + <slot |
| 165 | + v-if="slots.previous" |
| 166 | + name="previous" |
| 167 | + :page=" |
| 168 | + getPage(current - 1, { |
| 169 | + disabled: !hasPrev, |
| 170 | + class: 'pagination-previous', |
| 171 | + 'aria-label': ariaPreviousLabel, |
| 172 | + }) |
| 173 | + "> |
| 174 | + « |
| 175 | + </slot> |
| 176 | + <pagination-item |
| 177 | + v-else |
| 178 | + class="pagination-previous" |
| 179 | + :disabled="!hasPrev" |
| 180 | + :page="getPage(current - 1)" |
| 181 | + :aria-label="ariaPreviousLabel"> |
| 182 | + « |
| 183 | + </pagination-item> |
| 184 | + <slot |
| 185 | + v-if="slots.next" |
| 186 | + name="next" |
| 187 | + :page=" |
| 188 | + getPage(current + 1, { |
| 189 | + disabled: !hasNext, |
| 190 | + class: 'pagination-next', |
| 191 | + 'aria-label': ariaNextLabel, |
| 192 | + }) |
| 193 | + "> |
| 194 | + » |
| 195 | + </slot> |
| 196 | + <pagination-item |
| 197 | + v-else |
| 198 | + class="pagination-next" |
| 199 | + :disabled="!hasNext" |
| 200 | + :page="getPage(current + 1)" |
| 201 | + :aria-label="ariaNextLabel"> |
| 202 | + » |
| 203 | + </pagination-item> |
| 204 | + |
| 205 | + <small class="info" v-if="simple"> |
| 206 | + <template v-if="perPage == 1"> {{ firstItem }} / {{ total }} </template> |
| 207 | + <template v-else> |
| 208 | + {{ firstItem }}-{{ Math.min(current * perPage, total) }} / {{ total }} |
| 209 | + </template> |
| 210 | + </small> |
| 211 | + <ul class="pagination-list" v-else> |
| 212 | + <li v-if="hasFirst"> |
| 213 | + <slot v-if="slots.default" :page="getPage(1)" /> |
| 214 | + <pagination-item v-else :page="getPage(1)" /> |
| 215 | + </li> |
| 216 | + <li v-if="hasFirstEllipsis"> |
| 217 | + <span class="pagination-ellipsis">…</span> |
| 218 | + </li> |
| 219 | + |
| 220 | + <li v-for="page in pagesInRange" :key="page.number"> |
| 221 | + <slot v-if="slots.default" :page="page" /> |
| 222 | + <pagination-item v-else :page="page" /> |
| 223 | + </li> |
| 224 | + |
| 225 | + <li v-if="hasLastEllipsis"> |
| 226 | + <span class="pagination-ellipsis">…</span> |
| 227 | + </li> |
| 228 | + <li v-if="hasLast"> |
| 229 | + <slot v-if="slots.default" :page="getPage(pageCount)" /> |
| 230 | + <pagination-item v-else :page="getPage(pageCount)" /> |
| 231 | + </li> |
| 232 | + </ul> |
| 233 | + </nav> |
| 234 | +</template> |
0 commit comments