core/analysis/Split.js

  1. import { Cesium } from '../../../namespace'
  2. import { BaseLayer } from '../Layers'
  3. /**
  4. * 卷帘分析类
  5. * @class
  6. */
  7. class Split {
  8. /**
  9. * 构造函数
  10. * @param {Object} viewer 地图场景对象
  11. */
  12. constructor(viewer) {
  13. this._viewer = viewer
  14. this._enabled = false
  15. this._splitLayer = BaseLayer.DefaultTdtVec
  16. this._splitDirection = Cesium.SplitDirection.RIGHT
  17. this._dom = undefined
  18. }
  19. /**
  20. * 是否启用
  21. * @type {Boolean}
  22. */
  23. get enabled() {
  24. return this._enabled
  25. }
  26. set enabled(bool) {
  27. if (bool === this._enabled) return
  28. this._enabled = bool
  29. bool ? this.init() : this.remove()
  30. }
  31. /**
  32. * 卷帘图层方向,-1:左侧,1:右侧
  33. * @type {Number}
  34. */
  35. get splitDirection() {
  36. return this._splitDirection
  37. }
  38. set splitDirection(val) {
  39. if (val === -1) {
  40. this._splitLayer.splitDirection = Cesium.SplitDirection.LEFT
  41. this._splitDirection = -1
  42. }
  43. if (val === 1) {
  44. this._splitLayer.splitDirection = Cesium.SplitDirection.RIGHT
  45. this._splitDirection = 1
  46. }
  47. }
  48. /**
  49. * 卷帘图层,ImageryLayer类型,参考Cesium
  50. * @type {ImageryLayer}
  51. */
  52. get splitLayer() {
  53. return this._splitLayer
  54. }
  55. set splitLayer(layer) {
  56. this._viewer.imageryLayers.remove(this._splitLayer)
  57. this._splitLayer = layer
  58. this._splitLayer.splitDirection = this._splitDirection
  59. this._viewer.imageryLayers.add(this._splitLayer)
  60. }
  61. init() {
  62. if (this._splitLayer.isDestroyed()) {
  63. this._splitLayer = BaseLayer.DefaultTdtVec
  64. }
  65. this._splitLayer.splitDirection = this._splitDirection
  66. this._viewer.imageryLayers.add(this._splitLayer)
  67. this._viewer.scene.splitPosition = 0.5
  68. this.initDom()
  69. }
  70. initDom() {
  71. this._dom = document.createElement('div')
  72. this._dom.id = 'cus-layer-slider'
  73. const innerHTML = `
  74. <div class="slider-main">
  75. <span id="optionOne" class="option left">方案一</span>
  76. <span class="line"></span>
  77. <span id="optionTwo" class="option right">方案二</span>
  78. </div>
  79. <div id="slider-button"></div>
  80. `
  81. this._dom.innerHTML = innerHTML
  82. this._viewer.container.appendChild(this._dom)
  83. // 给滑块添加鼠标进入事件监听器
  84. const sliderButton = this._dom.querySelector('#slider-button')
  85. sliderButton.addEventListener('mouseenter', this.mouseEnter.bind(this))
  86. sliderButton.addEventListener('mouseleave', this.mouseleave.bind(this))
  87. this._handler = new Cesium.ScreenSpaceEventHandler(sliderButton)
  88. let moveActive = false
  89. let left = '0'
  90. const $this = this
  91. function move(movement) {
  92. if (!moveActive) return
  93. const relativeOffset = movement.endPosition.x
  94. const splitPosition =
  95. ($this._dom.offsetLeft + relativeOffset) /
  96. $this._dom.parentElement.offsetWidth
  97. left = `${100.0 * splitPosition}%`
  98. $this._dom.style.left = left
  99. $this._viewer.scene.splitPosition = splitPosition
  100. }
  101. this._handler.setInputAction(function () {
  102. moveActive = true
  103. }, Cesium.ScreenSpaceEventType.LEFT_DOWN)
  104. this._handler.setInputAction(function () {
  105. moveActive = true
  106. }, Cesium.ScreenSpaceEventType.PINCH_START)
  107. this._handler.setInputAction(move, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
  108. this._handler.setInputAction(move, Cesium.ScreenSpaceEventType.PINCH_MOVE)
  109. this._handler.setInputAction(function () {
  110. moveActive = false
  111. }, Cesium.ScreenSpaceEventType.LEFT_UP)
  112. this._handler.setInputAction(function () {
  113. moveActive = false
  114. }, Cesium.ScreenSpaceEventType.PINCH_END)
  115. }
  116. mouseEnter(event) {
  117. this.active(true)
  118. }
  119. mouseleave(event) {
  120. this.active(false)
  121. }
  122. active(bool) {
  123. const line = this._dom.querySelector('.line')
  124. bool ? line.classList.add('active') : line.classList.remove('active')
  125. const sliderButton = this._dom.querySelector('#slider-button')
  126. bool
  127. ? sliderButton.classList.add('active')
  128. : sliderButton.classList.remove('active')
  129. }
  130. /**
  131. * 移除卷帘
  132. */
  133. remove() {
  134. this._viewer.container.removeChild(this._dom)
  135. this._dom = undefined
  136. this._handler && this._handler.destroy()
  137. this._handler = undefined
  138. this._viewer.imageryLayers.remove(this._splitLayer)
  139. }
  140. }
  141. export default Split