-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
114 lines (100 loc) · 4.26 KB
/
script.js
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
//pulls count from local storage or sets to 0 on first visit
let count;
if (window.localStorage.getItem("count") === null) {
count = 0;
}
else {
count = window.localStorage.getItem("count");
}
//pulls json of tasks from storage and display calls each task
function retrieveTasks() {
const max_tasks = 99;
num_tasks = window.localStorage.getItem("count");
for (let i = 0; i < num_tasks; i++) {
if (window.localStorage.getItem(`task-${i}`) === null && i < max_tasks) {
console.log("skipped");
num_tasks++;
continue;
}
displayTaskPane(JSON.parse(window.localStorage.getItem(`task-${i}`)), i);
}
}
//display's a new task that is in and editable state
function displayNewTask() {
if (document.getElementById("new-task") === null) {
document.getElementById("grid-container").innerHTML += `
<section class="container" id="new-task">
<form class="form" style="height: inherit;">
<input class="container-title" placeholder="Title" name="title"></input>
<textarea class="container-description" maxlength="400" placeholder="Description (400 characters)" name="description"></textarea>
<select class="container-tag" name="tag">
<option selected hidden>Importance</option>
<option>Urgent</option>
<option>Important</option>
<option>To do</option>
</select>
<input class="submit" type="submit" value="✓"></input>
<button class="cancel">✕</button>
</form>
</section>`
}
}
//converts a task pane to an editable task
function displayEditableTask(id) {
const task = window.localStorage.getItem(id);
const obj = JSON.parse(task);
document.getElementById("grid-container").innerHTML += `
<section class="container" id="new-task">
<form class="form" style="height: inherit;">
<input class="container-title" placeholder="Title" name="title" value="${obj.title}"}></input>
<textarea class="container-description" maxlength="400" placeholder="Description (400 characters)" name="description" >${obj.description}</textarea>
<select class="container-tag" name="tag">
<option selected hidden value="${obj.tag}">${obj.tag}</option>
<option>Urgent</option>
<option>Important</option>
<option>To do</option>
</select>
<input class="submit" type="submit" value="✓"></input>
<button class="cancel">✕</button>
</form>
</section>`
deleteTaskPane(id);
}
//responsible for deleting an incomplete task
function deleteNewTask() {
if (document.getElementById("new-task")) {
document.getElementById("new-task").outerHTML = "";
}
}
//deletes a task pane and removes from local storage
function deleteTaskPane(id) {
document.getElementById(id).outerHTML = "";
window.localStorage.removeItem(id);
count--;
window.localStorage.setItem("count", count);
}
//converts an editable task to a static task pane, with options to edit
function displayTaskPane(task, index) {
deleteNewTask();
document.getElementById("grid-container").innerHTML += `
<section class="container" id="task-${index}">
<p class="container-title">${task.title}</p>
<p class="container-description">${task.description}</p>
<p class="container-tag">${task.tag}</p>
<button class="edit" onclick="displayEditableTask(this.parentNode.id);">Edit</button>
<button class="delete" onclick="deleteTaskPane(this.parentNode.id);">Delete</button>
</section>
`
}
//listens for form submit, collects form into json, adds to local storage and calls conversion to pane
function onFormSubmit(event) {
event.preventDefault();
const data = new FormData(event.target);
const formJSON = Object.fromEntries(data.entries());
window.localStorage.setItem(`task-${count}`,JSON.stringify(formJSON));
displayTaskPane(formJSON, count);
count++;
window.localStorage.setItem("count", count);
}
const form = document.getElementById('grid-container');
form.addEventListener('submit', onFormSubmit);