DisplayController.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * DisplayController.js
  3. *
  4. * @author realor
  5. */
  6. import { PanelController } from './PanelController.js'
  7. import { Controller } from './Controller.js'
  8. import { Controls } from '../ui/Controls.js'
  9. import { I18N } from '../i18n/I18N.js'
  10. class DisplayController extends PanelController {
  11. constructor(object, name) {
  12. super(object, name)
  13. this.input = 0
  14. this.units = 'meters'
  15. this.decimals = 2
  16. this.detailUrl = ''
  17. this.detailTarget = '_blank'
  18. this.detailLabel = 'Show more'
  19. this.displayClass = 'default'
  20. }
  21. createPanel() {
  22. super.createPanel('left', 80)
  23. let panelElem = document.createElement('div')
  24. panelElem.className = 'display ' + (this.displayClass || 'default')
  25. this.displayElem = document.createElement('div')
  26. panelElem.appendChild(this.displayElem)
  27. this.panel.bodyElem.appendChild(panelElem)
  28. this.detailButton = Controls.addButton(this.panel.bodyElem, 'detail', this.detailLabel, () => this.openUrl(), 'detail')
  29. this.detailButton.style.display = 'none'
  30. this.update()
  31. }
  32. onNodeChanged(event) {
  33. if (event.type === 'nodeChanged' && this.hasChanged(event)) {
  34. this.update()
  35. }
  36. }
  37. update() {
  38. this.panel.title = this.title || ''
  39. let value = this.input
  40. const language = this.application.i18n.userLanguages[0]
  41. const nf = new Intl.NumberFormat(language, {
  42. maximumFractionDigits: this.decimals,
  43. minimumFractionDigits: this.decimals
  44. })
  45. let num = nf.format(parseFloat(value))
  46. let units = this.units || ''
  47. this.displayElem.innerHTML = '' + num + ' ' + units
  48. if (this.detailUrl.length > 0) {
  49. this.detailButton.style.display = ''
  50. I18N.set(this.detailButton, 'innerHTML', this.detailLabel)
  51. }
  52. }
  53. openUrl() {
  54. window.open(this.detailUrl, this.detailTarget).focus()
  55. }
  56. }
  57. Controller.addClass(DisplayController)
  58. export { DisplayController }