const AssetMenu = require('./asset.js'); const LoggerMenu = require('./logger.js'); const PhysicalPositionMenu = require('./physicalPosition.js'); class MenuManager { constructor() { this.registeredMenus = new Map(); // Store menu type instances this.registerMenu('asset', new AssetMenu()); // Register asset menu by default this.registerMenu('logger', new LoggerMenu()); // Register logger menu by default this.registerMenu('position', new PhysicalPositionMenu()); // Register position menu by default } /** * Register a menu type with its handler instance * @param {string} menuType - The type of menu (e.g., 'asset', 'logging') * @param {object} menuHandler - The menu handler instance */ registerMenu(menuType, menuHandler) { this.registeredMenus.set(menuType, menuHandler); } /** * Create a complete endpoint script with data and initialization functions * @param {string} nodeName - The name of the node type * @param {Array} menuTypes - Array of menu types to include * @returns {string} Complete JavaScript code to serve */ createEndpoint(nodeName, menuTypes) { // 1. Collect all menu data const menuData = {}; menuTypes.forEach(menuType => { const handler = this.registeredMenus.get(menuType); if (handler && typeof handler.getAllMenuData === 'function') { menuData[menuType] = handler.getAllMenuData(); } }); // Generate HTML injection code const htmlInjections = menuTypes.map(type => { const menu = this.registeredMenus.get(type); if (menu && menu.getHtmlInjectionCode) { return menu.getHtmlInjectionCode(nodeName); } return ''; }).join('\n'); // 2. Collect all client initialization code const initFunctions = []; menuTypes.forEach(menuType => { const handler = this.registeredMenus.get(menuType); if (handler && typeof handler.getClientInitCode === 'function') { initFunctions.push(handler.getClientInitCode(nodeName)); } }); // 3. Convert menu data to JSON const menuDataJSON = JSON.stringify(menuData, null, 2); // 4. Assemble the complete script return ` // Create the namespace structure window.EVOLV = window.EVOLV || {}; window.EVOLV.nodes = window.EVOLV.nodes || {}; window.EVOLV.nodes.${nodeName} = window.EVOLV.nodes.${nodeName} || {}; // Inject the pre-loaded menu data directly into the namespace window.EVOLV.nodes.${nodeName}.menuData = ${menuDataJSON}; ${initFunctions.join('\n\n')} // Main initialization function that calls all menu initializers window.EVOLV.nodes.${nodeName}.initEditor = function(node) { ${menuTypes.map(type => ` if (window.EVOLV.nodes.${nodeName}.${type}Menu && window.EVOLV.nodes.${nodeName}.${type}Menu.initEditor) { window.EVOLV.nodes.${nodeName}.${type}Menu.initEditor(node); }`).join('')} }; console.log('${nodeName} menu data and initializers loaded for: ${menuTypes.join(', ')}'); `; } } module.exports = MenuManager;