pagination-nav.js
9.34 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { extend } from '../../vue'
import { NAME_PAGINATION_NAV } from '../../constants/components'
import { IS_BROWSER } from '../../constants/env'
import { EVENT_NAME_CHANGE, EVENT_NAME_PAGE_CLICK } from '../../constants/events'
import {
PROP_TYPE_ARRAY,
PROP_TYPE_BOOLEAN,
PROP_TYPE_FUNCTION,
PROP_TYPE_NUMBER_STRING,
PROP_TYPE_STRING
} from '../../constants/props'
import { BvEvent } from '../../utils/bv-event.class'
import { attemptBlur, requestAF } from '../../utils/dom'
import { isArray, isUndefined, isObject } from '../../utils/inspect'
import { looseEqual } from '../../utils/loose-equal'
import { mathMax } from '../../utils/math'
import { toInteger } from '../../utils/number'
import { omit, sortKeys } from '../../utils/object'
import { hasPropFunction, makeProp, makePropsConfigurable, pluckProps } from '../../utils/props'
import { computeHref, parseQuery } from '../../utils/router'
import { toString } from '../../utils/string'
import { warn } from '../../utils/warn'
import { paginationMixin, props as paginationProps } from '../../mixins/pagination'
import { props as BLinkProps } from '../link/link'
// --- Helper methods ---
// Sanitize the provided number of pages (converting to a number)
export const sanitizeNumberOfPages = value => mathMax(toInteger(value, 0), 1)
// --- Props ---
const linkProps = omit(BLinkProps, ['event', 'routerTag'])
const props = makePropsConfigurable(
sortKeys({
...paginationProps,
...linkProps,
baseUrl: makeProp(PROP_TYPE_STRING, '/'),
linkGen: makeProp(PROP_TYPE_FUNCTION),
// Disable auto page number detection if `true`
noPageDetect: makeProp(PROP_TYPE_BOOLEAN, false),
numberOfPages: makeProp(
PROP_TYPE_NUMBER_STRING,
1,
/* istanbul ignore next */
value => {
const number = toInteger(value, 0)
if (number < 1) {
warn('Prop "number-of-pages" must be a number greater than "0"', NAME_PAGINATION_NAV)
return false
}
return true
}
),
pageGen: makeProp(PROP_TYPE_FUNCTION),
// Optional array of page links
pages: makeProp(PROP_TYPE_ARRAY),
useRouter: makeProp(PROP_TYPE_BOOLEAN, false)
}),
NAME_PAGINATION_NAV
)
// --- Main component ---
// @vue/component
export const BPaginationNav = /*#__PURE__*/ extend({
name: NAME_PAGINATION_NAV,
// The render function is brought in via the pagination mixin
mixins: [paginationMixin],
props,
computed: {
// Used by render function to trigger wrapping in '<nav>' element
isNav() {
return true
},
computedValue() {
// Returns the value prop as a number or `null` if undefined or < 1
const value = toInteger(this.value, 0)
return value < 1 ? null : value
}
},
watch: {
numberOfPages() {
this.$nextTick(() => {
this.setNumberOfPages()
})
},
pages() {
this.$nextTick(() => {
this.setNumberOfPages()
})
}
},
created() {
this.setNumberOfPages()
},
mounted() {
if (this.$router) {
// We only add the watcher if vue router is detected
this.$watch('$route', () => {
this.$nextTick(() => {
requestAF(() => {
this.guessCurrentPage()
})
})
})
}
},
methods: {
setNumberOfPages() {
if (isArray(this.pages) && this.pages.length > 0) {
this.localNumberOfPages = this.pages.length
} else {
this.localNumberOfPages = sanitizeNumberOfPages(this.numberOfPages)
}
this.$nextTick(() => {
this.guessCurrentPage()
})
},
onClick(event, pageNumber) {
// Dont do anything if clicking the current active page
if (pageNumber === this.currentPage) {
return
}
const target = event.currentTarget || event.target
// Emit a user-cancelable `page-click` event
const clickEvent = new BvEvent(EVENT_NAME_PAGE_CLICK, {
cancelable: true,
vueTarget: this,
target
})
this.$emit(clickEvent.type, clickEvent, pageNumber)
if (clickEvent.defaultPrevented) {
return
}
// Update the `v-model`
// Done in in requestAF() to allow browser to complete the
// native browser click handling of a link
requestAF(() => {
this.currentPage = pageNumber
this.$emit(EVENT_NAME_CHANGE, pageNumber)
})
// Emulate native link click page reloading behaviour by blurring the
// paginator and returning focus to the document
// Done in a `nextTick()` to ensure rendering complete
this.$nextTick(() => {
attemptBlur(target)
})
},
getPageInfo(pageNumber) {
if (
!isArray(this.pages) ||
this.pages.length === 0 ||
isUndefined(this.pages[pageNumber - 1])
) {
const link = `${this.baseUrl}${pageNumber}`
return {
link: this.useRouter ? { path: link } : link,
text: toString(pageNumber)
}
}
const info = this.pages[pageNumber - 1]
if (isObject(info)) {
const link = info.link
return {
// Normalize link for router use
link: isObject(link) ? link : this.useRouter ? { path: link } : link,
// Make sure text has a value
text: toString(info.text || pageNumber)
}
} else {
return { link: toString(info), text: toString(pageNumber) }
}
},
makePage(pageNumber) {
const { pageGen } = this
const info = this.getPageInfo(pageNumber)
if (hasPropFunction(pageGen)) {
return pageGen(pageNumber, info)
}
return info.text
},
makeLink(pageNumber) {
const { linkGen } = this
const info = this.getPageInfo(pageNumber)
if (hasPropFunction(linkGen)) {
return linkGen(pageNumber, info)
}
return info.link
},
linkProps(pageNumber) {
const props = pluckProps(linkProps, this)
const link = this.makeLink(pageNumber)
if (this.useRouter || isObject(link)) {
props.to = link
} else {
props.href = link
}
return props
},
resolveLink(to = '') {
// Given a to (or href string), convert to normalized route-like structure
// Works only client side!
let link
try {
// Convert the `to` to a HREF via a temporary `a` tag
link = document.createElement('a')
link.href = computeHref({ to }, 'a', '/', '/')
// We need to add the anchor to the document to make sure the
// `pathname` is correctly detected in any browser (i.e. IE)
document.body.appendChild(link)
// Once href is assigned, the link will be normalized to the full URL bits
const { pathname, hash, search } = link
// Remove link from document
document.body.removeChild(link)
// Return the location in a route-like object
return { path: pathname, hash, query: parseQuery(search) }
} catch (e) {
/* istanbul ignore next */
try {
link && link.parentNode && link.parentNode.removeChild(link)
} catch {}
/* istanbul ignore next */
return {}
}
},
resolveRoute(to = '') {
// Given a to (or href string), convert to normalized route location structure
// Works only when router available!
try {
const route = this.$router.resolve(to, this.$route).route
return { path: route.path, hash: route.hash, query: route.query }
} catch (e) {
/* istanbul ignore next */
return {}
}
},
guessCurrentPage() {
const { $router, $route } = this
let guess = this.computedValue
// This section only occurs if we are client side, or server-side with `$router`
if (!this.noPageDetect && !guess && (IS_BROWSER || (!IS_BROWSER && $router))) {
// Current route (if router available)
const currentRoute =
$router && $route ? { path: $route.path, hash: $route.hash, query: $route.query } : {}
// Current page full HREF (if client side)
// Can't be done as a computed prop!
const loc = IS_BROWSER ? window.location || document.location : null
const currentLink = loc
? { path: loc.pathname, hash: loc.hash, query: parseQuery(loc.search) }
: /* istanbul ignore next */ {}
// Loop through the possible pages looking for a match until found
for (let pageNumber = 1; !guess && pageNumber <= this.localNumberOfPages; pageNumber++) {
const to = this.makeLink(pageNumber)
if ($router && (isObject(to) || this.useRouter)) {
// Resolve the page via the `$router`
guess = looseEqual(this.resolveRoute(to), currentRoute) ? pageNumber : null
} else if (IS_BROWSER) {
// If no `$router` available (or `!this.useRouter` when `to` is a string)
// we compare using parsed URIs
guess = looseEqual(this.resolveLink(to), currentLink) ? pageNumber : null
} else {
// Probably SSR, but no `$router` so we can't guess,
// so lets break out of the loop early
/* istanbul ignore next */
guess = -1
}
}
}
// We set `currentPage` to `0` to trigger an `$emit('input', null)`
// As the default for `currentPage` is `-1` when no value is specified
// Valid page numbers are greater than `0`
this.currentPage = guess > 0 ? guess : 0
}
}
})