ConfirmDialog.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * ConfirmDialog.js
  3. *
  4. * @author realor
  5. */
  6. import { Dialog } from './Dialog.js'
  7. import { I18N } from '../i18n/I18N.js'
  8. class ConfirmDialog extends Dialog {
  9. action = null
  10. constructor(title, message, ...args) {
  11. super(title)
  12. if (typeof message !== 'string') {
  13. message = String(message)
  14. }
  15. this.addTextWithArgs(message, args)
  16. this.acceptButton = this.addButton('confirm_accept', 'button.accept', () => this.onAccept())
  17. this.cancelButton = this.addButton('confirm_cancel', 'button.cancel', () => this.onCancel())
  18. this.setClassName('confirm')
  19. }
  20. static create(title, message, ...args) {
  21. return new ConfirmDialog(title, message, ...args)
  22. }
  23. setAction(action) {
  24. this.action = action
  25. return this
  26. }
  27. setAcceptLabel(label) {
  28. I18N.set(this.acceptButton, 'innerHTML', label)
  29. return this
  30. }
  31. setCancelLabel(label) {
  32. I18N.set(this.cancelButton, 'innerHTML', label)
  33. return this
  34. }
  35. onShow() {
  36. this.cancelButton.focus()
  37. }
  38. onAccept() {
  39. this.action()
  40. this.hide()
  41. }
  42. onCancel() {
  43. this.hide()
  44. }
  45. }
  46. export { ConfirmDialog }