forked from seagullua/JS-BuyList
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathfunctionality.js
More file actions
190 lines (167 loc) · 7.32 KB
/
functionality.js
File metadata and controls
190 lines (167 loc) · 7.32 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
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
document.addEventListener('DOMContentLoaded', () => {
const shoppingList = document.querySelector('.main-shopping-list-section');
const summaryList = document.querySelector('.shopping-summary-section');
const newItemNameInput = document.querySelector('.new-item-name-input');
shoppingList.addEventListener('click', (event) => {
const target = event.target;
const productItem = target.closest('.shopping-list-item');
let correspondingSummary = productItem
? summaryList.querySelector(`.summary-item[data-name="${productItem.dataset.name}"]`)
: null;
if (target.classList.contains('add-item-button')) {
handleAddItem(newItemNameInput, shoppingList, summaryList);
return;
}
if (!productItem) return;
if (target.classList.contains('decrease-btn')) {
decrementQuantity(productItem, correspondingSummary);
} else if (target.classList.contains('increase-btn')) {
incrementQuantity(productItem, correspondingSummary);
} else if (target.classList.contains('item-status-label')) {
toggleItemStatus(productItem, correspondingSummary, summaryList);
} else if (target.classList.contains('item-name-display') && !productItem.classList.contains('bought')) {
enableNameEdit(target, correspondingSummary);
} else if (target.classList.contains('remove-item-btn')) {
removeItem(productItem, correspondingSummary);
}
});
updateButtons();
});
function handleAddItem(input, shoppingList, summaryList) {
const itemName = input.value.trim();
if (!itemName) return;
if (isItemPresent(shoppingList, itemName, summaryList)) {
input.value = '';
return;
}
const newItem = createShoppingListItem(itemName);
shoppingList.appendChild(newItem);
const newSummaryItem = createSummaryItem(itemName, 1);
const remainingSummaryList = summaryList.querySelector('.summary-items-list');
if (remainingSummaryList) {
remainingSummaryList.appendChild(newSummaryItem);
}
input.value = '';
updateButtons();
}
function isItemPresent(shoppingList, itemName, summaryList) {
const items = shoppingList.querySelectorAll('.shopping-list-item');
for (const element of items) {
if (element.querySelector('.item-name-display').textContent === itemName) {
const summary = summaryList.querySelector(`.summary-item[data-name="${element.dataset.name}"]`);
incrementQuantity(element, summary);
return true;
}
}
return false;
}
function createShoppingListItem(itemName) {
const newItem = document.createElement('div');
newItem.className = 'shopping-list-item';
newItem.dataset.name = itemName;
newItem.innerHTML = `
<span class="item-name-display">${itemName}</span>
<div class="item-quantity-controls">
<button class="quantity-adjust-btn decrease-btn" data-tooltip="Відняти одиницю товару">-</button>
<span class="current-quantity-display">1</span>
<button class="quantity-adjust-btn increase-btn" data-tooltip="Додати одиницю товару">+</button>
</div>
<div class="item-status-and-actions">
<button class="item-status-label" data-tooltip="Змінити статус товару">Куплено</button>
<button class="remove-item-btn" data-tooltip="Видалити товар зі спикску">x</button>
</div>
`;
return newItem;
}
function createSummaryItem(itemName, quantity) {
const summaryItem = document.createElement('span');
summaryItem.className = 'summary-item';
summaryItem.dataset.name = itemName;
summaryItem.innerHTML = `
<span class="summary-item-name">${itemName}</span>
<span class="summary-item-quantity">${quantity}</span>
`;
return summaryItem;
}
function incrementQuantity(productItem, correspondingSummary) {
const quantityDisplay = productItem.querySelector('.current-quantity-display');
let quantity = parseInt(quantityDisplay.textContent);
quantityDisplay.textContent = quantity + 1;
if (correspondingSummary) {
correspondingSummary.querySelector('.summary-item-quantity').textContent = quantity + 1;
if (quantity < 2) productItem.querySelector('.decrease-btn').disabled = false;
}
}
function decrementQuantity(productItem, correspondingSummary) {
const quantityDisplay = productItem.querySelector('.current-quantity-display');
let quantity = parseInt(quantityDisplay.textContent);
if (quantity > 1) {
quantityDisplay.textContent = quantity - 1;
if (correspondingSummary)
correspondingSummary.querySelector('.summary-item-quantity').textContent = quantity - 1;
}
if (quantity < 3) productItem.querySelector('.decrease-btn').disabled = true;
}
function toggleItemStatus(productItem, correspondingSummary, summaryList) {
const statusBtn = productItem.querySelector('.item-status-label');
const sumLists = Array.from(summaryList.querySelectorAll('.summary-items-list'));
if (!correspondingSummary) {
correspondingSummary = createSummaryItem(
productItem.querySelector('.item-name-display').textContent,
productItem.querySelector('.current-quantity-display').textContent
);
}
if (productItem.classList.contains('bought')) {
statusBtn.textContent = "Куплено";
productItem.classList.remove('bought');
correspondingSummary.classList.remove('bought');
sumLists[0].appendChild(correspondingSummary);
if (sumLists[1].contains(correspondingSummary)) {
sumLists[1].removeChild(correspondingSummary);
}
} else {
statusBtn.textContent = "Не куплено";
productItem.classList.add('bought');
correspondingSummary.classList.add('bought');
sumLists[1].appendChild(correspondingSummary);
if (sumLists[0].contains(correspondingSummary)) {
sumLists[0].removeChild(correspondingSummary);
}
}
}
function enableNameEdit(target, correspondingSummary) {
const originalText = target.textContent;
const input = document.createElement('input');
input.className = 'item-name-edit-input';
input.type = 'text';
input.value = originalText;
target.replaceWith(input);
input.focus();
input.select();
input.addEventListener('blur', () => {
let label = input.value || originalText;
const newText = document.createElement('span');
newText.className = 'item-name-display';
newText.textContent = label;
input.replaceWith(newText);
if (correspondingSummary) {
correspondingSummary.querySelector('.summary-item-name').textContent = label;
}
});
}
function removeItem(productItem, correspondingSummary) {
productItem.remove();
if (correspondingSummary) {
correspondingSummary.remove();
}
}
function updateButtons() {
document.querySelectorAll('.shopping-list-item').forEach(element => {
let decBtn = element.querySelector('.decrease-btn');
if (parseInt(element.querySelector('.current-quantity-display').textContent) <= 1) {
decBtn.disabled = true;
} else {
decBtn.disabled = false;
}
});
}