UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

148 lines (147 loc) • 13.8 kB
import { html as uHtml } from 'uhtml'; // SPDX-License-Identifier: Apache-2.0 import GirafeHTMLElement from '../../base/GirafeHTMLElement.js'; import { AVAILABLE_CELL_COMPONENTS, distributeCells, getDefaultSizes, getLayoutCells, isCellComponent, isCellComponentAvailable } from '../../tools/state/layout.js'; const LAYOUT_ICONS = { '1x1': 'icons/layout-1x1.svg', '2x1': 'icons/layout-2x1.svg', '1x2': 'icons/layout-1x2.svg', '2x2': 'icons/layout-2x2.svg', '2+1': 'icons/layout-2+1.svg', '1+2': 'icons/layout-1+2.svg' }; class LayoutComponent 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> #panel{color:var(--text-color)}#panel>div{padding:1rem}.hidden{display:none}fieldset{margin-top:1rem}.layouts-row{gap:.25rem;margin-top:.5rem;display:flex}.layouts-row button{border:1px solid #0000;border-radius:3px}.layouts-row button.selected{border-color:var(--text-color-grad2);background-color:var(--bkg-color-grad1)}.cells-row{gap:.25rem;margin-top:.5rem;display:flex}.cell-select{flex:1 1 0;min-width:0}.option{margin-top:.5rem;display:flex}.option select,.option input{flex-grow:1}.option input.gg-checkbox{flex-grow:0}.option label{margin-right:.5rem}.option label:after{content:" :"} </style> <style>${this.customStyle}</style> <div id="panel"><div><fieldset><legend i18n="layout-type">Layout</legend><div class="layouts-row">${this.layoutButtons.map((layout) => uHtml ` <button tip="${layout.type}" class="${layout.type === this.state.layout.type ? 'gg-icon-button gg-medium selected' : 'gg-icon-button gg-medium'}" onclick="${() => this.onLayoutSelect(layout.type)}"><img alt="${layout.type}" src="${layout.icon}"></button> `)}</div></fieldset><fieldset class="cells-group"><legend i18n="Displayed components">Displayed components</legend>${this.cellRows.map((cellRow) => uHtml `<div class="cells-row">${cellRow.map((cell) => uHtml ` <select class="gg-select cell-select" onchange="${(e) => this.onCellSelect(e, cell.index)}"><option i18n="(empty)" value="" ?selected="${cell.content === null}">(empty)</option>${this.availableComponents.map((component) => uHtml `<option i18n="${component.label}" value="${component.id}" ?selected="${cell.content === component.id}">${component.label}</option>`)}</select> `)}</div>`)}</fieldset><fieldset class="${this.is3dDisplayed ? 'shadows-group' : 'shadows-group hidden'}"><legend i18n="Shadows">Shadows</legend><div class="option shadows-option"><label for="shadowsCheckbox" i18n="Enable shadows" class="gg-label">Enable shadows</label> <input id="shadowsCheckbox" type="checkbox" class="gg-checkbox" ?checked="${this.state.globe.shadows}" onchange="${this.onShadowsToggle}"></div><div class="${this.state.globe.shadows ? 'option shadows-date' : 'option shadows-date hidden'}"><label for="shadowsDate" i18n="Date" class="gg-label">Date</label> <input id="shadowsDate" type="datetime-local" class="gg-input"></div></fieldset></div></div> ${this.htmlUnsafe(this.feedbackTemplateHtml ?? '')}`; }; isPanelVisible = false; panelTitle = 'layout-panel'; panelTogglePath = 'interface.layoutPanelVisible'; /** The Components the user can choose between for each Cell of the Grid. */ get availableComponents() { const streetviewConfig = this.context.configManager.Config.streetview; return AVAILABLE_CELL_COMPONENTS.filter((component) => isCellComponentAvailable(component.id, streetviewConfig)); } constructor() { super('layout'); this.onShadowsToggle = this.onShadowsToggle.bind(this); } get layout() { return this.state.layout; } /** The Layouts the user can choose between, as configured in the application. */ get allowedLayouts() { return this.context.configManager.Config.interface.layouts; } /** The Layouts the user can choose between, with the icon illustrating each of them. */ get layoutButtons() { return this.allowedLayouts.map((type) => ({ type: type, icon: LAYOUT_ICONS[type] })); } /** The Cells of the current Layout, one line of the schema per row of the Grid. */ get cellRows() { const contents = this.layout.cells; const rows = []; for (const [index, cell] of getLayoutCells(this.layout.type).entries()) { rows[cell.row] ??= []; rows[cell.row].push({ index: index, content: contents[index] ?? null }); } return rows; } registerEvents() { this.subscribe('layout.type', () => this.onLayoutChanged()); this.subscribe('layout.cells', () => this.onLayoutChanged()); } togglePanel(visible) { this.isPanelVisible = visible; this.render(); } onLayoutChanged() { if (this.isPanelVisible) { this.refreshRender(); this.syncSelects(); } } /** * uHtml only updates the <selected> attribute of the options, which the browser ignores * as soon as the user has interacted with a select. The values are therefore applied explicitly, * so that the panel also follows the changes made outside of it (shared state, other components). */ syncSelects() { const cells = this.layout.cells; this.shadow.querySelectorAll('.cell-select').forEach((select, index) => { select.value = cells[index] ?? ''; }); } onLayoutSelect(selectedLayout) { this.layout.cells = distributeCells(selectedLayout, this.layout.cells); this.layout.sizes = getDefaultSizes(selectedLayout); this.layout.type = selectedLayout; } /** * Places a Component in a Cell of the Grid. * A Component can only be displayed once: if it is already displayed in another Cell, * both Cells are swapped, so that the Component that was in the target Cell is not lost. */ onCellSelect(event, cellIndex) { const value = event.target?.value; const component = isCellComponent(value) ? value : null; const cells = [...this.layout.cells]; const previousIndex = component === null ? -1 : cells.indexOf(component); if (previousIndex >= 0) { cells[previousIndex] = cells[cellIndex]; } cells[cellIndex] = component; this.layout.cells = cells; } /** Whether one of the Cells displays a 3D Map, which the shadows options depend on. */ get is3dDisplayed() { return this.layout.cells.includes('map-3d'); } onShadowsToggle(event) { const shadowsDate = this.shadowRoot?.querySelector('.shadows-date'); const isChecked = event.target.checked; shadowsDate?.classList.toggle('hidden', !isChecked); this.state.globe.shadows = isChecked; if (isChecked) { this.setDateTimePicker(); } } setDateTimePicker() { const timeDatePicker = this.shadowRoot?.querySelector('#shadowsDate'); if (timeDatePicker) { const date = new Date(); timeDatePicker.valueAsNumber = Math.round((date.valueOf() - date.getTimezoneOffset() * 60000) / 60000) * 60000; timeDatePicker.addEventListener('input', () => { this.update3dMapTimestamp(new Date(timeDatePicker.value)); }); } } update3dMapTimestamp(date) { this.state.globe.shadowsTimestamp = date.valueOf(); } connectedCallback() { super.connectedCallback(); this.render(); this.registerEvents(); } render() { if (this.isPanelVisible) { super.render(); this.syncSelects(); } else { this.hide(); } super.girafeTranslate(); } } export default LayoutComponent;