-
Notifications
You must be signed in to change notification settings - Fork 2
/
ToDo.js
67 lines (59 loc) · 1.82 KB
/
ToDo.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
function ToDo(){
this.todo = [];
}
ToDo.prototype.addTodo= function(item){
this.todo.push(item)
}
ToDo.prototype.getItems= function(){
return this.todo
}
ToDo.prototype.delete = function(id){
this.todo = this.todo.filter(item => item.id !== id)
}
ToDo.prototype.complete = function(id){
this.todo.find(item => item.id == id).complete = true;
}
// const todo = new ToDo();
// todo.addTodo({complete: false,id : 1, title: 'aurel'})
// todo.addTodo({ complete: false, id: 2, title: 'bob'})
// todo.complete(1)
// console.log(todo.getItems(1))
function DomManipulation(){}
DomManipulation.prototype.init = function(){
const form = document.createElement('form');
const input = document.createElement('input')
const ul = document.createElement('ul')
input.id = "AddItemInput"
form.id="addItemForm"
input.placeholder = "Add an item"
form.appendChild(input);
return {
form, ul
}
}
DomManipulation.prototype.displayItem = function(item){
const li = document.createElement('li');
li.innerText = item.title
return li;
}
DomManipulation.prototype.createUniqueId = function(){
}
DomManipulation.prototype.addTodoEvent = function(form, createTodo, unorderedList){
const displayItem = this.displayItem;
const id = new Date().getUTCMilliseconds();
form.addEventListener('submit', function(e){
e.preventDefault();
const input = document.querySelector('input').value
const item = {complete: false,id : id, title: input}
createTodo(item);
unorderedList.appendChild(displayItem(item))
})
}
const dom = new DomManipulation();
document.querySelector('#app').appendChild(dom.init().form)
document.querySelector('#app').appendChild(dom.init().ul)
const todo = new ToDo();
dom.addTodoEvent(
document.getElementById('addItemForm'),
todo.addTodo.bind(todo),
document.querySelector('ul'))