-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
204 lines (170 loc) · 6.61 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
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
if (window.history.replaceState) { //removes check on form resubmittion
window.history.replaceState(null, null, window.location.href);
}
//task logic
function createTask(name, dueDate, description) {
this.name = name;
this.dueDate = dueDate;
this.description = description;
return { name, dueDate, description };
}
const tasks = (() => {
const toStorage = () => {
localStorage.setItem("tasks", JSON.stringify(taskArray));
}
const fromStorage = () => {
if (localStorage.getItem("tasks") == null) {
return [createTask("my first to-do", "", "this is my very first to-do!")];
} else {
return JSON.parse(localStorage.getItem("tasks"));
}
}
const taskArray = fromStorage();
const add = (task) => {
taskArray.push(task);
toStorage();
}
const remove = (task) => {
taskArray.splice(taskArray.indexOf(task), 1);
toStorage();
}
const replace = (task, newTask) => {
taskArray[taskArray.indexOf(task)] = newTask;
task.name = newTask.name;
task.dueDate = newTask.dueDate;
task.description = newTask.description;
toStorage();
}
const getAll = () => { return taskArray; }
return { add, remove, replace, getAll };
})();
const popupController = (() => {
let currTaskCard = null;
const popup = document.querySelector("#popup");
const form = document.querySelector("form");
//button connections
const buttonConnections = () => {
popup.addEventListener("mousedown", (e) => {
if (e.target.id == "popup") { close(); }
})
document.querySelector("form").addEventListener("submit", submit);
document.querySelector("#complete").addEventListener("click", complete);
document.querySelector("#edit").addEventListener("click", edit);
document.querySelector("#trash").addEventListener("click", trash);
document.querySelector("#close").addEventListener("click", close);
document.querySelector("#cornerbob").addEventListener("click", close)
}
const setTaskCard = (taskCard) => {
currTaskCard = taskCard;
}
const submit = (e) => {
e.preventDefault();
const formContents = Object.fromEntries(new FormData(e.target));
const newTask = createTask(formContents["title"], formContents["date"], formContents["description"]);
if (currTaskCard == null) {
tasks.add(newTask);
addToPage(newTask);
} else {
tasks.replace(currTaskCard.task, newTask);
currTaskCard.task = newTask;
currTaskCard.children[1].textContent = newTask.name;
}
close();
}
const complete = () => {
tasks.remove(currTaskCard.task);
currTaskCard.remove();
close();
}
const trash = () => {
tasks.remove(currTaskCard.task);
currTaskCard.remove();
close();
}
const open = () => {
popup.style.visibility = "visible";
}
const close = () => {
popup.style.visibility = "hidden";
currTaskCard = null;
document.querySelector("form").reset();
}
const formButtons = (submitButton, completeButton, editButton, trashButton, closeButton) => { //sets display of desired buttons
document.querySelector("#submit").style.display = (submitButton) ? "block" : "none";
document.querySelector("#complete").style.display = (completeButton) ? "block" : "none";
document.querySelector("#edit").style.display = (editButton) ? "block" : "none";
document.querySelector("#trash").style.display = (trashButton) ? "block" : "none";
document.querySelector("#close").style.display = (closeButton) ? "block" : "none";
}
const readOnly = (readOnly) => {
if (readOnly) {
document.querySelector("#title").setAttribute("readonly", "readonly");
document.querySelector("#date").setAttribute("readonly", "readonly");
document.querySelector("#description").setAttribute("readonly", "readonly");
} else {
document.querySelector("#title").removeAttribute("readonly");
document.querySelector("#date").removeAttribute("readonly");
document.querySelector("#description").removeAttribute("readonly");
}
}
const create = () => {
open();
document.querySelector("#title").focus();
formButtons(true, false, false, false, true);
readOnly(false)
document.querySelector("#form-title").textContent = "create to-do";
}
const view = () => {
open();
formButtons(false, true, true, true, true);
readOnly(true);
document.querySelector("#form-title").textContent = "view to-do";
document.querySelector("#title").value = currTaskCard.task.name;
document.querySelector("#date").value = currTaskCard.task.dueDate;
document.querySelector("#description").value = currTaskCard.task.description;
}
const edit = () => {
document.querySelector("#title").focus();
formButtons(true, false, false, true, true);
readOnly(false);
document.querySelector("#form-title").textContent = "edit to-do";
}
buttonConnections();
return { setTaskCard, create, view };
})();
//page logic
function addToPage(task) {
//create element
let taskCard = document.createElement("div");
taskCard.classList.add("task");
taskCard.style.top = `${Math.random() * 100}%`;
taskCard.style.left = `${Math.random() * 100}%`;
taskCard.style.transform = `translate(-50%, -50%) rotate(${Math.random() * 360}deg)`;
let taskCardImage = document.createElement("img");
taskCardImage.setAttribute("src", "./notepad.png");
taskCard.appendChild(taskCardImage);
let taskCardText = document.createElement("p");
taskCardText.textContent = task.name;
taskCard.appendChild(taskCardText);
taskCard.task = task;
//make draggable
taskCard.setAttribute("draggable", "true");
//taskCard.textContent = taskCard.task.name;
taskCard.addEventListener("click", () => {
popupController.setTaskCard(taskCard);
popupController.view()
})
//animation?
document.querySelector("main").appendChild(taskCard);
}
function reset() {
document.querySelector("main").textContent = "";
tasks.getAll().forEach((task) => { addToPage(task) });
}
document.querySelectorAll(".reset").forEach((e) => e.addEventListener("click", () => {
reset();
}))
document.querySelectorAll(".create").forEach((e) => e.addEventListener("click", () => {
popupController.create();
}))
reset();