forked from Open-Source-Contributors-JSS/Hacktoberfest2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitinit.js
167 lines (149 loc) · 4.39 KB
/
gitinit.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
166
167
let data = getcommandsData();
let current_page;
let lastActive;
let numberOfPages;
let list;
function getcommandsData(){
let url = '../beginner/json-files/git-commands.json';
return new Promise((resolve, reject) => {
fetch(url)
.then(data => data.json())
.then(jsondata => resolve(jsondata))
.catch(err => reject(err));
})
}
data.then(json => htmldata(json))
let len=0;
function htmldata(jsondata){
list = [];
jsondata.forEach(item => {
list.push(item);
len++;
})
numberOfPages = Math.ceil( len / numberPerPage);
current_page = 1;
makeButtons(numberOfPages);
lastActive = numberOfPages;
firstPage();
}
// pagination logic
let pageList = [];
let numberPerPage = 9;
const makeButtons = (numberOfPages) => {
var buttons = '<button id="-1" class="btn btn-light"><</button>';
for(var i = 1; i<=numberOfPages; i++){
buttons += `
<button id="${i}" class="btn btn-light">${i}</button>
`
}
buttons += '<button id="+1" class="btn btn-light">></button>'
document.querySelectorAll(".button-container")[0].innerHTML = buttons;
addEvent();
}
const addEvent = () => {
let totalButtons = document.querySelectorAll(".button-container")[0].querySelectorAll("button").length;
for(var i=0; i < totalButtons; i++){
document.querySelectorAll(".button-container")[0].querySelectorAll("button")[i].onclick = function(e){
var button_no = parseInt(this.id);
console.log(button_no);
getCurrentPage(button_no);
}
}
}
const previousPage = () => {
current_page -= 1;
showButton();
loadList();
check();
}
const nextPage = () => {
current_page += 1;
showButton();
loadList();
check();
}
const firstPage = () => {
current_page = 1;
showButton();
loadList();
check();
}
const getCurrentPage = (e) => {
if(e == -1 || e == +1){
if(e == -1) previousPage();
else nextPage();
}
else{
current_page = e;
console.log(e);
showButton();
loadList();
check();
}
}
const showButton = () =>{
var i;
if(current_page == 1 || current_page == 2 || current_page == 3){
for(i = 1; i<=numberOfPages; i++){
if(i <= 3) {
document.querySelectorAll(".button-container")[0].querySelectorAll("button")[i].style.display = "inline-block";
}
else {
document.querySelectorAll(".button-container")[0].querySelectorAll("button")[i].style.display = "none";
}
}
}
else if(current_page == numberOfPages){
for(i = 1; i<=numberOfPages; i++){
if(i >= numberOfPages-2) {
document.querySelectorAll(".button-container")[0].querySelectorAll("button")[i].style.display = "inline-block";
}
else {
document.querySelectorAll(".button-container")[0].querySelectorAll("button")[i].style.display = "none";
}
}
}
else {
for(i = 1; i<=numberOfPages; i++){
if(i == current_page || i == current_page - 1 || i == current_page + 1) {
document.querySelectorAll(".button-container")[0].querySelectorAll("button")[i].style.display = "inline-block";
}
else {
document.querySelectorAll(".button-container")[0].querySelectorAll("button")[i].style.display = "none";
}
}
}
activateButton();
}
const activateButton = () => {
document.querySelectorAll(".button-container")[0].querySelectorAll("button")[lastActive].classList.remove("btn-active");
document.querySelectorAll(".button-container")[0].querySelectorAll("button")[current_page].classList.add("btn-active");
}
const loadList = () => {
let begin = ((current_page - 1) * numberPerPage);
let end = begin + numberPerPage;
pageList = list.slice(begin, end);
drawList();
lastActive = current_page;
}
const drawList = () => {
let output = '';
pageList.forEach(item => {
output += `
<div class="">
<div class="card">
<div class="card-header">
<h3 class="title text-center is-4">${item.title}</h3>
</div>
<div class="card-body">
<p>${item.description}</p>
</div>
</div>
</div>`
})
document.getElementsByClassName("grid-container")[0].innerHTML = output;
}
const check = () => {
document.getElementById("-1").style.display = ( current_page == 1 || current_page == 2 || current_page == 3) ? 'none' : 'inline-block';
document.getElementById("+1").style.display = ( current_page == numberOfPages || current_page == numberOfPages -1 || current_page == numberOfPages + 1) ? 'none' : 'inline-block';
}