PropertyDialog.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * PropertyDialog.js
  3. *
  4. * @author realor
  5. */
  6. import { Dialog } from './Dialog.js'
  7. class PropertyDialog extends Dialog {
  8. constructor(application, object, dictionary) {
  9. super('title.object_properties')
  10. this.application = application
  11. this.object = object
  12. this.dictionary = dictionary
  13. this.setI18N(this.application.i18n)
  14. this.setSize(240, 210)
  15. this.nameElem = this.addTextField('propertyName', 'label.property_name', '')
  16. this.nameElem.setAttribute('spellcheck', false)
  17. this.typeElem = this.addSelectField('propertyType', 'label.property_type', ['string', 'number', 'boolean', 'object'])
  18. this.valueElem = this.addTextField('propertyValue', 'label.property_value', '')
  19. this.acceptButton = this.addButton('accept', 'button.accept', () => this.onAccept())
  20. this.acceptButton.disabled = true
  21. this.cancelButton = this.addButton('cancel', 'button.cancel', () => this.onCancel())
  22. this.nameElem.addEventListener('input', () => {
  23. this.nameElem.value = this.nameElem.value.replace(/\s/g, '')
  24. this.acceptButton.disabled = this.nameElem.value.length === 0
  25. })
  26. }
  27. onShow() {
  28. this.nameElem.focus()
  29. }
  30. onAccept() {
  31. const propertyName = this.nameElem.value
  32. const propertyType = this.typeElem.value
  33. const propertyValue = this.valueElem.value
  34. const dictionary = this.dictionary
  35. this.hide()
  36. switch (propertyType) {
  37. case 'string':
  38. dictionary[propertyName] = propertyValue
  39. break
  40. case 'number':
  41. dictionary[propertyName] = Number(propertyValue)
  42. break
  43. case 'boolean':
  44. dictionary[propertyName] = Boolean(propertyValue)
  45. break
  46. case 'object':
  47. dictionary[propertyName] = {}
  48. break
  49. }
  50. this.application.notifyObjectsChanged(this.object, this)
  51. }
  52. onCancel() {
  53. this.hide()
  54. }
  55. }
  56. export { PropertyDialog }