-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
100 lines (81 loc) · 2.75 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
function changeTheme(){
let themeColor = document.body
let iconTheme = document.getElementById("iconTheme")
themeColor.classList.toggle("dark-mode")
if(iconTheme.innerHTML == "nightlight"){
iconTheme.innerHTML = "light_mode"
}else{
iconTheme.innerHTML = "nightlight"
}
}
const listItems = document.querySelector("#items-list")
const textBox = document.querySelector("#text-input")
const addButton = document.querySelector("#submit-button")
const listOptions = document.querySelector("#listOptions")
addButton.addEventListener('click', () => {
const textToList = textBox.value
if(textToList == ""){
swal.fire({
title: 'Não foi possível adicionar',
text: 'Digite algo para adicionar na sua lista',
icon: 'error'
})
}else{
textBox.value = ''
listItems.appendChild(addTask(textToList))
showListOptions()
textBox.focus()
}
})
function addTask(textToList){
const elementLi = document.createElement('li')
const elementSpan = document.createElement('span')
elementSpan.setAttribute('id','task')
elementSpan.textContent = textToList
elementLi.className = 'toDo'
elementLi.appendChild(elementSpan)
elementLi.appendChild(removeButton())
elementSpan.addEventListener('click',function(){
if(this.id === 'task'){
if(this.parentNode.className === 'toDo'){
this.parentNode.className = 'done'
}else{
this.parentNode.className = 'toDo'
}
}
})
return elementLi;
}
function removeButton(){
const removeButton = document.createElement('button')
removeButton.setAttribute('id','delete')
removeButton.textContent = '✘'
removeButton.className = 'remove'
removeButton.addEventListener('click', function(){
listItems.removeChild(this.parentNode)
showListOptions()
})
//https://www.toptal.com/designers/htmlarrows/math/
return removeButton;
}
function showListOptions(){
const elementSpan = document.querySelector('#task')
if(elementSpan === null){
listOptions.setAttribute('hidden','hidden')
}else{
listOptions.removeAttribute('hidden')
}
}
listOptions.addEventListener('change', function(){
if(listOptions.selectedIndex === 1 || listOptions.selectedIndex === 2){
const taskVector = document.querySelectorAll('#task')
for(task of taskVector){
task.dispatchEvent(new Event('click'))
}
}else if(listOptions.selectedIndex === 3){
const deleteButtonVector = document.querySelectorAll("#delete")
for(task of deleteButtonVector){
task.dispatchEvent(new Event('click'))
}
}
})