-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom.js
177 lines (156 loc) · 5.87 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// The function here is called an iife,
// it keeps everything inside hidden from the rest of our application
(function() {
// Get all necessary dom node references
var container = document.getElementById('todo-container');
var addTodoForm = document.getElementById('add-todo');
var sortOldButton= document.getElementById('sortOld');
var sortNewButton= document.getElementById('sortNew');
var sortStarButton = document.getElementById('sortStar');
var priorityStar = document.getElementById("priority");
//get local storage references
var localState = localStorage.getItem('state');
if (localState) {
var state = JSON.parse(localState);
//set the base id to the current max id in list
todoFunctions.generateId.set(state.reduce(function(a, b) {
return a > parseInt(b.id) ? a : parseInt(b.id);
}, 0));
} else {
//initial to do list if no stored state
var state = [
{ id: -3, description: 'first todo', done: false, priority: false},
{ id: -2, description: 'second todo', done: false, priority: false },
{ id: -1, description: 'third todo', done: false, priority: false },
];
}
var createTodoNode = function(todo) {
var todoNode = document.createElement('li');
// create and append button to toggle whether item done
var markTodoNode = document.createElement('button');
markTodoNode.setAttribute('aria-label', 'Mark Todo done button');
if (todo.done) {
markTodoNode.className = "fa fa-check-square-o mark-on";
} else {
markTodoNode.className = "fa fa-square-o mark-off";
}
markTodoNode.addEventListener('click', function(event) {
var newState = todoFunctions.markTodo(state, todo.id);
update(newState);
});
todoNode.appendChild(markTodoNode);
// create and append span to hold description (clicking also toggles done property)
var itemSpan = document.createElement("span");
itemSpan.className = "description ";
itemSpan.addEventListener('click', function(event) {
var newState = todoFunctions.markTodo(state, todo.id);
update(newState);
})
var itemDesc = document.createTextNode(todo.description);
itemSpan.appendChild(itemDesc);
todoNode.appendChild(itemSpan);
// this adds a star button that toggles the priority property
var starButtonNode = document.createElement('button');
starButtonNode.setAttribute('aria-label', 'Priority flag button');
if(todo.priority){
starButtonNode.className = "fa fa-star star-on";
} else {
starButtonNode.className = "fa fa-star-o star-off";
}
starButtonNode.addEventListener('click', function(event) {
var newState = todoFunctions.starTodo(state, todo.id);
update(newState);
});
todoNode.appendChild(starButtonNode);
// this adds the delete button
var deleteButtonNode = document.createElement('button');
deleteButtonNode.setAttribute('aria-label', 'Delete Button');
deleteButtonNode.className = "fa fa-times delete";
deleteButtonNode.addEventListener('click', function(event) {
var newState = todoFunctions.deleteTodo(state, todo.id);
update(newState);
});
todoNode.appendChild(deleteButtonNode);
return todoNode;
};
// Event Listeners for form to add new To Dos
if (addTodoForm) {
addTodoForm.addEventListener('submit', function(event) {
event.preventDefault();
//only take action if at least one character entered.
if (event.target.description.value.length > 1) {
var todoObj = {};
todoObj.description = event.target.description.value;
event.target.description.value = "";
todoObj.done = false;
todoObj.priority = event.target.priority.checked;
event.target.priority.checked = false;
priorityStar.className = "fa fa-star-o star-off checkbox";
var newState = todoFunctions.addTodo(state, todoObj);
update(newState);
}
});
priorityStar.addEventListener('click', function(e) {
if (addTodoForm.priority.checked == true) {
e.target.className = "fa fa-star star-on checkbox";
} else {
e.target.className = "fa fa-star-o star-on checkbox";
}
});
};
//Sort Button Listeners
if(sortOldButton){
sortOldButton.addEventListener('click',function(event){
activateSort(this,[sortNewButton,sortStarButton]);
});
};
if(sortNewButton){
sortNewButton.addEventListener('click',function(event){
activateSort(this,[sortOldButton,sortStarButton]);
});
};
if(sortStarButton) {
sortStarButton.addEventListener('click', function(event) {
activateSort(this,[sortOldButton,sortNewButton]);
});
};
//Function to activate Sort
var activateSort = function(turnOn, offArr) {
turnOn.classList.add("highlight");
offArr.forEach(function(x) {
x.classList.remove("highlight");
});
if (turnOn == sortOldButton) {
var sortFunction = function(a,b) {
return parseInt(a.id)-parseInt(b.id);
};
} else if (turnOn == sortNewButton) {
var sortFunction = function(a,b) {
return parseInt(b.id)-parseInt(a.id);
};
} else {
var sortFunction = function(a,b) {
return b.priority-a.priority;
};
};
var newState = todoFunctions.sortTodos(state, sortFunction);
update(newState);
}
// you should not need to change this function
var update = function(newState) {
state = newState;
renderState(state);
};
// you do not need to change this function
var renderState = function(state) {
var todoListNode = document.createElement('ul');
state.forEach(function(todo) {
todoListNode.appendChild(createTodoNode(todo));
});
//Stores the state in local memory
localStorage.setItem('state', JSON.stringify(state));
// you may want to add a class for css
container.replaceChild(todoListNode, container.firstChild);
};
if (container) renderState(state);
})();