-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
221 lines (177 loc) · 6.22 KB
/
script.js
File metadata and controls
221 lines (177 loc) · 6.22 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const addTaskbtn = document.querySelector("#addTaskbtn");
const taskPopUp = document.querySelector("#taskPopup");
const createTaskbtn = document.querySelector("#createTaskbtn");
const taskInput = document.querySelector("#taskInput");
const pinkBtn = document.querySelector(".pinkColour");
const blueBtn = document.querySelector(".blueColour");
const greenBtn = document.querySelector(".greenColour");
let editSticky = null;
let editStatus = null;
const editPopup = document.querySelector("#editPopup");
const editInput = document.querySelector("#editInput");
const saveEditBtn = document.querySelector("#saveEditBtn");
const editPink = document.querySelector(".editPink");
const editBlue = document.querySelector(".editBlue");
const editGreen = document.querySelector(".editGreen");
// audio for DONE ding
// put a small ding sound at this path, for example "ding.mp3"
const doneSound = new Audio("ding.mp3");
addTaskbtn.addEventListener("click", function () {
taskPopUp.classList.remove("hidden");
});
function clearSelections() {
document.querySelectorAll(".color-btn div").forEach((div) => {
div.classList.remove("selected-colour");
});
}
let selectedColour = null;
pinkBtn.addEventListener("click", function () {
selectedColour = "todo";
clearSelections();
pinkBtn.classList.add("selected-colour");
});
blueBtn.addEventListener("click", function () {
selectedColour = "doing";
clearSelections();
blueBtn.classList.add("selected-colour");
});
greenBtn.addEventListener("click", function () {
selectedColour = "done";
clearSelections();
greenBtn.classList.add("selected-colour");
});
createTaskbtn.addEventListener("click", function () {
let text = taskInput.value.trim();
if (text === "") {
alert("Please enter a task");
return;
}
if (!selectedColour) {
alert("Please pick a task status!");
return;
}
createSticky(text, selectedColour);
taskPopUp.classList.add("hidden");
selectedColour = null;
taskInput.value = "";
});
function makeStickyDraggable(sticky) {
sticky.setAttribute("draggable", "true");
sticky.addEventListener("dragstart", (e) => {
e.dataTransfer.setData("text/plain", sticky.id);
sticky.classList.add("dragging");
});
sticky.addEventListener("dragend", () => {
sticky.classList.remove("dragging");
});
}
let stickyIdCounter = 0;
function createSticky(text, status) {
let sticky = document.createElement("div");
sticky.classList.add("sticky-note");
// unique id for drag-and-drop
stickyIdCounter += 1;
sticky.id = "sticky-" + stickyIdCounter;
let taskText = document.createElement("p");
taskText.innerText = text;
sticky.appendChild(taskText);
let deleteBtn = document.createElement("button");
deleteBtn.innerText = "✖";
deleteBtn.classList.add("delete-btn");
let editBtn = document.createElement("button");
editBtn.innerText = "✎";
editBtn.classList.add("edit-btn");
editBtn.addEventListener("click", function () {
editSticky = sticky; // store this sticky
editInput.value = taskText.innerText; // load existing text
// pre-select correct colour
clearEditColours();
if (status === "todo") editPink.classList.add("selected-edit");
if (status === "doing") editBlue.classList.add("selected-edit");
if (status === "done") editGreen.classList.add("selected-edit");
editStatus = status; // store current status
editPopup.classList.remove("hidden");
});
sticky.appendChild(editBtn);
deleteBtn.addEventListener("click", function () {
sticky.remove(); // deletes the sticky
});
sticky.appendChild(deleteBtn);
// initial placement
placeStickyInColumn(sticky, status, true);
// make draggable
makeStickyDraggable(sticky);
}
function placeStickyInColumn(sticky, status, fromCreate = false) {
if (status === "todo") {
document.querySelector(".todo").appendChild(sticky);
sticky.style.backgroundColor = "rgb(255, 177, 230)";
} else if (status === "doing") {
document.querySelector(".doing").appendChild(sticky);
sticky.style.backgroundColor = "rgb(241, 224, 123)";
} else if (status === "done") {
document.querySelector(".done").appendChild(sticky);
sticky.style.backgroundColor = "rgb(230, 255, 172)";
// play ding when moved/created in DONE
// user interaction (drag/click) has already happened so this is allowed in browsers [web:6][web:12][web:17]
if (!fromCreate || fromCreate) {
doneSound.currentTime = 0;
doneSound.play().catch(() => {});
}
}
}
// drag-and-drop for columns
document.querySelectorAll("#task-status > div").forEach((column) => {
column.addEventListener("dragover", (e) => {
e.preventDefault();
});
column.addEventListener("drop", (e) => {
e.preventDefault();
const id = e.dataTransfer.getData("text/plain");
const sticky = document.getElementById(id);
if (!sticky) return;
column.appendChild(sticky);
// update status + border based on column class, and play sound if DONE
if (column.classList.contains("todo")) {
sticky.style.backgroundColor = "hsl(318, 100.00%, 83.90%)";
} else if (column.classList.contains("doing")) {
sticky.style.backgroundColor = "rgb(241, 224, 123)";
} else if (column.classList.contains("done")) {
sticky.style.backgroundColor = "rgb(230, 255, 172)";
doneSound.currentTime = 0;
doneSound.play().catch(() => {});
}
});
});
function clearEditColours() {
document.querySelectorAll("#edit-color-section div").forEach((div) => {
div.classList.remove("selected-edit");
});
}
editPink.addEventListener("click", () => {
clearEditColours();
editPink.classList.add("selected-edit");
editStatus = "todo";
});
editBlue.addEventListener("click", () => {
clearEditColours();
editBlue.classList.add("selected-edit");
editStatus = "doing";
});
editGreen.addEventListener("click", () => {
clearEditColours();
editGreen.classList.add("selected-edit");
editStatus = "done";
});
saveEditBtn.addEventListener("click", function () {
let newText = editInput.value.trim();
if (newText === "") {
alert("Task cannot be empty");
return;
}
// Update task text
editSticky.querySelector("p").innerText = newText;
// Move sticky to new status column
placeStickyInColumn(editSticky, editStatus);
editPopup.classList.add("hidden");
});