-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.js
163 lines (138 loc) · 5.8 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
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
// part 2 linking it all together
// The function here is called an iife,
// it keeps everything inside hidden from the rest of our application
(function() {
// This is the dom node where we will keep our todo
var container = document.getElementById('todo-container');
var addTodoForm = document.getElementById('add-todo');
var formText = document.querySelector(".input-form__text");
var sortDescend = document.getElementById('button-descend');
var sortAZ = document.getElementById('button-az');
// var myState = [
// { id: -3, description: 'first todo', done: false },
// { id: -2, description: 'second todo', done: false },
// { id: -1, description: 'third todo', done: false },
// ];
if(typeof localStorage.getItem('state') == 'undefined' || localStorage.getItem('state') == null){
var state = [
{ id: -3, description: 'first todo', done: false },
{ id: -2, description: 'second todo', done: false },
{ id: -1, description: 'third todo', done: false },
];
} else {
var newState = localStorage.getItem('state');
var parsedNewState = JSON.parse(newState);
var state = parsedNewState;
}
console.log('updated4');
// this is our initial todoList
sortDescend.addEventListener("click", function(e){
var newState = todoFunctions.sortTodos(state, todoFunctions.sortDescending);
update(newState);
});
sortAZ.addEventListener("click", function(e){
var newState = todoFunctions.sortTodos(state, todoFunctions.sortAZ);
update(newState);
});
// This function takes a todo, it returns the DOM node representing that todo
var createTodoNode = function(todo) {
var todoNode = document.createElement('li');
// you will need to use addEventListener
todoNode.addEventListener('click', function(event) {
var newState = todoFunctions.markTodo(state, todo.id);
update(newState);
});
//making wrapping div inside li
// add span holding description
var liContent = document.createElement('div');
var para = document.createElement('p');
para.addEventListener("click", function(){
event.stopPropagation();
})
var text = document.createTextNode(todo.description);
var x = document.createTextNode('X')
para.appendChild(text);
liContent.appendChild(para);
todoNode.appendChild(liContent);
//this adds container for BUTTONS
var buttonsContainer = document.createElement("div");
buttonsContainer.setAttribute("class", "buttons-container");
todoNode.appendChild(buttonsContainer);
// this adds the delete button
var deleteButtonNode = document.createElement('button');
deleteButtonNode.addEventListener('click', function(event) {
var newState = todoFunctions.deleteTodo(state, todo.id);
update(newState);
});
deleteButtonNode.appendChild(x)
buttonsContainer.appendChild(deleteButtonNode);
// add markTodo button
var markButtonNode = document.createElement('button');
markButtonNode.addEventListener('click', function(event) {
var newState = todoFunctions.markTodo(state, todo.id);
//marking it twice to fix bubbling
var newnewState = todoFunctions.markTodo(newState, todo.id);
update(newnewState);
});
buttonsContainer.appendChild(markButtonNode);
// add classes for css
todoNode.setAttribute("class", "todo-item")
liContent.setAttribute("class", "todo-item__content")
para.setAttribute("class", "content__p")
para.setAttribute("contenteditable", "true")
deleteButtonNode.setAttribute("class", "button__delete")
markButtonNode.setAttribute("class", "button__mark")
if(todo.done===true){
para.classList.add('done');
}
return todoNode;
};
// bind create todo form
if (addTodoForm) {
addTodoForm.addEventListener('submit', function(event) {
// https://developer.mozilla.org/en-US/docs/Web/Events/submit
// what does event.preventDefault do?
// what is inside event.target?
event.preventDefault();
var description = event.target.description.value; // event.target ....
if(!description){
document.querySelector("#validateSpan").style.visibility = "visible";
}
else{
var itemToAdd={done: false};
document.querySelector("#validateSpan").style.visibility = "hidden";
itemToAdd.description = description;
// hint: todoFunctions.addTodo
var newState = todoFunctions.addTodo(state, itemToAdd); // ?? change this!
update(newState);
formText.value="";
}
});
}
// LOCAL STORAGE ATTEMPT
localStorage.setItem('state', JSON.stringify(state));
myState = localStorage.getItem('state');
console.log('myState: ', JSON.parse(myState));
// you should not need to change this function
var update = function(newState) {
state = newState;
localStorage.setItem('state', JSON.stringify(state));
myState = localStorage.getItem('state');
console.log('myState: ', JSON.parse(myState));
renderState(state);
};
// you do not need to change this function
var renderState = function(state) {
var todoListNode = document.createElement('ul');
todoListNode.setAttribute("class", "todos-list")
state.forEach(function(todo) {
todoListNode.insertBefore(createTodoNode(todo), todoListNode.childNodes[0]);
});
// you may want to add a class for css
container.replaceChild(todoListNode, container.firstChild);
};
if (container) renderState(state);
var styleInvalid = function(){
document.querySelector("#input-form__text").style.property = "background-color: red;"
}
})();