-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
213 lines (178 loc) · 7.6 KB
/
script.js
File metadata and controls
213 lines (178 loc) · 7.6 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
//Get data-lists element
const listsContainer = document.querySelector('[data-lists]')
//get list form element
const newListForm = document.querySelector('[data-new-list-form]')
//get list input element
const newListInput = document.querySelector('[data-new-list-input]')
//get delete list button
const deleteListButton = document.querySelector('[data-delete-list-button]')
//get list display container
const listDisplayContainer = document.querySelector('[data-list-display-container]')
//get list title element
const listTitleElement = document.querySelector('[data-list-title]')
//get list count element
const listCountElement = document.querySelector('[data-list-count]')
//get list's tasks element
const tasksContainer = document.querySelector('[data-tasks]')
//HTML task template for creating blank tasks.
const taskTemplate = document.getElementById('task-template')
// new task item form
const newTaskForm = document.querySelector('[data-new-task-form]')
//get new task input section
const newTaskInput = document.querySelector('[data-new-task-input]')
//get clear completed tasks element for event
const clearCompleteTasksButton = document.querySelector('[data-clear-complete-tasks-button]')
//get delete task button element
const deleteTaskButton = document.querySelector('[data-delete-task-button]')
//create key for storage name space to store separate from any other site
const LOCAL_STORAGE_LIST_KEY = 'task.lists'
//selected list key
const LOCAL_STORAGE_SELECTED_LIST_ID_KEY = 'task.selectedListId'
//Create an event listener for all dynamically added "li" events so when a list is clicked, it will get the ID and set it to selected.
listsContainer.addEventListener ('click', e => {
if (e.target.tagName.toLowerCase() === 'li') {
selectedListID = e.target.dataset.listId
saveAndRender();
}
})
//Create an array, first check local storage and populate using JSON.parse for any existing list, otherwise load blank array.
let lists = JSON.parse(localStorage.getItem(LOCAL_STORAGE_LIST_KEY)) || []
//Select list
let selectedListID = localStorage.getItem(LOCAL_STORAGE_SELECTED_LIST_ID_KEY)
//Add event listener for delete button click
deleteListButton.addEventListener ('click', e => {
lists = lists.filter(list => list.id !== selectedListID)
selectedListID = null
saveAndRender()
})
addGlobalEventListener("click", "[data-delete-task-button]", e => {
e.preventDefault()
const selectedList = lists.find(list => list.id === selectedListID)
const selectedTask = e.target.parentNode
// this only removes the Div but doesn't remove it from the saved list
//tasksContainer.removeChild(selectedTask)
//this removes it from the local storage using .pop() function and gratting Selected Task.
selectedList.tasks.pop(selectedTask)
saveAndRender();
})
//Global Event LIstener so every click that matches function will happen.
function addGlobalEventListener(type, selector, callback) {
document.addEventListener(type, e => {
if (e.target.matches(selector)) callback (e)
})
}
clearCompleteTasksButton.addEventListener('click', e => {
const selectedList = lists.find(list => list.id === selectedListID)
selectedList.tasks = selectedList.tasks.filter(task => !task.complete)
saveAndRender();
}
)
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()
renderTaskCount(selectedList)
}
})
//On submit, take the value, add it to the list, then clear the value in the input and render().
newListForm.addEventListener('submit', e => {
//prevent form from refreshing page
e.preventDefault()
const listName = newListInput.value
if (listName == null || listName === '') return
const list = createList(listName)
newListInput.value = null
lists.push(list)
saveAndRender();
})
//On submit, take the value, add it to the task list, then clear the value in the input and render().
newTaskForm.addEventListener('submit', e => {
//prevent form from refreshing page
e.preventDefault()
const taskName = newTaskInput.value
if (taskName == null || taskName === '') return
const task = createList(taskName)
newTaskInput.value = null
const selectedList = lists.find(list => list.id === selectedListID)
selectedList.tasks.push(task)
saveAndRender();
})
function saveAndRender() {
save()
render()
}
//save function for saving to local storage (LOCAL_STORAGE_LIST_KEY)
function save() {
//save list iems to local storage
localStorage.setItem(LOCAL_STORAGE_LIST_KEY, JSON.stringify(lists))
//save the selected list to local storage so it populates on load as selected list.
localStorage.setItem(LOCAL_STORAGE_SELECTED_LIST_ID_KEY, selectedListID)
}
//Function creates the last name and assigns the ID using current date/time to get a unique ID number
function createList(name) {
return {id: Date.now().toString(), name: name, tasks: []}
}
//Function creates new task in list.
function createTask(name) {
return {id: Date.now().toString(), name: name, complete: false}
}
//Create a function that will creeate a LI, add the list-name class, and populate the text, append it to the end of the Lists Container array
function render() {
clearElement(listsContainer)
renderLists()
const selectedList = lists.find(list => list.id === selectedListID)
if (selectedListID == null) {
listDisplayContainer.style.display = 'none'
} else {
listDisplayContainer.style.display = ''
listTitleElement.innerText = selectedList.name
renderTaskCount(selectedList)
clearElement(tasksContainer)
renderTasks(selectedList)
}
}
function deleteTasks(e) {
e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode);
}
//Create task list by importing HTML task template and assigning values.
function renderTasks(selectedList) {
selectedList.tasks.forEach(task => {
const taskElement = document.importNode(taskTemplate.content, true)
const checkbox = taskElement.querySelector('input')
checkbox.id = task.id
checkbox.checked = task.complete
const label = taskElement.querySelector('label')
label.htmlFor = task.id
label.append(task.name)
tasksContainer.appendChild(taskElement)
})
}
function renderTaskCount(selectedList) {
const incompleteTaskCount = selectedList.tasks.filter(task => !task.complete).length
if (incompleteTaskCount === 0){
listCountElement.innerText = "Congratulations, no tasks!"
}
else {
//singular or plural
const taskString = incompleteTaskCount === 1 ? "task" : "tasks"
listCountElement.innerText = `${incompleteTaskCount} ${taskString} remaining`
}
}
function renderLists() {
lists.forEach(list => {
const listElement = document.createElement('li')
listElement.dataset.listId = list.id
listElement.classList.add("list-name")
listElement.innerText = list.name
if (list.id === selectedListID) {listElement.classList.add('active-list')}
listsContainer.appendChild(listElement)
})
}
function clearElement(element) {
while(element.firstChild) {
element.removeChild(element.firstChild)
}
}
render();