-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscript.js
More file actions
191 lines (160 loc) Β· 4.95 KB
/
script.js
File metadata and controls
191 lines (160 loc) Β· 4.95 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
const modal = document.querySelector(".confirm-modal");
const columnsContainer = document.querySelector(".columns");
const columns = columnsContainer.querySelectorAll(".column");
let currentTask = null;
//* functions
const handleDragover = (event) => {
event.preventDefault(); // allow drop
const draggedTask = document.querySelector(".dragging");
const target = event.target.closest(".task, .tasks");
if (!target || target === draggedTask) return;
if (target.classList.contains("tasks")) {
// target is the tasks element
const lastTask = target.lastElementChild;
if (!lastTask) {
// tasks is empty
target.appendChild(draggedTask);
} else {
const { bottom } = lastTask.getBoundingClientRect();
event.clientY > bottom && target.appendChild(draggedTask);
}
} else {
// target is another
const { top, height } = target.getBoundingClientRect();
const distance = top + height / 2;
if (event.clientY < distance) {
target.before(draggedTask);
} else {
target.after(draggedTask);
}
}
};
const handleDrop = (event) => {
event.preventDefault();
};
const handleDragend = (event) => {
event.target.classList.remove("dragging");
};
const handleDragstart = (event) => {
event.dataTransfer.effectsAllowed = "move";
event.dataTransfer.setData("text/plain", "");
requestAnimationFrame(() => event.target.classList.add("dragging"));
};
const handleDelete = (event) => {
currentTask = event.target.closest(".task");
// show preview
modal.querySelector(".preview").innerText = currentTask.innerText.substring(
0,
100
);
modal.showModal();
};
const handleEdit = (event) => {
const task = event.target.closest(".task");
const input = createTaskInput(task.innerText);
task.replaceWith(input);
input.focus();
// move cursor to the end
const selection = window.getSelection();
selection.selectAllChildren(input);
selection.collapseToEnd();
};
const handleBlur = (event) => {
const input = event.target;
const content = input.innerText.trim() || "Untitled";
const task = createTask(content.replace(/\n/g, "<br>"));
input.replaceWith(task);
};
const handleAdd = (event) => {
const tasksEl = event.target.closest(".column").lastElementChild;
const input = createTaskInput();
tasksEl.appendChild(input);
input.focus();
};
const updateTaskCount = (column) => {
const tasks = column.querySelector(".tasks").children;
const taskCount = tasks.length;
column.querySelector(".column-title h3").dataset.tasks = taskCount;
};
const observeTaskChanges = () => {
for (const column of columns) {
const observer = new MutationObserver(() => updateTaskCount(column));
observer.observe(column.querySelector(".tasks"), { childList: true });
}
};
observeTaskChanges();
const createTask = (content) => {
const task = document.createElement("div");
task.className = "task";
task.draggable = true;
task.innerHTML = `
<div>${content}</div>
<menu>
<button data-edit><i class="bi bi-pencil-square"></i></button>
<button data-delete><i class="bi bi-trash"></i></button>
</menu>`;
task.addEventListener("dragstart", handleDragstart);
task.addEventListener("dragend", handleDragend);
return task;
};
const createTaskInput = (text = "") => {
const input = document.createElement("div");
input.className = "task-input";
input.dataset.placeholder = "Task name";
input.contentEditable = true;
input.innerText = text;
input.addEventListener("blur", handleBlur);
return input;
};
//* event listeners
// dragover and drop
tasksElements = columnsContainer.querySelectorAll(".tasks");
for (const tasksEl of tasksElements) {
tasksEl.addEventListener("dragover", handleDragover);
tasksEl.addEventListener("drop", handleDrop);
}
// add, edit and delete task
columnsContainer.addEventListener("click", (event) => {
if (event.target.closest("button[data-add]")) {
handleAdd(event);
} else if (event.target.closest("button[data-edit]")) {
handleEdit(event);
} else if (event.target.closest("button[data-delete]")) {
handleDelete(event);
}
});
// confirm deletion
modal.addEventListener("submit", () => currentTask && currentTask.remove());
// cancel deletion
modal.querySelector("#cancel").addEventListener("click", () => modal.close());
// clear current task
modal.addEventListener("close", () => (currentTask = null));
//* placeholder tasks
let tasks = [
[
"Gather inspiration for layout ideas ποΈ",
"Research Color Palette ποΈ",
"Brand and Logo Design π¨",
],
[
"Optimize Image Assets ποΈ",
"Cross-Browser Testing π",
"Integrate Livechat π¬",
],
[
"Set Up Custom Domain π",
"Deploy Website π",
"Fix Bugs π οΈ",
"Team Meeting π
",
],
[
"Write Report π",
"Code Review π»",
"Implement Billing and Subscription π°",
],
];
tasks.forEach((col, idx) => {
for (const item of col) {
columns[idx].querySelector(".tasks").appendChild(createTask(item));
}
});