-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.js
149 lines (131 loc) · 4.59 KB
/
task.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
class Task {
// initializing the localstorage and add eventlistener to addBtn
constructor() {
this.allTask = JSON.parse(localStorage.getItem('data')) || [];
this.addBtn = document.querySelector('.add-btn');
this.addBtn.addEventListener('click', this.addTask.bind(this));
}
// function for saving the data to localStorage
saveData() {
localStorage.setItem('data', JSON.stringify(this.allTask));
}
// function for reindexing the Tasks
reindex() {
this.allTask.forEach((element, index = 1) => {
element.index = index;
});
}
// function for deleting the Tasks
removeTask(index) {
this.allTask.splice(index, 1);
this.saveData();
this.displayTask();
this.reindex();
}
// function for adding Tasks to "allTask" array of object
addTask() {
const inputAdd = document.querySelector('.input-add');
if (inputAdd.value) {
const newTask = {
description: inputAdd.value,
completed: false,
index: this.allTask.length,
};
this.allTask.push(newTask);
inputAdd.value = '';
this.saveData();
this.displayTask();
}
}
// function for both displaying and editing
displayTask() {
const taskList = document.querySelector('.to-do-list');
taskList.innerHTML = '';
// displaying Tasks
this.allTask.forEach((element, addindex) => {
const item = document.createElement('li');
item.className = 'item-element';
const taskComplete = document.createElement('input');
taskComplete.type = 'checkbox';
taskComplete.className = 'is-completed';
taskComplete.checked = element.completed;
taskComplete.addEventListener('change', () => {
element.completed = taskComplete.checked;
this.saveData();
this.displayTask();
});
const taskText = document.createElement('p');
taskText.textContent = element.description;
const editIcon = document.createElement('ion-icon');
editIcon.name = 'construct-outline';
editIcon.classList.add('editIcon');
const deleteIcon = document.createElement('ion-icon');
deleteIcon.name = 'trash-outline';
deleteIcon.classList.add('removeIcon');
item.appendChild(taskComplete);
item.appendChild(taskText);
item.appendChild(editIcon);
item.appendChild(deleteIcon);
taskList.appendChild(item);
deleteIcon.addEventListener('click', () => {
this.removeTask(addindex);
this.saveData();
this.reindex();
});
// adding edit popup
const editItem = document.createElement('li');
editItem.className = 'edit-div';
editItem.classList.add('display-none');
const editForm = document.createElement('form');
const editDescription = document.createElement('input');
editDescription.type = 'text';
editDescription.className = 'edit-input';
editDescription.required = true;
editDescription.value = `${element.description}`;
editDescription.placeholder = `Edit "${element.description}"`;
const saveEdit = document.createElement('button');
saveEdit.type = 'submit';
saveEdit.textContent = 'Save';
saveEdit.className = 'save-edit-btn';
const cancelEdit = document.createElement('button');
cancelEdit.type = 'text';
cancelEdit.textContent = 'Cancel';
cancelEdit.className = 'cancel-edit-btn';
editForm.appendChild(editDescription);
editForm.appendChild(cancelEdit);
editForm.appendChild(saveEdit);
editItem.appendChild(editForm);
taskList.appendChild(editItem);
editIcon.addEventListener('click', () => {
editItem.classList.remove('display-none');
editDescription.focus();
this.saveData();
});
cancelEdit.addEventListener('click', () => {
editItem.classList.add('display-none');
this.displayTask();
});
saveEdit.addEventListener('click', (event) => {
event.preventDefault();
if (editDescription.value !== '') {
element.description = editDescription.value;
this.addTask();
this.saveData();
this.displayTask();
editItem.classList.add('display-none');
} else {
editDescription.reportValidity();
}
});
// function for clearing all completed task
const allCompletedBtn = document.querySelector('.all-completed');
allCompletedBtn.addEventListener('click', () => {
this.allTask = this.allTask.filter((el) => !el.completed);
this.reindex();
this.saveData();
this.displayTask();
});
});
}
}
export default Task;