Files
generalFunctions/src/menu/logger.js
2025-06-25 10:55:50 +02:00

134 lines
4.5 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.

class LoggerMenu {
constructor() {
// no external data files for logger all static
}
// 1) Serverside: return the static menuData
getAllMenuData() {
return {
logLevels: [
{ value: 'error', label: 'Error', description: 'Only error messages' },
{ value: 'warn', label: 'Warn', description: 'Warning and error messages' },
{ value: 'info', label: 'Info', description: 'Info, warning and error messages' },
{ value: 'debug', label: 'Debug', description: 'All messages including debug' }
]
};
}
// 2) Clientside: inject the dropdown options
getDataInjectionCode(nodeName) {
return `
// Logger data loader for ${nodeName}
window.EVOLV.nodes.${nodeName}.loggerMenu.loadData = function(node) {
const data = window.EVOLV.nodes.${nodeName}.menuData.logger;
const sel = document.getElementById('node-input-logLevel');
if (!sel) return;
sel.innerHTML = '';
data.logLevels.forEach(l => {
const opt = document.createElement('option');
opt.value = l.value;
opt.textContent = l.label;
opt.title = l.description;
sel.appendChild(opt);
});
sel.value = node.logLevel || 'info';
};
`;
}
getHtmlInjectionCode(nodeName) {
const tpl = `
<h3>Internal logging</h3>
<div class="form-row">
<label for="node-input-enableLog"><i class="fa fa-bug"></i>Logging</label>
<input type="checkbox" id="node-input-enableLog"/>
</div>
<div class="form-row" id="row-logLevel">
<label for="node-input-logLevel"><i class="fa fa-list"></i> Log Level</label>
<select id="node-input-logLevel" style="width:60%;"></select>
</div>
`.replace(/`/g,'\\`').replace(/\$/g,'\\$');
return `
// Logger HTML injection for ${nodeName}
window.EVOLV.nodes.${nodeName}.loggerMenu.injectHtml = function() {
const ph = document.getElementById('logger-fields-placeholder');
if (ph && !ph.hasChildNodes()) {
ph.innerHTML = \`${tpl}\`;
}
};
`;
}
// 3) Clientside: wire up the enabletoggle behavior
getEventInjectionCode(nodeName) {
return `
// Logger event wiring for ${nodeName}
window.EVOLV.nodes.${nodeName}.loggerMenu.wireEvents = function(node) {
const chk = document.getElementById('node-input-enableLog');
const row = document.getElementById('row-logLevel');
if (!chk || !row) return;
const toggle = () => {
row.style.display = chk.checked ? 'block' : 'none';
};
chk.checked = node.enableLog || false;
toggle();
chk.addEventListener('change', toggle);
};
`;
}
// 4) Clientside: save logic
getSaveInjectionCode(nodeName) {
return `
// Logger Save injection for ${nodeName}
window.EVOLV.nodes.${nodeName}.loggerMenu.saveEditor = function(node) {
console.log('Saving logger properties for ${nodeName}…');
const chk = document.getElementById('node-input-enableLog');
const sel = document.getElementById('node-input-logLevel');
node.enableLog = chk ? chk.checked : false;
node.logLevel = sel ? sel.value : 'info';
const errors = [];
if (node.enableLog && !node.logLevel) {
errors.push('Log level must be selected when logging is enabled.');
}
errors.forEach(e => RED.notify(e,'error'));
// --- DEBUG: what was saved ---
console.log('→ loggerMenu.saveEditor result:', {
enableLog: node.enableLog,
logLevel: node.logLevel
});
return errors.length === 0;
};
`;
}
// 5) Compose everything into one clientside payload
getClientInitCode(nodeName) {
const dataCode = this.getDataInjectionCode(nodeName);
const eventCode = this.getEventInjectionCode(nodeName);
const saveCode = this.getSaveInjectionCode(nodeName);
const htmlCode = this.getHtmlInjectionCode(nodeName);
return `
// --- LoggerMenu for ${nodeName} ---
window.EVOLV.nodes.${nodeName}.loggerMenu =
window.EVOLV.nodes.${nodeName}.loggerMenu || {};
${htmlCode}
${dataCode}
${eventCode}
${saveCode}
// oneditprepare calls this
window.EVOLV.nodes.${nodeName}.loggerMenu.initEditor = function(node) {
// ------------------ BELOW sequence is important! -------------------------------
this.injectHtml();
this.loadData(node);
this.wireEvents(node);
};
`;
}
}
module.exports = LoggerMenu;