Radio.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /************************************************************************
  2. * Radio.js
  3. ************************************************************************
  4. * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
  5. *
  6. * This file is part of Pandora.
  7. *
  8. * Pandora is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Pandora is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Pandora. If not, see <https://www.gnu.org/licenses/>.
  20. *************************************************************************/
  21. class Radio extends UIObject
  22. {
  23. constructor(name)
  24. {
  25. super(name);
  26. this.P5Element = createRadio();
  27. this.setPosition(10, 10);
  28. this.setStyle(DEFAULT_STYLE);
  29. this.multiLine = false;
  30. this.connectCallbacks();
  31. this.P5Element.changed(this.onChanged);
  32. }
  33. // Setters
  34. setSelected(value)
  35. {
  36. this.P5Element.selected(value);
  37. }
  38. // Getters
  39. getSelected()
  40. {
  41. return this.P5Element.selected();
  42. }
  43. // Methods
  44. addOption(value)
  45. {
  46. this.P5Element.option(value);
  47. if (this.multiLine) this.makeMultiline();
  48. }
  49. // TODO make this work question mark?
  50. // removeOption(value)
  51. // {
  52. // this.P5Element.remove(value);
  53. // }
  54. makeMultiline()
  55. {
  56. this.multiLine = true;
  57. const inputs = selectAll('input', this.P5Element),
  58. labels = selectAll('label', this.P5Element),
  59. len = inputs.length;
  60. for (let i = 0; i < len; ++i)
  61. createDiv().parent(this.P5Element).child(inputs[i]).child(labels[i]);
  62. this.fixRadioDivElement();
  63. }
  64. fixRadioDivElement()
  65. {
  66. this.P5Element._getInputChildrenArray = function()
  67. {
  68. return this.elt.getElementsByTagName('input');
  69. }
  70. }
  71. // Callbacks
  72. _onChanged()
  73. {
  74. console.log(this.getSelected());
  75. }
  76. onChanged()
  77. {
  78. this.pandoraObject._onChanged();
  79. }
  80. }