SelectByQRCodeTool.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * SelectByQRCodeTool.js
  3. *
  4. * @author realor
  5. */
  6. import { Tool } from './Tool.js'
  7. import { Dialog } from '../ui/Dialog.js'
  8. import { MessageDialog } from '../ui/MessageDialog.js'
  9. import { ObjectUtils } from '../utils/ObjectUtils.js'
  10. import { Html5QrcodeScanner } from '../lib/html5-qrcode.min.js'
  11. class SelectByQRCodeTool extends Tool {
  12. constructor(application, options) {
  13. super(application)
  14. this.name = 'select_by_qrcode'
  15. this.label = 'tool.select_by_qrcode.label'
  16. this.className = 'select_by_qrcode'
  17. this.immediate = true
  18. this.setOptions(options)
  19. this.dialog = this.createDialog()
  20. }
  21. createDialog() {
  22. this.dialog = new Dialog('title.select_by_qrcode')
  23. const dialog = this.dialog
  24. dialog.setSize(400, 550)
  25. dialog.setI18N(this.application.i18n)
  26. const divElem = document.createElement('div')
  27. divElem.id = 'qrcode_reader'
  28. dialog.bodyElem.appendChild(divElem)
  29. dialog.addButton('close', 'button.close', event => this.closeDialog())
  30. return dialog
  31. }
  32. execute() {
  33. this.dialog.show()
  34. const onScanSuccess = code => {
  35. this.closeDialog()
  36. this.selectObjectsByCode(code)
  37. }
  38. this.codeScanner = new Html5QrcodeScanner('qrcode_reader', { fps: 10, qrbox: 200 })
  39. this.codeScanner.render(onScanSuccess)
  40. }
  41. closeDialog() {
  42. this.codeScanner.clear()
  43. this.dialog.hide()
  44. }
  45. selectObjectsByCode(code) {
  46. console.info('CODE', code)
  47. const application = this.application
  48. const baseObject = application.baseObject
  49. const objects = []
  50. console.info('1', objects)
  51. baseObject.traverse(object => {
  52. if (object.uuid === code || object.name === code || this.dataContains(object.userData, code)) {
  53. console.info('PUSH', object)
  54. objects.push(object)
  55. }
  56. })
  57. if (objects.length > 0) {
  58. const container = application.container
  59. const aspect = container.clientWidth / container.clientHeight
  60. const camera = application.camera
  61. application.scene.updateMatrixWorld(true)
  62. ObjectUtils.zoomAll(camera, objects, aspect, true)
  63. application.notifyObjectsChanged(camera, this)
  64. application.selection.set(...objects)
  65. } else {
  66. MessageDialog.create('title.select_by_qrcode', 'message.no_object_for_code', code)
  67. .setClassName('info')
  68. .setI18N(application.i18n)
  69. .show()
  70. }
  71. }
  72. dataContains(data, code) {
  73. for (let key in data) {
  74. let value = data[key]
  75. if (typeof value === 'object') {
  76. if (this.dataContains(value, code)) return true
  77. } else if (typeof value === 'string') {
  78. if (value === code) return true
  79. } else if (typeof value === 'number') {
  80. if (String(value) === code) return true
  81. }
  82. }
  83. return false
  84. }
  85. }
  86. export { SelectByQRCodeTool }