Dialog.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {Pane} from 'tui-lib/ui/presentation'
  2. import {FocusElement} from 'tui-lib/ui/primitives'
  3. import telc from 'tui-lib/util/telchars'
  4. export default class Dialog extends FocusElement {
  5. // A simple base dialog.
  6. //
  7. // Emits the 'cancelled' event when the cancel key (escape) is pressed,
  8. // which should (probably) be handled by the dialog's creator.
  9. //
  10. // Doesn't do anything when focused by default - this should be overridden
  11. // in subclasses.
  12. //
  13. // Automatically adjusts to fill its parent. Has a pane child (this.pane),
  14. // but the pane isn't adjusted at all (you should change its size and
  15. // likely center it in your subclass).
  16. constructor() {
  17. super()
  18. this.pane = new Pane()
  19. this.addChild(this.pane)
  20. }
  21. fixLayout() {
  22. this.w = this.parent.contentW
  23. this.h = this.parent.contentH
  24. }
  25. open() {
  26. this.oldSelectedElement = this.root.selectedElement
  27. this.opened()
  28. this.visible = true
  29. this.root.select(this)
  30. this.fixLayout()
  31. }
  32. close() {
  33. this.closed()
  34. this.visible = false
  35. this.root.select(this.oldSelectedElement)
  36. }
  37. opened() {}
  38. closed() {}
  39. keyPressed(keyBuf) {
  40. if (telc.isCancel(keyBuf)) {
  41. this.emit('cancelled')
  42. return false
  43. }
  44. }
  45. }