FocusElement.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import DisplayElement from './DisplayElement.js'
  2. export default class FocusElement extends DisplayElement {
  3. // A basic element that can receive cursor focus.
  4. constructor() {
  5. super()
  6. this.cursorVisible = false
  7. this.cursorX = 0
  8. this.cursorY = 0
  9. }
  10. selected() {
  11. // Should be overridden in subclasses.
  12. }
  13. unselected() {
  14. // Should be overridden in subclasses.
  15. }
  16. get selectable() {
  17. // Should be overridden if you want to make the element unselectable
  18. // (according to particular conditions).
  19. return true
  20. }
  21. keyPressed(keyBuf) {
  22. // Do something with a buffer containing the key pressed (that is,
  23. // telnet data sent). Should be overridden in subclasses.
  24. //
  25. // Arrow keys are sent as a buffer in the form of
  26. // ESC[# where # is A, B, C or D. See more here:
  27. // http://stackoverflow.com/a/11432632/4633828
  28. }
  29. get isSelected() {
  30. const selected = this.root.selectedElement
  31. return !!(selected && [selected, ...selected.directAncestors].includes(this))
  32. }
  33. get absCursorX() { return this.absX + this.cursorX }
  34. get absCursorY() { return this.absY + this.cursorY }
  35. }