You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Widget.js 1.8KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * @Author: Caven
  3. * @Date: 2020-01-15 19:17:52
  4. * @Last Modified by: Caven
  5. * @Last Modified time: 2020-01-21 12:13:45
  6. */
  7. import Cesium from '@/namespace'
  8. class Widget {
  9. constructor() {
  10. this._viewer = undefined
  11. this._position = undefined
  12. this._show = false
  13. this._wapper = undefined
  14. this._windowCoord = undefined
  15. this._state = undefined
  16. this.type = undefined
  17. }
  18. /**
  19. *
  20. * @param {*} windowCoord
  21. * 更新Widget位置需要子类覆盖
  22. */
  23. _updateWindowCoord(windowCoord) {}
  24. /**
  25. *
  26. * @param {*} viewer
  27. */
  28. setPosition(position) {
  29. this._position = position
  30. if (this._viewer) {
  31. let self = this
  32. let scene = this._viewer.scene
  33. scene.postRender.addEventListener(() => {
  34. let windowCoord = Cesium.SceneTransforms.wgs84ToWindowCoordinates(scene, self._position)
  35. this._setWindowCoord(windowCoord)
  36. })
  37. }
  38. }
  39. /**
  40. *
  41. * @param {*} viewer
  42. */
  43. install(viewer) {
  44. this._viewer = viewer
  45. this._wapper && this._viewer.dcContainer.appendChild(this._wapper)
  46. this._state = DC.WidgetState.INSTALLED
  47. }
  48. /**
  49. *
  50. * @param {*} position
  51. * 显示组件
  52. */
  53. show(position) {
  54. this._show = true
  55. position && position instanceof DC.Position && this.setPosition(position)
  56. this._wapper && (this._wapper.style.visibility = 'visible')
  57. this._state = DC.WidgetState.SHOW
  58. }
  59. /**
  60. * 隐藏组件
  61. */
  62. hide() {
  63. this._show = false
  64. this._wapper && (this._wapper.style.visibility = 'hidden')
  65. this._state = DC.WidgetState.HIDDEN
  66. }
  67. /**
  68. *
  69. * @param {*} content
  70. */
  71. setContent(content) {
  72. if (content && typeof content === 'string') {
  73. this._wapper.innerHTML = content
  74. } else if (content && content instanceof Element) {
  75. this._wapper.appendChild(content)
  76. }
  77. }
  78. }
  79. export default Widget