CancelDialog.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import {Button, Form} from 'tui-lib/ui/controls'
  2. import {Label, Pane} from 'tui-lib/ui/presentation'
  3. import {FocusElement} from 'tui-lib/ui/primitives'
  4. import telc from 'tui-lib/util/telchars'
  5. export default class CancelDialog extends FocusElement {
  6. // A basic cancel dialog. Has one buttons, cancel, and a label.
  7. // The escape (esc) key can be used to exit the dialog (which sends a
  8. // 'cancelled' event, as the cancel button also does).
  9. constructor(text) {
  10. super()
  11. this.pane = new Pane()
  12. this.addChild(this.pane)
  13. this.cancelBtn = new Button('Cancel')
  14. this.pane.addChild(this.cancelBtn)
  15. this.label = new Label(text)
  16. this.pane.addChild(this.label)
  17. this.initEventListeners()
  18. }
  19. initEventListeners() {
  20. this.cancelBtn.on('pressed', () => this.cancelPressed())
  21. }
  22. fixLayout() {
  23. this.w = this.parent.contentW
  24. this.h = this.parent.contentH
  25. this.pane.w = Math.max(40, 4 + this.label.w)
  26. this.pane.h = 7
  27. this.pane.centerInParent()
  28. this.label.x = Math.floor((this.pane.contentW - this.label.w) / 2)
  29. this.label.y = 1
  30. this.cancelBtn.x = Math.floor(
  31. (this.pane.contentW - this.cancelBtn.w) / 2)
  32. this.cancelBtn.y = this.pane.contentH - 2
  33. }
  34. selected() {
  35. this.root.select(this.cancelBtn)
  36. }
  37. keyPressed(keyBuf) {
  38. if (telc.isCancel(keyBuf)) {
  39. this.emit('cancelled')
  40. }
  41. }
  42. cancelPressed() {
  43. this.emit('cancelled')
  44. }
  45. }