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.

processing.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.initController = exports.beforeEmitFactory = void 0;
  4. const miscellanious_1 = require("./miscellanious");
  5. const getColorForValue = (colorForValue, value, RED) => {
  6. let color, found = false;
  7. try {
  8. if (Array.isArray(colorForValue)) {
  9. for (let index = 0; index < colorForValue.length; index++) {
  10. const compareWith = colorForValue[index];
  11. if (RED.util.compareObjects(compareWith.value, value)) {
  12. color = compareWith.color;
  13. found = true;
  14. break;
  15. }
  16. }
  17. }
  18. else if (typeof colorForValue === 'object') {
  19. color = colorForValue[value];
  20. found = color !== undefined && color !== null;
  21. }
  22. }
  23. catch (_error) {
  24. // TODO: Not an error to receive an unaccounted for value, but we should log to Node-RED debug log
  25. // console.log("Error trying to find color for value '" + value + "'", error)
  26. }
  27. if (found === false || color === undefined) {
  28. color = 'gray';
  29. }
  30. return [color, found];
  31. };
  32. const beforeEmitFactory = (node, RED) => {
  33. return (
  34. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  35. msg, value) => {
  36. if (node.allowColorForValueInMessage === true &&
  37. typeof msg.colorForValue !== 'undefined') {
  38. const ledMsg = msg;
  39. const msgColorForValue = ledMsg.colorForValue;
  40. if (msgColorForValue !== undefined) {
  41. node.overrideColorForValue = msgColorForValue;
  42. }
  43. }
  44. const colorForValue = node.overrideColorForValue || node.colorForValue;
  45. const [color, glow] = getColorForValue(colorForValue, value, RED);
  46. return {
  47. msg: {
  48. ...msg,
  49. color,
  50. glow: node.showGlow ? glow : false,
  51. sizeMultiplier: node.height
  52. }
  53. };
  54. };
  55. };
  56. exports.beforeEmitFactory = beforeEmitFactory;
  57. // TODO: why is initController stringed and evaled??? we have to move erryone into this file :/
  58. const initController = ($scope, _events) => {
  59. $scope.flag = true;
  60. // TODO: From miscellanious.ts, we need to resolve this issue
  61. // Based on: https://stackoverflow.com/a/14570614
  62. const observeDOMFactory = () => {
  63. const MutationObserver = window.MutationObserver || miscellanious_1.WebKitMutationObserver;
  64. return (observe, callback) => {
  65. if (!observe) {
  66. throw new Error('Element to observe not provided');
  67. }
  68. if (observe.nodeType !== 1 &&
  69. observe.nodeType !== 9 &&
  70. observe.nodeType !== 11) {
  71. throw new Error('Unexpected Node type (' + observe.nodeType + ') provided: ' + observe);
  72. }
  73. if (MutationObserver) {
  74. const observer = new MutationObserver((mutations, observer) => {
  75. observer.disconnect();
  76. callback(mutations);
  77. });
  78. observer.observe(observe, {
  79. childList: true,
  80. subtree: true
  81. });
  82. }
  83. else if (window.addEventListener !== undefined) {
  84. const options = {
  85. capture: false,
  86. once: true
  87. };
  88. observe.addEventListener('DOMNodeInserted', callback, options);
  89. observe.addEventListener('DOMNodeRemoved', callback, options);
  90. }
  91. };
  92. };
  93. const glowSize = 7;
  94. const ledStyle = (color, glow, sizeMultiplier) => {
  95. if (glow) {
  96. return `
  97. background-color: ${color};
  98. box-shadow:
  99. #0000009e 0 0px ${2 / window.devicePixelRatio}px 0px,
  100. ${color} 0 0px ${glowSize * sizeMultiplier}px ${Math.floor((glowSize * sizeMultiplier) / 3)}px,
  101. inset #00000017 0 -1px 1px 0px;`;
  102. }
  103. else {
  104. // TODO: duplicate code because of execution scope, fix this shit :|
  105. return `
  106. background-color: ${color};
  107. box-shadow:
  108. #0000009e 0 0px ${2 / window.devicePixelRatio}px 0px,
  109. inset #ffffff8c 0px 1px 2px,
  110. inset #00000033 0 -1px 1px 0px,
  111. inset ${color} 0 -1px 2px;`;
  112. }
  113. };
  114. const update = (msg, element) => {
  115. if (!msg) {
  116. return;
  117. }
  118. if (!element) {
  119. return;
  120. }
  121. const color = msg.color;
  122. const glow = msg.glow;
  123. const sizeMultiplier = msg.sizeMultiplier;
  124. $(element).attr('style', ledStyle(color, glow, sizeMultiplier));
  125. };
  126. const retrieveElementFromDocument = (id, document) => {
  127. // TODO: share code to make sure we're always using the same id composure
  128. const elementId = 'led_' + id;
  129. if (!document) {
  130. return undefined;
  131. }
  132. return document.getElementById(elementId);
  133. };
  134. const observeDOM = observeDOMFactory();
  135. const updateWithScope = (msg) => {
  136. if (!$scope) {
  137. return;
  138. }
  139. const id = $scope.$eval('$id');
  140. const attemptUpdate = () => {
  141. const element = retrieveElementFromDocument(id, document);
  142. if (element) {
  143. update(msg, element);
  144. }
  145. else {
  146. // HACK: is there a proper way to wait for this node's element to be rendered?
  147. observeDOM(document, (_change) => {
  148. attemptUpdate();
  149. });
  150. }
  151. };
  152. attemptUpdate();
  153. };
  154. $scope.$watch('msg', updateWithScope);
  155. };
  156. exports.initController = initController;
  157. //# sourceMappingURL=processing.js.map