-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
200 lines (163 loc) · 5.79 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
// const addedRows = [];
// ADD NEW ITEMS
function addItem() {
const mainContainer = document.getElementById("mainContainer");
const texto = document.getElementById("texto");
const remove = document.getElementById("remove");
const newRow = document.createElement("div");
newRow.className = "mainContainer";
newRow.style.display = "flex";
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.className = "checkbox";
checkbox.style.display = "flex";
const itemName = document.createElement("input");
itemName.type = "text";
itemName.className = "itemName";
itemName.placeholder = "Item";
const qty = document.createElement("input");
qty.type = "number";
qty.className = "quantity";
qty.placeholder = "Qty";
newRow.appendChild(checkbox);
newRow.appendChild(itemName);
newRow.appendChild(qty);
mainContainer.appendChild(newRow);
addedRows.push(newRow);
texto.textContent = "";
// LINE-THROUGH COMPLETED ITEMS
checkbox.addEventListener("change", function () {
if (checkbox.checked) {
itemName.style.textDecoration = "line-through";
qty.style.textDecoration = "line-through";
} else {
itemName.style.textDecoration = "none";
qty.style.textDecoration = "none";
}
});
}
// REMOVE LAST ROW
function removeRow() {
if (addedRows.length > 0) {
const lastRow = addedRows.pop();
lastRow.remove();
}
}
const fechaActual = new Date();
// SAVING A LIST
function saveList() {
const listName = prompt("Introduce a list name: ");
const listNameFinal = listName || fechaActual;
const mainContainer = document.getElementById("mainContainer");
const items = mainContainer.querySelectorAll(".mainContainer");
// CREATE A LIST TO STORE ELEMENTS
const addedRows = [];
items.forEach(function (item) {
const checkbox = item.querySelector(".checkbox");
const itemName = item.querySelector(".itemName");
const qty = item.querySelector(".quantity");
// ADD AN ELEMENT TO THE LIST
addedRows.push({
checkboxChecked: checkbox.checked,
itemName: itemName.value,
qty: qty.value,
});
});
// SAVING A LIST
// CREATE AN OBJECT IN WHICH I STORE THE LIST NAME AND THE ITEM (addedRows) IT CONTAINS
const groceriesList = {
name: listNameFinal,
items: addedRows,
};
localStorage.setItem(listNameFinal, JSON.stringify(groceriesList));
}
let addedRows = [];
// LOAD AND SHOW STORED LIST
const listShow = document.getElementById("storedList");
const listContainer = document.getElementById("listContainer");
console.log("Running properly");
function loadList() {
listContainer.style.display = "flex";
listShow.innerHTML = ""; // REMOVE PREVIOUS CONTENT
const listNameFinalNames = Object.keys(localStorage);
for (const listNameFinal of listNameFinalNames) {
const storedList = localStorage.getItem(listNameFinal);
if (storedList) {
const groceriesList = JSON.parse(storedList);
const name = groceriesList.name;
// SHOWING LIST NAMES
const listItem = document.createElement("div");
listItem.textContent = "List: " + name;
listShow.appendChild(listItem);
listItem.dataset.listName = name;
listItem.addEventListener("click", function () {
const selectedListName = listItem.dataset.listName;
cargarItemsDeLista(selectedListName);
});
}
}
}
function cargarItemsDeLista(selectedListName) {
// LOAD THE SELECTED LIST FROM LOCAL
const storedList = localStorage.getItem(selectedListName);
if (storedList) {
const groceriesList = JSON.parse(storedList);
addedRows = groceriesList.items || [];
// Mostrar los elementos de la lista en la interfaz de usuario
const mainContainer = document.getElementById("mainContainer");
mainContainer.innerHTML = ""; // CLEAN PREVIOUS CONTENT
for (const item of addedRows) {
// CREATE ELEMENTS FOR EACH ITEM OF THE LIST
const newRow = document.createElement("div");
newRow.className = "mainContainer";
newRow.style.display = "flex";
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.className = "checkbox";
checkbox.style.display = "flex";
if (item.checkboxChecked) {
checkbox.checked = true;
}
const itemName = document.createElement("input");
itemName.type = "text";
itemName.className = "itemName";
itemName.placeholder = "Item";
itemName.value = item.itemName || "";
const qty = document.createElement("input");
qty.type = "number";
qty.className = "quantity";
qty.placeholder = "Qty";
qty.value = item.qty || "";
newRow.appendChild(checkbox);
newRow.appendChild(itemName);
newRow.appendChild(qty);
mainContainer.appendChild(newRow);
// ADD CHECK FOR LINE THROUGH EVENT
checkbox.addEventListener("change", function () {
if (checkbox.checked) {
itemName.style.textDecoration = "line-through";
qty.style.textDecoration = "line-through";
item.checkboxChecked = true;
} else {
itemName.style.textDecoration = "none";
qty.style.textDecoration = "none";
item.checkboxChecked = false;
}
});
}
}
}
function closeList() {
listContainer.style.display = "none";
}
//CLEAN BUTTON
const clearButton = document.getElementById("clearButton");
clearButton.addEventListener("click", function () {
const confirm = window.confirm(
"Would you like to clean the memory and delete the stored list?"
);
if (confirm === true) {
localStorage.clear();
listShow.innerHTML = "";
}
});