2023西安数博会CIM演示-【前端】-Web
AdaKing88
2023-08-21 bc03b832caa49bbcd2674fe4cae3701b5059bf95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
 * @module olcs.OverlaySynchronizer
 */
import olcsSynchronizedOverlay from './SynchronizedOverlay.js';
import {getUid} from './util.js';
 
class OverlaySynchronizer {
  /**
  * @param {!ol.Map} map
  * @param {!Cesium.Scene} scene
  * @constructor
  * @template T
  * @api
  */
  constructor(map, scene) {
    /**
    * @type {!ol.Map}
    * @protected
    */
    this.map = map;
 
    /**
    * @type {ol.Collection.<ol.Overlay>}
    * @private
    */
    this.overlays_ = this.map.getOverlays();
 
    /**
    * @type {!Cesium.Scene}
    * @protected
    */
    this.scene = scene;
 
    /**
    * @private
    * @type {!Element}
    */
    this.overlayContainerStopEvent_ = document.createElement('DIV');
    this.overlayContainerStopEvent_.className = 'ol-overlaycontainer-stopevent';
    const overlayEvents = ['click', 'dblclick', 'mousedown', 'touchstart', 'MSPointerDown', 'pointerdown', 'mousewheel', 'wheel'];
    overlayEvents.forEach((event) => {
      this.overlayContainerStopEvent_.addEventListener(event, evt => evt.stopPropagation());
    });
    this.scene.canvas.parentElement.appendChild(this.overlayContainerStopEvent_);
 
    /**
    * @private
    * @type {!Element}
    */
    this.overlayContainer_ = document.createElement('DIV');
    this.overlayContainer_.className = 'ol-overlaycontainer';
    this.scene.canvas.parentElement.appendChild(this.overlayContainer_);
 
 
    /**
    * @type {!Object<?,olcs.SynchronizedOverlay>}
    * @private
    */
    this.overlayMap_ = {};
  }
 
  /**
  * Get the element that serves as a container for overlays that don't allow
  * event propagation. Elements added to this container won't let mousedown and
  * touchstart events through to the map, so clicks and gestures on an overlay
  * don't trigger any {@link ol.MapBrowserEvent}.
  * @return {!Element} The map's overlay container that stops events.
  */
  getOverlayContainerStopEvent() {
    return this.overlayContainerStopEvent_;
  }
 
  /**
  * Get the element that serves as a container for overlays.
  * @return {!Element} The map's overlay container.
  */
  getOverlayContainer() {
    return this.overlayContainer_;
  }
 
  /**
  * Destroy all and perform complete synchronization of the overlays.
  * @api
  */
  synchronize() {
    this.destroyAll();
    this.addOverlays();
    this.overlays_.on('add', this.addOverlayFromEvent_.bind(this));
    this.overlays_.on('remove', this.removeOverlayFromEvent_.bind(this));
  }
 
  /**
  * @param {ol.Collection.Event} event
  * @private
  */
  addOverlayFromEvent_(event) {
    const overlay = /** @type {ol.Overlay} */ (event.element);
    this.addOverlay(overlay);
  }
 
  /**
  * @api
  */
  addOverlays() {
    this.overlays_.forEach((overlay) => { this.addOverlay(overlay); });
  }
 
  /**
  * @param {ol.Overlay} overlay
  * @api
  */
  addOverlay(overlay) {
    if (!overlay) {
      return;
    }
    const cesiumOverlay = new olcsSynchronizedOverlay({
      scene: this.scene,
      synchronizer: this,
      parent: overlay
    });
 
    const overlayId = getUid(overlay).toString();
    this.overlayMap_[overlayId] = cesiumOverlay;
  }
 
  /**
  * @param {ol.Collection.Event} event
  * @private
  */
  removeOverlayFromEvent_(event) {
    const removedOverlay = /** @type {ol.Overlay} */ (event.element);
    this.removeOverlay(removedOverlay);
  }
 
  /**
  * Removes an overlay from the scene
  * @param {ol.Overlay} overlay
  * @api
  */
  removeOverlay(overlay) {
    const overlayId = getUid(overlay).toString();
    const csOverlay = this.overlayMap_[overlayId];
    if (csOverlay) {
      csOverlay.destroy();
      delete this.overlayMap_[overlayId];
    }
  }
 
  /**
  * Destroys all the created Cesium objects.
  * @protected
  */
  destroyAll() {
    Object.keys(this.overlayMap_).forEach((key) => {
      const overlay = this.overlayMap_[key];
      overlay.destroy();
      delete this.overlayMap_[key];
    });
  }
}
 
 
export default OverlaySynchronizer;