Files
RollCalcPython/static/rollcalc_improvements.js
T

129 lines
4.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* ROLLCALC IMPROVEMENTS V14
* Article selection + automatic weight calculation
*/
function populateArticleDropdowns_v14() {
const data = window.ARTICLE_DATA || [];
console.log('[ArticleDropdown] Populating with', data.length, 'articles');
['d-art', 'e-art'].forEach(function(inputId) {
const dl = document.getElementById(inputId + '-list');
if (!dl) return;
data.forEach(function(article) {
const displayText = article.nr + ' – ' + article.name;
const opt = document.createElement('option');
opt.value = displayText;
opt.dataset.articleNr = article.nr;
opt.dataset.thickness = article.thickness || 0;
opt.dataset.areaWeight = article.area_weight || 0;
opt.dataset.width = extractWidthFromName(article.name) || 0;
opt.dataset.stddev_thickness = article.thickness_stddev || 0;
opt.dataset.stddev_areaweight = article.area_weight_stddev || 0;
dl.appendChild(opt);
});
});
const dArt = document.getElementById('d-art');
if (dArt) {
dArt.addEventListener('input', function() {
const inputVal = dArt.value.trim();
const match = data.find(function(a) {
const displayText = a.nr + ' – ' + a.name;
return displayText === inputVal || inputVal.startsWith(a.nr + ' –') || inputVal === a.nr;
});
if (match) {
document.getElementById('d-th').value = match.thickness || 0;
document.getElementById('d-th').dataset.stddev = match.thickness_stddev || 0;
if (match.area_weight && match.area_weight > 0) {
document.getElementById('d-aw').value = match.area_weight;
document.getElementById('d-aw').dataset.stddev = match.area_weight_stddev || 0;
}
const extractedWidth = extractWidthFromName(match.name);
if (extractedWidth > 0) {
document.getElementById('de-wi').value = extractedWidth;
}
setTimeout(function() {
triggerAutoWeightCalculation();
}, 100);
}
});
}
document.querySelectorAll('.core-preset-btn').forEach(function(btn) {
btn.addEventListener('click', function() {
const coreVal = btn.dataset.core;
const pane = btn.closest('.tab-pane');
if (pane) {
const inp = pane.querySelector('input[id$="-co"]');
if (inp) {
inp.value = coreVal;
pane.querySelectorAll('.core-preset-btn').forEach(function(b) { b.classList.remove('active'); });
btn.classList.add('active');
}
}
});
});
}
function extractWidthFromName(productName) {
if (!productName) return 0;
const match = productName.match(/(\d+[,\.]\d+)\s*x\s*\d+\s*m/i);
if (match && match[1]) {
const widthStr = match[1].replace(',', '.');
const width = parseFloat(widthStr);
if (width > 0 && width < 100) return width;
}
return 0;
}
function triggerAutoWeightCalculation() {
const wiEl = document.getElementById('de-wi');
const awEl = document.getElementById('d-aw');
const LEL = document.getElementById('d-le');
const resultEl = document.getElementById('d-wo');
if (!wiEl || !awEl || !LEL || !resultEl) return;
const wi = parseFloat(wiEl.value) || 0;
const aw = parseFloat(awEl.value) || 0;
const L = parseFloat(LEL.value) || 0;
if (wi > 0 && aw > 0 && L > 0) {
const weight = (aw * L * wi / 1000).toFixed(1);
resultEl.value = weight;
console.log('[AutoCalc] Weight: ' + weight + ' kg');
return true;
}
return false;
}
function setupWeightCalculationListeners() {
const inputIds = ['de-wi', 'd-aw', 'd-le'];
inputIds.forEach(function(id) {
const el = document.getElementById(id);
if (el) {
el.addEventListener('change', triggerAutoWeightCalculation);
el.addEventListener('blur', triggerAutoWeightCalculation);
}
});
}
window.RollCalcV14 = {
populateArticleDropdowns_v14: populateArticleDropdowns_v14,
triggerAutoWeightCalculation: triggerAutoWeightCalculation,
extractWidthFromName: extractWidthFromName
};
document.addEventListener('DOMContentLoaded', function() {
if (window.ARTICLE_DATA && window.ARTICLE_DATA.length > 0) {
populateArticleDropdowns_v14();
setupWeightCalculationListeners();
}
});