-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
114 lines (98 loc) · 2.93 KB
/
Copy pathscript.js
File metadata and controls
114 lines (98 loc) · 2.93 KB
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
const fecha = document.querySelector('#fecha')
const lista = document.querySelector('#lista')
const input = document.querySelector('#input')
const botonEnter = document.querySelector('#enter')
const check = 'fa-check-circle'
const uncheck = 'fa-circle'
const lineThrough = 'line-through'
let id
let LIST
//creacion de fecha
const FECHA = new Date()
fecha.innerHTML = FECHA.toLocaleDateString('es-MX',{weekday:'long',month:'long',day:'numeric'})
//funcion agregar tarea
function agregarTarea(tarea,id,realizado,eliminado){
if(eliminado){return}
const REALIZADO = realizado ?check : uncheck
const LINE = realizado ?lineThrough : ''
const elemento =`
<li id=elemento>
<i class="far ${REALIZADO}" data="realizado" id=${id}></i>
<p class="text ${LINE}">${tarea}</p>
<i class="fas fa-trash or" data="eliminado" id=${id}></i>
</li>
`
lista.insertAdjacentHTML("beforeend",elemento)
}
//Funcion de tarea Realizada
function tareaRealizada(element){
element.classList.toggle(check)
element.classList.toggle(uncheck)
element.parentNode.querySelector('.text').classList.toggle(lineThrough)
LIST[element.id].realizado = LIST[element.id].realizado ?false : true
}
//Funcion de tarea eliminado
function tareaElimada(element){
element.parentNode.parentNode.removeChild(element.parentNode)
LIST[element.id].eliminado = true
};
botonEnter.addEventListener('click',()=> {
const tarea = input.value
if(tarea){
agregarTarea(tarea,id,false,false)
LIST.push({
nombre: tarea,
id:id,
realizado:false,
eliminado:false
})
}
localStorage.setItem('TODO',JSON.stringify(LIST))
input.value=''
id++
console.log(LIST)
})
document.addEventListener('keyup',function(event){
if(event.key=='Enter'){
const tarea = input.value
if(tarea){
agregarTarea(tarea,id,false,false)
LIST.push({
nombre: tarea,
id:id,
realizado:false,
eliminado:false
})
}
localStorage.setItem('TODO',JSON.stringify(LIST))
input.value=''
id++
console.log(LIST)
}
})
lista.addEventListener('click',function(event){
const element = event.target
const elementData = element.attributes.data.value
if(elementData ==='realizado'){
tareaRealizada(element)
}
else if(elementData ==='eliminado'){
tareaElimada(element)
}
localStorage.setItem('TODO',JSON.stringify(LIST))
})
//local storage get item
let data = localStorage.getItem('TODO')
if(data){
LIST = JSON.parse(data)
id = LIST.length
cargarLista(LIST)
}else{
LIST = []
id = 0
}
function cargarLista(DATA){
DATA.forEach(function(i){
agregarTarea(i.nombre,i.id,i.realizado,i.eliminado)
})
}