Skip to content

Commit e65fdbe

Browse files
committed
mixins/pagination: Reorder computed properties
This essentially orders them by dependencies, so that the ones that don't depend on others are at the top
1 parent d582909 commit e65fdbe

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

app/mixins/pagination.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,9 @@ const VIEWABLE_PAGES = 9;
66

77
// eslint-disable-next-line ember/no-new-mixins
88
export default Mixin.create({
9-
// Gives page numbers to the surrounding 9 pages.
10-
pages: computed('currentPage', 'availablePages', function () {
11-
let pages = [];
12-
let currentPage = this.currentPage;
13-
let availablePages = this.availablePages;
14-
let lowerBound = 0;
15-
let upperBound = 0;
16-
17-
// Always show the same number of pages even if we're
18-
// at the beginning or at the end of the list.
19-
if (availablePages - currentPage < Math.ceil(VIEWABLE_PAGES / 2)) {
20-
lowerBound = Math.max(0, availablePages - VIEWABLE_PAGES);
21-
upperBound = availablePages;
22-
} else if (currentPage <= Math.ceil(VIEWABLE_PAGES / 2)) {
23-
lowerBound = 0;
24-
upperBound = Math.min(availablePages, VIEWABLE_PAGES);
25-
} else {
26-
lowerBound = currentPage - Math.ceil(VIEWABLE_PAGES / 2);
27-
upperBound = currentPage + Math.floor(VIEWABLE_PAGES / 2);
28-
}
29-
for (let i = lowerBound; i < upperBound; i++) {
30-
pages.push(i + 1);
31-
}
32-
return pages;
33-
}),
9+
// wire up these ember-style variables to the expected query parameters
10+
itemsPerPage: readOnly('per_page'),
11+
selectedPage: readOnly('page'),
3412

3513
currentPage: computed('selectedPage', function () {
3614
return parseInt(this.selectedPage, 10) || 1;
@@ -47,6 +25,10 @@ export default Mixin.create({
4725
return Math.min(this.currentPage * this.itemsPerPage, this.totalItems);
4826
}),
4927

28+
availablePages: computed('totalItems', 'itemsPerPage', function () {
29+
return Math.ceil(this.totalItems / this.itemsPerPage || 1);
30+
}),
31+
5032
nextPage: computed('currentPage', 'availablePages', function () {
5133
let nextPage = this.currentPage + 1;
5234
let availablePages = this.availablePages;
@@ -66,11 +48,29 @@ export default Mixin.create({
6648
}
6749
}),
6850

69-
availablePages: computed('totalItems', 'itemsPerPage', function () {
70-
return Math.ceil(this.totalItems / this.itemsPerPage || 1);
71-
}),
51+
// Gives page numbers to the surrounding 9 pages.
52+
pages: computed('currentPage', 'availablePages', function () {
53+
let pages = [];
54+
let currentPage = this.currentPage;
55+
let availablePages = this.availablePages;
56+
let lowerBound = 0;
57+
let upperBound = 0;
7258

73-
// wire up these ember-style variables to the expected query parameters
74-
itemsPerPage: readOnly('per_page'),
75-
selectedPage: readOnly('page'),
59+
// Always show the same number of pages even if we're
60+
// at the beginning or at the end of the list.
61+
if (availablePages - currentPage < Math.ceil(VIEWABLE_PAGES / 2)) {
62+
lowerBound = Math.max(0, availablePages - VIEWABLE_PAGES);
63+
upperBound = availablePages;
64+
} else if (currentPage <= Math.ceil(VIEWABLE_PAGES / 2)) {
65+
lowerBound = 0;
66+
upperBound = Math.min(availablePages, VIEWABLE_PAGES);
67+
} else {
68+
lowerBound = currentPage - Math.ceil(VIEWABLE_PAGES / 2);
69+
upperBound = currentPage + Math.floor(VIEWABLE_PAGES / 2);
70+
}
71+
for (let i = lowerBound; i < upperBound; i++) {
72+
pages.push(i + 1);
73+
}
74+
return pages;
75+
}),
7676
});

0 commit comments

Comments
 (0)