Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Overlay.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-01-03 12:18:17
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import State from '@dc-modules/state/State'
  7. import { Util } from '@dc-modules/utils'
  8. import { OverlayEventType, OverlayEvent } from '@dc-modules/event'
  9. import OverlayType from './OverlayType'
  10. class Overlay {
  11. constructor() {
  12. this._id = Util.uuid()
  13. this._bid = Util.uuid() // Business id
  14. this._delegate = undefined
  15. this._layer = undefined
  16. this._state = undefined
  17. this._show = true
  18. this._style = {}
  19. this._attr = {}
  20. this._allowDrillPicking = false
  21. this._contextMenu = []
  22. this._overlayEvent = new OverlayEvent()
  23. this._overlayEvent.on(OverlayEventType.ADD, this._onAdd, this)
  24. this._overlayEvent.on(OverlayEventType.REMOVE, this._onRemove, this)
  25. }
  26. get overlayId() {
  27. return this._id
  28. }
  29. get type() {
  30. return ''
  31. }
  32. set id(id) {
  33. this._bid = id
  34. return this
  35. }
  36. get id() {
  37. return this._bid
  38. }
  39. set show(show) {
  40. this._show = show
  41. this._delegate && (this._delegate.show = this._show)
  42. return this
  43. }
  44. get show() {
  45. return this._show
  46. }
  47. set attr(attr) {
  48. this._attr = attr
  49. return this
  50. }
  51. get attr() {
  52. return this._attr
  53. }
  54. set allowDrillPicking(allowDrillPicking) {
  55. this._allowDrillPicking = allowDrillPicking
  56. return this
  57. }
  58. get allowDrillPicking() {
  59. return this._allowDrillPicking
  60. }
  61. get overlayEvent() {
  62. return this._overlayEvent
  63. }
  64. get delegate() {
  65. return this._delegate
  66. }
  67. get state() {
  68. return this._state
  69. }
  70. set contextMenu(menus) {
  71. this._contextMenu = menus
  72. return this
  73. }
  74. get contextMenu() {
  75. return this._contextMenu
  76. }
  77. /**
  78. * The hook for mount layer
  79. * Subclasses need to be overridden
  80. * @private
  81. */
  82. _mountedHook() {}
  83. /**
  84. * The hook for added
  85. * @returns {boolean}
  86. * @private
  87. */
  88. _addedHook() {
  89. if (!this._delegate) {
  90. return false
  91. }
  92. this._delegate.layerId = this._layer?.layerId
  93. this._delegate.overlayId = this._id
  94. }
  95. /**
  96. * The hook for removed
  97. * Subclasses need to be overridden
  98. * @private
  99. */
  100. _removedHook() {}
  101. /**
  102. * Add handler
  103. * @param layer
  104. * @private
  105. */
  106. _onAdd(layer) {
  107. if (!layer) {
  108. return
  109. }
  110. this._layer = layer
  111. this._mountedHook && this._mountedHook()
  112. // for Entity
  113. if (this._layer?.delegate?.entities && this._delegate) {
  114. this._layer.delegate.entities.add(this._delegate)
  115. }
  116. // for Primitive
  117. else if (this._layer?.delegate?.add) {
  118. const collection = {
  119. point_primitive: this._layer.points,
  120. billboard_primitive: this._layer.billboards,
  121. bounce_billboard_primitive: this._layer.billboards,
  122. label_primitive: this._layer.labels,
  123. bounce_label_primitive: this._layer.labels,
  124. polyline_primitive: this._layer.polylines,
  125. cloud_primitive: this._layer.clouds
  126. }
  127. if (this.type && collection[this.type] && this._delegate) {
  128. this._delegate = collection[this.type].add(this._delegate)
  129. // for custom primitve
  130. if (
  131. typeof this['update'] === 'function' &&
  132. typeof this['destroy'] === 'function'
  133. ) {
  134. this._layer.delegate.add(this)
  135. }
  136. } else if (
  137. typeof this['update'] === 'function' &&
  138. typeof this['destroy'] === 'function'
  139. ) {
  140. this._layer.delegate.add(this)
  141. } else if (this._delegate) {
  142. this._layer.delegate.add(this._delegate)
  143. }
  144. }
  145. this._addedHook && this._addedHook()
  146. this._state = State.ADDED
  147. }
  148. /**
  149. * Remove handler
  150. * @private
  151. */
  152. _onRemove() {
  153. if (!this._layer || !this._delegate) {
  154. return
  155. }
  156. // for Entity
  157. if (this._layer?.delegate?.entities) {
  158. this._layer.delegate.entities.remove(this._delegate)
  159. }
  160. // for Primitive
  161. else if (this._layer?.delegate?.remove) {
  162. let collection = {
  163. point_primitive: this._layer.points,
  164. billboard_primitive: this._layer.billboards,
  165. bounce_billboard_primitive: this._layer.billboards,
  166. label_primitive: this._layer.labels,
  167. bounce_label_primitive: this._layer.labels,
  168. polyline_primitive: this._layer.polylines,
  169. cloud_primitive: this._layer.clouds
  170. }
  171. if (this.type && collection[this.type]) {
  172. collection[this.type].remove(this._delegate)
  173. // for custom primitve
  174. if (
  175. typeof this['update'] === 'function' &&
  176. typeof this['destroy'] === 'function'
  177. ) {
  178. this._layer.delegate.remove(this)
  179. }
  180. } else if (
  181. typeof this['update'] === 'function' &&
  182. typeof this['destroy'] === 'function'
  183. ) {
  184. this._layer.delegate.remove(this)
  185. } else {
  186. this._layer.delegate.remove(this._delegate)
  187. }
  188. }
  189. this._removedHook && this._removedHook()
  190. this._state = State.REMOVED
  191. }
  192. /**
  193. * Sets Text with Style
  194. * @param text
  195. * @param textStyle
  196. * @returns {Overlay}
  197. */
  198. setLabel(text, textStyle) {
  199. if (!this._delegate) {
  200. return this
  201. }
  202. if (this._delegate instanceof Cesium.Entity) {
  203. this._delegate.label = {
  204. ...textStyle,
  205. text: text
  206. }
  207. }
  208. return this
  209. }
  210. /**
  211. * Sets style
  212. * @param style
  213. * @returns {Overlay}
  214. */
  215. setStyle(style) {
  216. return this
  217. }
  218. /**
  219. * Removes from layer
  220. * @returns {Overlay}
  221. */
  222. remove() {
  223. if (this._layer) {
  224. this._layer.removeOverlay(this)
  225. }
  226. return this
  227. }
  228. /**
  229. * adds to layer
  230. * @param layer
  231. * @returns {Overlay}
  232. */
  233. addTo(layer) {
  234. if (layer && layer.addOverlay) {
  235. layer.addOverlay(this)
  236. }
  237. return this
  238. }
  239. /**
  240. * Subscribe event
  241. * @param type
  242. * @param callback
  243. * @param context
  244. * @returns {Overlay}
  245. */
  246. on(type, callback, context) {
  247. this._overlayEvent.on(type, callback, context || this)
  248. return this
  249. }
  250. /**
  251. * Unsubscribe event
  252. * @param type
  253. * @param callback
  254. * @param context
  255. * @returns {Overlay}
  256. */
  257. off(type, callback, context) {
  258. this._overlayEvent.off(type, callback, context || this)
  259. return this
  260. }
  261. /**
  262. * Trigger subscription event
  263. * @param type
  264. * @param params
  265. * @returns {Overlay}
  266. */
  267. fire(type, params) {
  268. this._overlayEvent.fire(type, params)
  269. return this
  270. }
  271. /**
  272. *
  273. * @param type
  274. */
  275. static registerType(type) {
  276. if (type) {
  277. OverlayType[type.toLocaleUpperCase()] = type.toLocaleLowerCase()
  278. }
  279. }
  280. /**
  281. *
  282. * @param type
  283. * @returns {*|undefined}
  284. */
  285. static getOverlayType(type) {
  286. return OverlayType[type.toLocaleUpperCase()] || undefined
  287. }
  288. }
  289. export default Overlay