/
pagination.js
139 lines (131 loc) · 3.92 KB
/
pagination.js
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
135
136
137
138
139
import Vue from '../../utils/vue'
import { getComponentConfig } from '../../utils/config'
import { isVisible } from '../../utils/dom'
import { isUndefinedOrNull } from '../../utils/inspect'
import { toInteger } from '../../utils/number'
import paginationMixin from '../../mixins/pagination'
// --- Constants ---
const NAME = 'BPagination'
const DEFAULT_PER_PAGE = 20
const DEFAULT_TOTAL_ROWS = 0
const props = {
size: {
type: String,
default: () => getComponentConfig(NAME, 'size')
},
perPage: {
type: [Number, String],
default: DEFAULT_PER_PAGE
},
totalRows: {
type: [Number, String],
default: DEFAULT_TOTAL_ROWS
},
ariaControls: {
type: String,
default: null
}
}
// --- Helper functions ---
// Sanitize the provided per page number (converting to a number)
const sanitizePerPage = val => {
const perPage = toInteger(val) || DEFAULT_PER_PAGE
return perPage < 1 ? 1 : perPage
}
// Sanitize the provided total rows number (converting to a number)
const sanitizeTotalRows = val => {
const totalRows = toInteger(val) || DEFAULT_TOTAL_ROWS
return totalRows < 0 ? 0 : totalRows
}
// The render function is brought in via the `paginationMixin`
// @vue/component
export const BPagination = /*#__PURE__*/ Vue.extend({
name: NAME,
mixins: [paginationMixin],
props,
computed: {
numberOfPages() {
const result = Math.ceil(sanitizeTotalRows(this.totalRows) / sanitizePerPage(this.perPage))
return result < 1 ? 1 : result
},
pageSizeNumberOfPages() {
// Used for watching changes to `perPage` and `numberOfPages`
return {
perPage: sanitizePerPage(this.perPage),
totalRows: sanitizeTotalRows(this.totalRows),
numberOfPages: this.numberOfPages
}
}
},
watch: {
pageSizeNumberOfPages(newVal, oldVal) {
if (!isUndefinedOrNull(oldVal)) {
if (newVal.perPage !== oldVal.perPage && newVal.totalRows === oldVal.totalRows) {
// If the page size changes, reset to page 1
this.currentPage = 1
} else if (
newVal.numberOfPages !== oldVal.numberOfPages &&
this.currentPage > newVal.numberOfPages
) {
// If `numberOfPages` changes and is less than
// the `currentPage` number, reset to page 1
this.currentPage = 1
}
}
this.localNumberOfPages = newVal.numberOfPages
}
},
created() {
// Set the initial page count
this.localNumberOfPages = this.numberOfPages
// Set the initial page value
const currentPage = toInteger(this.value) || 0
if (currentPage > 0) {
this.currentPage = currentPage
} else {
this.$nextTick(() => {
// If this value parses to NaN or a value less than 1
// Trigger an initial emit of 'null' if no page specified
this.currentPage = 0
})
}
},
mounted() {
// Set the initial page count
this.localNumberOfPages = this.numberOfPages
},
methods: {
// These methods are used by the render function
onClick(num, evt) {
// Handle edge cases where number of pages has changed (i.e. if perPage changes)
// This should normally not happen, but just in case.
if (num > this.numberOfPages) {
/* istanbul ignore next */
num = this.numberOfPages
} else if (num < 1) {
/* istanbul ignore next */
num = 1
}
// Update the v-model
this.currentPage = num
// Emit event triggered by user interaction
this.$emit('change', this.currentPage)
this.$nextTick(() => {
// Keep the current button focused if possible
const target = evt.target
if (isVisible(target) && this.$el.contains(target) && target.focus) {
target.focus()
} else {
this.focusCurrent()
}
})
},
makePage(pageNum) {
return pageNum
},
linkProps() {
// No props, since we render a plain button
return {}
}
}
})