Add inline norm and quantity choices with strict recalculation

This commit is contained in:
anor
2026-12-03 21:56:16 +10:00
parent d908d183d5
commit a13cdaf902
8 changed files with 269 additions and 11 deletions
+55 -4
View File
@@ -365,7 +365,9 @@ button { font-family: inherit; }
.inline-basis-trigger { border: 0; border-bottom: 1px dotted currentColor; padding: 0; background: transparent; color: var(--accent); font: inherit; cursor: pointer; }
.inline-basis-trigger:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.inline-basis-picker { display: inline-flex; flex-wrap: wrap; align-items: center; gap: .35rem; padding: .2rem; border: 1px solid var(--border); border-radius: 6px; background: var(--bg-card); max-width: 100%; }
.inline-basis-picker select, .inline-basis-picker button { max-width: 100%; min-width: 0; padding: .2rem .35rem; font: inherit; color: var(--text); background: var(--bg-card); border: 1px solid var(--border); border-radius: 5px; }
.inline-basis-picker select, .inline-basis-picker button, .inline-basis-picker input { max-width: 100%; min-width: 0; padding: .2rem .35rem; font: inherit; color: var(--text); background: var(--bg-card); border: 1px solid var(--border); border-radius: 5px; }
.inline-basis-picker .inline-norm-select { max-width: min(28rem, 100%); }
.inline-basis-picker .inline-quantity { width: 6rem; }
.inline-basis-picker .inline-basis-apply { color: var(--accent); cursor: pointer; }
.inline-basis-picker .est-basis-error { flex-basis: 100%; }
.est-input { color: var(--text-muted); font-size: .85em; }
@@ -2265,6 +2267,36 @@ function bindFreeformBasis(host) {
fragment.append(document.createTextNode(node.textContent.slice(start)));
node.replaceWith(fragment);
});
const firstZone = raw.match(pattern);
if (!firstZone) return;
const wrapChoice = (regex, title) => {
const textWalker = document.createTreeWalker(body, NodeFilter.SHOW_TEXT);
const matches = [];
while (textWalker.nextNode()) {
const node = textWalker.currentNode;
if (node.parentElement && !node.parentElement.closest('button, a, select, .inline-basis-picker')
&& regex.test(node.textContent)) matches.push(node);
regex.lastIndex = 0;
}
matches.forEach(node => {
const fragment = document.createDocumentFragment();
let start = 0;
for (const match of node.textContent.matchAll(regex)) {
fragment.append(document.createTextNode(node.textContent.slice(start, match.index)));
const trigger = document.createElement('button');
trigger.type = 'button'; trigger.className = 'inline-basis-trigger';
trigger.textContent = match[0]; trigger.title = title;
trigger.onclick = () => openFreeformBasis(trigger, host, firstZone[0]);
fragment.append(trigger);
start = match.index + match[0].length;
}
fragment.append(document.createTextNode(node.textContent.slice(start)));
node.replaceWith(fragment);
});
};
wrapChoice(/\b\d{2}-\d{2}-\d{3}-\d{2}\b/g, 'Выбрать норму для этого расчёта');
wrapChoice(/Объ[её]м(?:\s+работ)?\s*[=:—-]\s*\d+(?:[.,]\d+)?\s*шт\.?/gi,
'Изменить объём для этого расчёта');
}
async function openFreeformBasis(button, host, label) {
@@ -2296,18 +2328,36 @@ async function openFreeformBasis(button, host, label) {
if (named) periodSelect.value = String(named.period_id);
};
fillPeriods(); zoneSelect.onchange = fillPeriods;
const normRows = options.norms || [];
const position = options.position;
const normSelect = document.createElement('select');
normSelect.className = 'inline-norm-select';
normSelect.setAttribute('aria-label', 'Норма');
normRows.forEach(n => { const o = document.createElement('option'); o.value = n.code;
o.textContent = n.code + ' — ' + n.name; normSelect.appendChild(o); });
if (position) normSelect.value = position.code;
const quantity = document.createElement('input');
quantity.className = 'inline-quantity'; quantity.type = 'number';
quantity.min = '0.000001'; quantity.max = '1000000'; quantity.step = 'any';
quantity.setAttribute('aria-label', 'Объём, шт.');
if (position) quantity.value = String(position.quantity);
const apply = document.createElement('button'); apply.type = 'button';
apply.className = 'inline-basis-apply'; apply.textContent = 'Пересчитать';
const cancel = document.createElement('button'); cancel.type = 'button'; cancel.textContent = 'Отмена';
cancel.onclick = () => picker.replaceWith(button);
apply.onclick = async () => {
if (!periodSelect.value) { periodSelect.focus(); return; }
picker.querySelectorAll('button, select').forEach(el => { el.disabled = true; });
if (normRows.length && (!quantity.value || !quantity.checkValidity()
|| !Number.isFinite(Number(quantity.value)) || Number(quantity.value) <= 0)) {
quantity.focus(); return;
}
picker.querySelectorAll('button, select, input').forEach(el => { el.disabled = true; });
apply.textContent = 'Пересчитываю…';
try {
const response = await authFetch(url, {method: 'POST', headers: {'Content-Type':'application/json'},
body: JSON.stringify({label, zone_id:Number(zoneSelect.value),
period_id:Number(periodSelect.value), revision:options.revision})});
period_id:Number(periodSelect.value), revision:options.revision,
...(normRows.length ? {norm_code:normSelect.value, quantity:Number(quantity.value)} : {})})});
if (!response.ok) throw new Error(await reasonOrText(response));
const updated = await response.json();
if (requestTab !== tabId || !picker.isConnected) return;
@@ -2321,10 +2371,11 @@ async function openFreeformBasis(button, host, label) {
const err = document.createElement('span'); err.className = 'est-basis-error';
err.setAttribute('role', 'alert'); err.textContent = e.message || 'Не удалось пересчитать';
picker.appendChild(err);
picker.querySelectorAll('button, select').forEach(el => { el.disabled = false; });
picker.querySelectorAll('button, select, input').forEach(el => { el.disabled = false; });
apply.textContent = 'Пересчитать';
}
};
if (normRows.length) picker.append(normSelect, quantity);
picker.append(zoneSelect, periodSelect, apply, cancel);
button.replaceWith(picker); zoneSelect.focus();
} catch (e) {