-
Notifications
You must be signed in to change notification settings - Fork 0
/
kanban1.js
277 lines (234 loc) · 8.84 KB
/
kanban1.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const openModalButtonRef = document.querySelector('.quick-action .icon.add');
const modalRef = document.querySelector('.modal');
const closeModalButtonRef = document.querySelector('.modal .right-section .close');
const textareaRef = document.querySelector('.modal textarea');
const priorityBoxesRef = document.querySelectorAll('.modal .right-section .priority-filter .box');
const ticketsSectionRef = document.querySelector('.ticket-section');
const deleteDivRef = document.querySelector('.quick-action .icon.delete');
const deleteIconRef = document.querySelector('.quick-action .icon.delete .fa-trash');
const priorityFilterRef = document.querySelector('.priority-filter');
const clearFilterRef = document.querySelector('.clear-filter');
let tasks;
function initializeLocalStorage() {
tasks = getTasksFromLocalStorage();
if (!tasks) {
tasks = [
{
id: (new ShortUniqueId()).rnd(),
description: 'Task 1',
priority: 'p1'
},
{
id: (new ShortUniqueId()).rnd(),
description: 'Task 2',
priority: 'p2'
},{
id: (new ShortUniqueId()).rnd(),
description: 'Task 3',
priority: 'p3'
}
]
updateTasksInLocalStorage(tasks);
}
}
initializeLocalStorage();
const newTask = {
id: '',
description: '',
priority: ''
};
function openModal() {
modalRef.classList.remove('hide');
}
function closeModal() {
modalRef.classList.add('hide');
}
openModalButtonRef.addEventListener('click', function () {
const isHideClassApplied = [ ...modalRef.classList].includes('hide');
if (isHideClassApplied) {
openModal()
} else {
closeModal()
}
});
closeModalButtonRef.addEventListener('click', function () {
closeModal();
});
textareaRef.addEventListener('keyup', function(ev) {
if (ev.key == 'Shift') {
const description = ev.target.value;
const priority = getSelectedClassPriority();
var uid = new ShortUniqueId();
tasks.push({
id: uid.rnd(),
description: description,
priority: priority
});
listTickets(tasks);
updateTasksInLocalStorage(tasks);
closeModal();
}
});
//self below
// textareaRef.addEventListener('keydown', function (ev) {
// if (ev.key === 'Enter' && !ev.shiftKey) {
// ev.preventDefault(); // Prevent the default behavior of adding a new line
// const description = ev.target.value.trim(); // Remove leading/trailing spaces
// if (description) {
// const priority = getSelectedClassPriority();
// var uid = new ShortUniqueId();
// tasks.push({
// id: uid(),
// description: description,
// priority: priority
// });
// listTickets(tasks);
// updateTasksInLocalStorage(tasks);
// ev.target.value = ''; // Clear the textarea
// }
// }
// });
function getSelectedClassPriority() {
let priority = '';
priorityBoxesRef.forEach(function(singleBoxRef, idx) {
if ([...singleBoxRef.classList].includes('selected')) {
priority = `p${idx+1}`;
}
});
return priority;
}
function removeSelectedClassFromBox() {
priorityBoxesRef.forEach(function(singleBoxRef) {
singleBoxRef.classList.remove('selected');
})
}
function addSelectedClassToCurrentBox(boxRef) {
boxRef.classList.add('selected');
}
priorityBoxesRef.forEach(function(boxRef) {
boxRef.addEventListener('click', function(ev) {
// remove selected class from all box
removeSelectedClassFromBox();
// add selected class to current box
addSelectedClassToCurrentBox(ev.target);
})
});
function createTicket(ticket) {
return `
<div class="ticket-priority" data-priority=${ticket.priority}></div>
<div class="ticket-id">${ticket.id}</div>
<div class="ticket-content">
<textarea disabled>${ticket.description}</textarea>
</div>
<div class="ticket-lock locked">
<i class="fa-solid fa-lock"></i>
<i class="fa-solid fa-lock-open"></i>
</div>
<div class="ticket-delete">
<i class="fa-solid fa-trash"></i>
</div>
`;
}
function clearList() {
ticketsSectionRef.innerHTML = '';
}
function listTickets(tickets) {
clearList();
tickets.forEach((ticket) => {
console.log(ticket);
const ticketContainerRef = document.createElement('div');
ticketContainerRef.setAttribute('class', 'ticket-container');
ticketContainerRef.setAttribute('data-id', ticket.id);
const ticketHTML = createTicket(ticket);
ticketContainerRef.innerHTML = ticketHTML;
ticketsSectionRef.appendChild(ticketContainerRef);
const textAreaRef = ticketContainerRef.querySelector('.ticket-content textarea');
textAreaRef.addEventListener('blur', function(ev) {
const currentTicketContainerRef = ev.target.closest('.ticket-container');
const currentTicketId = currentTicketContainerRef.getAttribute('data-id');
updateTaskDescription(currentTicketId, textAreaRef.value);
});
});
}
function updateTaskDescription(id, updatedDescription) {
const selectedTask = tasks.find((task) => task.id === id);
selectedTask.description = updatedDescription;
updateTasksInLocalStorage(tasks);
console.log(tasks);
}
ticketsSectionRef.addEventListener('click', function(ev) {
if ([...ev.target.classList].includes('fa-lock') ||[...ev.target.classList].includes('fa-lock-open') ) {
const currentTicketContainerRef = ev.target.closest('.ticket-container');
const currentTextAreaRef = currentTicketContainerRef.querySelector('.ticket-content textarea')
const lockRef = currentTicketContainerRef.querySelector('.ticket-lock');
const isEditable = lockRef.classList.contains('locked');
if (isEditable) {
lockRef.classList.remove('locked');
currentTextAreaRef.removeAttribute('disabled');
} else {
lockRef.classList.add('locked');
currentTextAreaRef.setAttribute('disabled', true);
}
}
if ([...ev.target.classList].includes('fa-trash')) {
const currentTicketContainerRef = ev.target.closest('.ticket-container');
const taskId = currentTicketContainerRef.getAttribute('data-id');
deleteTask(taskId);
listTickets(tasks);
}
if (ev.target.classList.contains('ticket-priority')) {
changePriority(ev.target);
}
});
listTickets(tasks);
function deleteTask(taskId) {
// Delete task from tasks
tasks = tasks.filter(task => task.id != taskId);
// update in localStorage
updateTasksInLocalStorage(tasks);
}
function updateTasksInLocalStorage(tasks) {
localStorage.setItem('tasks', JSON.stringify(tasks));
}
function getTasksFromLocalStorage() {
const tasksData = localStorage.getItem('tasks');
return tasksData ? JSON.parse(tasksData) : undefined;
}
deleteDivRef.addEventListener('click', function(ev) {
const isDeleteEnabled = ev.currentTarget.classList.contains('enabled');
if (!isDeleteEnabled) {
ev.currentTarget.classList.add('enabled');
ticketsSectionRef.classList.add('enable-delete');
} else {
ev.currentTarget.classList.remove('enabled');
ticketsSectionRef.classList.remove('enable-delete');
}
});
priorityFilterRef.addEventListener('click', function(ev) {
if (ev.target.classList.contains('box')) {
const selectedPriority = ev.target.id;
const filteredTasks = tasks.filter(task => task.priority === selectedPriority);
listTickets(filteredTasks);
}
});
clearFilterRef.addEventListener('click', function(ev) {
listTickets(tasks);
});
function getNextPriority(priorityStr) {
const priority = Number(priorityStr.split('p')[1]);
const priorities = [1, 2, 3, 4];
return `p${(priority % priorities.length) + 1}`;
}
function changePriority(domRef) {
// Change in DOM
const currentPriority = domRef.getAttribute('data-priority');
const currentTicketContainerRef = domRef.closest('.ticket-container');
const taskId = currentTicketContainerRef.getAttribute('data-id');
const nextPriority = getNextPriority(currentPriority);
domRef.setAttribute('data-priority', nextPriority);
// Update in tasks (in memory)
impactedTask = tasks.find(task => task.id === taskId);
impactedTask.priority = nextPriority;
// Update in localStorage
updateTasksInLocalStorage(tasks);
}