port_select.js 1.8 KB

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