ConfirmDialog.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 ConfirmDialog extends FocusElement {
  6. // A basic yes/no dialog. Has two buttons, confirm/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.form = new Form()
  14. this.pane.addChild(this.form)
  15. this.confirmBtn = new Button('Confirm')
  16. this.form.addInput(this.confirmBtn)
  17. this.cancelBtn = new Button('Cancel')
  18. this.form.addInput(this.cancelBtn)
  19. this.label = new Label(text)
  20. this.form.addChild(this.label)
  21. this.initEventListeners()
  22. }
  23. initEventListeners() {
  24. this.confirmBtn.on('pressed', () => this.confirmPressed())
  25. this.cancelBtn.on('pressed', () => this.cancelPressed())
  26. }
  27. fixLayout() {
  28. this.w = this.parent.contentW
  29. this.h = this.parent.contentH
  30. this.pane.w = Math.max(40, 2 + this.label.w)
  31. this.pane.h = 7
  32. this.pane.centerInParent()
  33. this.form.w = this.pane.contentW
  34. this.form.h = this.pane.contentH
  35. this.label.x = Math.floor((this.form.contentW - this.label.w) / 2)
  36. this.label.y = 1
  37. this.confirmBtn.x = 1
  38. this.confirmBtn.y = this.form.contentH - 2
  39. this.cancelBtn.x = this.form.right - this.cancelBtn.w - 1
  40. this.cancelBtn.y = this.form.contentH - 2
  41. }
  42. selected() {
  43. this.root.select(this.form)
  44. }
  45. keyPressed(keyBuf) {
  46. if (telc.isCancel(keyBuf)) {
  47. this.emit('cancelled')
  48. }
  49. }
  50. confirmPressed() {
  51. this.emit('confirmed')
  52. }
  53. cancelPressed() {
  54. this.emit('cancelled')
  55. }
  56. }