forked from FinUKMA/JS-BuyList
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
159 lines (140 loc) · 5 KB
/
script.js
File metadata and controls
159 lines (140 loc) · 5 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
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
//Задаємо змінні
document.addEventListener("DOMContentLoaded", () => {
const input = document.querySelector(".input-group input");
const addButton = document.querySelector(".input-group button");
const itemList = document.querySelector(".card:first-child");
const badgeLeft = document.querySelectorAll(".badge-list")[0];
const badgeBought = document.querySelectorAll(".badge-list")[1];
//Створення або завантаження списку
let items = JSON.parse(localStorage.getItem("buylist")) || [
{ name: "Помідори", count: 2, bought: true },
{ name: "Печиво", count: 2, bought: false },
{ name: "Сир", count: 1, bought: false },
];
//Збереження поточного списку
function saveState() {
localStorage.setItem("buylist", JSON.stringify(items));
}
//
function render() {
document.querySelectorAll(".item").forEach((el, i) => {
if (i > 0) el.remove();
});
badgeLeft.innerHTML = "";
badgeBought.innerHTML = "";
items.forEach((item, index) => {
const itemDiv = document.createElement("div");
itemDiv.className = "item";
const row = document.createElement("div");
row.className = "item-row";
// Назва товару
const nameSpan = document.createElement("span");
nameSpan.className = "item-name";
nameSpan.textContent = item.name;
if (item.bought) nameSpan.classList.add("bought");
else {
nameSpan.addEventListener("click", () => editName(nameSpan, index));
}
row.appendChild(nameSpan);
// Лічильник
const counter = document.createElement("div");
counter.className = "counter-wrapper";
const controls = document.createElement("div");
controls.className = "counter-controls";
if (!item.bought) {
const minus = document.createElement("button");
minus.textContent = "−";
minus.className = "btn red";
minus.setAttribute("data-tooltip", "Зменшити");
minus.disabled = item.count <= 1;
minus.onclick = () => {
item.count--;
saveState();
render();
};
const plus = document.createElement("button");
plus.textContent = "+";
plus.className = "btn green";
plus.setAttribute("data-tooltip", "Збільшити");
plus.onclick = () => {
item.count++;
saveState();
render();
};
controls.append(minus, createQty(item.count), plus);
} else {
controls.appendChild(createQty(item.count));
}
counter.appendChild(controls);
row.appendChild(counter);
// Дії
const actions = document.createElement("div");
actions.className = "actions";
const statusBtn = document.createElement("button");
statusBtn.className = "status-btn";
statusBtn.setAttribute("data-tooltip", item.bought ? "Позначити як не куплене" : "Позначити як куплене");
statusBtn.textContent = item.bought ? "Не куплено" : "Куплено";
statusBtn.onclick = () => {
item.bought = !item.bought;
saveState();
render();
};
actions.appendChild(statusBtn);
if (!item.bought) {
const removeBtn = document.createElement("button");
removeBtn.textContent = "×";
removeBtn.className = "btn remove";
removeBtn.setAttribute("data-tooltip", "Видалити");
removeBtn.onclick = () => {
items.splice(index, 1);
saveState();
render();
};
actions.appendChild(removeBtn);
}
row.appendChild(actions);
itemDiv.appendChild(row);
itemList.appendChild(itemDiv);
const badge = document.createElement("div");
badge.className = "badge";
badge.innerHTML = `${item.bought ? '<span><s>' + item.name + '</s></span>' : `<span>${item.name}</span>`} <span class="count">${item.count}</span>`;
(item.bought ? badgeBought : badgeLeft).appendChild(badge);
});
}
function createQty(count) {
const span = document.createElement("span");
span.className = "qty";
span.textContent = count;
return span;
}
// Зміна назви товару
function editName(span, index) {
const input = document.createElement("input");
input.type = "text";
input.value = items[index].name;
input.className = "item-name";
input.onblur = () => {
items[index].name = input.value.trim() || items[index].name;
saveState();
render();
};
span.replaceWith(input);
input.focus();
}
addButton.onclick = () => addItem();
input.addEventListener("keydown", e => {
if (e.key === "Enter") addItem();
});
//Додавання товару
function addItem() {
const name = input.value.trim();
if (name) {
items.push({ name, count: 1, bought: false });
input.value = "";
input.focus();
saveState();
render();
}
}
render();
});