Source: view/menu/Button.js

  1. /** @module */
  2. /**
  3. * Class for a single button.
  4. * @param {*} buttonConfig
  5. * @param {*} onChoose
  6. * @constructor
  7. */
  8. export default function Button(buttonConfig, onChoose) {
  9. const self = this
  10. self.config = buttonConfig
  11. self.name = buttonConfig.name
  12. self.nameIsHTML = buttonConfig.nameIsHTML || false
  13. self.dom = document.createElement('div')
  14. self.dom.setAttribute('class', 'button button2')
  15. // overwrite margins. The old way was not working.
  16. let fixMargin = buttonConfig.margin
  17. fixMargin = fixMargin || 0
  18. fixMargin = Math.max(fixMargin, 4)
  19. self.dom.style.marginRight = `${fixMargin}px`
  20. // Click!
  21. self.draw = function () {
  22. if (self.nameIsHTML) {
  23. self.dom.innerHTML = self.name
  24. } else {
  25. self.dom.innerText = self.name
  26. }
  27. }
  28. self.draw()
  29. self.dom.setAttribute('title', buttonConfig.explain || '')
  30. self.onClick = function () {
  31. onChoose(self, buttonConfig)
  32. }
  33. self.dom.onclick = self.onClick
  34. // Turn on or off!
  35. self.turnOff = function () {
  36. self.isOn = false
  37. self.config.isOn = false
  38. self.dom.setAttribute('on', 'no')
  39. }
  40. self.turnOn = function () {
  41. self.isOn = true
  42. self.config.isOn = true
  43. self.dom.setAttribute('on', 'yes')
  44. }
  45. self.turnOff()
  46. }