Button.js 921 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import {FocusElement} from 'tui-lib/ui/primitives'
  2. import * as ansi from 'tui-lib/util/ansi'
  3. import telc from 'tui-lib/util/telchars'
  4. export default class Button extends FocusElement {
  5. // A button.
  6. constructor(text) {
  7. super()
  8. this.text = text
  9. this.cursorX = null
  10. this.cursorY = null
  11. }
  12. fixLayout() {
  13. this.w = ansi.measureColumns(this.text)
  14. this.h = 1
  15. }
  16. drawTo(writable) {
  17. if (this.isSelected) {
  18. writable.write(ansi.invert())
  19. }
  20. writable.write(ansi.moveCursor(this.absTop, this.absLeft))
  21. writable.write(this.text)
  22. writable.write(ansi.resetAttributes())
  23. super.drawTo(writable)
  24. }
  25. keyPressed(keyBuf) {
  26. if (telc.isSelect(keyBuf)) {
  27. this.emit('pressed')
  28. }
  29. }
  30. clicked(button) {
  31. if (button === 'left') {
  32. if (this.isSelected) {
  33. this.emit('pressed')
  34. } else {
  35. this.root.select(this)
  36. }
  37. }
  38. }
  39. }