Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ui_button.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const statestore = require('../store/state.js')
  2. const { appendTopic } = require('../utils/index.js')
  3. module.exports = function (RED) {
  4. function ButtonNode (config) {
  5. // create node in Node-RED
  6. RED.nodes.createNode(this, config)
  7. const node = this
  8. // which group are we rendering this widget
  9. const group = RED.nodes.getNode(config.group)
  10. const beforeSend = async function (msg) {
  11. let error = null
  12. // retrieve the payload we're sending from this button
  13. let payloadType = config.payloadType
  14. let payload = config.payload
  15. if (payloadType === 'flow' || payloadType === 'global') {
  16. try {
  17. const parts = RED.util.normalisePropertyExpression(payload)
  18. if (parts.length === 0) {
  19. throw new Error()
  20. }
  21. payload = RED.util.evaluateNodeProperty(payload, payloadType, node)
  22. } catch (err) {
  23. node.warn('Invalid payload property expression - defaulting to node id')
  24. payload = node.id
  25. payloadType = 'str'
  26. }
  27. } else if (payloadType === 'date') {
  28. payload = Date.now()
  29. } else {
  30. try {
  31. payload = RED.util.evaluateNodeProperty(payload, payloadType, node)
  32. } catch (err) {
  33. error = err
  34. if (payloadType === 'bin') {
  35. node.error('Badly formatted buffer')
  36. } else {
  37. node.error(err, payload)
  38. }
  39. }
  40. }
  41. msg.payload = payload
  42. const updates = msg.ui_update
  43. if (updates) {
  44. // dynamic properties
  45. if (typeof updates.label !== 'undefined') {
  46. // dynamically set "label" property
  47. statestore.set(group.getBase(), node, msg, 'label', updates.label)
  48. }
  49. if (typeof updates.icon !== 'undefined') {
  50. // dynamically set "icon" property
  51. statestore.set(group.getBase(), node, msg, 'icon', updates.icon)
  52. }
  53. if (typeof updates.iconPosition !== 'undefined') {
  54. // dynamically set "iconPosition" property
  55. statestore.set(group.getBase(), node, msg, 'iconPosition', updates.iconPosition)
  56. }
  57. if (typeof updates.buttonColor !== 'undefined') {
  58. // dynamically set "buttonColor" property
  59. statestore.set(group.getBase(), node, msg, 'buttonColor', updates.buttonColor)
  60. }
  61. if (typeof updates.textColor !== 'undefined') {
  62. // dynamically set "textColor" property
  63. statestore.set(group.getBase(), node, msg, 'textColor', updates.textColor)
  64. }
  65. if (typeof updates.iconColor !== 'undefined') {
  66. // dynamically set "iconColor" property
  67. statestore.set(group.getBase(), node, msg, 'iconColor', updates.iconColor)
  68. }
  69. }
  70. if (!error) {
  71. return msg
  72. } else {
  73. node.error(error)
  74. return null
  75. }
  76. }
  77. const evts = {
  78. onAction: true,
  79. beforeSend,
  80. onInput: async function (msg) {
  81. if (config.emulateClick) {
  82. msg = await beforeSend(msg)
  83. if (config.topic || config.topicType) {
  84. msg = await appendTopic(RED, config, node, msg)
  85. }
  86. node.send(msg)
  87. }
  88. }
  89. }
  90. // inform the dashboard UI that we are adding this node
  91. if (group) {
  92. group.register(node, config, evts)
  93. } else {
  94. node.error('No group configured')
  95. }
  96. }
  97. RED.nodes.registerType('ui-button', ButtonNode)
  98. }