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_ui_control.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. module.exports = function(RED) {
  2. var ui = require('../ui')(RED);
  3. function UiControlNode(config) {
  4. RED.nodes.createNode(this, config);
  5. this.events = config.events || "all";
  6. var node = this;
  7. this.on('input', function(msg) {
  8. if (typeof msg.payload !== "object") { msg.payload = {tab:msg.payload}; }
  9. // show/hide or enable/disable tabs
  10. if (msg.payload.hasOwnProperty("tabs")) {
  11. ui.emit('ui-control', {tabs:msg.payload.tabs, socketid:msg.socketid});
  12. }
  13. // switch to tab name (or number)
  14. if (msg.payload.hasOwnProperty("tab")) {
  15. ui.emit('ui-control', {tab:msg.payload.tab, socketid:msg.socketid});
  16. }
  17. // show or hide ui groups
  18. if (msg.payload.hasOwnProperty("group")) {
  19. ui.emit('ui-control', {group:msg.payload.group, socketid:msg.socketid});
  20. }
  21. });
  22. var sendconnect = function(id, ip) {
  23. node.send({payload:"connect", socketid:id, socketip:ip});
  24. };
  25. var sendlost = function(id, ip) {
  26. node.send({payload:"lost", socketid:id, socketip:ip});
  27. };
  28. var sendchange = function(index, name, id, ip, p) {
  29. node.send({payload:"change", tab:index, name:name, socketid:id, socketip:ip, params:p});
  30. }
  31. var sendcollapse = function(group, state, id, ip) {
  32. node.send({payload:"group", group:group, open:state, socketid:id, socketip:ip,});
  33. }
  34. if (node.events === "connect") {
  35. ui.ev.on('newsocket', sendconnect);
  36. }
  37. else if (node.events === "change") {
  38. ui.ev.on('changetab', sendchange);
  39. ui.ev.on('collapse', sendcollapse);
  40. }
  41. else {
  42. ui.ev.on('newsocket', sendconnect);
  43. ui.ev.on('changetab', sendchange);
  44. ui.ev.on('collapse', sendcollapse);
  45. ui.ev.on('endsocket', sendlost);
  46. }
  47. this.on('close', function() {
  48. if (node.events === "connect") {
  49. ui.ev.removeListener('newsocket', sendconnect);
  50. }
  51. else if (node.events === "change") {
  52. ui.ev.removeListener('changetab', sendchange);
  53. ui.ev.removeListener('collapse', sendcollapse);
  54. }
  55. else {
  56. ui.ev.removeListener('newsocket', sendconnect);
  57. ui.ev.removeListener('changetab', sendchange);
  58. ui.ev.removeListener('collapse', sendcollapse);
  59. ui.ev.removeListener('endsocket', sendlost);
  60. }
  61. })
  62. }
  63. RED.nodes.registerType("ui_ui_control", UiControlNode);
  64. };