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.

index.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Copyright JS Foundation and other contributors, http://js.foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. **/
  16. var ui = null;
  17. function init(RED) {
  18. if (!ui) {
  19. ui = require("./ui")(RED);
  20. }
  21. }
  22. /* addWidget:
  23. - options
  24. - RED: RED object
  25. - options: options to create dashboard widget
  26. * [node] - the node that represents the control on a flow
  27. * format - HTML code of widget
  28. * [group] - group name (optional if templateScope = 'global')
  29. * [width] - width of widget (default automatic)
  30. * [height] - height of widget (default automatic)
  31. * [order] - property to hold the placement order of the widget (default 0)
  32. * [templateScope] - scope of widget/global or local (default local)
  33. * [emitOnlyNewValues] - boolean (default true).
  34. If true, it checks if the payload changed before sending it
  35. to the front-end. If the payload is the same no message is sent.
  36. * [forwardInputMessages] - boolean (default true).
  37. If true, forwards input messages to the output
  38. * [storeFrontEndInputAsState] - boolean (default true).
  39. If true, any message received from front-end is stored as state
  40. [persistantFrontEndValue] - boolean (default true).
  41. If true, last received message is send again when front end reconnect.
  42. * [convert] - callback to convert the value before sending it to the front-end
  43. * [beforeEmit] - callback to prepare the message that is emitted to the front-end
  44. * [convertBack] - callback to convert the message from front-end before sending it to the next connected node
  45. * [beforeSend] - callback to prepare the message that is sent to the output
  46. * [initController] - callback to initialize in controller
  47. */
  48. function addWidget(RED, options) {
  49. var is_local = (options.templateScope !== "global");
  50. var group = null;
  51. var tab = null;
  52. init(RED);
  53. var ui_control = {
  54. type: "template",
  55. order: options.order,
  56. format: options.format,
  57. class: "nr-dashboard-"+(options.node.type || "template-blank")
  58. };
  59. var node = options.node;
  60. if (isNaN(options.order)) {
  61. node.warn("*** Order property not set. Please contact developer. ***");
  62. }
  63. if (is_local) {
  64. group = RED.nodes.getNode(options.group);
  65. if (group === null) { return; }
  66. tab = RED.nodes.getNode(group.config.tab);
  67. ui_control.width = options.hasOwnProperty("width") ? options.width : group.config.width;
  68. ui_control.height = options.hasOwnProperty("height") ? options.height : 0;
  69. }
  70. else {
  71. node = {
  72. id: "-dummy-",
  73. on: function() {}
  74. };
  75. }
  76. ui_control.templateScope = options.hasOwnProperty("templateScope") ? options.templateScope : "local";
  77. var ui_options = {
  78. node: node,
  79. control: ui_control
  80. }
  81. if (is_local) {
  82. ui_options.group = group;
  83. ui_options.tab = tab;
  84. }
  85. if (options.hasOwnProperty("emitOnlyNewValues")) {
  86. ui_options.emitOnlyNewValues = options.emitOnlyNewValues;
  87. }
  88. if (options.hasOwnProperty("forwardInputMessages")) {
  89. ui_options.forwardInputMessages = options.forwardInputMessages;
  90. }
  91. if (options.hasOwnProperty("storeFrontEndInputAsState")) {
  92. ui_options.storeFrontEndInputAsState = options.storeFrontEndInputAsState;
  93. }
  94. if (options.hasOwnProperty("persistantFrontEndValue")) {
  95. ui_options.persistantFrontEndValue = options.persistantFrontEndValue;
  96. }
  97. if (options.hasOwnProperty("convert")) {
  98. ui_options.convert = options.convert;
  99. }
  100. if (options.hasOwnProperty("beforeEmit")) {
  101. ui_options.beforeEmit = options.beforeEmit;
  102. }
  103. if (options.hasOwnProperty("convertBack")) {
  104. ui_options.convertBack = options.convertBack;
  105. }
  106. if (options.hasOwnProperty("beforeSend")) {
  107. ui_options.beforeSend = options.beforeSend;
  108. }
  109. if (options.hasOwnProperty("initController")) {
  110. ui_control.initController = options.initController.toString();
  111. }
  112. return ui.add(ui_options);
  113. }
  114. /* getSizes:
  115. returns the grid size in pixels
  116. default - { sx: 48, sy: 48, gx: 6, gy: 6, cx: 6, cy: 6, px: 0, py: 0 }
  117. */
  118. /* getTheme:
  119. returns the current theme object
  120. */
  121. /* isDark:
  122. returns true or false if the dahsboard theme background is dark or light.
  123. */
  124. module.exports = function (RED) {
  125. return {
  126. addWidget: function (options) { return addWidget(RED, options); },
  127. getSizes: function() { return require("./ui")(RED).getSizes(); },
  128. getTheme: function() { return require("./ui")(RED).getTheme(); },
  129. isDark: function() { return require("./ui")(RED).isDark(); }
  130. };
  131. };