@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
140 lines (139 loc) • 12 kB
JavaScript
import { html as uHtml } from 'uhtml';
// SPDX-License-Identifier: Apache-2.0
import GirafeHTMLElement from '../../base/GirafeHTMLElement.js';
import LayerWms from '../../models/layers/layerwms.js';
/**
* The 3D Map.
*
* The Component itself stays lightweight: Cesium and OLCesium are only imported when the 3D Map
* is displayed for the first time. Everything that needs those libraries lives in ./tools/globe.
* The synchronization with the 2D Map is done by OLCesium, through the OpenLayers map of the
* MapManager. There is therefore no direct dependency to the map-2d Component.
*/
export default class Map3dComponent extends GirafeHTMLElement {
templateUrl = null;
styleUrls = null;
template = () => {
return uHtml `<style>
.hidden{display:none}.gg-rotate90{transform:rotate(90deg)}.gg-rotate180{transform:rotate(180deg)}.gg-rotate270{transform:rotate(270deg)}img{filter:var(--svg-filter)}img.legend-image{filter:var(--svg-map-filter);background:var(--svg-legend-bkg)}div{scrollbar-width:thin}a,a:visited{color:var(--link-color)}@keyframes spin{0%{transform:rotate(0)}to{transform:rotate(360deg)}}@keyframes spin-wait{0%{transform:rotate(0)}7%{transform:rotate(360deg)}to{transform:rotate(360deg)}}.gg-spin{animation-name:spin;animation-duration:2s;animation-timing-function:linear;animation-iteration-count:infinite}.gg-spin-wait{animation-name:spin-wait;animation-duration:10s;animation-timing-function:linear;animation-iteration-count:infinite}::-webkit-scrollbar{width:5px}::-webkit-scrollbar-thumb{background:#999}button,input,select,textarea{font:inherit}.gg-button,.gg-select,.gg-input,.gg-textarea{background-color:var(--bkg-color);color:var(--text-color);border:var(--app-standard-border);box-sizing:border-box;cursor:pointer;border-radius:3px;outline:0;margin:0;padding:0 0 0 .5rem;display:inline-block}.gg-label{background-color:var(--bkg-color);color:var(--text-color);border:none;align-items:center;margin:0;padding:0;display:flex}.gg-button,.gg-select,.gg-input,.gg-label{min-height:calc(var(--app-standard-height) / 1.5)}.gg-textarea{max-height:initial;resize:vertical;height:6rem;padding:.5rem;line-height:1.3rem}.gg-input{cursor:text}.gg-checkbox{accent-color:var(--text-color);width:1.2rem}.gg-range{accent-color:var(--text-color)}.gg-button{padding:0 .5rem}.gg-button.active{border:solid 1px var(--text-color-grad1);background-color:var(--bkg-color-grad2)}.gg-button:disabled{background-color:var(--bkg-color-grad1);color:var(--text-color-grad2);cursor:not-allowed;border:none}.gg-button:disabled img{filter:opacity(.6)}.gg-input:disabled,.gg-select:disabled,.gg-textarea:disabled{background-color:var(--bkg-color-grad1);color:var(--text-color-grad2);cursor:not-allowed}.gg-button>img{vertical-align:middle}.gg-icon-button{color:var(--text-color);cursor:pointer;background-color:#0000;border:none;flex-direction:column;justify-content:center;align-items:center;padding:0;display:flex}.gg-icon{justify-content:center;align-items:center;display:flex}.gg-big,.gg-big-withtext{min-width:var(--app-standard-height);min-height:var(--app-standard-height);max-height:var(--app-standard-height)}.gg-big img,.gg-big-withtext img{width:calc(var(--app-standard-height) - 1.5rem);margin:0}.gg-big-withtext span{font-variant:small-caps;padding:0 1rem;font-size:.9rem}.gg-medium,.gg-medium-withtext{min-width:calc(var(--app-standard-height) / 1.2);min-height:calc(var(--app-standard-height) / 1.2);max-height:calc(var(--app-standard-height) / 1.2);flex-direction:row}.gg-medium img{width:calc(var(--app-standard-height) / 2.4);margin:0}.gg-medium-withtext img{width:calc(var(--app-standard-height) / 2.4);margin-left:.5rem}.gg-medium-withtext span{padding:0 1rem 0 .5rem;font-size:.9rem}.gg-small,.gg-small-withtext{min-width:calc(var(--app-standard-height) / 2);min-height:calc(var(--app-standard-height) / 2);max-height:calc(var(--app-standard-height) / 2);flex-direction:row}.gg-small img{width:calc(var(--app-standard-height) / 3);margin:0}.gg-small-withtext img{width:calc(var(--app-standard-height) / 3);margin-left:.5rem}.gg-small-withtext span{padding:0 .5rem 0 .3rem;font-size:.9rem}.gg-button:hover:not(:disabled),.gg-select:hover:not(:disabled),.gg-input:hover:not(:disabled),.gg-textarea:hover:not(:disabled),.gg-icon-button:hover:not(:disabled){background-color:var(--bkg-color-grad1)}.gg-opacity{opacity:.5}.gg-opacity:hover{opacity:1;background-color:#0000}.gg-tabs{cursor:pointer;grid-auto-flow:column;padding-bottom:1rem;font-size:1rem;display:grid}.gg-tab{border:none;border-bottom:var(--app-standard-border);cursor:pointer;color:var(--text-color);background:0 0;padding:.5rem}.gg-tab.active{border-bottom:solid 1px var(--text-color)}.girafe-button-big,.girafe-button-large,.girafe-button-small,.girafe-button-tiny{color:var(--text-color);background-color:#0000;border:none;flex-direction:column;display:flex}.girafe-button-big:hover,.girafe-button-large:hover,.girafe-button-small:hover,.girafe-button-tiny:hover{background-color:var(--bkg-color-grad1);cursor:pointer}.girafe-button-big.dark,.girafe-button-large.dark,.girafe-button-small.dark,.girafe-button-tiny.dark{background-color:var(--bkg-color);filter:invert()}.girafe-button-big{width:var(--app-standard-height);height:var(--app-standard-height);align-items:center;padding:1rem}.girafe-button-big img{overflow:hidden}.girafe-button-large{flex-direction:row}.girafe-button-large img{height:2rem;margin:.3rem}.girafe-button-large span{height:2rem;margin:.3rem;line-height:2rem}.girafe-button-small{min-width:calc(var(--app-standard-height) / 2);height:calc(var(--app-standard-height) / 2);align-items:center;padding:.5rem}.girafe-button-small img{overflow:hidden}.girafe-button-small span{text-align:left;text-overflow:ellipsis;width:100%;overflow:hidden}.girafe-button-tiny{align-items:center;width:1rem;height:1rem;padding:0}.girafe-button-tiny img{overflow:hidden}.girafe-onboarding-theme{background-color:var(--bkg-color);color:var(--text-color)}.girafe-onboarding-theme button{background-color:var(--bkg-color);color:var(--text-color);text-shadow:none}.girafe-onboarding-theme button.driver-popover-close-btn{z-index:10000}.tippy-box{background-color:var(--bkg-color);border:var(--app-standard-border)}.tippy-content{color:var(--text-color)}.tippy-arrow{color:var(--bkg-color)}.tippy-box[data-theme~=error]{background-color:var(--error-color);white-space:pre-line}div.tippy-box[data-theme=image-popup]{border:var(--app-standard-border);& div.tippy-content{background-color:var(--bkg-color);& img{max-width:20rem;max-height:20rem}}& div.tippy-arrow{color:var(--bkg-color)}}
</style><style>
#cs-map{background-color:var(--bkg-color);width:100%;height:100%;position:absolute;top:0;overflow:hidden}.hidden{display:none}.center{text-align:center;margin:10px;position:absolute;top:50%;left:50%;transform:translate(-50%,-50%)}.loading span{color:var(--text-color);margin-top:1rem;font-weight:600;display:block}.loading span.quote{font-style:italic;font-weight:300}
</style>
<style>${this.customStyle}</style>
<div id="cs-map"><div class="${this.loading ? 'loading center' : 'loading hidden'}"><img alt="loading-icon" src="icons/loading.svg" class="gg-spin"> <span i18n="Loading cesium...">Loading cesium...</span> <span class="quote" i18n="cesium-loading-quote">Please be patient, like a giraffe reaching for the tastiest leaves.</span></div></div>
${this.htmlUnsafe(this.feedbackTemplateHtml ?? '')}`;
};
globe = null;
map3dTarget;
loading = false;
pixelTolerance = 10;
/** Promise of the currently running globe creation, to prevent creating it twice. */
globeCreation = null;
/** The OLCesium instance, once the globe has been created. */
get map3d() {
return this.globe?.olCesium;
}
get config() {
return this.context.configManager.Config;
}
constructor() {
super('map-3d');
}
subscribe(path, callback) {
// @ts-expect-error The call would have succeeded against this implementation,
// but implementation signatures of overloads are not externally visible.
return super.subscribe(path, callback);
}
/**
* The Map needs to be displayed in Fullscreen Mode.
*/
get isFullscreenComponent() {
return true;
}
connectedCallback() {
super.connectedCallback();
this.render();
this.registerEvents();
this.onGlobeToggled();
}
render() {
super.render();
this.map3dTarget = this.shadow.getElementById('cs-map');
}
registerEvents() {
this.subscribe('globe.display', async () => {
await this.onGlobeToggled();
this.globe?.setCamera(this.state.globe.camera);
});
this.subscribe('globe.shadows', (_oldShadows, newShadows) => this.globe?.setShadows(newShadows));
this.subscribe('globe.camera', async (_oldCamera, newCamera) => {
await this.onGlobeToggled();
this.globe?.setCamera(newCamera);
});
this.subscribe('activeBasemaps', (_, newBasemaps) => this.globe?.onChangeBasemaps(newBasemaps));
this.subscribe(/activeBasemaps\.\d\.opacity/, (_oldOpacity, _newOpacity, basemap) => this.globe?.changeBasemapOpacity(basemap));
this.subscribe(/layers\.layersList\..*\.activeState/, (_oldActive, _newActive, layer) => {
if (layer instanceof LayerWms) {
this.globe?.onLayerToggled(layer);
}
});
this.subscribe(/layers\.layersList\..*\.opacity/, (_oldOpacity, _newOpacity, layer) => this.globe?.changeOpacity(layer));
this.subscribe(/layers\.layersList\..*\.filter/, (_oldFilter, _newFilter, layer) => this.globe?.changeFilter(layer));
this.subscribe('application.isReady', (_, isLoaded) => {
if (isLoaded) {
// The globe may not be ready when the rest of the application is initialized.
// The camera of a shared state can only be applied once everything is loaded.
this.globe?.setCamera(this.state.globe.camera);
}
});
}
/**
* Lazily loads Cesium/OLCesium and creates the globe. Does nothing if no 3D configuration
* is available or if the globe has already been created.
*/
async createGlobe() {
if (this.globe || !this.config.map3d) {
return;
}
this.globeCreation ??= (async () => {
this.loading = true;
this.refreshRender();
const { default: Globe } = await import('./tools/globe');
const globe = new Globe(this.context, this.name, this.pixelTolerance);
await globe.initialize(this.map3dTarget);
this.globe = globe;
this.loading = false;
this.refreshRender();
this.state.globe.loaded = true;
})();
return this.globeCreation;
}
/**
* Shows or hides the 3D Map and sizes it depending on the current display mode.
* The 2D Map takes care of its own sizing.
*/
async onGlobeToggled() {
if (this.state.globe.display === '3D') {
// Full screen globe has been enabled
this.style.display = 'block';
this.style.left = '0';
this.style.width = '100%';
await this.createGlobe();
this.globe?.setEnabled(true);
}
else if (this.state.globe.display === '2D/3D') {
// Side by side has been enabled
this.style.display = 'block';
this.style.left = '45%';
this.style.width = '55%';
await this.createGlobe();
this.globe?.setEnabled(true);
}
else {
// 3d map is not visible
this.globe?.setEnabled(false);
this.style.display = 'none';
this.unregisterInteractionListeners('globe.select');
}
}
}