-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
216 lines (166 loc) · 5.59 KB
/
script.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
'use strict'
const initApp = function() {
// state
// -------------------------------
let AppKey = ''
let mainContainer = null
let tasks = []
let newToDoName = ''
const maxInput = 25
// form
// ---------------------------------
// input
const renderNewTaskInput = function() {
const input = document.createElement('input')
input.classList.add('task-form__input')
input.value = newToDoName
input.addEventListener('input', function(e) {
const value = e.target.value
if(value.length > maxInput) {
input.maxLength = maxInput + 1
input.classList.add('input-error')
} else {
input.classList.remove('input-error')
}
newToDoName = value.slice(0,maxInput)
})
setTimeout(() => input.focus(),0)
return input
}
// button
const renderNewTaskButton = function() {
const button = document.createElement('button')
button.classList.add('task-form__button')
button.textContent = 'ADD TASK'
return button
}
// form
const renderNewTaskForm = function() {
const container = document.createElement('form')
container.classList.add('task-form')
const renderInput = renderNewTaskInput()
const renderButton = renderNewTaskButton()
container.addEventListener('submit', function(event) {
event.preventDefault()
if(newToDoName.length === 0) {
renderInput.classList.add('input-error')
return
} else {
renderInput.classList.remove('input-error')
}
const id = Math.floor(Math.random() * 1000)
tasks = tasks.concat({
id: id,
name: newToDoName,
isCompleted: false
})
newToDoName = ''
update()
})
container.appendChild(renderInput)
container.appendChild(renderButton)
return container
}
// List of tasks
// ===============================
const appendArray = function(array, container) {
array.forEach(el => {
container.appendChild(el)
});
}
// delete - button
const onTaskDelete = function(id) {
tasks = tasks.filter(task => task.id !== id)
update()
}
const renderButtonDelete = function(id) {
const button = document.createElement('button')
button.textContent = '[ X ]'
button.classList.add('task__button--button')
button.addEventListener('click', function() {
onTaskDelete(id)
})
return button
}
// task name - div
const renderTaskName = function() {
const taskName = document.createElement('div')
return taskName
}
// wrapper task
const renderTaskWrapper = function() {
const wrapper = document.createElement('div')
wrapper.classList.add('task__wrapper')
return wrapper
}
const renderTask = function(task) {
const container = document.createElement('li')
container.className = 'task__element'
const renderElementName = renderTaskName()
renderElementName.textContent = task.name
renderElementName.classList.add('task__wrapper--name')
if(task.isCompleted) {
renderElementName.classList.add('task__completed')
}
renderElementName.addEventListener('click', function() {
task.isCompleted = !task.isCompleted
update()
})
const elementButtonDelete = renderButtonDelete(task.id)
const wrapperElement = renderTaskWrapper()
wrapperElement.appendChild(renderElementName)
wrapperElement.appendChild(elementButtonDelete)
container.appendChild(wrapperElement)
return container
}
const renderTaskList = function(tasks) {
const container = document.createElement('ol')
container.className='task'
const tasksElements = tasks.map(task => {
return renderTask(task)
})
appendArray(tasksElements, container)
return container
}
// render
const render = function() {
const container = document.createElement('div')
container.classList.add('app')
const renderElementForm = renderNewTaskForm()
const renderTasksElements = renderTaskList(tasks)
container.appendChild(renderElementForm)
container.appendChild(renderTasksElements)
return container
}
const update = function() {
mainContainer.innerHTML = ''
const app = render()
mainContainer.appendChild(app)
saveLocalStorage()
}
const saveLocalStorage = function() {
const state = {
tasks: tasks,
newToDoName: newToDoName
}
localStorage.setItem(AppKey, JSON.stringify(state))
}
const loadLocalStorage = function() {
let state = localStorage.getItem(AppKey)
if(!state) return
state = JSON.parse(state)
tasks = state.tasks
newToDoName = state.newToDoName
}
const init = function(selector, key) {
const container = document.querySelector(selector)
mainContainer = container
if(!mainContainer) return console.log('Selector not exists')
AppKey = key
loadLocalStorage()
const app = render()
mainContainer.appendChild(app)
return mainContainer
}
return init
}