Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-02-12 21:46:22
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import State from '@dc-modules/state/State'
  7. import Parse from '@dc-modules/parse/Parse'
  8. import { DomUtil, Util } from '@dc-modules/utils'
  9. import { MouseEventType } from '@dc-modules/event'
  10. import { isBetween } from '@dc-modules/math'
  11. import { Transform } from '@dc-modules/transform'
  12. import Overlay from '../Overlay'
  13. class DivIcon extends Overlay {
  14. constructor(position, content) {
  15. super()
  16. this._delegate = DomUtil.create('div', 'div-icon')
  17. this._position = Parse.parsePosition(position)
  18. this._delegate.setAttribute('id', this._id)
  19. Util.merge(this._delegate.style, {
  20. position: 'absolute',
  21. top: '0',
  22. left: '0'
  23. })
  24. this.content = content
  25. this._state = State.INITIALIZED
  26. }
  27. get type() {
  28. return Overlay.getOverlayType('div_icon')
  29. }
  30. set show(show) {
  31. this._show = show
  32. this._delegate.style.visibility = this._show ? 'visible' : 'hidden'
  33. return this
  34. }
  35. get show() {
  36. return this._show
  37. }
  38. set position(position) {
  39. this._position = Parse.parsePosition(position)
  40. return this
  41. }
  42. get position() {
  43. return this._position
  44. }
  45. set content(content) {
  46. if (content && typeof content === 'string') {
  47. this._delegate.innerHTML = content
  48. } else if (content && content instanceof Element) {
  49. while (this._delegate.hasChildNodes()) {
  50. this._delegate.removeChild(this._delegate.firstChild)
  51. }
  52. this._delegate.appendChild(content)
  53. }
  54. return this
  55. }
  56. get content() {
  57. return this._delegate.childNodes || []
  58. }
  59. /**
  60. * Updates style
  61. * @param style
  62. * @param distance
  63. * @private
  64. */
  65. _updateStyle(style, distance) {
  66. let translate3d = 'translate3d(0,0,0)'
  67. if (style.transform) {
  68. let x = style.transform.x - this._delegate.offsetWidth / 2
  69. let y = style.transform.y - this._delegate.offsetHeight / 2
  70. translate3d = `translate3d(${Math.round(x)}px,${Math.round(y)}px, 0)`
  71. }
  72. let scale3d = 'scale3d(1,1,1)'
  73. let scaleByDistance = this._style.scaleByDistance
  74. if (distance && scaleByDistance) {
  75. let nearValue = scaleByDistance.nearValue
  76. let farValue = scaleByDistance.farValue
  77. let f = distance / scaleByDistance.far
  78. if (distance < scaleByDistance.near) {
  79. scale3d = `scale3d(${nearValue},${nearValue},1)`
  80. } else if (distance > scaleByDistance.far) {
  81. scale3d = `scale3d(${farValue},${farValue},1)`
  82. } else {
  83. let scale = farValue + f * (nearValue - farValue)
  84. scale3d = `scale3d(${scale},${scale},1)`
  85. }
  86. }
  87. let distanceDisplayCondition = this._style.distanceDisplayCondition
  88. if (distance && distanceDisplayCondition) {
  89. this.show =
  90. this._show &&
  91. isBetween(
  92. distance,
  93. distanceDisplayCondition.near,
  94. distanceDisplayCondition.far
  95. )
  96. }
  97. this._delegate.style.transform = `${translate3d} ${scale3d}`
  98. }
  99. /**
  100. *
  101. * @param layer
  102. * @returns {boolean}
  103. * @private
  104. */
  105. _onAdd(layer) {
  106. this._layer = layer
  107. this._layer.delegate.appendChild(this._delegate)
  108. let params = {
  109. layer: layer,
  110. overlay: this,
  111. position: Transform.transformWGS84ToCartesian(this._position)
  112. }
  113. this._delegate.addEventListener('click', () => {
  114. this._overlayEvent.fire(MouseEventType.CLICK, params)
  115. })
  116. this._delegate.addEventListener('mouseover', () => {
  117. this._overlayEvent.fire(MouseEventType.MOUSE_OVER, params)
  118. })
  119. this._delegate.addEventListener('mouseout', () => {
  120. this._overlayEvent.fire(MouseEventType.MOUSE_OUT, params)
  121. })
  122. this._state = State.ADDED
  123. }
  124. /**
  125. *
  126. * @private
  127. */
  128. _onRemove() {
  129. if (this._layer) {
  130. this._layer.delegate.removeChild(this._delegate)
  131. this._state = State.REMOVED
  132. }
  133. }
  134. /**
  135. * Sets text
  136. * @param text
  137. * @param textStyle
  138. * @returns {DivIcon}
  139. */
  140. setLabel(text, textStyle) {
  141. return this
  142. }
  143. /**
  144. * Sets style
  145. * @param style
  146. * @returns {DivIcon}
  147. */
  148. setStyle(style) {
  149. if (!style || Object.keys(style).length === 0) {
  150. return this
  151. }
  152. this._style = style
  153. this._style.className &&
  154. DomUtil.addClass(this._delegate, this._style.className)
  155. return this
  156. }
  157. /**
  158. * Parse from entity
  159. * @param entity
  160. * @param content
  161. * @returns {DivIcon}
  162. */
  163. static fromEntity(entity, content) {
  164. let divIcon
  165. let now = Cesium.JulianDate.now()
  166. let position = Transform.transformCartesianToWGS84(
  167. entity.position.getValue(now)
  168. )
  169. divIcon = new DivIcon(position, content)
  170. if (entity.billboard) {
  171. divIcon.attr = {
  172. ...entity?.properties?.getValue(now)
  173. }
  174. }
  175. return divIcon
  176. }
  177. }
  178. Overlay.registerType('div_icon')
  179. export default DivIcon