OpenLocalTool.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * OpenLocalTool.js
  3. *
  4. * @author realor
  5. */
  6. import { Tool } from './Tool.js'
  7. import { IOManager } from '../io/IOManager.js'
  8. import { ObjectUtils } from '../utils/ObjectUtils.js'
  9. import { MessageDialog } from '../ui/MessageDialog.js'
  10. class OpenLocalTool extends Tool {
  11. constructor(application, options) {
  12. super(application)
  13. this.name = 'openlocal'
  14. this.label = 'tool.openlocal.label'
  15. this.help = 'tool.openlocal.help'
  16. this.className = 'openlocal'
  17. this.setOptions(options)
  18. this._onChange = this.onChange.bind(this)
  19. this._onFocus = this.onFocus.bind(this)
  20. }
  21. activate() {
  22. let inputFile = document.createElement('input')
  23. this.inputFile = inputFile
  24. inputFile.type = 'file'
  25. inputFile.id = this.name + '_file'
  26. const extensions = IOManager.getSupportedLoaderExtensions()
  27. inputFile.accept = extensions.map(extension => '.' + extension).join(', ')
  28. document.body.appendChild(inputFile)
  29. inputFile.addEventListener('change', this._onChange, false)
  30. document.body.addEventListener('focus', this._onFocus, true)
  31. inputFile.click()
  32. }
  33. deactivate() {
  34. if (this.inputFile) {
  35. let parentNode = this.inputFile.parentNode
  36. parentNode.removeChild(this.inputFile)
  37. }
  38. document.body.removeEventListener('focus', this._onFocus, true)
  39. }
  40. onChange(event) {
  41. let files = this.inputFile.files
  42. if (files.length > 0) {
  43. let file = files[0]
  44. let reader = new FileReader()
  45. const application = this.application
  46. const t0 = Date.now()
  47. reader.onload = evt => {
  48. const t1 = Date.now()
  49. console.info('File read as text in ' + (t1 - t0) + ' millis.')
  50. let data = evt.target.result
  51. let intent = {
  52. url: 'file://' + file.name,
  53. data: data,
  54. onProgress: data => {
  55. application.progressBar.progress = data.progress
  56. application.progressBar.message = data.message
  57. },
  58. onCompleted: object => {
  59. object.updateMatrix()
  60. application.initControllers(object)
  61. application.addObject(object, application.baseObject, false, true)
  62. let container = application.container
  63. let aspect = container.clientWidth / container.clientHeight
  64. let camera = application.camera
  65. object.updateMatrixWorld(true)
  66. ObjectUtils.zoomAll(camera, object, aspect)
  67. application.notifyObjectsChanged(camera, this)
  68. application.progressBar.visible = false
  69. },
  70. onError: error => {
  71. console.error(error)
  72. application.progressBar.visible = false
  73. MessageDialog.create('ERROR', error)
  74. .setClassName('error')
  75. .setI18N(application.i18n)
  76. .show()
  77. },
  78. manager: this.application.loadingManager,
  79. units: application.units
  80. }
  81. IOManager.load(intent) // async load
  82. }
  83. application.progressBar.message = 'Loading file...'
  84. application.progressBar.progress = undefined
  85. application.progressBar.visible = true
  86. let formatInfo = IOManager.getFormatInfo(file.name)
  87. if (formatInfo.loader.dataType === 'arraybuffer') {
  88. reader.readAsArrayBuffer(file)
  89. } else {
  90. reader.readAsText(file)
  91. }
  92. }
  93. }
  94. onFocus(event) {
  95. this.application.useTool(null)
  96. }
  97. }
  98. export { OpenLocalTool }