Added automatic Rollwidth and Area Weigth selection by Article
This commit is contained in:
@@ -0,0 +1,284 @@
|
||||
|
||||
/**
|
||||
* ROLLCALC V14 - ENHANCED ARTICLE SELECTION & AUTO-WEIGHT
|
||||
* =========================================================
|
||||
* Improvements:
|
||||
* 1. Auto-fill thickness + area_weight from article-data.json
|
||||
* 2. Auto-extract roll width from product name
|
||||
* 3. Auto-calculate weight when article is selected
|
||||
* 4. Better error handling and logging
|
||||
*/
|
||||
|
||||
// ===================================================================
|
||||
// AUTO-WEIGHT CALCULATION FUNCTION
|
||||
// ===================================================================
|
||||
function triggerAutoWeightCalculation() {
|
||||
console.log('[AutoCalc] Attempting automatic weight calculation...');
|
||||
|
||||
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) {
|
||||
console.warn('[AutoCalc] Missing weight calculation elements');
|
||||
return;
|
||||
}
|
||||
|
||||
const wi = parseFloat(wiEl.value) || 0;
|
||||
const aw = parseFloat(awEl.value) || 0;
|
||||
const L = parseFloat(LEL.value) || 0;
|
||||
|
||||
console.log('[AutoCalc] Values: Width=', wi, 'AreaWeight=', aw, 'Length=', L);
|
||||
|
||||
if (wi > 0 && aw > 0 && L > 0) {
|
||||
const weight = (aw * L * wi / 1000).toFixed(1);
|
||||
resultEl.value = weight;
|
||||
console.log('[AutoCalc] ✓ Weight calculated:', weight, 'kg');
|
||||
return true;
|
||||
} else {
|
||||
console.log('[AutoCalc] Cannot calculate - missing values');
|
||||
resultEl.value = '';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// IMPROVED ARTICLE DROPDOWN POPULATION
|
||||
// ===================================================================
|
||||
function populateArticleDropdowns_v14() {
|
||||
const data = window.ARTICLE_DATA || [];
|
||||
|
||||
if (!data || data.length === 0) {
|
||||
console.warn('[ArticleDropdown] No article data available');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('[ArticleDropdown] Populating with', data.length, 'articles');
|
||||
|
||||
// Populate both datalists
|
||||
['d-art', 'e-art'].forEach(function(inputId) {
|
||||
const dl = document.getElementById(inputId + '-list');
|
||||
if (!dl) {
|
||||
console.warn('[ArticleDropdown] Datalist not found:', inputId + '-list');
|
||||
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.fullData = JSON.stringify(article);
|
||||
|
||||
dl.appendChild(opt);
|
||||
});
|
||||
});
|
||||
|
||||
console.log('[ArticleDropdown] ✓ Datalists populated');
|
||||
|
||||
// ===================================================================
|
||||
// DIRECT CALCULATION TAB - Article Selection Handler
|
||||
// ===================================================================
|
||||
const dArt = document.getElementById('d-art');
|
||||
if (dArt) {
|
||||
dArt.addEventListener('input', function() {
|
||||
const inputVal = dArt.value.trim();
|
||||
|
||||
// Try to find matching article
|
||||
const match = data.find(function(a) {
|
||||
const displayText = a.nr + ' – ' + a.name;
|
||||
return displayText === inputVal ||
|
||||
inputVal.startsWith(a.nr + ' –') ||
|
||||
inputVal === a.nr;
|
||||
});
|
||||
|
||||
if (match) {
|
||||
console.log('[DirectCalc-Article] ✓ Article found:', match.nr);
|
||||
|
||||
// 1. AUTO-FILL THICKNESS
|
||||
document.getElementById('d-th').value = match.thickness || 0;
|
||||
console.log(' └─ Thickness:', match.thickness);
|
||||
|
||||
// 2. AUTO-FILL AREA WEIGHT (if available)
|
||||
if (match.area_weight && match.area_weight > 0) {
|
||||
document.getElementById('d-aw').value = match.area_weight;
|
||||
console.log(' └─ Area Weight:', match.area_weight, 'g/m²');
|
||||
} else {
|
||||
console.log(' └─ Area Weight: not available (0)');
|
||||
document.getElementById('d-aw').value = '';
|
||||
}
|
||||
|
||||
// 3. AUTO-EXTRACT ROLL WIDTH from product name (e.g., "5,00 x 30 m" → 5.00)
|
||||
const extractedWidth = extractWidthFromName(match.name);
|
||||
if (extractedWidth > 0) {
|
||||
document.getElementById('de-wi').value = extractedWidth;
|
||||
console.log(' └─ Roll Width extracted:', extractedWidth, 'm');
|
||||
}
|
||||
|
||||
// 4. TRIGGER automatic weight calculation (async)
|
||||
setTimeout(function() {
|
||||
triggerAutoWeightCalculation();
|
||||
}, 100);
|
||||
|
||||
} else if (inputVal.length > 0) {
|
||||
console.log('[DirectCalc-Article] No match found for:', inputVal);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// EXTRAPOLATION TAB - Article Selection Handler
|
||||
// ===================================================================
|
||||
const eArt = document.getElementById('e-art');
|
||||
if (eArt) {
|
||||
eArt.addEventListener('input', function() {
|
||||
const inputVal = eArt.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) {
|
||||
console.log('[Extrap-Article] ✓ Article found:', match.nr);
|
||||
|
||||
// Set core diameter (default 150 if not specified)
|
||||
const coreVal = match.core_type && match.core_type > 0 ? match.core_type : 150;
|
||||
document.getElementById('e-co').value = coreVal;
|
||||
console.log(' └─ Core Diameter:', coreVal, 'mm');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// CORE PRESET BUTTONS
|
||||
// ===================================================================
|
||||
document.querySelectorAll('.core-preset-btn').forEach(function(btn) {
|
||||
btn.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
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;
|
||||
|
||||
// Visual feedback
|
||||
pane.querySelectorAll('.core-preset-btn').forEach(function(b) {
|
||||
b.classList.remove('active');
|
||||
});
|
||||
btn.classList.add('active');
|
||||
|
||||
console.log('[CorePreset] Set to:', coreVal, 'mm');
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
console.log('[ArticleDropdown] ✓ All event listeners attached');
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// HELPER: Extract width from product name
|
||||
// ===================================================================
|
||||
function extractWidthFromName(productName) {
|
||||
if (!productName) return 0;
|
||||
|
||||
// Regex: Match patterns like "5,00 x 30 m" or "5.00 x 30 m" or "4,85 x 40 m"
|
||||
const match = productName.match(/(\d+[,\.]\d+)\s*x\s*\d+\s*m/i);
|
||||
|
||||
if (match && match[1]) {
|
||||
// Convert German decimal (comma) to English decimal (dot)
|
||||
const widthStr = match[1].replace(',', '.');
|
||||
const width = parseFloat(widthStr);
|
||||
|
||||
if (width > 0 && width < 100) { // Sanity check
|
||||
return width;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// ENHANCED WEIGHT CALCULATION - Listen to all relevant inputs
|
||||
// ===================================================================
|
||||
function setupWeightCalculationListeners() {
|
||||
console.log('[WeightCalc] Setting up automatic calculation listeners...');
|
||||
|
||||
const inputIds = ['de-wi', 'd-aw', 'd-le'];
|
||||
|
||||
inputIds.forEach(function(id) {
|
||||
const el = document.getElementById(id);
|
||||
if (el) {
|
||||
el.addEventListener('change', function() {
|
||||
console.log('[WeightCalc] Input changed:', id);
|
||||
triggerAutoWeightCalculation();
|
||||
});
|
||||
|
||||
// Also trigger on blur for better UX
|
||||
el.addEventListener('blur', function() {
|
||||
triggerAutoWeightCalculation();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
console.log('[WeightCalc] ✓ Listeners attached to:', inputIds.join(', '));
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// INITIALIZATION HOOK
|
||||
// ===================================================================
|
||||
function initRollCalcV14() {
|
||||
console.log('[RollCalcV14] Initializing enhanced features...');
|
||||
|
||||
// Wait for article data to be loaded
|
||||
const checkDataInterval = setInterval(function() {
|
||||
if (window.ARTICLE_DATA && window.ARTICLE_DATA.length > 0) {
|
||||
clearInterval(checkDataInterval);
|
||||
console.log('[RollCalcV14] ✓ Article data ready, populating dropdowns...');
|
||||
|
||||
// Use improved version
|
||||
populateArticleDropdowns_v14();
|
||||
setupWeightCalculationListeners();
|
||||
|
||||
console.log('[RollCalcV14] ✓ Initialization complete');
|
||||
}
|
||||
}, 100);
|
||||
|
||||
// Failsafe: timeout after 5 seconds
|
||||
setTimeout(function() {
|
||||
if (window.ARTICLE_DATA && window.ARTICLE_DATA.length > 0) {
|
||||
clearInterval(checkDataInterval);
|
||||
} else {
|
||||
console.warn('[RollCalcV14] Article data not loaded within 5s - using fallback');
|
||||
clearInterval(checkDataInterval);
|
||||
}
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// EXPORT & ATTACH TO WINDOW
|
||||
// ===================================================================
|
||||
window.RollCalcV14 = {
|
||||
initRollCalcV14: initRollCalcV14,
|
||||
triggerAutoWeightCalculation: triggerAutoWeightCalculation,
|
||||
populateArticleDropdowns_v14: populateArticleDropdowns_v14,
|
||||
extractWidthFromName: extractWidthFromName,
|
||||
setupWeightCalculationListeners: setupWeightCalculationListeners
|
||||
};
|
||||
|
||||
console.log('[RollCalcV14] Module loaded and ready');
|
||||
|
||||
// Auto-init when DOM is ready
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
console.log('[RollCalcV14] DOM ready - starting initialization');
|
||||
window.RollCalcV14.initRollCalcV14();
|
||||
});
|
||||
Reference in New Issue
Block a user