Radio.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(STYLE.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. initSignals()
  77. {
  78. this.addSignal("mousePressed");
  79. this.addSignal("doubleClicked");
  80. this.addSignal("mouseWheel");
  81. this.addSignal("mouseReleased");
  82. this.addSignal("mouseClicked");
  83. this.addSignal("mouseMoved");
  84. this.addSignal("mouseOver");
  85. this.addSignal("mouseOut");
  86. this.addSignal("touchStarted");
  87. this.addSignal("touchMoved");
  88. this.addSignal("touchEnded");
  89. this.addSignal("dragOver");
  90. this.addSignal("dragLeave");
  91. this.emitSignal("changed");
  92. this._initSignals();
  93. }
  94. onChanged()
  95. {
  96. this.pandoraObject.emitSignal("changed");
  97. this.pandoraObject._onChanged();
  98. }
  99. }