SaveDialog.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * SaveDialog.js
  3. *
  4. * @author realor
  5. */
  6. import { Dialog } from './Dialog.js'
  7. import { IOManager } from '../io/IOManager.js'
  8. class SaveDialog extends Dialog {
  9. static DEFAULT_EXPORT_FORMAT = 'brf'
  10. constructor(title, name) {
  11. super(title)
  12. this.setSize(250, 200)
  13. if (name === '') {
  14. name = 'scene'
  15. }
  16. this.nameElem = this.addTextField('saveEntryName', 'label.name', name)
  17. this.nameElem.setAttribute('spellcheck', 'false')
  18. let formatName = IOManager.getFormat(name)
  19. let formatInfo = IOManager.formats[formatName]
  20. if (!(formatInfo && formatInfo.exporterClass)) {
  21. formatName = SaveDialog.DEFAULT_EXPORT_FORMAT
  22. formatInfo = IOManager.formats[formatName]
  23. }
  24. let extension = formatInfo.extensions[0]
  25. this.setExtension(extension)
  26. let options = []
  27. for (let formatName in IOManager.formats) {
  28. let formatInfo = IOManager.formats[formatName]
  29. if (formatInfo.exporter) {
  30. options.push([formatName, formatInfo.description])
  31. }
  32. }
  33. this.formatSelect = this.addSelectField('saveFormat', 'label.format', options, formatName)
  34. this.formatSelect.addEventListener('change', event => this.setExtension(this.formatSelect.value))
  35. this.saveSelectionElem = this.addCheckBoxField('save_sel', 'label.save_selection', false)
  36. this.addButton('save', 'button.save', () => {
  37. this.hide()
  38. this.setExtension(this.formatSelect.value)
  39. this.onSave(this.nameElem.value, this.formatSelect.value, this.saveSelectionElem.checked)
  40. })
  41. this.cancelButton = this.addButton('cancel', 'button.cancel', () => this.onCancel())
  42. }
  43. onShow() {
  44. this.nameElem.focus()
  45. }
  46. onSave(name, format, onlySelection) {
  47. console.info('save ' + name + ' ' + format + ' ' + onlySelection)
  48. }
  49. onCancel() {
  50. this.hide()
  51. }
  52. setExtension(extension) {
  53. let name = this.nameElem.value
  54. let index = name.lastIndexOf('.')
  55. if (index !== -1) {
  56. name = name.substring(0, index + 1) + extension
  57. } else {
  58. name += '.' + extension
  59. }
  60. this.nameElem.value = name
  61. }
  62. }
  63. export { SaveDialog }