Dialog.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * Dialog.js
  3. *
  4. * @author realor
  5. */
  6. import { Controls } from './Controls.js'
  7. import { I18N } from '../i18n/I18N.js'
  8. class Dialog {
  9. constructor(title) {
  10. this.title = title
  11. this.i18n = null
  12. this.curtainElem = document.createElement('div')
  13. this.curtainElem.className = 'dialog_curtain'
  14. this.dialogElem = document.createElement('div')
  15. this.dialogElem.className = 'dialog'
  16. this.headerElem = document.createElement('div')
  17. I18N.set(this.headerElem, 'innerHTML', title)
  18. this.headerElem.className = 'header'
  19. this.dialogElem.appendChild(this.headerElem)
  20. this.bodyElem = document.createElement('div')
  21. this.bodyElem.className = 'body'
  22. this.dialogElem.appendChild(this.bodyElem)
  23. this.footerElem = document.createElement('div')
  24. this.footerElem.className = 'footer'
  25. this.dialogElem.appendChild(this.footerElem)
  26. this.setSize(300, 200)
  27. }
  28. setSize(width, height) {
  29. this.dialogElem.style.width = width + 'px'
  30. this.dialogElem.style.height = height + 'px'
  31. return this
  32. }
  33. setI18N(i18n) {
  34. this.i18n = i18n
  35. return this
  36. }
  37. setClassName(className) {
  38. this.bodyElem.classList.add(className)
  39. return this
  40. }
  41. show(parentNode) {
  42. if (!this.dialogElem.parentNode) {
  43. if (this.i18n) {
  44. this.i18n.updateTree(this.dialogElem)
  45. }
  46. parentNode = parentNode || document.body
  47. parentNode.appendChild(this.curtainElem)
  48. parentNode.appendChild(this.dialogElem)
  49. if (this.onShow) this.onShow()
  50. }
  51. return this
  52. }
  53. hide() {
  54. const parentNode = this.dialogElem.parentNode
  55. if (parentNode) {
  56. parentNode.removeChild(this.dialogElem)
  57. parentNode.removeChild(this.curtainElem)
  58. if (this.onHide) this.onHide()
  59. }
  60. return this
  61. }
  62. addText(text, className) {
  63. return Controls.addText(this.bodyElem, text, className)
  64. }
  65. addTextWithArgs(text, args, className) {
  66. return Controls.addTextWithArgs(this.bodyElem, text, args, className)
  67. }
  68. addCode(text, className) {
  69. return Controls.addCode(this.bodyElem, text, className)
  70. }
  71. addTextField(name, label, value, className) {
  72. return Controls.addTextField(this.bodyElem, name, label, value, className || 'text_field')
  73. }
  74. addNumberField(name, label, value, className) {
  75. return Controls.addNumberField(this.bodyElem, name, label, value, className || 'text_field')
  76. }
  77. addTextAreaField(name, label, value, className) {
  78. return Controls.addTextAreaField(this.bodyElem, name, label, value, className || 'text_field')
  79. }
  80. addPasswordField(name, label, value, className) {
  81. return Controls.addPasswordField(this.bodyElem, name, label, value, className || 'text_field')
  82. }
  83. addSelectField(name, label, options, value, className) {
  84. return Controls.addSelectField(this.bodyElem, name, label, options, value, className || 'select_field')
  85. }
  86. addCheckBoxField(name, label, checked, className) {
  87. return Controls.addCheckBoxField(this.bodyElem, name, label, checked, className || 'checkbox_field')
  88. }
  89. addRadioButtons(name, options, value, className) {
  90. return Controls.addRadioButtons(this.bodyElem, name, options, value, className || 'radio_buttons')
  91. }
  92. addButton(name, label, action) {
  93. return Controls.addButton(this.footerElem, name, label, action)
  94. }
  95. addCodeEditor(name, label, value, options) {
  96. return Controls.addCodeEditor(this.bodyElem, name, label, value, options)
  97. }
  98. }
  99. export { Dialog }