This repository has been archived by the owner on May 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
championstats.html
213 lines (203 loc) · 8.82 KB
/
championstats.html
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>LoLBS</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link href="https://stackpath.bootstrapcdn.com/bootswatch/4.3.1/superhero/bootstrap.min.css" rel="stylesheet" integrity="sha384-LS4/wo5Z/8SLpOLHs0IbuPAGOWTx30XSoZJ8o7WKH0UJhRpjXXTpODOjfVnNjeHu" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<link href="./open-iconic/font/css/open-iconic-bootstrap.min.css" rel="stylesheet">
<style>
.pointer {cursor: pointer;}
</style>
</head>
<body>
<nav class="navbar navbar-expand-sm bg-primary navbar-light">
<a class="navbar-brand">LoLBS</a>
<ul class="navbar-nav">
<li class="nav-item">
<a href="index.html" class="btn btn-secondary" role="button">Home</a>
</li>
<li class="nav-item">
<a href="championstats.html" class="btn btn-secondary active" role="button">Champion Stats</a>
</li>
<li>
<a href="championcomparison.html" class="btn btn-secondary" role="button">Champion Comparison</a>
</li>
<li>
<a href="help.html" class="btn btn-secondary" role="button">Help</a>
</li>
</ul>
</nav>
<div class="container mt-2">
<div id="showData"></div>
</div>
<script>
require('./renderer.js')
</script>
</body>
<script>
var Datastore = require('nedb')
, db = new Datastore({ filename: 'ChampionStats.txt', autoload: true });
var x = null;
// Gather all data from database
db.find({}, function (err, docs) {
x = docs;
// Due to asynchronous returns I have to call functions here
CreateTableFromJSON(x);
window.onload = sortTable(0, "alpha");
});
function CreateTableFromJSON(data) {
var myData = data
// Extract value for HTML header
var col = [];
for (var i = 0; i < myData.length; i++) {
for (var key in myData[i]) {
if (col.indexOf(key) === -1) {
col.push(key);
}
}
}
// Remove last column
col.splice(-1, 1)
// Create the dynamic table
var table = document.createElement("table");
table.setAttribute("id", "statsTable");
table.setAttribute("class", "table table-striped table-sm")
// Create HTML table header row using the extraced headers above
var tr = table.insertRow(-1); // Table row
// Tooltip data used when hovering over the headers
var toolTipData = ["Champion Name", "Health Points", "Health Points gained per level",
"Health Regeneration", "Health Regeneration gained per level",
"Mana Points", "Mana Points gained per level",
"Mana Regeneration", "Mana Regeneration gained per level",
"Attack Damage", "Attack Damage gained per level", "Attack Speed",
"Attack Speed gained per level", "Armour", "Armour gained per level",
"Magic Resist", "Magic Resist gained per level", "Movement Speed", "Auto Attack Range"];
// Loop through col.length i.e all the keys
for (var i = 0; i < col.length; i++) {
var th = document.createElement("th"); // Table header
th.innerHTML = col[i];
tr.appendChild(th);
// Set setAttributes for the th element
th.setAttribute("class", "sticky-top bg-primary border-0 align-middle text-center pointer");
th.setAttribute("data-toggle", "tooltip");
th.setAttribute("data-placement", "bottom");
// Only set
th.setAttribute("title", toolTipData[i]);
if (i == 0) {
th.setAttribute("onclick", `sortTable(${i}, "alpha")`);
} else {
th.setAttribute("onclick", `sortTable(${i}, "num")`);
}
var div = document.createElement("div");
th.appendChild(div);
div.setAttribute("class", "d-inline-block align-middle pl-1");
var spanUp = document.createElement("span");
var spanDown = document.createElement("span");
div.appendChild(spanUp);
div.appendChild(spanDown);
spanUp.setAttribute("class", "oi oi-caret-top d-block");
spanUp.setAttribute("id", `up${i}`);
spanUp.setAttribute("aria-hidden", "true");
spanDown.setAttribute("class", "oi oi-caret-bottom d-block");
spanDown.setAttribute("id", `down${i}`);
spanDown.setAttribute("aria-hidden", "true");
}
// Add JSON data to the table as rows
for (var i = 0; i < myData.length; i++) {
tr = table.insertRow(-1);
for (var j = 0; j < col.length; j++) {
var tabCell = tr.insertCell(-1);
tabCell.innerHTML = myData[i][col[j]];
}
}
// Add newly created table with JSON data to container
var divContainer = document.getElementById("showData");
divContainer.innerHTML = "";
divContainer.appendChild(table);
}
function sortTable(n, type) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById("statsTable");
switching = true;
type = type;
// Set the sorting direction to ascending:
dir = "asc";
// Ensure both carets are visible in other header rows
for (i = 0; i < 19; i++){
document.getElementById(`up${i}`).className = "oi oi-caret-top d-block visible";
document.getElementById(`down${i}`).className = "oi oi-caret-bottom d-block visible";
}
document.getElementById(`up${n}`).className = "oi oi-caret-top d-block invisible";
document.getElementById(`down${n}`).className = "oi oi-caret-bottom d-block visible";
/* Make a loop that will continue until
no switching has been done: */
while (switching) {
// Start by saying: no switching is done
switching = false;
rows = table.rows;
/* Loop through all table rows (except the
first, which contains table headers): */
for (i = 1; i < (rows.length - 1); i++) {
// Start by saying there should be no switching:
shouldSwitch = false;
/* Get the two elements you want to compare,
one from current row and one from the next: */
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
// Check if the type of sorting it alphabetical or numeric
if (type == "alpha") {
/* Check if the two rows should switch place,
based on the direction, asc or desc: */
if (dir == "asc") {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
// If so, mark as a switch and break the loop
shouldSwitch = true;
break;
}
} else if (dir == "desc") {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}
} else if (type == "num") {
// Stripe elements before sorting
xNum = x.innerHTML.replace(/[^\d.-]/g, "");
yNum = y.innerHTML.replace(/[^\d.-]/g, "");
if (dir == "asc") {
if (Number(xNum) > Number(yNum)) {
shouldSwitch = true;
break;
}
} else if (dir == "desc") {
if (Number(xNum) < Number(yNum)) {
shouldSwitch = true;
break;
}
}
}
}
if (shouldSwitch) {
/* If a switch has been marked, make the switch
and mark that a switch has been done: */
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
switchcount ++;
} else {
/* If no switching has been done AND the direction is "asc",
set the direction to "desc" and run the while loop again. */
if (switchcount == 0 && dir == "asc") {
dir = "desc";
document.getElementById(`up${n}`).className = "oi oi-caret-top d-block visible";
document.getElementById(`down${n}`).className = "oi oi-caret-bottom d-block invisible";
switching = true;
}
}
}
}
</script>
</html>