From d4529e6bdc0df8380306b9499a38467ebe7091f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Thu, 20 Aug 2026 19:53:50 +0200 Subject: [PATCH] fix in int up --- shopping_app/static/js/functions.js | 43 +++++++++++++++++++++++++- shopping_app/static/js/mass_add.js | 23 +++++++++++--- shopping_app/templates/list.html | 4 +-- shopping_app/templates/list_share.html | 4 +-- 4 files changed, 64 insertions(+), 10 deletions(-) diff --git a/shopping_app/static/js/functions.js b/shopping_app/static/js/functions.js index a38deec..d9f6a2d 100644 --- a/shopping_app/static/js/functions.js +++ b/shopping_app/static/js/functions.js @@ -90,6 +90,43 @@ function quantityLabel(value, unit) { return safeUnit === 'szt' ? `x${safeValue}` : `${safeValue} ${safeUnit}`; } +function quantityStepBase(value) { + const parsed = parseQuantityInput(value, 1); + const fraction = parsed - Math.floor(parsed); + + if (Math.abs(fraction) < 0.0000001) { + return 1; + } + + return Math.max(0.001, Math.round(fraction * 1000) / 1000); +} + +function syncQuantityStepBase(input) { + if (!input) return; + + input.step = '1'; + input.min = formatQuantityValue(quantityStepBase(input.value)); +} + +function setupQuantityStepper(input) { + if (!input || input.dataset.quantityStepperReady === 'true') return; + + input.dataset.quantityStepperReady = 'true'; + syncQuantityStepBase(input); + input.addEventListener('input', () => syncQuantityStepBase(input)); + input.addEventListener('focus', () => syncQuantityStepBase(input)); +} + +function setupQuantitySteppers(root = document) { + root.querySelectorAll('#newQuantity, #editItemQuantity').forEach(setupQuantityStepper); +} + +if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', () => setupQuantitySteppers()); +} else { + setupQuantitySteppers(); +} + function addItem(listId) { const name = document.getElementById('newItem').value; const quantityInput = document.getElementById('newQuantity'); @@ -108,7 +145,10 @@ function addItem(listId) { }); document.getElementById('newItem').value = ''; - if (quantityInput) quantityInput.value = 1; + if (quantityInput) { + quantityInput.value = 1; + syncQuantityStepBase(quantityInput); + } if (unitInput) unitInput.value = 'szt'; document.getElementById('newItem').focus(); } @@ -158,6 +198,7 @@ function openEditItemModal(event, id, oldName, oldQuantity, oldUnit = 'szt') { nameInput.value = String(oldName ?? '').trim(); quantityInput.value = formatQuantityValue(oldQuantity); + syncQuantityStepBase(quantityInput); unitInput.value = oldUnit || 'szt'; const modal = bootstrap.Modal.getOrCreateInstance(modalEl); diff --git a/shopping_app/static/js/mass_add.js b/shopping_app/static/js/mass_add.js index 86b07ce..5a069db 100644 --- a/shopping_app/static/js/mass_add.js +++ b/shopping_app/static/js/mass_add.js @@ -150,12 +150,13 @@ document.addEventListener('DOMContentLoaded', function () { const qty = document.createElement('input'); qty.type = 'number'; qty.min = 0.001; - qty.step = 0.001; + qty.step = 1; qty.value = initialValue || 1; qty.className = 'form-control text-center p-1 rounded'; qty.style.width = '62px'; qty.style.margin = '0 2px'; qty.title = 'Ilość / waga / objętość'; + setupQuantityStepper(qty); const plusBtn = document.createElement('button'); plusBtn.type = 'button'; @@ -165,8 +166,14 @@ document.addEventListener('DOMContentLoaded', function () { const unit = makeUnitSelect(); unit.value = initialUnit || 'szt'; - minusBtn.onclick = () => { qty.value = Math.max(0.001, readQuantity(qty) - 1); }; - plusBtn.onclick = () => { qty.value = readQuantity(qty) + 1; }; + minusBtn.onclick = () => { + qty.value = Math.max(0.001, readQuantity(qty) - 1); + syncQuantityStepBase(qty); + }; + plusBtn.onclick = () => { + qty.value = readQuantity(qty) + 1; + syncQuantityStepBase(qty); + }; qtyWrapper.append(minusBtn, qty, plusBtn, unit); li.appendChild(qtyWrapper); @@ -219,12 +226,13 @@ document.addEventListener('DOMContentLoaded', function () { const qty = document.createElement('input'); qty.type = 'number'; qty.min = 0.001; - qty.step = 0.001; + qty.step = 1; qty.value = 1; qty.className = 'form-control text-center p-1 rounded'; qty.style.width = '50px'; qty.style.margin = '0 2px'; qty.title = 'Ilość / waga / objętość'; + setupQuantityStepper(qty); const unit = makeUnitSelect(); @@ -235,9 +243,11 @@ document.addEventListener('DOMContentLoaded', function () { minusBtn.onclick = () => { qty.value = Math.max(0.001, readQuantity(qty) - 1); + syncQuantityStepBase(qty); }; plusBtn.onclick = () => { qty.value = readQuantity(qty) + 1; + syncQuantityStepBase(qty); }; qtyWrapper.append(minusBtn, qty, plusBtn, unit); @@ -337,12 +347,13 @@ document.addEventListener('DOMContentLoaded', function () { const qty = document.createElement('input'); qty.type = 'number'; qty.min = 0.001; - qty.step = 0.001; + qty.step = 1; qty.value = 1; qty.className = 'form-control text-center p-1 rounded'; qty.style.width = '50px'; qty.style.margin = '0 2px'; qty.title = 'Ilość / waga / objętość'; + setupQuantityStepper(qty); const unit = makeUnitSelect(); @@ -353,9 +364,11 @@ document.addEventListener('DOMContentLoaded', function () { minusBtn.onclick = () => { qty.value = Math.max(0.001, readQuantity(qty) - 1); + syncQuantityStepBase(qty); }; plusBtn.onclick = () => { qty.value = readQuantity(qty) + 1; + syncQuantityStepBase(qty); }; qtyWrapper.append(minusBtn, qty, plusBtn, unit); diff --git a/shopping_app/templates/list.html b/shopping_app/templates/list.html index 0ef1aab..efb61bb 100644 --- a/shopping_app/templates/list.html +++ b/shopping_app/templates/list.html @@ -201,7 +201,7 @@
- + + placeholder="Ilość" min="0.001" step="1" value="1"> + min="0.001" step="1" value="1" {% if not current_user.is_authenticated %}disabled{% endif %}> +