New files for stdev calculation for v0.2

This commit is contained in:
2026-07-02 13:08:22 +00:00
parent f621ba772b
commit c13e19dd13
17 changed files with 3187 additions and 3228 deletions
+232
View File
@@ -0,0 +1,232 @@
/**
* ROLLCALC V14 - ARTICLE SELECTION & AUTO-CALCULATION
* ====================================================
* Handles article selection, auto-fill of dimensions, and weight calculation
* Integrates with stddev_calculator.js for range calculations
*/
/**
* Populate article dropdowns from ARTICLE_DATA
*/
function initializeArticleDropdowns() {
const data = window.ARTICLE_DATA || [];
console.log('[ArticleDropdown] Initializing with', data.length, 'articles');
// Populate datalists for both tabs
['d-art', 'e-art'].forEach(inputId => {
const datalist = document.getElementById(inputId + '-list');
if (!datalist) {
console.warn('[ArticleDropdown] Datalist not found:', inputId + '-list');
return;
}
datalist.innerHTML = ''; // Clear existing
data.forEach(article => {
const option = document.createElement('option');
option.value = `${article.nr} – ${article.name}`;
option.dataset.articleNr = article.nr;
option.dataset.thickness = article.thickness || 0;
option.dataset.areaWeight = article.area_weight || 0;
option.dataset.thicknessStddev = article.thickness_stddev || 0;
option.dataset.areaWeightStddev = article.area_weight_stddev || 0;
datalist.appendChild(option);
});
});
// Setup event listeners for article selection
setupArticleSelectionListeners();
setupWeightCalculationListeners();
console.log('[ArticleDropdown] Initialization complete');
}
/**
* Setup listeners for when user selects an article
*/
function setupArticleSelectionListeners() {
const dArtInput = document.getElementById('d-art');
if (!dArtInput) return;
const dArtList = document.getElementById('d-art-list');
const data = window.ARTICLE_DATA || [];
dArtInput.addEventListener('input', function() {
const inputValue = dArtInput.value.trim();
// Find matching article
const match = data.find(a =>
inputValue === `${a.nr} – ${a.name}` ||
inputValue === a.nr ||
inputValue.startsWith(a.nr + ' –')
);
if (match) {
console.log('[ArticleSelection] Found:', match.nr, '-', match.name);
// 1. Auto-fill thickness
const thField = document.getElementById('d-th');
if (thField) {
thField.value = match.thickness || 0;
thField.dataset.stddev = match.thickness_stddev || 0;
}
// 2. Auto-fill area weight
const awField = document.getElementById('d-aw');
if (awField && match.area_weight && match.area_weight > 0) {
awField.value = match.area_weight;
awField.dataset.stddev = match.area_weight_stddev || 0;
}
// 3. Extract width from article name (e.g., "5.0 x 5m" → 5.0)
const widthMatch = match.name.match(/(\d+[,\.]\d+)\s*x\s*\d+\s*m/i);
if (widthMatch) {
const width = parseFloat(widthMatch[1].replace(',', '.'));
const wiField = document.getElementById('de-wi');
if (wiField && width > 0 && width < 100) {
wiField.value = width;
}
}
console.log('[ArticleSelection] Auto-filled:', {
thickness: match.thickness,
area_weight: match.area_weight,
thickness_stddev: match.thickness_stddev,
area_weight_stddev: match.area_weight_stddev
});
// Trigger auto-calculation after short delay
setTimeout(triggerAutoWeightCalculation, 100);
}
});
}
/**
* Setup listeners for weight field changes (Roll Width, Area Weight, Product Length)
*/
function setupWeightCalculationListeners() {
const fieldIds = ['de-wi', 'd-aw', 'd-le'];
fieldIds.forEach(id => {
const el = document.getElementById(id);
if (el) {
el.addEventListener('change', triggerAutoWeightCalculation);
el.addEventListener('blur', triggerAutoWeightCalculation);
el.addEventListener('input', () => {
// Debounce: recalculate after user stops typing
clearTimeout(el._autoCalcTimeout);
el._autoCalcTimeout = setTimeout(triggerAutoWeightCalculation, 300);
});
}
});
}
/**
* Auto-calculate weight when width, area_weight, or length changes
*/
function triggerAutoWeightCalculation() {
const wiEl = document.getElementById('de-wi');
const awEl = document.getElementById('d-aw');
const leEl = document.getElementById('d-le');
const woEl = document.getElementById('d-wo');
if (!wiEl || !awEl || !leEl || !woEl) return;
const wi = parseFloat(wiEl.value) || 0;
const aw = parseFloat(awEl.value) || 0;
const le = parseFloat(leEl.value) || 0;
if (wi > 0 && aw > 0 && le > 0) {
// Calculate with stddev if available
const aw_stddev = parseFloat(awEl.dataset.stddev) || 0;
if (window.StdDevCalculator && aw_stddev > 0) {
// Calculate weight ranges
const ranges = window.StdDevCalculator.calculateWeightRanges(aw, aw_stddev, le, wi);
woEl.value = ranges.nom.toFixed(1);
woEl.dataset.weight_nom = ranges.nom.toFixed(1);
woEl.dataset.weight_min = ranges.min.toFixed(1);
woEl.dataset.weight_max = ranges.max.toFixed(1);
console.log('[AutoCalc] Weight with stddev:', {
nom: ranges.nom.toFixed(1),
min: ranges.min.toFixed(1),
max: ranges.max.toFixed(1)
});
} else {
// Simple calculation without stddev
const weight = (aw * le * wi / 1000).toFixed(1);
woEl.value = weight;
woEl.dataset.weight_nom = weight;
woEl.dataset.weight_max = weight;
console.log('[AutoCalc] Weight (no stddev):', weight, 'kg');
}
return true;
}
woEl.value = '';
delete woEl.dataset.weight_nom;
delete woEl.dataset.weight_max;
return false;
}
/**
* Setup core preset buttons
*/
function setupCorePresetButtons() {
document.querySelectorAll('.core-preset-btn').forEach(btn => {
btn.addEventListener('click', function(e) {
e.preventDefault();
const coreValue = btn.dataset.core;
const tabPane = btn.closest('.tab-pane');
if (tabPane) {
const coreInput = tabPane.querySelector('input[id$="-co"]');
if (coreInput) {
coreInput.value = coreValue;
// Update active state
tabPane.querySelectorAll('.core-preset-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
console.log('[CorePreset] Set core diameter:', coreValue, 'mm');
}
}
});
});
}
/**
* Initialize everything when DOM is ready
*/
document.addEventListener('DOMContentLoaded', function() {
console.log('[RollCalcV14] Initializing...');
// Wait for article data to be loaded
const maxRetries = 50;
let retries = 0;
const waitForData = setInterval(function() {
if (window.ARTICLE_DATA && window.ARTICLE_DATA.length > 0) {
clearInterval(waitForData);
initializeArticleDropdowns();
setupCorePresetButtons();
console.log('[RollCalcV14] Ready');
} else if (retries++ > maxRetries) {
clearInterval(waitForData);
console.warn('[RollCalcV14] Article data not loaded after', maxRetries, 'retries');
}
}, 100);
});
// Export functions
window.RollCalcV14 = {
initializeArticleDropdowns,
setupArticleSelectionListeners,
setupWeightCalculationListeners,
triggerAutoWeightCalculation,
setupCorePresetButtons
};
console.log('[RollCalcV14] Module loaded');