-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
251 lines (197 loc) · 4.61 KB
/
app.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const todolistEl = document.querySelector('.todolist')
const inputEl = document.querySelector('#text-box')
const infoEl = document.querySelector('.amt')
const btnNew = document.querySelector('.btn-new-task')
const btnClear = document.querySelector('.btn-clear-all')
const btnDelete = document.querySelector('.btn-delete')
const radioEls = document.querySelectorAll('[name=radio]')
// construtor
const customPrototypeOfTheDatetime = {
getNowDateFormated: function() {
const dayNumber = this.now.getDate()
const dayName = this.dayNames[this.now.getDay()]
const monthName = this.monthNames[this.now.getMonth()]
const fullYear = this.now.getFullYear()
return {dayName, dayNumber, monthName, fullYear}
},
getNowTime: function() {
const hours = this.now.getHours()
const seconds = this.now.getMinutes()
return {
hh: this.getUnit(hours),
mm: this.getUnit(seconds)
}
}
}
function DateTime() {
this.now = new Date()
this.dayNames = [
'sunday',
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturnday'
]
this.monthNames = [
'january',
'february',
'march',
'april',
'may',
'june',
'july',
'august',
'september',
'october',
'november',
'december'
]
this.getUnit = unit => unit <= 10 ? `0${unit}` : unit
}
DateTime.prototype = customPrototypeOfTheDatetime
// objeto de principal
let todoApp = {
tasks: [],
reverse: true,
currentId: 0,
}
// local storage
function getLocalStorage() {
const data = localStorage.getItem('todo-app')
if (!(data === null)) { todoApp = JSON.parse(data) }
}
function updateLocalStorage() {
const data = JSON.stringify(todoApp)
localStorage.setItem('todo-app', data)
}
// visualização
function generateTemplateForTasks(tasks) {
return tasks.map(({id, content, created, pending}) => {
const {date, time} = created
const inputCheck = `
<input
type="checkbox"
name="task"
id="radio-${id}"
onInput="changePending(this, ${id})"
${pending ? '' : 'checked'}
/>
<label for="radio-${id}" class="check"></label>
`
return `
<li>
${inputCheck}
<div class="content-box">
<span class="content">${content}</span>
<span class="date">${date} - ${time}</span>
<button
class="btn btn-delete"
onClick="deleteTask(${id})"
>
<img class="icon" src="img/trash.svg" alt="icon">
</button>
</div>
</li>
`
}).join('')
}
function pendingTasks(tasks) {
infoEl.textContent = tasks.filter(({pending}) => {
return pending
}).length
}
function changePending(inputCheckEl, ID) {
const task = todoApp.tasks.filter(({id}) => id === ID)[0]
task.pending = !inputCheckEl.checked
pendingTasks(todoApp.tasks)
updateLocalStorage()
}
function addTasksAndInfoInToDOM() {
const {tasks, reverse} = todoApp
const allTasks = reverse ?
tasks.slice().reverse() : tasks
const template =
generateTemplateForTasks(allTasks)
if (tasks.length == 0) {
todolistEl.innerHTML = `
<li><span class="no-tasks">No tasks</span></li>
`
return
}
pendingTasks(allTasks)
todolistEl.innerHTML = template
}
// atualização informações
function update() {
inputEl.value = ''
todoApp.currentId++
updateLocalStorage()
addTasksAndInfoInToDOM()
}
// ordem dos itens
function ordering(event) {
const {id} = event.target
todoApp.reverse = id === 'reverse' ? true : false
update()
}
function checkOrder() {
radioEls.forEach(radioEl => {
radioEl.checked = radioEl.id === 'reverse' ?
todoApp.reverse : !todoApp.reverse
})
}
// controle de tarefas
function createTask() {
const {currentId} = todoApp
const content = inputEl.value.trim()
if (!content) {
alert('Task field cannot be empty')
throw new Error('Task field cannot be empty')
}
const date = new DateTime()
const {
dayName,
dayNumber,
monthName,
fullYear
} = date.getNowDateFormated()
const {hh, mm} = date.getNowTime()
const task = {
id: currentId + 1,
content: content,
created: {
date:
`${dayName}, ${monthName} ${dayNumber} ${fullYear}`,
time: `${hh}:${mm}`
},
pending: true
}
todoApp.tasks.push(task)
update()
}
// index definido no botão gerado pelo script
function deleteTask(index) {
const newArray = todoApp.tasks.filter(({id}) => {
return !(id === index)
})
todoApp.tasks = newArray
update()
}
function clearAll() {
todoApp.tasks = []
todoApp.currentId = 0
update()
}
// inicializador
function init() {
getLocalStorage()
checkOrder()
addTasksAndInfoInToDOM()
}
window.addEventListener('load', init)
btnNew.addEventListener('click', createTask)
btnClear.addEventListener('click', clearAll)
radioEls.forEach(radioEl =>
radioEl.addEventListener('input',ordering))