This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathPagination.vue
116 lines (112 loc) · 3.01 KB
/
Pagination.vue
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
<template>
<ul :class="bem()" v-show="totalResults > 0">
<li :class="[bem('item', 'first'), page === 1 ? bem('item', 'disabled', false) : '']">
<a href="#" @click.prevent="goToFirstPage" :class="bem('link')">
<slot name="first"><<</slot>
</a>
</li>
<li :class="[bem('item', 'previous'), page === 1 ? bem('item', 'disabled', false) : '']">
<a href="#" @click.prevent="goToPreviousPage" :class="bem('link')">
<slot name="previous"><</slot>
</a>
</li>
<li v-for="item in pages" :key="item" :class="[bem('item'), page === item ? bem('item', 'active', false) : '']" >
<a href="#" @click.prevent="goToPage(item)" :class="bem('link')">
<slot :value="item" :active="item === page">
{{ item }}
</slot>
</a>
</li>
<li :class="[bem('item', 'next'), page >= totalPages ? bem('item', 'disabled', false) : '']">
<a href="#" @click.prevent="goToNextPage" :class="bem('link')">
<slot name="next">></slot>
</a>
</li>
<li :class="[bem('item', 'last'), page >= totalPages ? bem('item', 'disabled', false) : '']">
<a href="#" @click.prevent="goToLastPage" :class="bem('link')">
<slot name="last">>></slot>
</a>
</li>
</ul>
</template>
<script>
import algoliaComponent from '../component';
export default {
mixins: [algoliaComponent],
props: {
padding: {
type: Number,
default: 3,
validator(value) {
return value > 0;
},
},
},
data() {
return {
blockClassName: 'ais-pagination',
};
},
computed: {
page() {
return this.searchStore.page;
},
totalPages() {
return this.searchStore.totalPages;
},
pages() {
let maxPages = this.padding * 2;
if (this.totalPages - 1 < maxPages) {
maxPages = this.totalPages - 1;
}
const pages = [this.page];
let even = false;
let lastPage = this.page;
let firstPage = this.page;
while (pages.length <= maxPages) {
even = !even;
if (even) {
if (firstPage <= 1) {
continue; // eslint-disable-line no-continue
}
firstPage--;
pages.unshift(firstPage);
} else {
if (lastPage >= this.totalPages) {
continue; // eslint-disable-line no-continue
}
lastPage++;
pages.push(lastPage);
}
}
return pages;
},
totalResults() {
return this.searchStore.totalResults;
},
},
methods: {
goToPage(page) {
let p = Math.max(1, page);
p = Math.min(this.totalPages, p);
if (this.searchStore.page === p) {
return;
}
this.searchStore.page = p;
this.$emit('page-change');
},
goToFirstPage() {
this.goToPage(1);
},
goToPreviousPage() {
this.goToPage(this.page - 1);
},
goToNextPage() {
this.goToPage(this.page + 1);
},
goToLastPage() {
this.goToPage(this.totalPages);
},
},
};
</script>