-
Notifications
You must be signed in to change notification settings - Fork 1
/
pagiator.js
165 lines (154 loc) · 5.99 KB
/
pagiator.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Pagination Creator
* Input: firstPage, lastPage, neighbor, currentPage
* Output: Array of Pagination
* Author: Lmint - CyberSkill
* Website: Cyberskill.tech
* Facebook: fb.com/minhthong.lu.58
*/
(function(global) {
function newPage(value, choosing) {
if (!value)
throw new Error('newPage is missing page number as parameter!');
return {
value: value,
choosing: choosing ? true : false
}
}
// Create array of numbers from x to y
function makeRange(start, end, current) {
if (!start || !end || !current)
throw new Error('makeRange is missing required parameters!');
if (typeof start !== 'number' || typeof end !== 'number') {
throw new Error('parameters must is a number!');
}
var range = [];
for (var i = start; i <= end; i++)
range.push(newPage(i, i === current));
return range;
}
function HTMLElementCreate(el, classnames, text) {
var html = document.createElement(el);
classnames.split(' ').forEach(function(classname) {
html.classList.add(classname);
})
if (text) {
var textNode = document.createTextNode(text);
html.appendChild(textNode);
}
return html;
}
// Function constructor of Pagi
function Pg(totalPage, neighbor) {
this.totalPage = totalPage || 1;
this.neighbor = neighbor || 2;
this.currentPage = 1;
}
// Setup Prototype use for all instances
Pg.prototype = {
firstPage: 1,
baseUrl: '',
textContent: {
leftDirection: '<',
rightDirection: '>'
},
HTMLClasses: {
container: 'pagination',
page: 'page',
currentPage: 'page choosing',
direction: 'direction'
},
setup: function(o) {
if (o.baseUrl)
this.baseUrl = o.baseUrl;
if (o.HTMLClasses)
this.HTMLClasses = o.HTMLClasses;
if (o.textContent)
this.textContent = o.textContent;
return this;
},
choose: function(currentPage) {
this.currentPage = currentPage;
return this;
},
ArrayRender: function() {
var totalPage = this.totalPage,
currentPage = this.currentPage,
firstPage = this.firstPage,
neighbor = this.neighbor,
totalBlock = this.neighbor * 2 + 5;
if (!currentPage)
throw new Error('Choosing page number is required!');
if (currentPage < 1)
throw new Error(`Can't choose negative number as page`);
if (currentPage > totalPage)
throw new Error(`You can't choose the page greater than last page`);
if (totalPage <= totalBlock) return makeRange(1, totalPage, currentPage);
var startOfRange = currentPage - neighbor,
endOfRange = currentPage + neighbor;
if (startOfRange < 3) {
return [
newPage(firstPage, currentPage === firstPage),
...makeRange(firstPage + 1, neighbor * 2 + 3, currentPage),
newPage('RIGHT'),
newPage(totalPage)
];
}
if (endOfRange >= totalPage - 1) {
return [
newPage(firstPage, currentPage === firstPage),
newPage('LEFT'),
...makeRange(totalPage - 2 - neighbor * 2, totalPage - 1, currentPage),
newPage(totalPage, totalPage === currentPage)
];
}
return [
newPage(1),
newPage('LEFT'),
...makeRange(startOfRange, endOfRange, currentPage),
newPage('RIGHT'),
newPage(totalPage)
]
},
HTMLRender: function(selector) {
var pagination = this.ArrayRender(this.currentPage),
currentPage = this.currentPage,
classes = this.HTMLClasses,
baseUrl = this.baseUrl || '',
t = this.textContent,
wrapper = HTMLElementCreate('div', this.HTMLClasses.container || 'pagination');
if (!document) {
console.log(`This enviroment doesn't have document Object (DOM)`);
return;
}
if (!selector)
throw new Error('Selector is required in HTMLRender!');
pagination.forEach(function(data) {
switch (data.value) {
case 'LEFT':
var leftDirection = HTMLElementCreate('a', classes.direction || 'direction', t.leftDirection);
leftDirection.href = baseUrl + '/' + (currentPage - 1);
wrapper.appendChild(leftDirection);
break;
case 'RIGHT':
var rightDirection = HTMLElementCreate('a', classes.direction || 'direction', t.rightDirection);
rightDirection.href = baseUrl + '/' + (currentPage + 1);
wrapper.appendChild(rightDirection);
break;
case currentPage:
var current = HTMLElementCreate('a', classes.currentPage || 'page choosing', data.value);
current.href = baseUrl + '/' + data.value;
wrapper.appendChild(current);
break;
default:
var page = HTMLElementCreate('a', classes.page || 'page', data.value);
page.href = baseUrl + '/' + data.value;
wrapper.appendChild(page);
break;
}
});
document.querySelectorAll(selector)[0].appendChild(wrapper);
}
}
this.Pg = Pg;
}(this));