UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

194 lines (193 loc) • 14.4 kB
import { html as uHtml } from 'uhtml'; import Feature from 'ol/Feature.js'; import Point from 'ol/geom/Point.js'; import VectorLayer from 'ol/layer/Vector.js'; import { fromLonLat, toLonLat } from 'ol/proj.js'; import VectorSource from 'ol/source/Vector.js'; import { Icon, Style } from 'ol/style.js'; import GirafeHTMLElement from '../../base/GirafeHTMLElement.js'; import { DEFAULT_STREETVIEW_SEARCH_RADIUS } from '../../tools/configuration/streetviewconfig.js'; /** * A StreetView, displaying the footage of a provider (Google Street View, ...) side by side with the 2D Map. * * The Component stays lightweight: the provider and its external library are only loaded when the * StreetView is displayed for the first time. A click on the map displays the closest footage, and a * marker on the map shows the position and the direction of sight of the displayed footage. * The map is accessed through the MapManager: there is no dependency to the map-2d Component. */ export default class StreetViewComponent extends GirafeHTMLElement { templateUrl = null; styleUrls = null; template = () => { return uHtml `<style> .hidden{display:none!important}.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);cursor:pointer;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)!important;color:var(--text-color)!important}.girafe-onboarding-theme button{background-color:var(--bkg-color)!important;color:var(--text-color)!important;text-shadow:none!important}.girafe-onboarding-theme button.driver-popover-close-btn{z-index:10000}.tippy-box{background-color:var(--bkg-color)!important;border:var(--app-standard-border)!important}.tippy-content{color:var(--text-color)!important}.tippy-arrow{color:var(--bkg-color)!important}.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)!important;& div.tippy-content{background-color:var(--bkg-color)!important;& img{max-width:20rem;max-height:20rem}}& div.tippy-arrow{color:var(--bkg-color)!important}} </style><style> #streetview{background-color:var(--bkg-color);width:100%;height:100%;position:relative;overflow:hidden}#viewer{position:absolute;inset:0}.overlay{text-align:center;color:var(--text-color);pointer-events:none;z-index:1;flex-direction:column;justify-content:center;align-items:center;gap:1rem;padding:1rem;font-weight:600;display:flex;position:absolute;inset:0}.no-data{color:#999;font-variant:small-caps;gap:.3rem;font-size:1rem;font-weight:400}.hidden{display:none} </style> <style>${this.customStyle}</style> <div id="streetview"><div id="viewer"></div><div class="${this.loading ? 'overlay' : 'overlay hidden'}"><img alt="loading-icon" src="icons/loading.svg" class="gg-spin"> <span i18n="Loading StreetView...">Loading StreetView...</span></div><div class="${!this.loading && !this.hasFootage ? 'overlay no-data' : 'overlay no-data hidden'}"><span i18n="Click on the map to display the StreetView at this location">Click on the map to display the StreetView at this location</span></div></div> ${this.htmlUnsafe(this.feedbackTemplateHtml ?? '')}`; }; loading = false; hasFootage = false; provider; adapter = null; /** Promise of the currently running adapter creation, to prevent creating it twice. */ adapterCreation = null; /** The view of the footage displayed by the adapter, to tell the views coming from a shared state apart. */ displayedView = null; markerIcon = new Icon({ src: 'icons/streetview-marker.svg', anchor: [0.5, 0.5], rotateWithView: true }); markerGeometry = new Point([0, 0]); marker = new Feature(this.markerGeometry); markerSource = new VectorSource(); markerLayer = new VectorLayer({ source: this.markerSource, zIndex: 2000 }); constructor(provider) { super(`streetview-${provider}`); this.provider = provider; this.marker.setStyle(new Style({ image: this.markerIcon })); this.onMapClick = this.onMapClick.bind(this); } get olMap() { return this.context.mapManager.getMap(); } get cellId() { return `streetview-${this.provider}`; } get isDisplayed() { return this.state.layout.cells.includes(this.cellId); } get searchRadius() { return this.context.configManager.Config.streetview?.searchRadius ?? DEFAULT_STREETVIEW_SEARCH_RADIUS; } /** The StreetView is displayed in a Cell of the Grid, like the Maps. */ get isFullscreenComponent() { return true; } connectedCallback() { super.connectedCallback(); this.render(); this.registerEvents(); void this.onDisplayToggled(); } disconnectedCallback() { super.disconnectedCallback(); this.olMap.un('singleclick', this.onMapClick); this.olMap.removeLayer(this.markerLayer); } render() { super.render(); this.girafeTranslate(); } registerEvents() { this.subscribe('layout.cells', () => void this.onDisplayToggled()); this.subscribe(`streetview.views.${this.provider}`, (_oldView, newView) => this.onViewChanged(newView)); this.olMap.on('singleclick', this.onMapClick); } /** * Loads the provider and shows its viewer when the StreetView is displayed in a Cell of the Grid, * and hides it when it is not displayed anymore. The clicks on the map are only followed while displayed. */ async onDisplayToggled() { if (!this.isDisplayed) { this.unregisterInteractionListeners('map.mouseclick'); this.olMap.removeLayer(this.markerLayer); this.adapter?.setVisible(false); return; } this.registerInteractionListener('map.mouseclick', false); if (!this.olMap.getLayers().getArray().includes(this.markerLayer)) { this.olMap.addLayer(this.markerLayer); } await this.createAdapterOnce(); this.adapter?.setVisible(true); this.showStateView(); } async createAdapterOnce() { if (this.adapter) { return; } this.adapterCreation ??= (async () => { this.loading = true; this.refreshRender(); try { const adapter = await this.createAdapter(); await adapter.initialize(this.getById('viewer'), (footage) => this.onFootageChanged(footage)); this.adapter = adapter; } catch (error) { console.error(error); // Allows to try again the next time the StreetView is displayed this.adapterCreation = null; } finally { this.loading = false; this.refreshRender(); } })(); return this.adapterCreation; } onMapClick(event) { if (!this.adapter || !this.isDisplayed || !this.canExecute('map.mouseclick')) { return; } this.showClosestFootage(this.adapter, event.coordinate).catch((error) => console.error(error)); } async showClosestFootage(adapter, coordinate) { const lonLat = toLonLat(coordinate, this.olMap.getView().getProjection()); const found = await adapter.showClosestFootage(lonLat, this.searchRadius); if (!found) { void window.gAlert('No StreetView footage was found near this location', 'StreetView'); } } onFootageChanged(footage) { const view = { position: fromLonLat(footage.lonLat, this.olMap.getView().getProjection()), heading: footage.heading, pitch: footage.pitch }; this.displayedView = view; this.state.streetview.views[this.provider] = view; } onViewChanged(view) { this.updateMarker(view); this.showStateView(); } /** * Displays the footage of the view in the state when it was not displayed by this Component itself, * which is the case when a shared state is restored. */ showStateView() { const view = this.state.streetview.views[this.provider]; if (!this.adapter || !this.isDisplayed || !view || this.isDisplayedView(view)) { return; } const lonLat = toLonLat(view.position, this.olMap.getView().getProjection()); this.adapter .showClosestFootage(lonLat, this.searchRadius, { heading: view.heading, pitch: view.pitch }) .catch((error) => console.error(error)); } isDisplayedView(view) { const displayed = this.displayedView; return (displayed !== null && displayed.position[0] === view.position[0] && displayed.position[1] === view.position[1] && displayed.heading === view.heading && displayed.pitch === view.pitch); } updateMarker(view) { this.markerGeometry.setCoordinates(view.position); this.markerIcon.setRotation((view.heading * Math.PI) / 180); this.marker.changed(); if (!this.markerSource.hasFeature(this.marker)) { this.markerSource.addFeature(this.marker); } if (!this.hasFootage) { this.hasFootage = true; this.refreshRender(); } } }