-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.html
206 lines (160 loc) · 7.59 KB
/
tasks.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
<html>
<head>
<title>Tasks - SkillTrack</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 == "tasks") {
// remove DOMs, rebuild from loadTasks()
var element = document.getElementById("taskList");
while (element.firstChild) {
element.removeChild(element.firstChild);
}
loadTasks();
}
};
// Where tasks are stored internally.
var tasks = {};
// Internal: Load tasks from localStorage.
function loadTasks() {
var loaded = localStorage.getItem("tasks");
if (loaded != null) {
tasks = JSON.parse(loaded);
console.log("Tasks loaded: {0}".format(loaded));
}
if (isEmpty(tasks)) createTask("Example Task");
for (var task in tasks) {
createTaskDOM(task, "priority"+tasks[task]);
}
};
// Internal: Save tasks to localStorage.
function saveTasks() {
var saveable = JSON.stringify(tasks);
localStorage.setItem("tasks", saveable);
console.log("Tasks saved.");
};
// Internal: Create task.
function createTask(task, priority) {
if (priority == null) priority = 0;
tasks[task] = priority;
saveTasks();
console.log("Task created: {0}".format(task));
};
// Internal: Delete task.
function deleteTask(task) {
delete tasks[task];
saveTasks();
console.log("Task deleted: {0}".format(task));
};
// Internal (bad design): Create initial task DOM elements.
var nextID = 0; //shitty thing
function createTaskDOM(task, priorityClass) {
var element = document.createElement("tr");
element.setAttribute("id", "id" + nextID); //this is shit and I feel like shit
element.className = priorityClass;
element.innerHTML = "<td><button class='pure-button button-done' onclick='javascript:removeTask(\"{0}\", \"{1}\");' title='Mark this task done (delete it).'><span class='fa fa-check-circle'></span></button></td><td>{0}</td><td><select onchange='changePriority(\"{0}\", \"{1}\", arguments[0]);' title='Select task priority.'><option value='0'>None</option><option value='1'>(1) Maximum</option><option value='2'>(2) High</option><option value='3'>(3) Medium</option><option value='4'>(4) Low</option><option value='5'>(5) Minimum</option></select></td>".format(task, "id"+nextID);
//convoluted as fuck: Tr -> Td -> Select -> Option[priority] -> "selected"
element.childNodes[2].childNodes[0].childNodes[priorityClass.slice(8)].setAttribute("selected", "selected");
var taskList = document.getElementById("taskList");
taskList.appendChild(element);
nextID++;
};
// Interface: Creates a new task, resets 'new task' input.
function newTask() {
var element = document.getElementById("newTask");
if (element.value == "") {
alert("Please enter a name for the task!");
console.log("Attempt to create null task: '{0}'".format(element.value));
} else {
if (typeof tasks[element.value] != "undefined") {
alert("Please enter a UNIQUE name for each task!");
console.log("Attempt to create an existing task: '{0}'".format(element.value));
} else {
var className = document.getElementById("newTaskRow").className;
if (className == "") className = "priority0";
createTask(element.value, className.slice(8));
createTaskDOM(element.value, className);
element.value = "";
element.focus();
}
}
};
// Interface: Changes background color of a task.
function changePriority(task, id, event) {
var priority = event.path[0].value;
if (task != null) {
//duplicated code from createTask() :\
tasks[task] = priority;
saveTasks();
console.log("{0} priority changed to {1}".format(task, priority));
}
var element = document.getElementById(id);
element.className = "priority"+priority;
};
// Interface: Removes (and deletes) a task.
function removeTask(task, id) {
var element = document.getElementById(id);
element.parentNode.removeChild(element);
deleteTask(task);
};
// Interface: Prompts, then removes all tasks.
function removeAllTasks() {
if (confirm("Are you sure you want to delete all tasks and start over?")) {
var element = document.getElementById("taskList");
while (element.firstChild) {
element.removeChild(element.firstChild);
}
for (var task in tasks) {
deleteTask(task);
}
console.log("All tasks deleted.");
}
};
window.onload = function() {
setTimeout(loadTasks, 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 button-delete" onclick="javascript:removeAllTasks();" title="Remove all tasks."><span class="fa fa-ban"></span></button></th>
<th class="header-text">Tasks</th>
<th><button class="pure-button button-info" title="Does nothing. Might do something in the future."><span class="fa fa-exclamation-circle"></span></button></th>
</tr>
</thead>
<tbody id="taskList"></tbody>
<tfoot>
<tr id="newTaskRow" class="priority0">
<td><button class="pure-button button-new" onclick="javascript:newTask();" title="Create new task!"><span class="fa fa-plus-circle"></span></button></td>
<td><input type="text" placeholder="New Task" id="newTask" onkeydown="if (event.keyCode == 13) newTask();" title="Input the text of a new task."></input></td>
<td>
<select onchange="changePriority(null, 'newTaskRow', arguments[0]);" title="Select the priority of this task.">
<option value="0" selected>None</option>
<option value="1">(1) Maximum</option>
<option value="2">(2) High</option>
<option value="3">(3) Medium</option>
<option value="4">(4) Low</option>
<option value="5">(5) Minimum</option>
</select>
</td>
</tr>
</tfoot>
</table>
<div id="footer">
<div class="centered">
<span><a href="index.html">Skills</a> | <a href="#">Tasks</a></span>
</div>
</div>
</body>
</html>