|
| 1 | +goog.provide('olcs.Overlay'); |
| 2 | + |
| 3 | + |
| 4 | +goog.require('ol'); |
| 5 | +goog.require('ol.proj'); |
| 6 | +goog.require('ol.dom'); |
| 7 | +goog.require('ol.Observable'); |
| 8 | +goog.require('ol.css'); |
| 9 | + |
| 10 | +/** |
| 11 | + * @constructor |
| 12 | + * @param {{ |
| 13 | + * scene: !Cesium.Scene, |
| 14 | + * parent: !ol.Overlay, |
| 15 | + * synchronizer: !olcs.OverlaySynchronizer |
| 16 | + * }} options Overlay options. |
| 17 | + * @api |
| 18 | + */ |
| 19 | +olcs.Overlay = function(options) { |
| 20 | + /** |
| 21 | + * @private |
| 22 | + * @type {?Function} |
| 23 | + */ |
| 24 | + this.scenePostRenderListenerRemover_ = null; |
| 25 | + |
| 26 | + /** |
| 27 | + * @type {!Cesium.Scene} |
| 28 | + */ |
| 29 | + this.scene = options.scene; |
| 30 | + |
| 31 | + /** @type {!olcs.OverlaySynchronizer} */ |
| 32 | + this.synchronizer = options.synchronizer; |
| 33 | + |
| 34 | + /** |
| 35 | + * @private |
| 36 | + * @type {boolean} |
| 37 | + * @todo read from parent |
| 38 | + */ |
| 39 | + this.insertFirst_ = true; |
| 40 | + |
| 41 | + /** |
| 42 | + * @private |
| 43 | + * @type {boolean} |
| 44 | + * @todo read from parent |
| 45 | + */ |
| 46 | + this.stopEvent_ = true; |
| 47 | + |
| 48 | + /** |
| 49 | + * @private |
| 50 | + * @type {Element} |
| 51 | + */ |
| 52 | + this.element_ = document.createElement('DIV'); |
| 53 | + this.element_.className = `ol-overlay-container ${ol.css.CLASS_SELECTABLE}`; |
| 54 | + this.element_.style.position = 'absolute'; |
| 55 | + |
| 56 | + /** |
| 57 | + * @private |
| 58 | + * @type {!ol.Overlay} |
| 59 | + */ |
| 60 | + this.parent_ = options['parent']; |
| 61 | + |
| 62 | + /** @type {ol.Coordinate|undefined} */ |
| 63 | + this.position = this.parent_.getPosition(); |
| 64 | + |
| 65 | + /** @type {Element|undefined} */ |
| 66 | + this.element = this.parent_.getElement(); |
| 67 | + |
| 68 | + /** |
| 69 | + * @private |
| 70 | + * @type {{bottom_: string, |
| 71 | + * left_: string, |
| 72 | + * right_: string, |
| 73 | + * top_: string, |
| 74 | + * visible: boolean}} |
| 75 | + */ |
| 76 | + this.rendered_ = { |
| 77 | + bottom_: '', |
| 78 | + left_: '', |
| 79 | + right_: '', |
| 80 | + top_: '', |
| 81 | + visible: true |
| 82 | + }; |
| 83 | + |
| 84 | + /** |
| 85 | + * @type {MutationObserver|null} |
| 86 | + * @private |
| 87 | + */ |
| 88 | + this.observer_ = null; |
| 89 | + |
| 90 | + if (this.parent_.getElement()) { |
| 91 | + this.addMutationObserver_(this.parent_.getElement().parentNode); |
| 92 | + } |
| 93 | + |
| 94 | + /** @type {Array<ol.EventsKey>} */ |
| 95 | + this.listenerKeys_ = []; |
| 96 | + this.listenerKeys_.push(this.parent_.on('change:position', this.changePosition_.bind(this))); |
| 97 | + this.listenerKeys_.push(this.parent_.on('change:element', this.changeElement_.bind(this))); |
| 98 | +}; |
| 99 | + |
| 100 | +/** |
| 101 | + * @param {Node} target |
| 102 | + * @private |
| 103 | + */ |
| 104 | +olcs.Overlay.prototype.addMutationObserver_ = function(target) { |
| 105 | + if (this.observer_ === null) { |
| 106 | + this.observer_ = new MutationObserver(this.changeElement_.bind(this)); |
| 107 | + this.observer_.observe(target, { |
| 108 | + attributes: true, |
| 109 | + childList: true, |
| 110 | + characterData: false, |
| 111 | + subtree: true |
| 112 | + }); |
| 113 | + } |
| 114 | +}; |
| 115 | + |
| 116 | +/** |
| 117 | + * Get the scene associated with this overlay. |
| 118 | + * @see ol.Overlay.prototype.getMap |
| 119 | + * @return {!Cesium.Scene} The scene that the overlay is part of. |
| 120 | + * @observable |
| 121 | + * @api |
| 122 | + */ |
| 123 | +olcs.Overlay.prototype.getScene = function() { |
| 124 | + return this.scene; |
| 125 | +}; |
| 126 | + |
| 127 | +/** |
| 128 | + * Modify the visibility of the element. |
| 129 | + * @param {boolean} visible Element visibility. |
| 130 | + * @protected |
| 131 | + */ |
| 132 | +olcs.Overlay.prototype.setVisible = function(visible) { |
| 133 | + if (this.rendered_.visible !== visible) { |
| 134 | + this.element_.style.display = visible ? '' : 'none'; |
| 135 | + this.rendered_.visible = visible; |
| 136 | + } |
| 137 | +}; |
| 138 | + |
| 139 | +/** |
| 140 | + * @api |
| 141 | + */ |
| 142 | +olcs.Overlay.prototype.handleMapChanged = function() { |
| 143 | + if (this.scenePostRenderListenerRemover_) { |
| 144 | + this.scenePostRenderListenerRemover_(); |
| 145 | + } |
| 146 | + this.scenePostRenderListenerRemover_ = null; |
| 147 | + |
| 148 | + const scene = this.getScene(); |
| 149 | + if (scene) { |
| 150 | + this.scenePostRenderListenerRemover_ = scene.postRender.addEventListener(this.updatePixelPosition.bind(this)); |
| 151 | + this.updatePixelPosition(); |
| 152 | + const container = this.stopEvent_ ? |
| 153 | + this.synchronizer.getOverlayContainerStopEvent() : this.synchronizer.getOverlayContainer(); // TODO respect stop-event flag in synchronizer |
| 154 | + if (this.insertFirst_) { |
| 155 | + container.insertBefore(this.element_, container.childNodes[0] || null); |
| 156 | + } else { |
| 157 | + container.appendChild(this.element_); |
| 158 | + } |
| 159 | + } |
| 160 | +}; |
| 161 | + |
| 162 | +/** |
| 163 | + * @protected |
| 164 | + */ |
| 165 | +olcs.Overlay.prototype.handleElementChanged = function() { |
| 166 | + ol.dom.removeChildren(this.element_); |
| 167 | + const element = this.getElement(); |
| 168 | + if (element) { |
| 169 | + this.element_.appendChild(element); |
| 170 | + } |
| 171 | +}; |
| 172 | + |
| 173 | + |
| 174 | +/** |
| 175 | + * Sets up a freshly created overlay |
| 176 | + * @api |
| 177 | + */ |
| 178 | +olcs.Overlay.prototype.init = function() { |
| 179 | + this.changePosition_(); |
| 180 | + this.changeElement_(); |
| 181 | + this.handleMapChanged(); |
| 182 | +}; |
| 183 | + |
| 184 | +/** |
| 185 | + * @return {ol.Coordinate|undefined} |
| 186 | + * @api |
| 187 | + */ |
| 188 | +olcs.Overlay.prototype.getPosition = function() { |
| 189 | + return this.position; |
| 190 | +}; |
| 191 | + |
| 192 | +/** |
| 193 | + * @return {Array.<number>} |
| 194 | + * @api |
| 195 | + */ |
| 196 | +olcs.Overlay.prototype.getOffset = function() { |
| 197 | + return this.parent_.getOffset(); |
| 198 | +}; |
| 199 | + |
| 200 | +/** |
| 201 | + * @return {ol.OverlayPositioning} |
| 202 | + * @api |
| 203 | + */ |
| 204 | +olcs.Overlay.prototype.getPositioning = function() { |
| 205 | + return this.parent_.getPositioning(); |
| 206 | +}; |
| 207 | + |
| 208 | +/** |
| 209 | + * @return {Element|undefined} |
| 210 | + */ |
| 211 | +olcs.Overlay.prototype.getElement = function() { |
| 212 | + return this.element; |
| 213 | +}; |
| 214 | + |
| 215 | +/** |
| 216 | + * @param {ol.Coordinate|undefined} position |
| 217 | + */ |
| 218 | +olcs.Overlay.prototype.setPosition = function(position) { |
| 219 | + this.position = position; |
| 220 | +}; |
| 221 | + |
| 222 | +/** |
| 223 | + * @param {Element} element |
| 224 | + */ |
| 225 | +olcs.Overlay.prototype.setElement = function(element) { |
| 226 | + this.element = element; |
| 227 | + this.handleElementChanged(); |
| 228 | +}; |
| 229 | + |
| 230 | +/** |
| 231 | + * @private |
| 232 | + */ |
| 233 | +olcs.Overlay.prototype.changePosition_ = function() { |
| 234 | + const position = this.parent_.getPosition(); |
| 235 | + |
| 236 | + if (position) { |
| 237 | + this.setVisible(true); |
| 238 | + const sourceProjection = this.parent_.getMap().getView().getProjection(); |
| 239 | + const coords = ol.proj.transform(position, sourceProjection, 'EPSG:4326'); |
| 240 | + this.setPosition(coords); |
| 241 | + this.handleMapChanged(); |
| 242 | + } else { |
| 243 | + this.setPosition(undefined); |
| 244 | + this.setVisible(false); |
| 245 | + } |
| 246 | +}; |
| 247 | + |
| 248 | +/** |
| 249 | + * @private |
| 250 | + */ |
| 251 | +olcs.Overlay.prototype.changeElement_ = function() { |
| 252 | + function cloneNode(node, parent) { |
| 253 | + const clone = node.cloneNode(); |
| 254 | + if (parent) { |
| 255 | + parent.appendChild(clone); |
| 256 | + } |
| 257 | + if (node.nodeType != Node.TEXT_NODE) { |
| 258 | + clone.addEventListener('click', (event) => { |
| 259 | + node.dispatchEvent(new MouseEvent('click', event)); |
| 260 | + event.stopPropagation(); |
| 261 | + }); |
| 262 | + } |
| 263 | + const nodes = node.childNodes; |
| 264 | + for (let i = 0; i < nodes.length; i++) { |
| 265 | + if (!nodes[i]) { |
| 266 | + continue; |
| 267 | + } |
| 268 | + cloneNode(nodes[i], clone); |
| 269 | + } |
| 270 | + return clone; |
| 271 | + } |
| 272 | + |
| 273 | + if (this.parent_.getElement()) { |
| 274 | + const clonedNode = cloneNode(this.parent_.getElement(), null); |
| 275 | + |
| 276 | + this.setElement(clonedNode); |
| 277 | + const parentNode = this.getElement().parentNode; |
| 278 | + if (parentNode) { |
| 279 | + this.addMutationObserver_(parentNode); |
| 280 | + while (parentNode.firstChild) { |
| 281 | + parentNode.removeChild(parentNode.firstChild); |
| 282 | + } |
| 283 | + const childNodes = this.parent_.getElement().parentNode.childNodes; |
| 284 | + for (let i = 0; i < childNodes.length; i++) { |
| 285 | + cloneNode(childNodes[i], parentNode); |
| 286 | + } |
| 287 | + } |
| 288 | + } |
| 289 | +}; |
| 290 | + |
| 291 | +/** |
| 292 | + * Update pixel position. |
| 293 | + * @suppress {checkTypes, accessControls} |
| 294 | + */ |
| 295 | +olcs.Overlay.prototype.updatePixelPosition = function() { |
| 296 | + const scene = this.getScene(); |
| 297 | + const position = this.getPosition(); |
| 298 | + if (!scene || !position) { |
| 299 | + this.setVisible(false); |
| 300 | + return; |
| 301 | + } |
| 302 | + let cartesian; |
| 303 | + if (position.length === 2) { |
| 304 | + cartesian = Cesium.Cartesian3.fromDegreesArray(position)[0]; |
| 305 | + } else { |
| 306 | + cartesian = Cesium.Cartesian3.fromDegreesArrayHeights(position)[0]; |
| 307 | + } |
| 308 | + |
| 309 | + const ellipsoidBoundingSphere = new Cesium.BoundingSphere(new Cesium.Cartesian3(), 6356752); |
| 310 | + const occluder = new Cesium.Occluder(ellipsoidBoundingSphere, scene.camera.position); |
| 311 | + // check if overlay position is behind the horizon |
| 312 | + if (!occluder.isPointVisible(cartesian)) { |
| 313 | + this.setVisible(false); |
| 314 | + return; |
| 315 | + } |
| 316 | + const cullingVolume = scene.camera.frustum.computeCullingVolume(scene.camera.position, scene.camera.direction, scene.camera.up); |
| 317 | + // check if overlay position is visible from the camera |
| 318 | + if (cullingVolume.computeVisibility(new Cesium.BoundingSphere(cartesian)) !== 1) { |
| 319 | + this.setVisible(false); |
| 320 | + return; |
| 321 | + } |
| 322 | + this.setVisible(true); |
| 323 | + |
| 324 | + const pixelCartesian = scene.cartesianToCanvasCoordinates(cartesian); |
| 325 | + const pixel = [pixelCartesian.x, pixelCartesian.y]; |
| 326 | + const mapSize = [scene.canvas.width, scene.canvas.height]; |
| 327 | + const that = /** @type {ol.Overlay} */ (this); |
| 328 | + ol.Overlay.prototype.updateRenderedPosition.call(that, pixel, mapSize); |
| 329 | +}; |
| 330 | + |
| 331 | +/** |
| 332 | + * Destroys the overlay, removing all its listeners and elements |
| 333 | + * @api |
| 334 | + */ |
| 335 | +olcs.Overlay.prototype.destroy = function() { |
| 336 | + if (this.scenePostRenderListenerRemover_) { |
| 337 | + this.scenePostRenderListenerRemover_(); |
| 338 | + } |
| 339 | + if (this.observer_) { |
| 340 | + this.observer_.disconnect(); |
| 341 | + this.observer_ = null; |
| 342 | + } |
| 343 | + ol.Observable.unByKey(this.listenerKeys_); |
| 344 | + this.listenerKeys_.splice(0); |
| 345 | + if (this.element_.removeNode) { |
| 346 | + this.element_.removeNode(true); |
| 347 | + } else { |
| 348 | + this.element_.remove(); |
| 349 | + } |
| 350 | +}; |
0 commit comments