fix in int up

This commit is contained in:
Mateusz Gruszczyński
2026-08-20 19:53:50 +02:00
parent fc7dc0e166
commit d4529e6bdc
4 changed files with 64 additions and 10 deletions
+42 -1
View File
@@ -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);
+18 -5
View File
@@ -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);
+2 -2
View File
@@ -201,7 +201,7 @@
<div>
<label for="editItemQuantity" class="form-label">Ilość / waga / objętość</label>
<div class="input-group">
<input type="number" id="editItemQuantity" class="form-control bg-dark text-white border-secondary" min="0.001" step="any" required>
<input type="number" id="editItemQuantity" class="form-control bg-dark text-white border-secondary" min="0.001" step="1" required>
<select id="editItemUnit" class="form-select bg-dark text-white border-secondary" style="max-width: 110px;">
<option value="szt">szt</option>
<option value="kg">kg</option>
@@ -238,7 +238,7 @@
<input type="number" id="newQuantity" name="quantity"
class="form-control bg-dark text-white border-secondary shopping-qty-input"
placeholder="Ilość" min="0.001" step="any" value="1">
placeholder="Ilość" min="0.001" step="1" value="1">
<select id="newUnit" name="quantity_unit"
class="form-select bg-dark text-white border-secondary shopping-qty-unit"
+2 -2
View File
@@ -103,7 +103,7 @@
<input id="newItem" class="form-control bg-dark text-white border-secondary shopping-product-name-input" placeholder="Dodaj produkt" {% if
not current_user.is_authenticated %}disabled{% endif %}>
<input id="newQuantity" type="number" class="form-control bg-dark text-white border-secondary shopping-qty-input" placeholder="Ilość"
min="0.001" step="any" value="1" {% if not current_user.is_authenticated %}disabled{% endif %}>
min="0.001" step="1" value="1" {% if not current_user.is_authenticated %}disabled{% endif %}>
<select id="newUnit" name="quantity_unit"
class="form-select bg-dark text-white border-secondary shopping-qty-unit"
style="max-width: 105px;" {% if not current_user.is_authenticated %}disabled{% endif %}>
@@ -325,7 +325,7 @@
<div>
<label for="editItemQuantity" class="form-label">Ilość / waga / objętość</label>
<div class="input-group">
<input type="number" id="editItemQuantity" class="form-control bg-dark text-white border-secondary" min="0.001" step="any" required>
<input type="number" id="editItemQuantity" class="form-control bg-dark text-white border-secondary" min="0.001" step="1" required>
<select id="editItemUnit" class="form-select bg-dark text-white border-secondary" style="max-width: 110px;">
<option value="szt">szt</option>
<option value="kg">kg</option>