-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
111 lines (101 loc) · 2.61 KB
/
script.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
const selectElement = document.querySelector("#selectImportance");
const input = document.querySelector("input");
var todos = JSON.parse(localStorage.getItem("todoList")) ?? [
{
title: "倒垃圾",
category: "normal",
isCompleted: false,
},
{
title: "繳電話費",
category: "important",
isCompleted: false,
},
{
title: "採買本週食材",
category: "urgent",
isCompleted: false,
},
];
const importanceBackgroundColor = {
normal: "white",
important: "orange",
urgent: "red",
};
function render() {
// console.log(todos);
const root = document.querySelector("#root");
root.textContent = "";
const ul = document.createElement("ul");
root.append(ul);
todos.forEach((todo, index) => {
const li = document.createElement("li");
const delBtn = document.createElement("button");
const toggleBtn = document.createElement("button");
const title = document.createElement("span");
title.textContent = todo.title;
title.style.backgroundColor = importanceBackgroundColor[todo.category];
title.className = "title";
delBtn.textContent = "刪除";
delBtn.onclick = () => del(index);
if (!todo.isCompleted) {
toggleBtn.textContent = "標示為已完成";
}
if (todo.isCompleted) {
toggleBtn.textContent = "標示為未完成";
const span = document.createElement("span");
span.textContent = "(已完成)";
li.append(span);
}
toggleBtn.onclick = () => toggleDone(index);
ul.append(li);
li.append(delBtn, toggleBtn, title);
});
}
function selectImportanceChange() {
input.style.backgroundColor =
importanceBackgroundColor[
selectElement.options[selectElement.selectedIndex].value
];
}
function add() {
todos.push({
title: input.value,
category: selectElement.options[selectElement.selectedIndex].value,
isCompleted: false,
});
// console.log(input);
render();
}
function del(index) {
todos.splice(index, 1);
render();
}
function toggleDone(index) {
todos[index].isCompleted = !todos[index].isCompleted;
render();
}
function exportTodos() {
console.log("log");
const asterisk = {
normal: "",
important: "*",
urgent: "**",
};
const output = todos.map((todo, index) => {
const output =
(index + 1).toString() +
"、" +
asterisk[todo.category] +
todo.title +
(todo.isCompleted ? "(已完成)" : "") +
asterisk[todo.category];
return output;
});
alert("今日待辦:" + output.join(" "));
}
function save() {
const saveData = JSON.stringify(todos);
localStorage.setItem("todoList", saveData);
}
render();