FormulaDialog.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * FormulaDialog.js
  3. *
  4. * @author realor
  5. */
  6. import { Dialog } from './Dialog.js'
  7. import { Formula } from '../formula/Formula.js'
  8. import { I18N } from '../i18n/I18N.js'
  9. class FormulaDialog extends Dialog {
  10. constructor(application, object, formula) {
  11. super('title.formula')
  12. this.application = application
  13. this.object = object
  14. this.setI18N(this.application.i18n)
  15. this.setSize(640, 300)
  16. const path = formula ? formula.path : ''
  17. const expression = formula ? formula.expression : ''
  18. this.pathElem = this.addTextField('path', 'label.formula.path', path, 'code')
  19. this.editorView = this.addCodeEditor('editor', 'label.formula.expression', expression, { language: 'javascript', height: 'calc(100% - 80px)' })
  20. this.errorElem = document.createElement('div')
  21. this.errorElem.className = 'error'
  22. this.bodyElem.appendChild(this.errorElem)
  23. this.addButton('accept', 'button.accept', () => {
  24. this.onAccept()
  25. })
  26. this.cancelButton = this.addButton('cancel', 'button.cancel', () => this.onCancel())
  27. }
  28. onShow() {
  29. if (this.pathElem.value.length === 0) {
  30. this.pathElem.focus()
  31. } else {
  32. this.editorView.focus()
  33. }
  34. }
  35. onAccept() {
  36. const path = this.pathElem.value
  37. const expression = this.editorView.state.doc.toString()
  38. if (path) {
  39. if (expression.length > 0) {
  40. try {
  41. Formula.create(this.object, path, expression)
  42. this.application.notifyObjectsChanged(this.object, this)
  43. this.hide()
  44. } catch (ex) {
  45. this.errorElem.innerHTML = String(ex)
  46. }
  47. } else {
  48. Formula.remove(this.object, path)
  49. this.application.notifyObjectsChanged(this.object, this)
  50. this.hide()
  51. }
  52. }
  53. }
  54. onCancel() {
  55. this.hide()
  56. }
  57. }
  58. export { FormulaDialog }