-
Notifications
You must be signed in to change notification settings - Fork 0
/
script2.js
326 lines (291 loc) · 14.9 KB
/
script2.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
function init() {
const listsContainer = document.querySelector('[data-lists]')
const newListForm = document.querySelector('[data-new-list-form]')
const newListInput = document.querySelector('[data-new-list-input]')
const deleteListButton = document.querySelector('[data-delete-list-button]')
const deleteTaskButton = document.querySelector('[data-delete-task-button]')
const backListButton = document.querySelector('[data-back-list-button]')
const backTaskButton = document.querySelector('[data-back-task-button]')
const listDisplayContainer = document.querySelector('[data-list-display-container]')
const taskPriorityContainer = document.querySelector('[data-task-priority-container]')
const listTitleElement = document.querySelector('[data-list-title]')
const listCountElement = document.querySelector('[data-list-count]')
const tasksContainer = document.querySelector('[data-tasks]')
const newTaskForm = document.querySelector('[data-new-task-form]')
const newTaskInput = document.querySelector('[data-new-task-input]')
const clearCompleteTasksButton = document.querySelector('[data-clear-complete-tasks-button]')
const addTaskPriority = document.querySelector('[data-add-task-priority]')
const noteText = document.querySelector('[data-note-text]')
const datePriority = document.querySelector('[data-date-priority]')
const selectPriority = document.querySelector('[data-select-priority]')
const taskTitleElement = document.querySelector('[data-task-title]')
const LOCAL_STORAGE_LIST_KEY = "task.lists"
let lists = JSON.parse(localStorage.getItem(LOCAL_STORAGE_LIST_KEY)) || []
let selectedListId = null
let selectedParaId = null
// Selecting the List
listsContainer.addEventListener('click', (e) => {
if (e.target.tagName.toLowerCase() === 'li') {
selectedListId = e.target.dataset.listId
save(LOCAL_STORAGE_LIST_KEY, lists)
render(listsContainer, lists, selectedListId, listDisplayContainer, listTitleElement, listCountElement, selectedParaId, taskPriorityContainer, tasksContainer, noteText, datePriority, selectPriority, taskTitleElement)
}
})
// Selecting the Task Item
tasksContainer.addEventListener('click', (e) => {
if (e.target.tagName.toLowerCase() === 'p') {
selectedParaId = e.target.dataset.paraId
save(LOCAL_STORAGE_LIST_KEY, lists)
render(listsContainer, lists, selectedListId, listDisplayContainer, listTitleElement, listCountElement, selectedParaId, taskPriorityContainer, tasksContainer, noteText, datePriority, selectPriority, taskTitleElement)
}
})
// Checkbox the Task Item
tasksContainer.addEventListener('click', (e) => {
if (e.target.tagName.toLowerCase() === 'input') {
const selectedList = lists.find(list => list.id === selectedListId)
const selectedTask = selectedList.tasks.find(task => task.id === e.target.id)
selectedTask.complete = e.target.checked
save(LOCAL_STORAGE_LIST_KEY, lists)
renderTaskCount(selectedList, listCountElement)
}
})
// Clearing the task those are completed
clearCompleteTasksButton.addEventListener('click', (e) => {
const selectedList = lists.find(list => list.id === selectedListId)
selectedList.tasks = selectedList.tasks.filter(task => !task.complete)
save(LOCAL_STORAGE_LIST_KEY, lists)
render(listsContainer, lists, selectedListId, listDisplayContainer, listTitleElement, listCountElement, selectedParaId, taskPriorityContainer, tasksContainer, noteText, datePriority, selectPriority, taskTitleElement)
})
// Deleting everything for list to list item
deleteListButton.addEventListener('click', (e) => {
lists = lists.filter(list => list.id !== selectedListId)
selectedListId = null
save(LOCAL_STORAGE_LIST_KEY, lists)
render(listsContainer, lists, selectedListId, listDisplayContainer, listTitleElement, listCountElement, selectedParaId, taskPriorityContainer, tasksContainer, noteText, datePriority, selectPriority, taskTitleElement)
})
// Deleting everything for list to list item
deleteTaskButton.addEventListener('click', (e) => {
const selectedList = lists.find(list => list.id === selectedListId)
selectedList.tasks = selectedList.tasks.filter(task => task.id !== selectedParaId)
selectedParaId = null
save(LOCAL_STORAGE_LIST_KEY, lists)
render(listsContainer, lists, selectedListId, listDisplayContainer, listTitleElement, listCountElement, selectedParaId, taskPriorityContainer, tasksContainer, noteText, datePriority, selectPriority, taskTitleElement)
})
// back from task
backListButton.addEventListener('click', (e) => {
listDisplayContainer.style.display = 'none'
selectedListId = null
selectedParaId = null
save(LOCAL_STORAGE_LIST_KEY, lists)
render(listsContainer, lists, selectedListId, listDisplayContainer, listTitleElement, listCountElement, selectedParaId, taskPriorityContainer, tasksContainer, noteText, datePriority, selectPriority, taskTitleElement)
})
// Back from task priority
backTaskButton.addEventListener('click', (e) => {
taskPriorityContainer.style.display = 'none'
selectedParaId = null
save(LOCAL_STORAGE_LIST_KEY, lists)
render(listsContainer, lists, selectedListId, listDisplayContainer, listTitleElement, listCountElement, selectedParaId, taskPriorityContainer, tasksContainer, noteText, datePriority, selectPriority, taskTitleElement)
})
// Adding New list
newListForm.addEventListener('submit', (e) => {
e.preventDefault()
const listName = newListInput.value
if (!listName) return
const list = createList(listName)
newListInput.value = null
lists.push(list)
save(LOCAL_STORAGE_LIST_KEY, lists)
render(listsContainer, lists, selectedListId, listDisplayContainer, listTitleElement, listCountElement, selectedParaId, taskPriorityContainer, tasksContainer, noteText, datePriority, selectPriority, taskTitleElement)
})
// Adding new Task Item
newTaskForm.addEventListener('submit', (e) => {
e.preventDefault()
const taskName = newTaskInput.value
if (!taskName) return
const task = createTask(taskName)
newTaskInput.value = null
const selectedList = lists.find(list => list.id === selectedListId)
selectedList.tasks.push(task)
save(LOCAL_STORAGE_LIST_KEY, lists)
render(listsContainer, lists, selectedListId, listDisplayContainer, listTitleElement, listCountElement, selectedParaId, taskPriorityContainer, tasksContainer, noteText, datePriority, selectPriority, taskTitleElement)
})
// Adding Priority Container
addTaskPriority.addEventListener('click', (e) => {
const selectedList = lists.find(list => list.id === selectedListId)
const paraList = selectedList.tasks.find(list => list.id === selectedParaId)
if(!noteText.value) paraList.noteText = ""
paraList.noteText = noteText.value
if(!datePriority.value) paraList.displayDate = ''
paraList.displayDate = datePriority.value
//paraList.priorityValue = selectPriority.value
if(selectPriority.value === 'low') {
paraList.lowPriority = true
paraList.mediumPriority = false
paraList.highPriority = false
paraList.priorityValue = 'Low'
}
if(selectPriority.value === 'medium') {
paraList.lowPriority = false
paraList.mediumPriority = true
paraList.highPriority = false
paraList.priorityValue = 'Medium'
}
if(selectPriority.value === 'high') {
paraList.lowPriority = false
paraList.mediumPriority = false
paraList.highPriority = true
paraList.priorityValue = 'High'
}
if(selectPriority.value === 'none') {
paraList.lowPriority = false
paraList.mediumPriority = false
paraList.highPriority = false
paraList.priorityValue = 'None'
}
save(LOCAL_STORAGE_LIST_KEY, lists)
render(listsContainer, lists, selectedListId, listDisplayContainer, listTitleElement, listCountElement, selectedParaId, taskPriorityContainer, tasksContainer, noteText, datePriority, selectPriority, taskTitleElement)
})
render(listsContainer, lists, selectedListId, listDisplayContainer, listTitleElement, listCountElement, selectedParaId, taskPriorityContainer, tasksContainer, noteText, datePriority, selectPriority, taskTitleElement)
}
init()
// This is format of create task
function createList(name) {
return {
id: Date.now().toString(),
name: name,
tasks: []
}
}
// This is the task item
function createTask(name) {
return {
id: Date.now().toString(),
name: name,
complete: false,
noteText: "",
displayDate: '',
lowPriority: false,
mediumPriority: false,
highPriority: false,
priorityValue: "None"
}
}
function save(LOCAL_STORAGE_LIST_KEY, lists) {
localStorage.setItem(LOCAL_STORAGE_LIST_KEY, JSON.stringify(lists))
}
// Rendering the task or task item
function render(listsContainer, lists, selectedListId, listDisplayContainer, listTitleElement, listCountElement, selectedParaId, taskPriorityContainer, tasksContainer, noteText, datePriority, selectPriority, taskTitleElement) {
clearElement(listsContainer)
renderLists(lists, selectedListId, listsContainer)
const selectedList = lists.find(list => list.id === selectedListId)
if (!selectedList) {
taskPriorityContainer.style.display = 'none'
listDisplayContainer.style.display = 'none'
document.querySelector('.all-tasks').style.display = ''
} else {
document.querySelector('.all-tasks').style.display = 'none'
listDisplayContainer.style.display = ''
listTitleElement.innerText = selectedList.name
renderTaskCount(selectedList, listCountElement)
clearElement(tasksContainer)
renderTasks(selectedList, tasksContainer, selectedParaId)
// Selecting Priority box for task item
const paraList = selectedList.tasks.find(list => list.id === selectedParaId)
if (!paraList) {
taskPriorityContainer.style.display = 'none'
listDisplayContainer.style.display = ''
document.querySelector('.all-tasks').style.display = 'none'
}
else {
taskPriorityContainer.style.display = ''
listDisplayContainer.style.display = '' //none
document.querySelector('.all-tasks').style.display = 'none'
renderPriority(paraList, noteText, datePriority, selectPriority, taskTitleElement)
}
}
}
// Showing the count of task left
function renderTaskCount(selectedList, listCountElement) {
const incompleteTaskCount = selectedList.tasks.filter(task => !task.complete).length
const taskString = incompleteTaskCount === 1 ? "task" : "tasks"
listCountElement.innerText = `${incompleteTaskCount} ${taskString} remaining`
}
// rendering the task list
function renderLists(lists, selectedListId, listsContainer) {
lists.forEach(list => {
const listElement = document.createElement('li')
listElement.dataset.listId = list.id //dataset
listElement.classList.add('list-name')
listElement.innerText = list.name
if (list.id === selectedListId) listElement.classList.add('active-list')
listsContainer.appendChild(listElement)
})
}
// Rendering the task item
function renderTasks(selectedList, tasksContainer, selectedParaId) {
const highPriority = []
const mediumPriority = []
const lowPriority = []
const nonePriority = []
selectedList.tasks.forEach(task => {
if (task.highPriority) highPriority.push(task)
if (task.mediumPriority) mediumPriority.push(task)
if (task.lowPriority) lowPriority.push(task)
if (task.priorityValue.toLowerCase() === "none") nonePriority.push(task)
})
const highPriority1 = filterTask(highPriority)
const mediumPriority1 = filterTask(mediumPriority)
const lowPriority1 = filterTask(lowPriority)
const nonePriority1 = filterTask(nonePriority)
selectedList.tasks = []
const concatPrio = highPriority1.concat(mediumPriority1).concat(lowPriority1).concat(nonePriority1)
for (let task of concatPrio) {
selectedList.tasks.push(task)
}
selectedList.tasks.forEach(task => {
const taskElement = document.createElement('div')
if (task.highPriority) taskElement.classList.add('red')
if (task.mediumPriority) taskElement.classList.add('yellow')
if (task.lowPriority) taskElement.classList.add('green')
const checkbox = document.createElement('input')
checkbox.setAttribute('type', 'checkbox')
checkbox.id = task.id
checkbox.checked = task.complete
const p = document.createElement('p')
p.style.display = 'inline'
p.dataset.paraId = task.id
p.innerText = task.name
if(task.id === selectedParaId) p.classList.add('active-task')
taskElement.appendChild(checkbox)
taskElement.appendChild(p)
tasksContainer.appendChild(taskElement)
})
}
// Rendering the task Priority
function renderPriority(paraList, noteText, datePriority, selectPriority, taskTitleElement) {
taskTitleElement.innerText = paraList.name
noteText.value = paraList.noteText
datePriority.value = paraList.displayDate
const prior = ['None', 'High', 'Medium', 'Low']
const index = prior.indexOf(paraList.priorityValue)
selectPriority.selectedIndex = index.toString()
}
function filterTask(priorityTask){
const noDate = []
const highAfter = []
const highBefore = []
priorityTask.forEach(task => {
if (!task.displayDate) noDate.push(task)
if(task.displayDate && new Date(task.displayDate).toISOString().slice(0, 10) < new Date().toISOString().slice(0, 10)) highBefore.push(task)
if(task.displayDate && new Date(task.displayDate).toISOString().slice(0, 10) >= new Date().toISOString().slice(0, 10)) highAfter.push(task)
})
highBefore.sort((a,b) => new Date(b.displayDate ) - new Date(a.displayDate))
highAfter.sort((a,b) => new Date(a.displayDate) - new Date(b.displayDate))
return [].concat(highAfter).concat(highBefore).concat(noDate)
}
// Clearing the childnode if exist
function clearElement(element) {
while (element.firstChild) {
element.removeChild(element.firstChild)
}
}