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_switch.html 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <script type="text/javascript">
  2. (function () {
  3. function hasProperty (obj, prop) {
  4. return Object.prototype.hasOwnProperty.call(obj, prop)
  5. }
  6. RED.nodes.registerType('ui-switch', {
  7. category: RED._('@flowfuse/node-red-dashboard/ui-base:ui-base.label.category'),
  8. color: RED._('@flowfuse/node-red-dashboard/ui-base:ui-base.colors.light'),
  9. defaults: {
  10. name: { value: '' },
  11. label: { value: 'switch' },
  12. // tooltip: {value: ''},
  13. group: { type: 'ui-group', required: true },
  14. order: { value: 0 },
  15. width: {
  16. value: 0,
  17. validate: function (v) {
  18. const width = v || 0
  19. const currentGroup = $('#node-input-group').val() || this.group
  20. const groupNode = RED.nodes.node(currentGroup)
  21. const valid = !groupNode || +width <= +groupNode.width
  22. $('#node-input-size').toggleClass('input-error', !valid)
  23. return valid
  24. }
  25. },
  26. height: { value: 0 },
  27. passthru: { value: false },
  28. decouple: { value: false },
  29. topic: { value: 'topic', validate: (hasProperty(RED.validators, 'typedInput') ? RED.validators.typedInput('topicType') : function (v) { return true }) },
  30. topicType: { value: 'msg' },
  31. style: { value: '' },
  32. className: { value: '' },
  33. // on state
  34. onvalue: { value: true, validate: (hasProperty(RED.validators, 'typedInput') ? RED.validators.typedInput('onvalueType') : function (v) { return true }) },
  35. onvalueType: { value: 'bool' },
  36. onicon: { value: '' },
  37. oncolor: { value: '' },
  38. // off state
  39. offvalue: { value: false, validate: (hasProperty(RED.validators, 'typedInput') ? RED.validators.typedInput('offvalueType') : function (v) { return true }) },
  40. offvalueType: { value: 'bool' },
  41. officon: { value: '' },
  42. offcolor: { value: '' }
  43. },
  44. inputs: 1,
  45. outputs: 1,
  46. icon: 'font-awesome/fa-toggle-on',
  47. paletteLabel: 'switch',
  48. label: function () { return this.name || (~this.label.indexOf('{' + '{') ? null : this.label) || 'switch' },
  49. labelStyle: function () { return this.name ? 'node_label_italic' : '' },
  50. oneditprepare: function () {
  51. // if this groups parent is a subflow template, set the node-config-input-width and node-config-input-height up
  52. // as typedInputs and hide the elementSizer (as it doesn't make sense for subflow templates)
  53. if (RED.nodes.subflow(this.z)) {
  54. // change inputs from hidden to text & display them
  55. $('#node-input-width').attr('type', 'text')
  56. $('#node-input-height').attr('type', 'text')
  57. $('div.form-row.nr-db-ui-element-sizer-row').hide()
  58. $('div.form-row.nr-db-ui-manual-size-row').show()
  59. } else {
  60. // not in a subflow, use the elementSizer
  61. $('div.form-row.nr-db-ui-element-sizer-row').show()
  62. $('div.form-row.nr-db-ui-manual-size-row').hide()
  63. $('#node-input-size').elementSizer({
  64. width: '#node-input-width',
  65. height: '#node-input-height',
  66. group: '#node-input-group'
  67. })
  68. }
  69. $('#node-input-custom-icons').on('change', function () {
  70. if ($('#node-input-custom-icons').val() === 'default') {
  71. $('.form-row-custom-icons').hide()
  72. } else {
  73. $('.form-row-custom-icons').show()
  74. }
  75. })
  76. if (this.onicon !== '' || this.oncolor !== '' || this.officon !== '' || this.offcolor !== '') {
  77. $('#node-input-custom-icons').val('custom')
  78. } else {
  79. $('.form-row-custom-icons').hide()
  80. $('#node-input-custom-icons').change()
  81. }
  82. $('#node-input-onvalue').typedInput({
  83. default: 'str',
  84. typeField: $('#node-input-onvalueType'),
  85. types: ['str', 'num', 'bool', 'json', 'bin', 'date', 'flow', 'global']
  86. })
  87. $('#node-input-offvalue').typedInput({
  88. default: 'str',
  89. typeField: $('#node-input-offvalueType'),
  90. types: ['str', 'num', 'bool', 'json', 'bin', 'date', 'flow', 'global']
  91. })
  92. $('#node-input-topic').typedInput({
  93. default: 'str',
  94. typeField: $('#node-input-topicType'),
  95. types: ['str', 'msg', 'flow', 'global']
  96. })
  97. $('#node-input-passthru').on('change', function () {
  98. if (this.checked) {
  99. $('#node-field-decouple').val('false')
  100. $('.form-row-decouple').hide()
  101. } else {
  102. $('.form-row-decouple').show()
  103. }
  104. })
  105. if (typeof this.decouple === 'undefined') {
  106. // backward compatibility
  107. this.decouple = false
  108. }
  109. // set decouple option correctly
  110. $('#node-field-decouple').val(this.decouple.toString()).change()
  111. // use jQuery UI tooltip to convert the plain old title attribute to a nice tooltip
  112. $('.ui-node-popover-title').tooltip({
  113. show: {
  114. effect: 'slideDown',
  115. delay: 150
  116. }
  117. })
  118. },
  119. oneditsave: function () {
  120. if ($('#node-input-custom-icons').val() === 'default') {
  121. $('#node-input-onicon').val('')
  122. $('#node-input-officon').val('')
  123. $('#node-input-oncolor').val('')
  124. $('#node-input-offcolor').val('')
  125. }
  126. const decouple = $('#node-field-decouple').val()
  127. if (decouple === 'true') {
  128. this.decouple = true
  129. } else {
  130. this.decouple = false
  131. }
  132. }
  133. })
  134. })()
  135. </script>
  136. <script type="text/html" data-template-name="ui-switch">
  137. <div class="form-row">
  138. <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
  139. <input type="text" id="node-input-name">
  140. </div>
  141. <div class="form-row">
  142. <label for="node-input-group"><i class="fa fa-table"></i> Group</label>
  143. <input type="text" id="node-input-group">
  144. </div>
  145. <div class="form-row nr-db-ui-element-sizer-row">
  146. <label><i class="fa fa-object-group"></i> <span data-i18n="ui-switch.label.size">Size</label>
  147. <button class="editor-button" id="node-input-size"></button>
  148. </div>
  149. <div class="form-row nr-db-ui-manual-size-row">
  150. <label><i class="fa fa-arrows-h"></i> <span data-i18n="ui-switch.label.width">Width</label>
  151. <input type="hidden" id="node-input-width">
  152. </div>
  153. <div class="form-row nr-db-ui-manual-size-row">
  154. <label><i class="fa fa-arrows-v"></i> <span data-i18n="ui-switch.label.height">Height</label>
  155. <input type="hidden" id="node-input-height">
  156. </div>
  157. <div class="form-row">
  158. <label for="node-input-label"><i class="fa fa-i-cursor"></i> Label</label>
  159. <input type="text" id="node-input-label">
  160. </div>
  161. <div class="form-row">
  162. <label for="node-input-className"><i class="fa fa-code"></i> Class</label>
  163. <div style="display: inline;">
  164. <input style="width: 70%;" type="text" id="node-input-className" placeholder="Optional CSS class name(s)" style="flex-grow: 1;">
  165. <a
  166. data-html="true"
  167. title="Dynamic Property: Send msg.class to append new classes to this widget. NOTE: classes set at runtime will be applied in addition to any class(es) set in the nodes class field."
  168. class="red-ui-button ui-node-popover-title"
  169. style="margin-left: 4px; cursor: help; font-size: 0.625rem; border-radius: 50%; width: 24px; height: 24px; display: inline-flex; justify-content: center; align-items: center;">
  170. <i style="font-family: ui-serif;">fx</i>
  171. </a>
  172. </div>
  173. </div>
  174. <!--<div class="form-row">
  175. <label for="node-input-tooltip"><i class="fa fa-info-circle"></i> Tooltip</label>
  176. <input type="text" id="node-input-tooltip" placeholder="optional tooltip">
  177. </div>-->
  178. <div class="form-row">
  179. <label for="node-input-custom-icons"><i class="fa fa-picture-o"></i> Icon</label>
  180. <select id="node-input-custom-icons" style="width:35%">
  181. <option value="default">Default</option>
  182. <option value="custom">Custom</option>
  183. </select>
  184. </div>
  185. <div class="form-row form-row-custom-icons">
  186. <label for="node-input-onicon" style="text-align:right;"><i class="fa fa-toggle-on"></i> On Icon</label>
  187. <input type="text" id="node-input-onicon" style="width:120px">
  188. <label for="node-input-oncolor" style="width:50px; text-align:right;">Colour</label>
  189. <input type="text" id="node-input-oncolor" style="width:120px">
  190. </div>
  191. <div class="form-row form-row-custom-icons">
  192. <label for="node-input-officon" style="text-align:right;"><i class="fa fa-toggle-off"></i> Off Icon</label>
  193. <input type="text" id="node-input-officon" style="width:120px">
  194. <label for="node-input-offcolor" style="width:50px; text-align:right;">Colour</label>
  195. <input type="text" id="node-input-offcolor" style="width:120px">
  196. </div>
  197. <div class="form-row">
  198. <label style="width:auto" for="node-input-passthru"><i class="fa fa-arrow-right"></i> If <code>msg</code> arrives on input, pass through to output: </label>
  199. <input type="checkbox" checked id="node-input-passthru" style="display:inline-block; width:auto; vertical-align:top;">
  200. </div>
  201. <div class="form-row form-row-decouple">
  202. <!-- do not us node-input-<property> so we can enforce boolean types -->
  203. <label for="node-field-decouple"><i class="fa fa-toggle-on"></i> Indicator</label>
  204. <select id="node-field-decouple" style="display: inline-block; vertical-align: middle; width:70%;">
  205. <option value="false">Switch icon shows state of the output</option>
  206. <option value="true">Switch icon shows state of the input</option>
  207. </select>
  208. </div>
  209. <div class="form-row">
  210. <label style="width:auto" for="node-input-onvalue"><i class="fa fa-envelope-o"></i> When clicked, send:</label>
  211. </div>
  212. <div class="form-row">
  213. <label for="node-input-onvalue" style="padding-left:25px; margin-right:-25px">On Payload</label>
  214. <input type="text" id="node-input-onvalue" style="width:70%">
  215. <input type="hidden" id="node-input-onvalueType">
  216. </div>
  217. <div class="form-row">
  218. <label for="node-input-offvalue" style="padding-left:25px; margin-right:-25px">Off Payload</label>
  219. <input type="text" id="node-input-offvalue" style="width:70%">
  220. <input type="hidden" id="node-input-offvalueType">
  221. </div>
  222. <div class="form-row">
  223. <label for="node-input-topic" style="padding-left:25px; margin-right:-25px">Topic</label>
  224. <input type="text" id="node-input-topic">
  225. <input type="hidden" id="node-input-topicType">
  226. </div>
  227. </script>