-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo-tracker.html
executable file
·190 lines (173 loc) · 7.4 KB
/
todo-tracker.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
<!DOCTYPE html>
<!-- Copyright 2021, 2022 ultasun. All rights reserved. -->
<html lang='en'>
<head>
<meta charset='UTF-8'>
<title>TODO Tracker</title>
<style>
.todoItem:hover {
cursor: pointer;
}
</style>
</head>
<body>
<noscript><h1>JavaScript is required to use this tool!</h1></noscript>
<h1 id='pageTitleH1'>TODO Tracker</h1>
<hr>
<p>Copyright 2021, 2022 <a href="https://github.com/ultasun">ultasun</a>, all rights reserved.</a> Check out the <a href="https://github.com/ultasun/js-todo-tracker">source on GitHub!</a></p>
<hr>
<p><i><b>TODO Tracker</b>: Clicking on a <b>Pending Todo</b> will mark it as complete. Clicking on a <b>Completed Todo</b> will remove it from the list. <b>Swipe</b> up or down on either list to remove items. A few <b>Undo</b> buttons are available to help accidental removals. Either list may be <b>Cleared</b>. To save the document, press <b>Save Todo List</b> first, and the asterisked <b>Save</b> second. Saving the page does <b>not</b> save the removal histories. A <b>Completed Todo</b> can <b><u>not</u></b> be reassigned as a <b>Pending Todo</b>.</i></p>
<hr>
<h2>Add Todo</h2>
<i>What do you need to do?</i>
<input type='text' id='todo-input'>
<button id='addTodoBtn' onclick='addTodo()'>Add New Todo</button>
<hr>
<h2>Manage Todos</h2>
<p><i>This document was created </i><b id='whenCreated'>originally</b><i>, was last opened at </i><b id='lastOpened'>never</b><i>, and was last saved at </i><b id='lastSaved'>never</b><i>.</i></p>
<i>List title</i> <input type='text' id='pageTitle'>
<button id='setPageTitleBtn' onclick='setPageTitle()'>Set List Title</button>
<button id='clearBtn' onclick='clearAllTodos()'>Clear All Todos</button>
<button id='clearPendingBtn' onclick='clearAllPending()'>Clear Only Pending Todos</button>
<button id='clearCompletedBtn' onclick='clearAllCompleted()'>Clear Only Completed Todos</button>
<button id='undoLastCompletedClearBtn' onclick='undoLastRemoval()'>Undo Last Completed Todo Removal</button>
<button id='undoLastTodoRemovalBtn' onclick='undoLastTodoRemoval()'>Undo Last Todo Removal</button>
<button id='exportPageBtn' onclick='exportPage()'>Save Todo List</button><a id='savePage' target='_blank' download='latest_todo_tracker.html'></a>
<hr>
<div class='todos'>
<h2>Pending Todos</h2>
<ul class='pendingTodoWrapper'>
<li class='todoItem'>close the door after you open it</li>
</ul>
</div>
<hr>
<h2>Completed Todos</h2>
<ul class='completedTodoWrapper'>
<div class='completed'></div>
</ul>
<hr>
</body>
<script>
let lastRemoval = [];
let lastPendingRemoval = [];
function setPageTitle() {
const inputBox = document.querySelector('#pageTitle');
const pageTitleH1 = document.querySelector('#pageTitleH1');
document.title = inputBox.value;
pageTitleH1.innerHTML = inputBox.value;
}
function addTodo() {
const inputBox = document.querySelector('#todo-input');
const pendingTodoWrapper =
document.querySelector('.pendingTodoWrapper');
const newPendingTodoListItem = document.createElement('li');
newPendingTodoListItem.classList.add('todoItem');
newPendingTodoListItem.innerHTML = inputBox.value;
newPendingTodoListItem.addEventListener('click', reassignTodoEvent);
newPendingTodoListItem.addEventListener('wheel', removeTodoEvent);
pendingTodoWrapper.appendChild(newPendingTodoListItem);
}
function removeTodoEvent(e) {
lastPendingRemoval.push(e.target.innerHTML);
e.target.remove();
}
function removeCompletedTodo(e) {
lastRemoval.push(e.target.innerHTML);
e.target.remove();
}
function clearAllPending() {
const pendingTodos = document.querySelectorAll('.todoItem');
pendingTodos.forEach(li => {
lastPendingRemoval.push(li.innerHTML);
li.remove();
});
}
function clearAllCompleted() {
const completedTodos = document.querySelectorAll('.completedTodo');
completedTodos.forEach(li => {
lastRemoval.push(li.innerHTML);
li.remove();
});
}
function clearAllTodos() {
clearAllPending();
clearAllCompleted();
}
function undoLastRemoval() {
if(lastRemoval.length <= 0) {
alert('The history stack is empty.');
return;
}
const completedTodoWrapper = document.querySelector('.completedTodoWrapper');
const newCompletedTodoListItem = document.createElement('li');
newCompletedTodoListItem.classList.add('completedTodo');
newCompletedTodoListItem.innerHTML = lastRemoval.pop();
newCompletedTodoListItem.addEventListener('click', removeCompletedTodo);
newCompletedTodoListItem.addEventListener('wheel', removeCompletedTodo);
completedTodoWrapper.appendChild(newCompletedTodoListItem);
}
function undoLastTodoRemoval() {
if(lastPendingRemoval.length <= 0) {
alert('The history stack is empty.');
return;
}
const pendingTodoWrapper = document.querySelector('.pendingTodoWrapper');
const newPendingTodoListItem = document.createElement('li');
newPendingTodoListItem.classList.add('todoItem');
newPendingTodoListItem.innerHTML = lastPendingRemoval.pop();
newPendingTodoListItem.addEventListener('click', reassignTodoEvent);
newPendingTodoListItem.addEventListener('wheel', removeTodoEvent);
pendingTodoWrapper.appendChild(newPendingTodoListItem);
}
function reassignTodoEvent(e) {
const listItemInnerHtml = e.target.innerHTML;
const completedTodoWrapper = document.querySelector('.completedTodoWrapper');
const newCompletedTodoListItem = document.createElement('li');
const newTextString = listItemInnerHtml;
newCompletedTodoListItem.classList.add('completedTodo');
newCompletedTodoListItem.innerHTML = newTextString;
newCompletedTodoListItem.addEventListener('click', removeCompletedTodo);
newCompletedTodoListItem.addEventListener('wheel', removeCompletedTodo);
completedTodoWrapper.appendChild(newCompletedTodoListItem);
e.target.remove();
}
function exportPage() {
document.querySelector('#lastSaved').innerHTML = new Date();
let file;
let data = [];
data.push(document.documentElement.outerHTML);
let p = {type: 'text/html'};
try {
file = new File(data, 'latest_todo_tracker.html', p);
} catch (e) {
file = new Blob(data, p);
}
let url = URL.createObjectURL(file);
let saveAsLink = document.querySelector('#savePage');
saveAsLink.href = url;
saveAsLink.innerHTML = '*** SAVE ***';
}
function enableEventsOnPreexistingListItems() {
const existingListItems = document.querySelectorAll('.todoItem');
const existingCompletedItems = document.querySelectorAll('.completedTodo');
existingListItems.forEach(li => {
li.addEventListener('click', reassignTodoEvent);
li.addEventListener('wheel', removeTodoEvent);
});
existingCompletedItems.forEach(li => {
li.addEventListener('click', removeCompletedTodo);
li.addEventListener('wheel', removeCompletedTodo);
});
}
function pageStartUp() {
document.querySelector('#savePage').innerHTML = '';
document.querySelector('#lastOpened').innerHTML = new Date();
enableEventsOnPreexistingListItems();
const creationDate = document.querySelector('#whenCreated');
if(creationDate.innerHTML === 'originally') {
creationDate.innerHTML = new Date();
}
}
pageStartUp();
</script>
</html>