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.

Overlay.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. *
  79. * @param type
  80. * @return {undefined}
  81. * @private
  82. */
  83. _getLayerCollection(type) {
  84. let collection = undefined
  85. switch (type) {
  86. case 'point_primitive':
  87. collection = this._layer.points
  88. break
  89. case 'billboard_primitive':
  90. case 'bounce_billboard_primitive':
  91. collection = this._layer.billboards
  92. break
  93. case 'label_primitive':
  94. case 'bounce_label_primitive':
  95. collection = this._layer.labels
  96. break
  97. case 'polyline_primitive':
  98. collection = this._layer.polylines
  99. break
  100. case 'cloud_primitive':
  101. collection = this._layer.clouds
  102. break
  103. default:
  104. break
  105. }
  106. return collection
  107. }
  108. /**
  109. * The hook for mount layer
  110. * Subclasses need to be overridden
  111. * @private
  112. */
  113. _mountedHook() {}
  114. /**
  115. * The hook for added
  116. * @returns {boolean}
  117. * @private
  118. */
  119. _addedHook() {
  120. if (!this._delegate) {
  121. return false
  122. }
  123. this._delegate.layerId = this._layer?.layerId
  124. this._delegate.overlayId = this._id
  125. }
  126. /**
  127. * The hook for removed
  128. * Subclasses need to be overridden
  129. * @private
  130. */
  131. _removedHook() {}
  132. /**
  133. * Add handler
  134. * @param layer
  135. * @private
  136. */
  137. _onAdd(layer) {
  138. if (!layer) {
  139. return
  140. }
  141. this._layer = layer
  142. this._mountedHook && this._mountedHook()
  143. // for Entity
  144. if (this._layer?.delegate?.entities && this._delegate) {
  145. this._layer.delegate.entities.add(this._delegate)
  146. }
  147. // for Primitive
  148. else if (this._layer?.delegate?.add) {
  149. let collection = this._getLayerCollection(this.type)
  150. if (collection) {
  151. this._delegate && (this._delegate = collection.add(this._delegate))
  152. // for custom primitve
  153. if (
  154. typeof this['update'] === 'function' &&
  155. typeof this['destroy'] === 'function'
  156. ) {
  157. this._layer.delegate.add(this)
  158. }
  159. } else if (
  160. typeof this['update'] === 'function' &&
  161. typeof this['destroy'] === 'function'
  162. ) {
  163. this._layer.delegate.add(this)
  164. } else {
  165. this._delegate && this._layer.delegate.add(this._delegate)
  166. }
  167. }
  168. this._addedHook && this._addedHook()
  169. this._state = State.ADDED
  170. }
  171. /**
  172. * Remove handler
  173. * @private
  174. */
  175. _onRemove() {
  176. if (!this._layer || !this._delegate) {
  177. return
  178. }
  179. // for Entity
  180. if (this._layer?.delegate?.entities) {
  181. this._layer.delegate.entities.remove(this._delegate)
  182. }
  183. // for Primitive
  184. else if (this._layer?.delegate?.remove) {
  185. let collection = this._getLayerCollection(this.type)
  186. if (collection) {
  187. this._delegate && collection.remove(this._delegate)
  188. // for custom primitve
  189. if (
  190. typeof this['update'] === 'function' &&
  191. typeof this['destroy'] === 'function'
  192. ) {
  193. this._layer.delegate.remove(this)
  194. }
  195. } else if (
  196. typeof this['update'] === 'function' &&
  197. typeof this['destroy'] === 'function'
  198. ) {
  199. this._layer.delegate.remove(this)
  200. } else {
  201. this._delegate && this._layer.delegate.remove(this._delegate)
  202. }
  203. }
  204. this._removedHook && this._removedHook()
  205. this._state = State.REMOVED
  206. }
  207. /**
  208. * Sets Text with Style
  209. * @param text
  210. * @param textStyle
  211. * @returns {Overlay}
  212. */
  213. setLabel(text, textStyle) {
  214. if (!this._delegate) {
  215. return this
  216. }
  217. if (this._delegate instanceof Cesium.Entity) {
  218. this._delegate.label = {
  219. ...textStyle,
  220. text: text
  221. }
  222. }
  223. return this
  224. }
  225. /**
  226. * Sets style
  227. * @param style
  228. * @returns {Overlay}
  229. */
  230. setStyle(style) {
  231. return this
  232. }
  233. /**
  234. * Removes from layer
  235. * @returns {Overlay}
  236. */
  237. remove() {
  238. if (this._layer) {
  239. this._layer.removeOverlay(this)
  240. }
  241. return this
  242. }
  243. /**
  244. * adds to layer
  245. * @param layer
  246. * @returns {Overlay}
  247. */
  248. addTo(layer) {
  249. if (layer && layer.addOverlay) {
  250. layer.addOverlay(this)
  251. }
  252. return this
  253. }
  254. /**
  255. * Subscribe event
  256. * @param type
  257. * @param callback
  258. * @param context
  259. * @returns {Overlay}
  260. */
  261. on(type, callback, context) {
  262. this._overlayEvent.on(type, callback, context || this)
  263. return this
  264. }
  265. /**
  266. * Unsubscribe event
  267. * @param type
  268. * @param callback
  269. * @param context
  270. * @returns {Overlay}
  271. */
  272. off(type, callback, context) {
  273. this._overlayEvent.off(type, callback, context || this)
  274. return this
  275. }
  276. /**
  277. * Trigger subscription event
  278. * @param type
  279. * @param params
  280. * @returns {Overlay}
  281. */
  282. fire(type, params) {
  283. this._overlayEvent.fire(type, params)
  284. return this
  285. }
  286. /**
  287. *
  288. * @param type
  289. */
  290. static registerType(type) {
  291. if (type) {
  292. OverlayType[type.toLocaleUpperCase()] = type.toLocaleLowerCase()
  293. }
  294. }
  295. /**
  296. *
  297. * @param type
  298. * @returns {*|undefined}
  299. */
  300. static getOverlayType(type) {
  301. return OverlayType[type.toLocaleUpperCase()] || undefined
  302. }
  303. }
  304. export default Overlay