Skip to content

Commit 09f173c

Browse files
committed
refactor: CPagination logic redesing
1 parent 7747524 commit 09f173c

File tree

1 file changed

+33
-64
lines changed

1 file changed

+33
-64
lines changed

src/components/Pagination/CPagination.vue

Lines changed: 33 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</CLink>
2727
</li>
2828
<li
29-
v-if="showFirstDots"
29+
v-if="beforeDots"
3030
role="separator"
3131
class="c-page-item c-disabled c-d-none c-d-sm-flex"
3232
>
@@ -36,7 +36,7 @@
3636
<li
3737
v-for="(item, index) in items"
3838
:key="index"
39-
:class="setStyle(item)"
39+
:class="[{ 'c-active': activePage === item }, 'c-page-item']"
4040
>
4141
<CLink
4242
class="c-page-link"
@@ -48,7 +48,7 @@
4848
</li>
4949

5050
<li
51-
v-if="showLastDots"
51+
v-if="afterDots"
5252
role="separator"
5353
class="c-page-item c-disabled c-d-none c-d-sm-flex"
5454
>
@@ -131,16 +131,11 @@
131131
},
132132
data () {
133133
return {
134-
showFirstDots: false,
135-
showLastDots: false,
136134
rwd: this.size,
137135
erd: elementResizeDetectorMaker()
138136
}
139137
},
140138
watch: {
141-
// activePage (val) {
142-
// this.page = val
143-
// },
144139
pages (val) {
145140
if (val < this.activePage) {
146141
this.$emit('update:activePage', val)
@@ -163,59 +158,38 @@
163158
return `c-pagination c-pagination-${this.rwd} c-justify-content-${this.align} `
164159
},
165160
dots () {
166-
return this.hideDots || this.limit < 5 ? false : true
161+
return !this.hideDots && this.limit > 4 && this.limit < this.pages
162+
},
163+
maxPrevItems () {
164+
return Math.floor((this.limit - 1) / 2)
165+
},
166+
maxNextItems () {
167+
return Math.ceil((this.limit - 1) / 2)
168+
},
169+
beforeDots () {
170+
return this.dots && this.activePage > this.maxPrevItems + 1
171+
},
172+
afterDots () {
173+
return this.dots && this.activePage < this.pages - this.maxNextItems
174+
},
175+
computedLimit () {
176+
return this.limit - this.afterDots - this.beforeDots
177+
},
178+
range () {
179+
return this.activePage + this.maxNextItems
180+
},
181+
lastItem () {
182+
return this.range >= this.pages ? this.pages : this.range-this.afterDots
167183
},
168184
items () {
169-
let maxPrevItems = Math.floor((this.limit - 1) / 2)
170-
let maxNextItems = Math.ceil((this.limit - 1) / 2)
171-
let items = []
172-
173-
if (!this.dots) {
174-
this.showFirstDots = false
175-
this.showLastDots = false
176-
if (this.activePage <= maxPrevItems) {
177-
for (let i = 1; i <= this.limit; i++)
178-
items.push(i)
179-
} else {
180-
let max = this.activePage + maxNextItems > this.pages ? this.pages : this.activePage + maxNextItems
181-
for (let i = max - this.limit + 1; i <= max; i++)
182-
items.push(i)
183-
}
184-
return items
185-
}
186-
187-
if (this.limit >= this.pages) {
188-
this.showFirstDots = false
189-
this.showLastDots = false
190-
for (let i = 1; i <= this.pages; i++)
191-
items.push(i)
192-
return items
185+
if (this.activePage - this.maxPrevItems <= 1 ) {
186+
return Array.from({ length: this.computedLimit }, (v, i) => i + 1 )
187+
} else {
188+
return Array.from({length: this.computedLimit}, (v, i) => {
189+
return this.lastItem - i
190+
}).reverse()
193191
}
194-
195-
if (this.activePage <= maxPrevItems) {
196-
this.showFirstDots = false
197-
this.showLastDots = true
198-
for (let i = 1; i <= this.limit - 1; i++)
199-
items.push(i)
200-
return items
201-
}
202-
203-
if (this.activePage > maxPrevItems && this.activePage < this.pages - maxNextItems) {
204-
this.showFirstDots = true
205-
this.showLastDots = true
206-
for (let i = 1 ; i < this.limit - 1 ; i++)
207-
items.push(this.activePage - maxPrevItems + i)
208-
return items
209-
}
210-
211-
if (this.activePage > maxPrevItems && this.activePage >= this.pages - maxNextItems) {
212-
this.showFirstDots = true
213-
this.showLastDots = false
214-
for (let i = this.pages - this.limit + 2 ; i <= this.pages; i++)
215-
items.push(i)
216-
return items
217-
}
218-
},
192+
}
219193
},
220194
methods: {
221195
onWrapperResize (el) {
@@ -224,14 +198,9 @@
224198
this.rwd = 'md' : this.rwd = 'sm'
225199
},
226200
setPage (number) {
227-
if(number !== this.activePage)
201+
if (number !== this.activePage) {
228202
this.$emit('update:activePage', number)
229-
},
230-
setStyle (item) {
231-
if(this.activePage === item) {
232-
return 'c-page-item c-active'
233203
}
234-
return 'c-page-item'
235204
}
236205
}
237206
}

0 commit comments

Comments
 (0)