UNPKG

@5minds/node-red-dashboard-2-processcube-ui-page-navigation

Version:

A node to handle the navigation between pages in the Node-RED Dashboard

63 lines (55 loc) 2.12 kB
module.exports = function (RED) { function UIPageNavigation(config) { RED.nodes.createNode(this, config); const node = this; const page = RED.nodes.getNode(config.page); const pageName = page?.name; config.pagename = pageName; function addConnectionCredentials(RED, msg, conn, config) { if (config.includeClientData) { if (!msg._client) { msg._client = {}; } RED.plugins.getByType('node-red-dashboard-2').forEach((plugin) => { if (plugin.hooks?.onAddConnectionCredentials && msg) { msg = plugin.hooks.onAddConnectionCredentials(conn, msg); } }); msg._client = { ...msg._client, ...{ socketId: conn.id, socketIp: conn.handshake?.address, }, }; } return msg; } const ui = page.getBase(); const evts = { onSocket: { 'ui-event': function (conn, id, msg) { if (config.event != 'any' && config.event != msg.topic) return; if (pageName != '' && pageName != msg.payload.page.name) return; const wNode = RED.nodes.getNode(node.id); if (!wNode) { console.log('ui-page-navigation node not found', id); } if (wNode) { msg = addConnectionCredentials(RED, msg, conn, ui); wNode.send(msg); } }, }, }; ui.register(null, null, node, config, evts); node.on('close', function (removed, done) { if (removed) { // handle node being removed ui.deregister(null, null, node); } done(); }); } RED.nodes.registerType('ui-page-navigation', UIPageNavigation); };