Skip to content

Commit 4d39723

Browse files
committed
mixins/pagination: Extract pagination computed property macro
1 parent e65fdbe commit 4d39723

File tree

1 file changed

+60
-60
lines changed

1 file changed

+60
-60
lines changed

app/mixins/pagination.js

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

77
// eslint-disable-next-line ember/no-new-mixins
88
export default Mixin.create({
9-
// wire up these ember-style variables to the expected query parameters
10-
itemsPerPage: readOnly('per_page'),
11-
selectedPage: readOnly('page'),
9+
pagination: pagination(),
1210

13-
currentPage: computed('selectedPage', function () {
14-
return parseInt(this.selectedPage, 10) || 1;
15-
}),
11+
currentPage: readOnly('pagination.currentPage'),
12+
currentPageStart: readOnly('pagination.currentPageStart'),
13+
currentPageEnd: readOnly('pagination.currentPageEnd'),
1614

17-
currentPageStart: computed('currentPage', 'itemsPerPage', 'totalItems', function () {
18-
if (this.totalItems === 0) {
19-
return 0;
20-
}
21-
return (this.currentPage - 1) * this.itemsPerPage + 1;
22-
}),
15+
availablePages: readOnly('pagination.availablePages'),
2316

24-
currentPageEnd: computed('currentPage', 'itemsPerPage', 'totalItems', function () {
25-
return Math.min(this.currentPage * this.itemsPerPage, this.totalItems);
26-
}),
17+
nextPage: readOnly('pagination.nextPage'),
18+
prevPage: readOnly('pagination.prevPage'),
2719

28-
availablePages: computed('totalItems', 'itemsPerPage', function () {
29-
return Math.ceil(this.totalItems / this.itemsPerPage || 1);
30-
}),
20+
pages: readOnly('pagination.pages'),
21+
});
3122

32-
nextPage: computed('currentPage', 'availablePages', function () {
33-
let nextPage = this.currentPage + 1;
34-
let availablePages = this.availablePages;
35-
if (nextPage <= availablePages) {
36-
return nextPage;
37-
} else {
38-
return this.currentPage;
39-
}
40-
}),
23+
function pagination() {
24+
return computed('page', 'per_page', 'totalItems', function () {
25+
let { page, per_page: perPage, totalItems } = this;
26+
return _pagination(page, perPage, totalItems);
27+
});
28+
}
4129

42-
prevPage: computed('currentPage', function () {
43-
let prevPage = this.currentPage - 1;
44-
if (prevPage > 0) {
45-
return prevPage;
46-
} else {
47-
return this.currentPage;
48-
}
49-
}),
30+
function _pagination(page, perPage, totalItems) {
31+
let currentPage = parseInt(page, 10) || 1;
5032

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;
33+
let currentPageStart = totalItems === 0 ? 0 : (currentPage - 1) * perPage + 1;
34+
let currentPageEnd = Math.min(currentPage * perPage, totalItems);
5835

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-
}),
76-
});
36+
let availablePages = Math.ceil(totalItems / perPage || 1);
37+
38+
let nextPage = currentPage + 1;
39+
if (nextPage > availablePages) {
40+
nextPage = currentPage;
41+
}
42+
43+
let prevPage = currentPage - 1;
44+
if (prevPage <= 0) {
45+
prevPage = currentPage;
46+
}
47+
48+
// Always show the same number of pages even if we're
49+
// at the beginning or at the end of the list.
50+
let lowerBound, upperBound;
51+
if (availablePages - currentPage < Math.ceil(VIEWABLE_PAGES / 2)) {
52+
lowerBound = Math.max(0, availablePages - VIEWABLE_PAGES);
53+
upperBound = availablePages;
54+
} else if (currentPage <= Math.ceil(VIEWABLE_PAGES / 2)) {
55+
lowerBound = 0;
56+
upperBound = Math.min(availablePages, VIEWABLE_PAGES);
57+
} else {
58+
lowerBound = currentPage - Math.ceil(VIEWABLE_PAGES / 2);
59+
upperBound = currentPage + Math.floor(VIEWABLE_PAGES / 2);
60+
}
61+
62+
let pages = [];
63+
for (let i = lowerBound; i < upperBound; i++) {
64+
pages.push(i + 1);
65+
}
66+
67+
return {
68+
currentPage,
69+
currentPageStart,
70+
currentPageEnd,
71+
availablePages,
72+
nextPage,
73+
prevPage,
74+
pages,
75+
};
76+
}

0 commit comments

Comments
 (0)