Radio.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. class Radio extends UIObject
  2. {
  3. constructor(name)
  4. {
  5. super(name);
  6. this.P5Element = createRadio();
  7. this.setPosition(10, 10);
  8. this.setStyle(DEFAULT_STYLE);
  9. this.multiLine = false;
  10. this.connectCallbacks();
  11. this.P5Element.changed(this.onChanged);
  12. }
  13. // Setters
  14. setSelected(value)
  15. {
  16. this.P5Element.selected(value);
  17. }
  18. // Getters
  19. getSelected()
  20. {
  21. return this.P5Element.selected();
  22. }
  23. // Methods
  24. addOption(value)
  25. {
  26. this.P5Element.option(value);
  27. if (this.multiLine) this.makeMultiline();
  28. }
  29. // TODO make this work question mark?
  30. // removeOption(value)
  31. // {
  32. // this.P5Element.remove(value);
  33. // }
  34. makeMultiline()
  35. {
  36. this.multiLine = true;
  37. const inputs = selectAll('input', this.P5Element),
  38. labels = selectAll('label', this.P5Element),
  39. len = inputs.length;
  40. for (let i = 0; i < len; ++i)
  41. createDiv().parent(this.P5Element).child(inputs[i]).child(labels[i]);
  42. this.fixRadioDivElement();
  43. }
  44. fixRadioDivElement()
  45. {
  46. this.P5Element._getInputChildrenArray = function()
  47. {
  48. return this.elt.getElementsByTagName('input');
  49. }
  50. }
  51. // Callbacks
  52. _onChanged()
  53. {
  54. console.log(this.getSelected());
  55. }
  56. onChanged()
  57. {
  58. this.pandoraObject._onChanged();
  59. }
  60. }