port_select.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. module.exports = class PortSelect {
  3. constructor(selectElement) {
  4. this.selectElement = selectElement;
  5. this.map = {}; // Cache matching the text value of a port to OPTION element.
  6. }
  7. /**
  8. * Appends a single port to the end of serial port SELECT element.
  9. * Adds port to map.
  10. * @param port
  11. */
  12. add(port) {
  13. const option = this.createOption(port);
  14. this.map[port] = option;
  15. this.selectElement.appendChild(option);
  16. }
  17. /**
  18. * Removed single port from the serial port SELECT element.
  19. * Removes port from map.
  20. * @param port
  21. */
  22. remove(port) {
  23. this.selectElement.removeChild(this.map[port]);
  24. delete this.map[port];
  25. }
  26. /**
  27. * Removes existing comment, adds ports to the serial port SELECT element.
  28. * @param ports. An Array of strings.
  29. */
  30. addAll(ports) {
  31. ports.forEach(port => {
  32. this.add(port);
  33. });
  34. }
  35. /**
  36. * Creates option with the port text and value.
  37. * @param port
  38. * @returns {Element}
  39. */
  40. createOption(port) {
  41. const option = document.createElement("option");
  42. option.textContent = port;
  43. option.value = port;
  44. return option;
  45. }
  46. /**
  47. * Pass through.
  48. * Updates the disabled attribute on the SELECT element.
  49. * @param value
  50. */
  51. set disabled (value) {
  52. this.selectElement.disabled = value;
  53. }
  54. get value() {
  55. return this.selectElement.value;
  56. }
  57. };