-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday-38.js
More file actions
116 lines (95 loc) · 3.25 KB
/
day-38.js
File metadata and controls
116 lines (95 loc) · 3.25 KB
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
class Pagination {
constructor(container, config) {
const newConfig = config || {};
this.html = '';
this.total = newConfig.total || 10;
this.page = newConfig.page || 1;
this.step = newConfig.step || 3;
this.container = container;
this.list = container.querySelector('.pagination-list');
this.prev = container.querySelector('.prev');
this.next = container.querySelector('.next');
// start - example
this.main = document.querySelector('[data-js="main"]');
// end - example
this.handleClick = this.handleClick.bind(this);
this.handlePrevClick = this.handlePrevClick.bind(this);
this.handleNextClick = this.handleNextClick.bind(this);
this.render();
}
renderFirst() {
return '<li class="pagination-item"><button class="pagination-button">1</button></li><li class="pagination-item"><span>...</span></li>';
}
renderItems(begin, end) {
let items = '';
for (let i = begin; i < end; i++) {
items += '<li class="pagination-item"><button class="pagination-button">' + i + '</button></li>';
}
return items;
}
renderLast() {
return '<li class="pagination-item"><span>...</span></li><li class="pagination-item"><button class="pagination-button">' + this.total + '</button></li>';
}
handleClick(evt) {
this.page = parseInt(evt.target.innerText);
// start - example
this.main.innerHTML = this.page;
// end - example
this.render();
}
handlePrevClick() {
this.page--;
if (this.page < 1) {
this.page = 1;
}
this.render();
}
handleNextClick() {
this.page++;
if (this.page > this.total) {
this.page = this.total;
}
this.render();
}
bind() {
const items = this.list.querySelectorAll('.pagination-button');
this.prev.addEventListener('click', this.handlePrevClick);
this.next.addEventListener('click', this.handleNextClick);
for (let i = 0; i < items.length; i++) {
if (parseInt(items[i].innerText) === this.page) items[i].classList.add('active');
items[i].addEventListener('click', this.handleClick);
}
}
render() {
// minimun: total = 10, step = 1
if (this.total < this.step * 2 + 9) {
this.html += this.renderItems(1, this.total + 1);
}
// ex: total = 20, page = 4 , step = 2; 4 < 5
else if (this.page < this.step * 2 + 1) {
this.html += this.renderItems(1, this.step * 2 + 4);
this.html += this.renderLast();
}
// ex: total = 20, page = 17 , step = 2; === 17 > 16
else if (this.page > this.total - this.step * 2) {
this.html += this.renderFirst();
this.html += this.renderItems(this.total - this.step * 2 - 2, this.total + 1);
}
// ex: total = 20, page = 5,6,7,8,9,10,11,12,13,14,15,16 , step = 2;
else {
this.html += this.renderFirst();
this.html += this.renderItems(this.page - this.step, this.page + this.step + 1);
this.html += this.renderLast();
}
// start - example
this.main.innerHTML = this.page;
// end - example
this.list.innerHTML = this.html;
this.html = '';
this.bind();
}
};
const thirtyEight = () => {
new Pagination(document.querySelector('[data-js="pagination"]'), { total: 40, page: 1, step: 2 });
};
document.addEventListener('DOMContentLoaded', thirtyEight);