-
Notifications
You must be signed in to change notification settings - Fork 426
/
Copy pathpage-resolver.js
133 lines (116 loc) · 3.81 KB
/
page-resolver.js
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
/* eslint no-mixed-operators: 0 */
export default ExtendBase =>
class PageResolver extends ExtendBase {
backToPrevPage() {
const { currPage, pageStartIndex } = this.props;
return (currPage - 1) < pageStartIndex ? pageStartIndex : currPage - 1;
}
goToNextPage() {
const { currPage } = this.props;
const { lastPage } = this.state;
return (currPage + 1) > lastPage ? lastPage : currPage + 1;
}
initialState() {
const totalPages = this.calculateTotalPage();
const lastPage = this.calculateLastPage(totalPages);
return { totalPages, lastPage, dropdownOpen: false };
}
calculateTotalPage(sizePerPage = this.props.currSizePerPage, dataSize = this.props.dataSize) {
return Math.ceil(dataSize / sizePerPage);
}
calculateLastPage(totalPages) {
const { pageStartIndex } = this.props;
return pageStartIndex + totalPages - 1;
}
calculatePages(
totalPages = this.state.totalPages,
lastPage = this.state.lastPage) {
const {
currPage,
paginationSize,
pageStartIndex,
withFirstAndLast,
firstPageText,
prePageText,
nextPageText,
lastPageText,
alwaysShowAllBtns
} = this.props;
let pages;
let endPage = totalPages;
if (endPage <= 0) return [];
let startPage = Math.max(currPage - Math.floor(paginationSize / 2), pageStartIndex);
endPage = startPage + paginationSize - 1;
if (endPage > lastPage) {
endPage = lastPage;
startPage = endPage - paginationSize + 1;
}
if (startPage !== pageStartIndex && totalPages > paginationSize && withFirstAndLast) {
pages = [firstPageText, prePageText];
} else if (totalPages > 1 || alwaysShowAllBtns) {
pages = [prePageText];
} else {
pages = [];
}
for (let i = startPage; i <= endPage; i += 1) {
if (i >= pageStartIndex) pages.push(i);
}
if (endPage <= lastPage && pages.length > 1) {
pages.push(nextPageText);
}
if (endPage !== lastPage && withFirstAndLast) {
pages.push(lastPageText);
}
return pages;
}
calculatePageStatus(pages = [], lastPage = this.state.lastPage) {
const {
currPage,
pageStartIndex,
firstPageText,
prePageText,
nextPageText,
lastPageText,
alwaysShowAllBtns
} = this.props;
const isStart = page =>
(currPage === pageStartIndex && (page === firstPageText || page === prePageText));
const isEnd = page =>
(currPage === lastPage && (page === nextPageText || page === lastPageText));
return pages
.filter((page) => {
if (alwaysShowAllBtns) {
return true;
}
return !(isStart(page) || isEnd(page));
})
.map((page) => {
let title;
const active = page === currPage;
const disabled = (isStart(page) || isEnd(page));
if (page === nextPageText) {
title = this.props.nextPageTitle;
} else if (page === prePageText) {
title = this.props.prePageTitle;
} else if (page === firstPageText) {
title = this.props.firstPageTitle;
} else if (page === lastPageText) {
title = this.props.lastPageTitle;
} else {
title = `${page}`;
}
return { page, active, disabled, title };
});
}
calculateSizePerPageStatus() {
const { sizePerPageList } = this.props;
return sizePerPageList.map((_sizePerPage) => {
const pageText = _sizePerPage.text || _sizePerPage;
const pageNumber = _sizePerPage.value || _sizePerPage;
return {
text: `${pageText}`,
page: pageNumber
};
});
}
};