diff --git a/docs/nodes/widgets/ui-template.md b/docs/nodes/widgets/ui-template.md index 77857a147..ac7c06f66 100644 --- a/docs/nodes/widgets/ui-template.md +++ b/docs/nodes/widgets/ui-template.md @@ -5,6 +5,10 @@ props: Scope: "Template nodes can be used for 3 purposes/scopes: " Class: Appends CSS classes to the widget Template: The content of the widget or CSS <style>. If using this for CSS, you do not need to include any <style> tags, as these will be automatically added. +dynamic: + Format: + payload: msg.ui_update.format + structure: ["String"] --- \ No newline at end of file + diff --git a/nodes/widgets/ui_template.js b/nodes/widgets/ui_template.js index 3090fbdaf..e8dc0df69 100644 --- a/nodes/widgets/ui_template.js +++ b/nodes/widgets/ui_template.js @@ -1,3 +1,5 @@ +const statestore = require('../store/state.js') + module.exports = function (RED) { function TemplateNode (config) { const node = this @@ -5,10 +7,6 @@ module.exports = function (RED) { // create node in Node-RED RED.nodes.createNode(this, config) - const evts = { - onAction: true // TODO: think we need an onSend event for template nodes that matches up with a `widget-send` message - } - if (config.templateScope === 'local') { config.page = '' config.ui = '' @@ -31,18 +29,37 @@ module.exports = function (RED) { config.passthru = true } - // which group are we rendering this widget + // determine which group/page/ui are we rendering this widget + let group, page, ui if (config.group) { - const group = RED.nodes.getNode(config.group) - // inform the dashboard UI that we are adding this node - group.register(node, config, evts) + group = RED.nodes.getNode(config.group) } else if (config.page) { - const page = RED.nodes.getNode(config.page) - // inform the dashboard UI that we are adding this node - page.register(null, node, config, evts) + page = RED.nodes.getNode(config.page) } else if (config.ui) { - const ui = RED.nodes.getNode(config.ui) - // inform the dashboard UI that we are adding this node + ui = RED.nodes.getNode(config.ui) + } + + const evts = { + onAction: true, // TODO: think we need an onSend event for template nodes that matches up with a `widget-send` message + beforeSend: function (msg) { + if (msg.ui_update) { + const update = msg.ui_update + if (typeof update.format !== 'undefined') { + // dynamically set "format" property + const parent = group || page || ui + statestore.set(parent.getBase(), node, msg, 'format', update.format) + } + } + return msg + } + } + + // inform the dashboard group/page/ui that we are adding this node + if (group) { + group.register(node, config, evts) + } else if (page) { + page.register(null, node, config, evts) + } else if (ui) { ui.register(null, null, node, config, evts) } } diff --git a/ui/src/widgets/ui-template/UITemplate.vue b/ui/src/widgets/ui-template/UITemplate.vue index 4b479fdb4..540f12415 100644 --- a/ui/src/widgets/ui-template/UITemplate.vue +++ b/ui/src/widgets/ui-template/UITemplate.vue @@ -11,189 +11,25 @@ export default { inject: ['$socket', '$dataTracker'], props: { id: { type: String, required: true }, - props: { type: Object, default: () => ({}) } + props: { type: Object, default: () => ({}) }, + state: { type: Object, default: () => ({}) } }, - setup (props) { - const parser = new DOMParser() - const htmlDoc = parser.parseFromString(props.props.format, 'text/html') - - // check whether we have a fully defined Vue component, or _just_ HTML - const templates = htmlDoc.getElementsByTagName('template') - const template = templates.length ? templates[0] : props.props.format - - let head - - /* Check for any CSS