-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdom.js
114 lines (98 loc) · 3.42 KB
/
dom.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
// part 2 linking it all together
// The function here is called an iife,
// it keeps everything inside hidden from the rest of our application
(() => {
// This is the dom node where we will keep our todo
let container = document.getElementById("todo-container");
let addTodoForm = document.getElementById("add-todo");
let state = [
{
id: -3,
description: "first todo"
},
{
id: -2,
description: "second todo"
},
{
id: -1,
description: "third todo"
}
]; // this is our initial todoList
// This function takes a todo, it returns the DOM node representing that todo
let createTodoNode = todo => {
let todoNode = document.createElement("li");
// you will need to use addEventListener
todoNode.addEventListener("click", function(mark) {
// Come back to?
});
// add span holding description
let todoSpan = document.createElement("span");
let descNode = document.createTextNode(todo.description);
todoNode.appendChild(todoSpan);
todoSpan.appendChild(descNode);
// add container for both buttons
let buttonContainer = document.createElement("div");
todoNode.appendChild(buttonContainer);
// add markTodo button
let markTodoButtonNode = document.createElement("button");
markTodoButtonNode.setAttribute("aria-label", "Mark as done");
buttonContainer.appendChild(markTodoButtonNode);
if (todo.done) {
markTodoButtonNode.setAttribute("aria-label", "Mark as not done");
markTodoButtonNode.textContent = "✔";
todoSpan.style.textDecoration = "line-through";
}
todoNode.addEventListener("click", e => {
let newState = todoFunctions.markTodo(state, todo.id);
update(newState);
});
// this adds the delete button
let deleteButtonNode = document.createElement("button");
deleteButtonNode.textContent = "Delete";
deleteButtonNode.addEventListener("click", event => {
let newState = todoFunctions.deleteTodo(state, todo.id);
update(newState);
});
buttonContainer.appendChild(deleteButtonNode);
// add classes for css
todoNode.className = "todo";
todoSpan.className = "todospan";
buttonContainer.className = "buttoncontainer";
deleteButtonNode.classList.add("button", "delete");
markTodoButtonNode.classList.add("button", "mark");
return todoNode;
};
// bind create todo form
if (addTodoForm) {
addTodoForm.addEventListener("submit", event => {
// https://developer.mozilla.org/en-US/docs/Web/Events/submit
// what does event.preventDefault do?
// what is inside event.target?
event.preventDefault();
let description = event.target.elements.description.value;
if (description !== "") {
var newState = todoFunctions.addTodo(state, {
description: description
}); // ?? change this!
event.target.elements.description.value = "";
update(newState);
}
});
}
// you should not need to change this function
let update = newState => {
state = newState;
renderState(state);
};
// you do not need to change this function
let renderState = state => {
let todoListNode = document.createElement("ul");
state.forEach(todo => {
todoListNode.appendChild(createTodoNode(todo));
});
// you may want to add a class for css
container.replaceChild(todoListNode, container.firstChild);
};
if (container) renderState(state);
})();