-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
130 lines (101 loc) · 4.63 KB
/
app.js
File metadata and controls
130 lines (101 loc) · 4.63 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// obtenemos los botones principales, agregar, editar y borrar
let btnAddNote = document.getElementById('btnAddNote'),
btnEditNote = document.getElementById('btnEditNote'),
btnDeleteNote = document.getElementById('btnDeleteNote');
btnClearNote = document.getElementById('btnClearNote');
//Evento para crear la nota
btnAddNote.addEventListener('click', createNote)
// Evento para actualizar nota
btnEditNote.addEventListener('click', editNote)
// Evento para borrar nota
btnDeleteNote.addEventListener('click', deleteNote)
// Evento para Limpiar LocalStorage
btnClearNote.addEventListener('click', clearNote)
//Funcion para crear un article con sus clases y contenido
function createNote(e) {
e.preventDefault();
let titleNote = document.getElementById('addTitleNote').value.trim(),
textNote = document.getElementById('addTextNote').value.trim();
if (titleNote != '' && titleNote != null && textNote != '' && textNote != null) {
let notes = {
titleNote,
textNote
}
if (localStorage.getItem('notes') === null) {
let notesStorage = []
notesStorage.push(notes)
localStorage.setItem('notes', JSON.stringify(notesStorage))
} else {
let notesStorage = JSON.parse(localStorage.getItem('notes'))
notesStorage.push(notes)
localStorage.setItem('notes', JSON.stringify(notesStorage))
}
noteView()
document.getElementById('formAddNotes').reset()
} else {
alert('Debes ingresar datos reales')
}
}
// Funcion para agregar nota al DOM, y agregar elementos option para editar y eliminar
function noteView() {
let notesStorage = JSON.parse(localStorage.getItem('notes'))
let notesContainer = document.getElementById('notes')
let listContainer = document.getElementById('editNote')
let listDeleteContainer = document.getElementById('deleteNote')
notesContainer.innerHTML = ''
listContainer.innerHTML = ''
listDeleteContainer.innerHTML = ''
for (let i = 0; i < notesStorage.length; i++){
let title = notesStorage[i].titleNote
let text = notesStorage[i].textNote
notesContainer.innerHTML += `<article class="note">
<header>
<h2>${title}</h2>
</header>
<div>
<p>${text}</p>
</div>
</article>`
listContainer.innerHTML += `<option value="${i}">${title}</option>`
listDeleteContainer.innerHTML += `<option value="${i}">${title}</option>`
}
}
//Función para editar notas
function editNote() {
let indexSelect = document.getElementById('editNote').selectedOptions[0].value //guardamos indice
let notesIndexStorage = JSON.parse(localStorage.getItem('notes'))[indexSelect] //con el indice seleccionamos la nota
let notesGetStorage = JSON.parse(localStorage.getItem('notes')) //obtenemos todas las notas
let optionsEdit = document.getElementById('editNote').selectedOptions[0].text //obtenemos la seleccion del usuario
if (notesIndexStorage.titleNote == optionsEdit) {
let titleNote = prompt('Nuevo titulo')
let textNote = prompt('Nuevo texto')
notesUpStorage = {
titleNote,
textNote
}
notesGetStorage.splice(indexSelect, 1, notesUpStorage) // reemplazamos por los nuevos valores
localStorage.setItem('notes', JSON.stringify(notesGetStorage))
}
noteView()
}
//Funcion para borrar notas
function deleteNote() {
let indexSelect = document.getElementById('deleteNote').selectedOptions[0].value //guardamos indice
let notesIndexStorage = JSON.parse(localStorage.getItem('notes'))[indexSelect] //con el indice seleccionamos la nota
let notesGetStorage = JSON.parse(localStorage.getItem('notes')) //obtenemos todas las notas
let optionsDelete = document.getElementById('deleteNote').selectedOptions[0].text //obtenemos la seleccion del usuario
if (notesIndexStorage.titleNote == optionsDelete) {
notesGetStorage.splice(indexSelect, 1) // Eliminamos la nota
localStorage.setItem('notes', JSON.stringify(notesGetStorage))
}
noteView()
}
//Funcion para Limpiar Localstorage
function clearNote() {
localStorage.clear()
location.reload(true)
}
// Comprobamos si localstorage contiene algo y luego mostramos las notas
if (localStorage.getItem('notes') != null) {
noteView()
}