-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
225 lines (200 loc) · 6.32 KB
/
Copy pathapp.js
File metadata and controls
225 lines (200 loc) · 6.32 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
214
215
216
217
218
219
220
221
222
223
224
225
let add = document.querySelector("form button");
let toDoBox = document.querySelector("section");
add.addEventListener("click", (e) => {
//Prevent form submit
e.preventDefault();
//Get input value
let form = e.target.parentElement;
let toDoText = form.children[0].value;
let toDoMonth = form.children[1].value;
let toDoDate = form.children[2].value;
//Create Element
let toDoItem = document.createElement("div");
let text = document.createElement("p");
let time = document.createElement("p");
let completeBtn = document.createElement("button");
let deleteBtn = document.createElement("button");
//Check To Do
if (toDoText == "") {
alert("請輸入代辦事項");
return;
}
if (toDoMonth > 12 || toDoMonth < 1) {
alert("請輸入正確月份");
return;
}
if (toDoDate > 31 || toDoDate < 1) {
alert("請輸入正確日期");
return;
}
//Put Value
text.innerText = toDoText;
time.innerText = toDoMonth + "/" + toDoDate;
completeBtn.innerHTML = `<i class="far fa-check-square"></i>`;
deleteBtn.innerHTML = `<i class="fas fa-trash"></i>`;
//Add ClassName
toDoItem.classList.add("todo");
text.classList.add("to-do-text");
time.classList.add("to-do-time");
completeBtn.classList.add("complete");
deleteBtn.classList.add("delete");
//Animation
toDoItem.style.animation = "scaleUp 0.3s forwards";
//Create ToDo Item
toDoItem.appendChild(text);
toDoItem.appendChild(time);
toDoItem.appendChild(completeBtn);
toDoItem.appendChild(deleteBtn);
toDoBox.appendChild(toDoItem);
//Complete & Delete
completeBtn.addEventListener("click", (e) => {
let toDoItem = e.target.parentElement;
console.log(e.target.parentElement);
toDoItem.classList.toggle("done");
});
deleteBtn.addEventListener("click", (e) => {
let deleteItem = e.target.parentElement;
deleteItem.style.animation = "scaleDown 0.3s forwards";
deleteItem.addEventListener("animationend", () => {
let text = toDoItem.children[0].innerText;
let myListArray = JSON.parse(localStorage.getItem("list"));
myListArray.forEach((item, index) => {
if (item.toDoText == text) {
//Delete Object From Array Index , 1 Element
myListArray.splice(index, 1);
console.log(index);
localStorage.setItem("list", JSON.stringify(myListArray));
}
});
deleteItem.remove();
});
});
//Clear To Do Input
form.children[0].value = "";
//Create Object
let myToDo = {
toDoText: toDoText,
toDoMonth: toDoMonth,
toDoDate: toDoDate,
};
//Store Data
let myList = localStorage.getItem("list");
if (myList == null) {
localStorage.setItem("list", JSON.stringify([myToDo]));
} else {
let myListArray = JSON.parse(myList);
myListArray.push(myToDo);
localStorage.setItem("list", JSON.stringify(myListArray));
}
localStorage.clear;
console.log(JSON.parse(localStorage.getItem("list")));
});
loadData();
//Load LocalStorage
function loadData() {
let myList = localStorage.getItem("list");
if (myList !== null) {
let myListArray = JSON.parse(myList);
myListArray.forEach((item) => {
//Create Element
let toDoItem = document.createElement("div");
toDoItem.classList.add("todo");
let text = document.createElement("p");
text.classList.add("to-do-text");
text.innerText = item.toDoText;
let time = document.createElement("p");
time.classList.add("to-do-time");
time.innerText = item.toDoMonth + "/" + item.toDoDate;
toDoItem.appendChild(text);
toDoItem.appendChild(time);
//Create Btn
let completeBtn = document.createElement("button");
let deleteBtn = document.createElement("button");
completeBtn.classList.add("complete");
deleteBtn.classList.add("delete");
completeBtn.innerHTML = `<i class="far fa-check-square"></i>`;
deleteBtn.innerHTML = `<i class="fas fa-trash"></i>`;
//Btn feature
completeBtn.addEventListener("click", (e) => {
let toDoItem = e.target.parentElement;
console.log(e.target.parentElement);
toDoItem.classList.toggle("done");
});
deleteBtn.addEventListener("click", (e) => {
let deleteItem = e.target.parentElement;
deleteItem.style.animation = "scaleDown 0.3s forwards";
deleteItem.addEventListener("animationend", () => {
let text = toDoItem.children[0].innerText;
let myListArray = JSON.parse(localStorage.getItem("list"));
myListArray.forEach((item, index) => {
console.log(item);
if (item.toDoText == text) {
myListArray.splice(index, 1);
localStorage.setItem("list", JSON.stringify(myListArray));
}
});
deleteItem.remove();
});
});
//Create Element
toDoItem.appendChild(completeBtn);
toDoItem.appendChild(deleteBtn);
toDoBox.appendChild(toDoItem);
});
}
}
//Sort
function mergeTime(arr1, arr2) {
let result = [];
let i = 0;
let j = 0;
while (i < arr1.length && j < arr2.length) {
if (Number(arr1[i].toDoMonth) > Number(arr2[j].toDoMonth)) {
result.push(arr2[j]);
j++;
} else if (Number(arr1[i].toDoMonth) < Number(arr2[j].toDoMonth)) {
result.push(arr1[i]);
i++;
} else if (Number(arr1[i].toDoMonth) == Number(arr2[j].toDoMonth)) {
if (Number(arr1[i].toDoDate) > Number(arr2[j].toDoDate)) {
result.push(arr2[j]);
j++;
} else {
result.push(arr1[i]);
i++;
}
}
}
while (i < arr1.length) {
result.push(arr1[i]);
i++;
}
while (j < arr2.length) {
result.push(arr2[j]);
j++;
}
return result;
}
function mergeSort(arr) {
if (arr.length === 1) {
return arr;
} else {
let middle = Math.floor(arr.length / 2);
let right = arr.slice(0, middle);
let left = arr.slice(middle, arr.length);
return mergeTime(mergeSort(right), mergeSort(left));
}
}
let sortButton = document.querySelector("div.sort button");
sortButton.addEventListener("click", () => {
// sort data
let sortedArray = mergeSort(JSON.parse(localStorage.getItem("list")));
localStorage.setItem("list", JSON.stringify(sortedArray));
// remove data
let len = toDoBox.children.length;
for (let i = 0; i < len; i++) {
toDoBox.children[0].remove();
}
// load data
loadData();
});