Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
225 lines (189 loc) · 5.58 KB
/
Copy pathindex.js
File metadata and controls
225 lines (189 loc) · 5.58 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
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
214
215
216
217
218
219
220
221
222
223
224
225
const bannerColor = document.getElementById("top-banner");
const body = document.getElementById("body");
const table = document.getElementById("table-list");
const rollResult = document.getElementById("roll-result");
const tableBody = document.getElementById("table-body");
const toggleInfo = document.getElementById("toggle-info");
const hiddenText = document.getElementById("hidden-text");
let tableRows = [];
let initVal = 0
let nameVal = "";
let hpVal = 0
let notesVal = "";
let newRow = "";
var light = true;
let rowsFromLocalStorage = JSON.parse( localStorage.getItem("tableRows") )
console.log(rowsFromLocalStorage)
hiddenText.remove();
let hiddenTextVisible = false;
if (rowsFromLocalStorage) {
tableRows = rowsFromLocalStorage
sortTableRows();
renderTableRows();
}
toggleInfo.addEventListener("click", function(){
if(!hiddenTextVisible)
{
toggleInfo.insertAdjacentElement("afterend", hiddenText);
hiddenTextVisible = true;
return;
}
hiddenText.remove();
hiddenTextVisible = false;
});
function rollD20()
{
return Math.floor(Math.random() * 20) + 1;
}
function setD20()
{
rollResult.innerHTML = rollD20();
}
function changeColor()
{
if(light)
{
body.style.backgroundColor = "#333";
body.style.color = "#00FFFF";
toggleInfo.style.color = "red";
light = false
}
else
{
body.style.backgroundColor = "whitesmoke";
body.style.color = "#333";
toggleInfo.style.color = "blue";
light = true;
}
}
function sortTableRows()
{
for(let i = 0; i < tableRows.length; i++)
{
for(let j = 0; j < tableRows.length - i - 1; j++)
{
let currInit = parseInt(tableRows[j].init);
let nextInit = parseInt(tableRows[j+1].init);
if(currInit < nextInit)
{
let temp = tableRows[j];
tableRows[j] = tableRows[j+1];
tableRows[j+1] = temp;
}
}
}
localStorage.setItem("tableRows", JSON.stringify(tableRows));
}
function renderTableRows()
{
tableBody.innerHTML = "";
for(let i = 0; i < tableRows.length; i++)
{
var init = tableRows[i].init;
var name = tableRows[i].name;
var hp = tableRows[i].hp;
var notes = tableRows[i].notes;
var rowAsHTML = buildRow(init, name, hp, notes);
tableBody.innerHTML += rowAsHTML;
}
}
function getInput()
{
var init = document.getElementById("initInput");
var name = document.getElementById("nameInput");
var hp = document.getElementById("hpInput");
var notes = document.getElementById("notesInput");
initVal = init.value;
nameVal = name.value;
hpVal = hp.value;
notesVal = notes.value;
console.log(init.value + " " + name.value + " " + hp.value + " " + notes.value);
init.value="";
name.value="";
hp.value="";
notes.value="";
}
function saveInput()
{
var rowObject =
{
init: initVal,
name: nameVal,
hp: hpVal,
notes: notesVal
};
if(localStorage.getItem(tableRows) === null)
{
tableRows.push(rowObject);
localStorage.setItem("tableRows", JSON.stringify(tableRows));
}
else
{
tableRows = JSON.parse(localStorage.getItem("tableRows"));
tableRows.push(rowObject);
localStorage.setItem("tableRows", JSON.stringify(tableRows));
}
}
function buildRow(init, name, hp, notes)
{
var string;
var removeButton = '<button id="remove-button" onclick="remove(this)">Remove</button>'
var stringHeader = '<tr class="table-row" initiative=' + initVal + ">";
var initData = "<td>" + init + "</td>";
var nameData = "<td contenteditable>" + name + "</td>";
var hpData = "<td contenteditable>" + hp + "</td>";
var notesData = "<td contenteditable>" + notes + "</td>";
var placeholder = "<td>" + removeButton + "</td>";
string = stringHeader;
string += initData;
string += nameData;
string += hpData;
string += notesData;
string += placeholder;
string += "</tr>";
return string;
}
function remove(elem)
{
var thisTableRow = elem.parentNode.parentNode;
var rowChildElements = thisTableRow.children;
var deletingInit;
var deletingName;
var deletingHp;
var deletingNotes;
if(rowChildElements)
{
deletingInit = rowChildElements[0].textContent;
deletingName = rowChildElements[1].textContent;
deletingHp = rowChildElements[2].textContent;
deletingNotes = rowChildElements[3].textContent;
}
removeFromLocalStorage(deletingInit, deletingName, deletingHp, deletingNotes)
thisTableRow.remove();
}
function removeFromLocalStorage(deletingInit, deletingName, deletingHp, deletingNotes)
{
for(let i = 0; i < tableRows.length; i++)
{
let init = tableRows[i].init;
let name = tableRows[i].name;
let hp = tableRows[i].hp;
let notes = tableRows[i].notes;
if(init === deletingInit &&
name === deletingName &&
hp === deletingHp &&
notes === deletingNotes)
{
tableRows.splice(i, 1);
localStorage.setItem("tableRows", JSON.stringify(tableRows));
return
}
}
}
function main()
{
getInput();
saveInput();
sortTableRows();
renderTableRows();
}