-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (118 loc) · 3.72 KB
/
index.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
// ---------------------------------------------------------------------------
// ELEMENTS
const taskListDOM = document.getElementById('task-list')
// ---------------------------------------------------------------------------
// STORAGE
const Storage = {
set: value => {
if (value !== null) localStorage.setItem('tasks', JSON.stringify(value))
},
get: () => {
const data = JSON.parse(localStorage.getItem('tasks'))
if (data !== null) return data
else return []
}
}
// ---------------------------------------------------------------------------
// PROGRAM
const App = {
// ---------------------------------------------------------------------------
// DATA
nextId: 1, // App.nextId
data: Storage.get(), // App.data
// ---------------------------------------------------------------------------
// DISPLAY DATA
// App.display()
display: (data = App.data) => {
taskListDOM.innerHTML = ''
data.forEach(item => {
const li = document.createElement('li')
li.innerHTML = `<span ${
item.completed ? 'class="completed"' : ''
} onclick="App.toggleCompleted(${item.id})">${item.text}</span>
<button onclick="App.edit(${item.id})">✎ EDIT</button>
<button onclick="App.remove(${item.id})">✖ REMOVE</button>`
taskListDOM.appendChild(li)
})
},
// ---------------------------------------------------------------------------
// SUBMIT NEW DATA
add: event => {
event.preventDefault()
const newTask = {
id: App.nextId,
text: document.getElementById('add-text').value,
completed: false
}
if (newTask !== '') {
// Push new data
App.data.push(newTask)
// Set new data into localStorage
Storage.set(App.data)
App.display()
document.getElementById('add-text').value = ''
App.nextId++
}
},
// ---------------------------------------------------------------------------
// REMOVE TASK BY ID
remove: id => {
const modifiedTasks = App.data.filter(item => {
return item.id !== id
})
App.data = modifiedTasks
Storage.set(App.data)
App.display()
},
// ---------------------------------------------------------------------------
// EDIT TASK TEXT
edit: id => {
const textInput = prompt('Edit task to...')
if (textInput !== null) {
const modifiedTasks = App.data.map(item => {
if (item.id === id) {
item.text = textInput
}
return item
})
App.data = modifiedTasks
Storage.set(App.data)
App.display()
}
},
// ---------------------------------------------------------------------------
// SEARCH TASK AFTER SUBMIT
search: event => {
event.preventDefault()
const keyword = document.getElementById('search-text').value
const foundTasks = App.data.filter(item => {
return item.text.toLowerCase().includes(keyword.toLowerCase())
})
App.display(foundTasks)
},
// ---------------------------------------------------------------------------
// SEARCH TASK AUTOMATICALLY
searchAuto: () => {
const keyword = document.getElementById('search-text').value
const foundTasks = App.data.filter(item => {
return item.text.toLowerCase().includes(keyword.toLowerCase())
})
App.display(foundTasks)
},
// ---------------------------------------------------------------------------
// TOGGLE TASK COMPLETED
toggleCompleted: id => {
const modifiedTasks = App.data.map(item => {
if (item.id === id) {
item.completed = !item.completed
}
return item
})
App.data = modifiedTasks
Storage.set(App.data)
App.display()
}
}
// ---------------------------------------------------------------------------
// RUN
App.display()