-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
133 lines (112 loc) · 3.13 KB
/
main.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
133
class Pagination {
constructor(appendId, pageSize = 100, maxShown = 5) {
this.appendId = appendId;
this.pageSize = pageSize; // use setter
this.maxShown = maxShown; // use setter
this._current = 1;
this.init();
}
// getter and setter
get pageSize() {
return this._pageSize;
}
get maxShown() {
return this._maxShown;
}
get current() {
return this._current;
}
set pageSize(pageSize) {
if (pageSize <= 0) throw new Error('Not a valid pageSize number.');
this._pageSize = pageSize;
}
set maxShown(num) {
if (num <= 0) throw new Error('Not a valid maxShown number');
this._maxShown = num;
}
set current(num) {
if (num > this._pageSize) num = this._pageSize;
if (num < 1) num = 1;
this._current = num;
this.render();
}
// main functions
init() {
let pagination = this.render();
// add click handlers to page item and nav
pagination.addEventListener('click', e => {
if (e.target.tagName === 'LI') {
if (!e.target.className.includes('nav')) {
let pageNum = +e.target.textContent;
this.goToPage(pageNum);
} else {
if (e.target.className.includes('lt')) this.prev();
else this.next();
}
}
});
document.getElementById(this.appendId).appendChild(pagination);
}
goToPage(pageNum) {
// save rendering times
if (pageNum !== this._current) this.current = pageNum;
}
prev() {
this.current -= 1;
}
next() {
this.current += 1;
}
render() {
// rerender according to the current page's position
let list = document.querySelector(`#${this.appendId} .easy-pagination`) || this.createWrapper();
let childHTML;
if (this._current <= 3) {
// E.g, <1,2,3,4,5,...,10>
childHTML = this.buildList('head');
} else if (this._current >= this._pageSize - 2) {
// E.g, <1,...,6,7,8,9,10>
childHTML = this.buildList('tail');
} else {
// E.g, <1,...,2,3,4,5,6,...,10>
childHTML = this.buildList('middle');
}
list.innerHTML = childHTML;
// add navigations
list.insertAdjacentHTML('afterbegin', '<li class="nav lt"><</li>');
list.insertAdjacentHTML('beforeend', '<li class="nav gt">></li>');
return list;
}
// helper methods
buildList(type) {
let html = '';
if (type === 'head') {
let initLen = Math.min(this._pageSize, this.maxShown);
html += this.buildListBody(1, initLen);
if (this._pageSize > 5)
html += `<span>...</span><li>${this._pageSize}</li>`;
} else if (type === 'tail') {
html += '<li>1</li><span>...</span>';
html += this.buildListBody(this._pageSize - 4, this.maxShown);
} else if (type === 'middle') {
html += '<li>1</li><span>...</span>';
html += this.buildListBody(this._current - 2, this.maxShown);
html += `<span>...</span><li>${this._pageSize}</li>`;
}
return html;
}
buildListBody(startFrom, length) {
return Array.from({ length }, (_, index) => {
let currPageNum = startFrom + index;
if (currPageNum === this._current) {
return `<li class='active'>${currPageNum}</li>`;
}
return `<li>${currPageNum}</li>`;
}).join('');
}
createWrapper() {
let pagination = document.createElement('ul');
pagination.className = 'easy-pagination';
return pagination;
}
}