Files
generalFunctions/src/helper/menu/htmlGeneration.js
Rene De Ren dec5f63b21 refactor: adopt POSITIONS constants, fix ESLint warnings, break menuUtils into modules
- Replace hardcoded position strings with POSITIONS.* constants
- Prefix unused variables with _ to resolve no-unused-vars warnings
- Fix no-prototype-builtins with Object.prototype.hasOwnProperty.call()
- Extract menuUtils.js (543 lines) into 6 focused modules under menu/
- menuUtils.js now 35 lines, delegates via prototype mixin pattern
- Add 158 unit tests for ConfigManager, MeasurementContainer, ValidationUtils

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-11 15:36:52 +01:00

74 lines
2.2 KiB
JavaScript

/**
* HTML generation and endpoint methods for MenuUtils.
* Handles generating dropdown HTML and serving MenuUtils code to the browser.
*/
const htmlGeneration = {
generateHtml(htmlElement, options, savedValue) {
htmlElement.innerHTML = options.length
? `<option value="">Select...</option>${options
.map((opt) => `<option value="${opt}">${opt}</option>`)
.join("")}`
: "<option value=''>No options available</option>";
if (savedValue && options.includes(savedValue)) {
htmlElement.value = savedValue;
}
},
createMenuUtilsEndpoint(RED, nodeName, customHelpers = {}) {
RED.httpAdmin.get(`/${nodeName}/resources/menuUtils.js`, function(req, res) {
console.log(`Serving menuUtils.js for ${nodeName} node`);
res.set('Content-Type', 'application/javascript');
const browserCode = this.generateMenuUtilsCode(nodeName, customHelpers);
res.send(browserCode);
}.bind(this));
},
generateMenuUtilsCode(nodeName, customHelpers = {}) {
const defaultHelpers = {
validateRequired: `function(value) {
return value && value.toString().trim() !== '';
}`,
formatDisplayValue: `function(value, unit) {
return \`\${value} \${unit || ''}\`.trim();
}`
};
const allHelpers = { ...defaultHelpers, ...customHelpers };
const helpersCode = Object.entries(allHelpers)
.map(([name, func]) => ` ${name}: ${func}`)
.join(',\n');
const classCode = this.constructor.toString(); // <-- this gives full class MenuUtils {...}
return `
// Create EVOLV namespace structure
window.EVOLV = window.EVOLV || {};
window.EVOLV.nodes = window.EVOLV.nodes || {};
window.EVOLV.nodes.${nodeName} = window.EVOLV.nodes.${nodeName} || {};
// Inject MenuUtils class
${classCode}
// Expose MenuUtils instance to namespace
window.EVOLV.nodes.${nodeName}.utils = {
menuUtils: new MenuUtils(),
helpers: {
${helpersCode}
}
};
// Optionally expose globally
window.MenuUtils = MenuUtils;
console.log('${nodeName} utilities loaded in namespace');
`;
},
};
module.exports = htmlGeneration;