Home Reference Source

cables_dev/cables/src/core/core_port_switch.js

  1.  
  2. import { Port } from "./core_port.js";
  3.  
  4.  
  5. class SwitchPort extends Port
  6. {
  7. constructor(__parent, name, type, uiAttribs, indexPort)
  8. {
  9. super(__parent, name, type, uiAttribs);
  10.  
  11. this.get = () =>
  12. {
  13. let s = super.get();
  14.  
  15. if (CABLES.UI)
  16. {
  17. if (
  18. s === "" ||
  19. s === null ||
  20. s === undefined ||
  21. (uiAttribs.values && uiAttribs.values.indexOf(String(s)) === -1)
  22. )
  23. {
  24. this.op.setUiError("invalidswitch", "Invalid Value [" + this.name + "]: \"" + s + "\"", 1);
  25. }
  26. else this.op.setUiError("invalidswitch", null);
  27. }
  28.  
  29. if (s === null || s === undefined)s = "";
  30.  
  31. return s;
  32. };
  33.  
  34. this.indexPort = indexPort;
  35. this.indexPort.set = (value) =>
  36. {
  37. const values = uiAttribs.values;
  38.  
  39. if (!values)
  40. {
  41. // console.log("switch port has no values", this);
  42. return;
  43. }
  44.  
  45. let intValue = Math.floor(value);
  46.  
  47. intValue = Math.min(intValue, values.length - 1);
  48. intValue = Math.max(intValue, 0);
  49.  
  50. this.indexPort.setValue(intValue);
  51. this.set(values[intValue]);
  52.  
  53. if (this.op.patch.isEditorMode() && performance.now() - (this.lastTime || 0) > 100 && window.gui && gui.patchView.isCurrentOp(this.op))
  54. {
  55. gui.opParams.show(this.op);
  56. this.lastTime = performance.now();
  57. }
  58. };
  59. }
  60.  
  61. setUiAttribs(attribs)
  62. {
  63. const hidePort = attribs.hidePort;
  64. attribs.hidePort = true;
  65. super.setUiAttribs(attribs);
  66. if (typeof hidePort !== "undefined")
  67. {
  68. this.indexPort.setUiAttribs({ hidePort });
  69. }
  70. }
  71. }
  72.  
  73. export { SwitchPort };