| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- /**
- * @Author: Caven
- * @Date: 2020-01-03 12:18:17
- */
-
- import { Cesium } from '@dc-modules/namespace'
- import State from '@dc-modules/state/State'
- import { Util } from '@dc-modules/utils'
- import { OverlayEventType, OverlayEvent } from '@dc-modules/event'
- import OverlayType from './OverlayType'
-
- class Overlay {
- constructor() {
- this._id = Util.uuid()
- this._bid = Util.uuid() // Business id
- this._delegate = undefined
- this._layer = undefined
- this._state = undefined
- this._show = true
- this._style = {}
- this._attr = {}
- this._allowDrillPicking = false
- this._contextMenu = []
- this._overlayEvent = new OverlayEvent()
- this._overlayEvent.on(OverlayEventType.ADD, this._onAdd, this)
- this._overlayEvent.on(OverlayEventType.REMOVE, this._onRemove, this)
- }
-
- get overlayId() {
- return this._id
- }
-
- get type() {
- return ''
- }
-
- set id(id) {
- this._bid = id
- return this
- }
-
- get id() {
- return this._bid
- }
-
- set show(show) {
- this._show = show
- this._delegate && (this._delegate.show = this._show)
- return this
- }
-
- get show() {
- return this._show
- }
-
- set attr(attr) {
- this._attr = attr
- return this
- }
-
- get attr() {
- return this._attr
- }
-
- set allowDrillPicking(allowDrillPicking) {
- this._allowDrillPicking = allowDrillPicking
- return this
- }
-
- get allowDrillPicking() {
- return this._allowDrillPicking
- }
-
- get overlayEvent() {
- return this._overlayEvent
- }
-
- get delegate() {
- return this._delegate
- }
-
- get state() {
- return this._state
- }
-
- set contextMenu(menus) {
- this._contextMenu = menus
- return this
- }
-
- get contextMenu() {
- return this._contextMenu
- }
-
- /**
- *
- * @param type
- * @return {undefined}
- * @private
- */
- _getLayerCollection(type) {
- let collection = undefined
- switch (type) {
- case 'point_primitive':
- collection = this._layer.points
- break
- case 'billboard_primitive':
- case 'bounce_billboard_primitive':
- collection = this._layer.billboards
- break
- case 'label_primitive':
- case 'bounce_label_primitive':
- collection = this._layer.labels
- break
- case 'polyline_primitive':
- collection = this._layer.polylines
- break
- case 'cloud_primitive':
- collection = this._layer.clouds
- break
- default:
- break
- }
- return collection
- }
-
- /**
- * The hook for mount layer
- * Subclasses need to be overridden
- * @private
- */
- _mountedHook() {}
-
- /**
- * The hook for added
- * @returns {boolean}
- * @private
- */
- _addedHook() {
- if (!this._delegate) {
- return false
- }
- this._delegate.layerId = this._layer?.layerId
- this._delegate.overlayId = this._id
- }
-
- /**
- * The hook for removed
- * Subclasses need to be overridden
- * @private
- */
- _removedHook() {}
-
- /**
- * Add handler
- * @param layer
- * @private
- */
- _onAdd(layer) {
- if (!layer) {
- return
- }
- this._layer = layer
-
- this._mountedHook && this._mountedHook()
-
- // for Entity
- if (this._layer?.delegate?.entities && this._delegate) {
- this._layer.delegate.entities.add(this._delegate)
- }
- // for Primitive
- else if (this._layer?.delegate?.add) {
- let collection = this._getLayerCollection(this.type)
-
- if (collection) {
- this._delegate && (this._delegate = collection.add(this._delegate))
- // for custom primitve
- if (
- typeof this['update'] === 'function' &&
- typeof this['destroy'] === 'function'
- ) {
- this._layer.delegate.add(this)
- }
- } else if (
- typeof this['update'] === 'function' &&
- typeof this['destroy'] === 'function'
- ) {
- this._layer.delegate.add(this)
- } else {
- this._delegate && this._layer.delegate.add(this._delegate)
- }
- }
- this._addedHook && this._addedHook()
- this._state = State.ADDED
- }
-
- /**
- * Remove handler
- * @private
- */
- _onRemove() {
- if (!this._layer || !this._delegate) {
- return
- }
- // for Entity
- if (this._layer?.delegate?.entities) {
- this._layer.delegate.entities.remove(this._delegate)
- }
- // for Primitive
- else if (this._layer?.delegate?.remove) {
- let collection = this._getLayerCollection(this.type)
- if (collection) {
- this._delegate && collection.remove(this._delegate)
- // for custom primitve
- if (
- typeof this['update'] === 'function' &&
- typeof this['destroy'] === 'function'
- ) {
- this._layer.delegate.remove(this)
- }
- } else if (
- typeof this['update'] === 'function' &&
- typeof this['destroy'] === 'function'
- ) {
- this._layer.delegate.remove(this)
- } else {
- this._delegate && this._layer.delegate.remove(this._delegate)
- }
- }
- this._removedHook && this._removedHook()
- this._state = State.REMOVED
- }
-
- /**
- * Sets Text with Style
- * @param text
- * @param textStyle
- * @returns {Overlay}
- */
- setLabel(text, textStyle) {
- if (!this._delegate) {
- return this
- }
- if (this._delegate instanceof Cesium.Entity) {
- this._delegate.label = {
- ...textStyle,
- text: text
- }
- }
- return this
- }
-
- /**
- * Sets style
- * @param style
- * @returns {Overlay}
- */
- setStyle(style) {
- return this
- }
-
- /**
- * Removes from layer
- * @returns {Overlay}
- */
- remove() {
- if (this._layer) {
- this._layer.removeOverlay(this)
- }
- return this
- }
-
- /**
- * adds to layer
- * @param layer
- * @returns {Overlay}
- */
- addTo(layer) {
- if (layer && layer.addOverlay) {
- layer.addOverlay(this)
- }
- return this
- }
-
- /**
- * Subscribe event
- * @param type
- * @param callback
- * @param context
- * @returns {Overlay}
- */
- on(type, callback, context) {
- this._overlayEvent.on(type, callback, context || this)
- return this
- }
-
- /**
- * Unsubscribe event
- * @param type
- * @param callback
- * @param context
- * @returns {Overlay}
- */
- off(type, callback, context) {
- this._overlayEvent.off(type, callback, context || this)
- return this
- }
-
- /**
- * Trigger subscription event
- * @param type
- * @param params
- * @returns {Overlay}
- */
- fire(type, params) {
- this._overlayEvent.fire(type, params)
- return this
- }
-
- /**
- *
- * @param type
- */
- static registerType(type) {
- if (type) {
- OverlayType[type.toLocaleUpperCase()] = type.toLocaleLowerCase()
- }
- }
-
- /**
- *
- * @param type
- * @returns {*|undefined}
- */
- static getOverlayType(type) {
- return OverlayType[type.toLocaleUpperCase()] || undefined
- }
- }
-
- export default Overlay
|