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_template.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. module.exports = function(RED) {
  2. var ui = require('../ui')(RED);
  3. function TemplateNode(config) {
  4. RED.nodes.createNode(this, config);
  5. var node = this;
  6. var group = RED.nodes.getNode(config.group);
  7. if (!group && config.templateScope !== 'global') { return; }
  8. var tab = null;
  9. if (config.templateScope !== 'global') {
  10. tab = RED.nodes.getNode(group.config.tab);
  11. if (!tab) { return; }
  12. if (!config.width) {
  13. config.width = group.config.width;
  14. }
  15. }
  16. var hei = Number(config.height|| 0);
  17. var previousTemplate = null
  18. var theme = ui.getTheme();
  19. var colortheme = {};
  20. for (var i in theme) {
  21. if (theme.hasOwnProperty(i)) {
  22. colortheme[i.replace(/-/g, "_")] = theme[i].value;
  23. }
  24. }
  25. var done = ui.add({
  26. forwardInputMessages: config.fwdInMessages,
  27. storeFrontEndInputAsState: config.storeOutMessages,
  28. persistantFrontEndValue: config.resendOnRefresh,
  29. emitOnlyNewValues: false,
  30. node: node,
  31. tab: tab,
  32. group: group,
  33. control: {
  34. type: 'template',
  35. order: config.order,
  36. width: config.width || 6,
  37. height: hei,
  38. format: config.format,
  39. templateScope: config.templateScope,
  40. theme: colortheme,
  41. className: config.className || '',
  42. },
  43. beforeEmit: function(msg) {
  44. var properties = Object.getOwnPropertyNames(msg).filter(function (p) { return p[0] != '_'; });
  45. var clonedMsg = {
  46. templateScope: config.templateScope
  47. };
  48. for (var i=0; i<properties.length; i++) {
  49. var property = properties[i];
  50. clonedMsg[property] = msg[property];
  51. }
  52. // transform to string if msg.template is buffer
  53. if (clonedMsg.template !== undefined && Buffer.isBuffer(clonedMsg.template)) {
  54. clonedMsg.template = clonedMsg.template.toString();
  55. }
  56. if (clonedMsg.template === undefined && previousTemplate !== null) {
  57. clonedMsg.template = previousTemplate;
  58. }
  59. //This updates the whole page if the template input changes and
  60. //height set to auto - performance killer, but here just in case
  61. // if ((config.height == "0") && (value !== node.oldvalue)) {
  62. // node.oldvalue = value;
  63. // setImmediate(function() { ui.updateUi(); });
  64. // }
  65. if (clonedMsg.template) {
  66. previousTemplate = clonedMsg.template
  67. }
  68. return { msg:clonedMsg };
  69. },
  70. beforeSend: function (msg, original) {
  71. if (original && original.hasOwnProperty("msg") && original.msg !== null) {
  72. var om = original.msg;
  73. om.socketid = original.socketid;
  74. return om;
  75. }
  76. }
  77. });
  78. node.on("close", done);
  79. }
  80. RED.nodes.registerType("ui_template", TemplateNode);
  81. RED.library.register("uitemplates");
  82. };