Skip to content

Instantly share code, notes, and snippets.

@TotallyInformation
Last active December 17, 2020 22:29
Simple uibuilder flow (Quote of the Day)

This simple flow will deliver a random quote of the day to a web URL using uibuilder.

All of the front-end html, javascript and css is included in the flow so simply add the flow and edit the front-end code using uibuilder's built-in editor.

See if you can work out how to use the control outputs (output port #2 on the uibuilder node) to trigger a new quote when you reload the page.

The real purpose of this flow is to help you learn a little bit about VueJS which is the default front-end framework provided with uibuilder v2.

The front-end code is extremely simple so do take a look and see how it works.

Many thanks to bakman2 for the inspiration.

I will include this flow in the uibuilder examples in a future release.

There are many more examples for uibuilder on the WIKI, please check them out.

[
{
"id": "ece9345a.f21808",
"type": "debug",
"z": "18cb249f.38bafb",
"name": "simple-debug",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 740,
"y": 1680,
"wires": []
},
{
"id": "bad4b165.e6a08",
"type": "uibuilder",
"z": "18cb249f.38bafb",
"name": "Simple Example",
"topic": "",
"url": "uib_simple",
"fwdInMessages": false,
"allowScripts": false,
"allowStyles": false,
"copyIndex": true,
"showfolder": false,
"x": 530,
"y": 1700,
"wires": [
[
"ece9345a.f21808"
],
[
"bf35e77f.4a7978"
]
]
},
{
"id": "ba6581d8.f5fa2",
"type": "inject",
"z": "18cb249f.38bafb",
"name": "",
"topic": "",
"payload": "",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": "",
"x": 150,
"y": 1700,
"wires": [
[
"eb406f24.13a48"
]
]
},
{
"id": "bf35e77f.4a7978",
"type": "debug",
"z": "18cb249f.38bafb",
"name": "uib controls",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 730,
"y": 1720,
"wires": []
},
{
"id": "5b8a68be.588ec8",
"type": "comment",
"z": "18cb249f.38bafb",
"name": "uibuilder/Simple Example",
"info": "[Front-End](/uib_simple)\n\n## Configuration\n\nUpdate the files:\n\n* `index.html`\n* `index.js`\n* `index.css`\n\nAccording to the example(s) in the 3 other comment nodes in this example.",
"x": 230,
"y": 1640,
"wires": []
},
{
"id": "4a7cc95e.3becd8",
"type": "comment",
"z": "18cb249f.38bafb",
"name": "index.html",
"info": "<!doctype html>\n<html lang=\"en\"><head>\n <meta charset=\"utf-8\">\n <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n <meta name=\"viewport\" content=\"width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes\">\n\n <title>uibuilder simple example</title>\n <meta name=\"description\" content=\"Node-RED uibuilder - Simple example using VueJS\">\n\n <link rel=\"icon\" href=\"./images/node-blue.ico\">\n\n <!-- Put your own custom styles in here -->\n <link rel=\"stylesheet\" href=\"./index.css\" media=\"all\">\n\n</head><body>\n <!-- The \"app\" element is where the code for dynamic updates is attached -->\n\t<div id=\"app\">\n\t <h1>A simple uibuilder page</h1>\n\t <p>\n\t The elements below are dynamically updated when you send\n\t a msg to your uibuilder node.\n\t </p>\n\t \n\t <div v-if=\"msg.payload\">\n\t <h2>Quote of the Day</h2>\n\t <blockquote>\n\t <i>{{ msg.payload.quote.body }}</i>\n \t <div>{{ msg.payload.quote.author }}</div>\n\t </blockquote>\n\t </div>\n\t \n\t <h2>The full msg object</h2>\n\t\t<code>{{ msg }}</code>\n\t\t\n\t</div>\n\n <!-- Vendor Libraries - Load in the right order -->\n <script src=\"../uibuilder/vendor/socket.io/socket.io.js\"></script>\n <script src=\"../uibuilder/vendor/vue/dist/vue.min.js\"></script>\n\n <!-- REQUIRED: Sets up Socket listeners, the uibuilder and msg objects -->\n <script src=\"./uibuilderfe.min.js\"></script>\n\n <!-- Put your own custom code in here -->\n <script src=\"./index.js\"></script>\n\n</body></html>",
"x": 420,
"y": 1640,
"wires": [],
"icon": "node-red/parser-html.svg"
},
{
"id": "1a004a18.d3c5e6",
"type": "comment",
"z": "18cb249f.38bafb",
"name": "index.js",
"info": "/* jshint browser: true, esversion: 5, asi: true */\n/*globals $, uibuilder */\n// @ts-nocheck\n/*\n Copyright (c) 2019 Julian Knight (Totally Information)\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*/\n'use strict'\n\n/** @see https://github.com/TotallyInformation/node-red-contrib-uibuilder/wiki/Front-End-Library---available-properties-and-methods */\n\n// When JQuery is ready, update\n$( document ).ready(function() {\n /** **REQUIRED** Start uibuilder comms with Node-RED @since v2.0.0-dev3\n * Pass the namespace and ioPath variables if hosting page is not in the instance root folder\n * e.g. If you get continual `uibuilderfe:ioSetup: SOCKET CONNECT ERROR` error messages.\n * e.g. uibuilder.start('/nr/uib', '/nr/uibuilder/vendor/socket.io') // change to use your paths/names\n */\n uibuilder.start()\n\n // Initial set\n $('#msgsReceived').text( uibuilder.get('msgsReceived') )\n $('#msgsControl').text( uibuilder.get('msgsCtrl') )\n $('#msgsSent').text( uibuilder.get('msgsSent') )\n $('#socketConnectedState').text( uibuilder.get('ioConnected') )\n $('#feVersion').text( uibuilder.get('version') )\n\n // Turn on debugging (default is off)\n //uibuilder.debug(true)\n\n // If msg changes - msg is updated when a standard msg is received from Node-RED over Socket.IO\n // Note that you can also listen for 'msgsReceived' as they are updated at the same time\n // but newVal relates to the attribute being listened to.\n uibuilder.onChange('msg', function(newVal){\n console.info('property msg changed!')\n console.dir(newVal)\n $('#showMsg').text(JSON.stringify(newVal))\n //uibuilder.set('msgCopy', newVal)\n })\n\n // You can get attributes manually. Non-existent attributes return 'undefined'\n //console.dir(uibuilder.get('msg'))\n\n // You can also set things manually. See the list of attributes top of page.\n // You can add arbitrary attributes to the object, you cannot overwrite internal attributes\n\n // Try setting a restricted, internal attribute - see the warning in the browser console\n uibuilder.set('msg', 'You tried but failed!')\n\n // Remember that onChange functions don't trigger if you haven't set them\n // up BEFORE an attribute change.\n uibuilder.onChange('msgCopy', function(newVal){\n console.info('msgCopy changed. New value: ', newVal)\n })\n\n // Now try setting a new attribute - this will be an empty object because\n // msg won't yet have been received\n uibuilder.set('msgCopy', uibuilder.msg)\n // Hint: Try putting this set into the onChange for 'msg'\n\n // As noted, we could get the msg here too\n uibuilder.onChange('msgsReceived', function(newVal){\n console.info('New msg sent to us from Node-RED over Socket.IO. Total Count: ', newVal)\n $('#msgsReceived').text(newVal)\n // uibuilder.msg is a shortcut for uibuilder.get('msg')\n //$('#showMsg').text(JSON.stringify(uibuilder.msg))\n })\n\n // If Socket.IO connects/disconnects\n uibuilder.onChange('ioConnected', function(newVal){\n console.info('Socket.IO Connection Status Changed: ', newVal)\n $('#socketConnectedState').text(newVal)\n })\n\n // If a message is sent back to Node-RED\n uibuilder.onChange('msgsSent', function(newVal){\n console.info('New msg sent to Node-RED over Socket.IO. Total Count: ', newVal)\n $('#msgsSent').text(newVal)\n $('#showMsgSent').text(JSON.stringify(uibuilder.get('sentMsg')))\n })\n\n // If we receive a control message from Node-RED\n uibuilder.onChange('msgsCtrl', function(newVal){\n console.info('New control msg sent to us from Node-RED over Socket.IO. Total Count: ', newVal)\n $('#msgsControl').text(newVal)\n $('#showCtrlMsg').text(JSON.stringify(uibuilder.get('ctrlMsg')))\n })\n\n //Manually send a message back to Node-RED after 2 seconds\n window.setTimeout(function(){\n console.info('Sending a message back to Node-RED - after 2s delay')\n uibuilder.send( { 'topic':'uibuilderfe', 'payload':'I am a message sent from the uibuilder front end' } )\n }, 2000)\n\n}) // --- End of JQuery Ready --- //\n\n// EOF\n",
"x": 550,
"y": 1640,
"wires": [],
"icon": "font-awesome/fa-code"
},
{
"id": "ddf41d65.687",
"type": "debug",
"z": "18cb249f.38bafb",
"name": "qod-debug",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 530,
"y": 1780,
"wires": []
},
{
"id": "eb406f24.13a48",
"type": "http request",
"z": "18cb249f.38bafb",
"name": "Quote of the day",
"method": "GET",
"ret": "obj",
"paytoqs": false,
"url": "https://favqs.com/api/qotd",
"tls": "",
"persist": false,
"proxy": "",
"authType": "",
"x": 310,
"y": 1700,
"wires": [
[
"bad4b165.e6a08",
"ddf41d65.687"
]
]
},
{
"id": "fa66e5de.8a81e8",
"type": "comment",
"z": "18cb249f.38bafb",
"name": "index.css",
"info": "body {font-family: sans-serif; font-size: 120%;}\ndiv, p, code { margin:0.3em; padding: 0.3em;}\n\nblockquote i {\n font-size: 1.5em; color:grey; font-style: italic;\n}",
"x": 680,
"y": 1640,
"wires": [],
"icon": "node-red/hash.svg"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment