-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
200 lines (155 loc) · 7.49 KB
/
index.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
<html>
<head>
<title>SkillTrack - A novelty skill tracker</title>
<link rel="stylesheet" href="./css/pure-0.6.0-min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<link rel="stylesheet" href="./css/style.css">
<script src="./js/util.js"></script>
<script>
function handleStorage(e) {
if (!e) { e = window.event; } //fuck you IE
if (e.key == "skills") {
// remove DOMs, rebuild from loadSkills()
var element = document.getElementById("skillsBody");
while (element.firstChild) {
element.removeChild(element.firstChild);
}
loadSkills();
}
};
// Where skills are stored internally.
var skills = {};
// Internal: Load skills from localStorage.
function loadSkills() {
var loaded = localStorage.getItem("skills");
if (loaded != null) {
skills = JSON.parse(loaded);
console.log("Skills loaded: {0}".format(loaded));
}
if (isEmpty(skills)) createSkill("Example Skill");
for (var skill in skills) {
createSkillDOM(skill, skills[skill]);
}
};
// Internal: Save skills to localStorage.
function saveSkills() {
var saveable = JSON.stringify(skills);
localStorage.setItem("skills", saveable);
console.log("Skills saved.");
};
// Internal: Create skill.
function createSkill(skill) {
skills[skill] = 0;
saveSkills();
console.log("Skill created: {0}".format(skill));
};
// Internal: Delete skill.
function deleteSkill(skill) {
delete skills[skill];
saveSkills();
console.log("Skill deleted: {0}".format(skill));
};
// Internal (bad design): Helper to write innerHTML of skill DOM elements.
function writeSkillInnerHTML(element, skill, experience, id) {
var level = Math.ceil(Math.sqrt(experience + 1) - 1);
var previous = Math.pow(level, 2);
var next = Math.pow(level+1, 2);
//var current = (experience - previous);
//var toNext = (next - previous);
element.innerHTML = "<td><button class='pure-button button-delete' onclick='javascript:removeSkill(\"{0}\", \"{1}\");' title='Delete this skill.'><span class='fa fa-ban'></span></td><td><div>{0}<div class='level'>Lv {2}</div></div><br /><progress value='{3}' max='{4}'></progress></td><td><button class='pure-button button-plus' onclick='javascript:skillUp(\"{0}\", \"{1}\");' title='+1 xp'><span class='fa fa-plus-circle'></span></button></td>".format(skill, id, level, (experience - previous), (next - previous));
console.log("{0}, Lv {1}. Previous Lv, Experience, Next Lv: {2}, {3}, {4}. {5} of {6} experience to next level.".format(skill, level, previous, experience, next, (experience - previous), (next - previous)));
}
// Internal (bad design): Create initial skill DOM elements.
var nextID = 0; //shitty thing
function createSkillDOM(name, experience) {
var element = document.createElement("tr");
element.setAttribute("id", "id" + nextID); //this is shit and I feel like shit
writeSkillInnerHTML(element, name, experience, "id"+nextID); //this is even worse!
var skillsParent = document.getElementById("skillsBody");
skillsParent.appendChild(element);
nextID++;
};
// Interface: Creates a new skill, resets 'new skill' input.
function newSkill() {
var element = document.getElementById("newSkill");
if (element.value == "") {
alert("Please enter a name for the skill!");
console.log("Attempt to create null skill: '{0}'".format(element.value));
} else {
if (typeof skills[element.value] != "undefined") {
alert("Please enter a UNIQUE name for each skill!");
console.log("Attempt to create an existing skill: '{0}'".format(element.value));
} else {
createSkill(element.value);
createSkillDOM(element.value, 0);
element.value = "";
element.focus();
}
}
};
// Interface: Removes (and deletes) a skill.
function removeSkill(skill, id) {
var element = document.getElementById(id);
element.parentNode.removeChild(element);
deleteSkill(skill);
};
// Interface: Adds experience to a skill.
//TODO make skillUp no longer access skills[skill] directly if possible ???
function skillUp(skill, id) {
if (typeof skills[skill] == "number") {
skills[skill]++;
saveSkills();
writeSkillInnerHTML(document.getElementById(id), skill, skills[skill], id); //this is terrible!
console.log("{0} has gained an experience point! Now at {1} experience.".format(skill, skills[skill]));
} else {
console.error("skills['{0}'] is not a number!".format(skill));
}
};
// Interface: Prompts, then removes all skills.
function removeAllSkills() {
if (confirm("Are you sure you want to delete all skills and start over?")) {
var element = document.getElementById("skillsBody");
while (element.firstChild) {
element.removeChild(element.firstChild);
}
for (var skill in skills) {
deleteSkill(skill);
}
console.log("All skills deleted.");
}
};
window.onload = function() {
setTimeout(loadSkills, 1);
if (window.addEventListener) {
window.addEventListener("storage", handleStorage, false);
} else {
window.attachEvent("onstorage", handleStorage);
}
};
</script>
</head>
<body>
<table class="pure-table centered">
<thead>
<tr>
<th><button class="pure-button" onclick="javascript:removeAllSkills();" title="Removes all skills."><span class="fa fa-ban"></span></button></th>
<th class="header-text">Skills</th>
<th><button class="pure-button" title="Currently does nothing. Might do something interesting in the future."><span class="fa fa-plus-circle"></span></button></th>
</tr>
</thead>
<tbody id="skillsBody"></tbody>
<tfoot>
<tr>
<td><button class="pure-button button-delete pure-button-disabled"><span class="fa fa-ban"></span></button></td>
<td><input type="text" placeholder="New Skill" id="newSkill" onkeydown="if (event.keyCode == 13) newSkill();" title="Input the name for a new skill."></input></td>
<td><button class="pure-button button-plus" onclick="javascript:newSkill();" title="Add a new skill!"><span class="fa fa-plus-circle"></span></button></td>
</tr>
</tfoot>
</table>
<div id="footer">
<div class="centered">
<span><a href="#">Skills</a> | <a href="tasks.html">Tasks</a></span>
</div>
</div>
</body>
</html>