PanelController.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * PanelController.js
  3. *
  4. * @author realor
  5. */
  6. import { Controller } from './Controller.js'
  7. class PanelController extends Controller {
  8. constructor(object, name) {
  9. super(object, name)
  10. this.title = 'Title'
  11. this.alwaysVisible = false
  12. this._onNodeChanged = this.onNodeChanged.bind(this)
  13. this._onSelection = this.onSelection.bind(this)
  14. this.panel = null
  15. }
  16. init(application) {
  17. this.application = application
  18. this.createPanel()
  19. if (this.autoStart) this.start()
  20. }
  21. onStart() {
  22. const application = this.application
  23. application.addEventListener('scene', this._onNodeChanged)
  24. application.addEventListener('selection', this._onSelection)
  25. this.panel.visible = this.isPanelVisible()
  26. if (this.panel.visible) {
  27. this.update()
  28. }
  29. }
  30. onStop() {
  31. const application = this.application
  32. application.removeEventListener('scene', this._onNodeChanged)
  33. application.removeEventListener('selection', this._onSelection)
  34. this.panel.visible = false
  35. }
  36. createPanel(position = 'left', height = 100) {
  37. this.panel = this.application.createPanel(this.name, position)
  38. this.panel.preferredHeight = height
  39. this.panel.bodyElem.classList.add('center')
  40. }
  41. onSelection(event) {
  42. this.panel.visible = this.isPanelVisible()
  43. if (this.panel.visible) {
  44. this.update()
  45. }
  46. }
  47. onNodeChanged(event) {}
  48. update() {
  49. // updates panel content
  50. }
  51. isPanelVisible() {
  52. const application = this.application
  53. const selection = application.selection
  54. return this.alwaysVisible || selection.contains(this.object)
  55. }
  56. }
  57. export { PanelController }