-
+
+
Залишилося
+-
+
+
Куплено
+-
+
+
diff --git a/buy-list.js b/buy-list.js new file mode 100644 index 00000000..5e767b93 --- /dev/null +++ b/buy-list.js @@ -0,0 +1,196 @@ +//2 +let products = [ + { + id: 1, name: "Помідори", amount: 2, isBought:true + }, + { + id: 2, name: "Печиво", amount: 2, isBought:false + }, + { + id: 3, name: "Сир", amount: 1, isBought:false + } +]; + +const addItem = document.getElementById("add-item"); +const item = document.getElementById("item"); +const productListItems = document.getElementById("product-list-items"); +const leftList = document.getElementById("left-list"); +const boughtList = document.getElementById("bought-list"); + +//1 +addItem.addEventListener("submit", (e)=>{ + e.preventDefault(); + + const name = item.value.trim(); + if(!name ) + return; + + const newItem={ + id: Date.now(), + name: name, + amount: 1, + isBought: false + }; + + products.push(newItem); + + item.value=""; + item.focus(); + render(); +}); + +function render(){ + + productListItems.innerHTML = ""; + leftList.innerHTML = ""; + boughtList.innerHTML = ""; + + products.forEach(product => { + + const li = document.createElement('li'); + + //5 + const nameSpan = document.createElement('span'); + nameSpan.className = 'product-item'; + nameSpan.textContent = product.name; + + if (product.isBought) { + nameSpan.style.textDecoration = 'line-through'; + } + else + { + nameSpan.style.cursor = 'pointer'; + nameSpan.addEventListener('click', () => { + + const inputEdit = document.createElement('input'); + inputEdit.type = 'text'; + inputEdit.value = product.name; + inputEdit.className = 'product-item-input'; + + li.replaceChild(inputEdit, nameSpan); + inputEdit.focus(); + + const saveName = () => { + const newName = inputEdit.value.trim(); + if (newName) { + product.name = newName; + } + + render(); + }; + + inputEdit.addEventListener('blur', saveName); + inputEdit.addEventListener('keydown', (e) => { + if (e.key === 'Enter') saveName(); + }); + }); + } + + li.appendChild(nameSpan); + + //6 + if (!product.isBought) { + const btnMinus = document.createElement('button'); + btnMinus.className = 'btn-minus'; + btnMinus.textContent = '-'; + btnMinus.setAttribute('data-tooltip', 'Зменшити кількість'); + + if (product.amount <= 1) { + btnMinus.style.opacity = '0.6'; + btnMinus.disabled = true; + btnMinus.style.cursor = 'not-allowed'; + } + else + { + btnMinus.addEventListener('click', () => { + product.amount--; + render(); + }); + } + li.appendChild(btnMinus); + + const amountSpan = document.createElement('span'); + amountSpan.className = 'amount'; + amountSpan.textContent = product.amount; + li.appendChild(amountSpan); + + const btnPlus = document.createElement('button'); + btnPlus.className = 'btn-plus'; + btnPlus.textContent = '+'; + btnPlus.setAttribute('data-tooltip', 'Збільшити кількість'); + btnPlus.addEventListener('click', () => { + product.amount++; + render(); + }); + + li.appendChild(btnPlus); + + } + else + { + const amountSpan = document.createElement('span'); + amountSpan.className = 'amount'; + amountSpan.textContent = product.amount; + amountSpan.style.marginLeft = 'auto'; + li.appendChild(amountSpan); + } + + //4 + const btnBought = document.createElement("button") + btnBought.className = "btn-bought"; + btnBought.textContent = product.isBought ? "Куплено" : "Не куплено"; + btnBought.setAttribute("data-tooltip", product.isBought ? "Призначити як не куплене" : "Призначити як куплене"); + btnBought.addEventListener("click", ()=> { + product.isBought = !product.isBought; + render(); + }); + + li.appendChild(btnBought); + + //3 + if(!product.isBought){ + const btnRemove = document.createElement("button"); + + btnRemove.className = "btn-remove"; + btnRemove.textContent = "x"; + btnRemove. setAttribute("data-tooltip", "Прибрати товар"); + btnRemove.addEventListener("click", ()=> { + products = products.filter(products => products.id !==product.id); + render(); + }); + + li.appendChild(btnRemove); + } + + productListItems.appendChild(li); + + //7 + if(!product.isBought){ + const lItem = document.createElement("li"); + lItem.className="l-item"; + lItem.textContent=product.name; + + const lAmount = document.createElement("span"); + lAmount.className="l-amount"; + lAmount.textContent=product.amount; + + lItem.appendChild(lAmount); + leftList.appendChild(lItem); + } + else + { + const bItem = document.createElement("li"); + bItem.className = "b-item"; + bItem.textContent=product.name; + + const bAmount = document.createElement("span"); + bAmount.className="b-amount"; + bAmount.textContent=product.amount; + + bItem.appendChild(bAmount); + boughtList.appendChild(bItem); + } +}); +} + +render(); \ No newline at end of file diff --git a/index.html b/index.html index 5971bf7d..5bbe36b9 100644 --- a/index.html +++ b/index.html @@ -1,16 +1,69 @@ - -
+ + -