UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

236 lines (235 loc) • 17.3 kB
import { html as uHtml } from 'uhtml'; // SPDX-License-Identifier: Apache-2.0 import GirafeHTMLElement from '../../base/GirafeHTMLElement.js'; import { AVAILABLE_CELL_COMPONENTS, getGridSize, getLayoutCells, MAX_CELL_SIZE, MIN_CELL_SIZE } from '../../tools/state/layout.js'; /** * The Grid in which the Map Components are displayed. * * The Components are written as children of this Element in the HTML of the application * and are moved into the Cells of the Grid, depending on <code>state.layout</code>. * A Component that is not placed in any Cell is not slotted, and therefore not displayed. */ class MapGridComponent 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> #grid{background-color:var(--bkg-color);width:100%;height:100%;display:grid;position:relative;overflow:hidden}.cell{min-width:0;min-height:0;position:relative;overflow:hidden}::slotted(*){width:100%;height:100%;display:block}.splitter{z-index:100;background:var(--bkg-color);border:var(--app-standard-border);touch-action:none;border-radius:1rem;position:absolute}.splitter-vertical{top:calc(var(--start) + 2rem);bottom:calc(var(--end) + 2rem);cursor:col-resize;width:6px;transform:translate(-50%)}.splitter-horizontal{left:calc(var(--start) + 2rem);right:calc(var(--end) + 2rem);cursor:row-resize;height:6px;transform:translateY(-50%)}.splitter:before,.splitter:after{content:"";background:var(--app-color-grey);position:absolute}.splitter-vertical:before,.splitter-vertical:after{width:1px;height:2rem;left:50%;transform:translate(-50%)}.splitter-vertical:before{height:calc(2rem + 1px);top:calc(-2rem - 1px)}.splitter-vertical:after{height:calc(2rem + 1px);bottom:calc(-2rem - 1px)}.splitter-horizontal:before,.splitter-horizontal:after{width:2rem;height:1px;top:50%;transform:translateY(-50%)}.splitter-horizontal:before{width:calc(2rem + 1px);left:calc(-2rem - 1px)}.splitter-horizontal:after{width:calc(2rem + 1px);right:calc(-2rem - 1px)} </style> <style>${this.customStyle}</style> <div id="grid">${this.cells.map((cell, index) => uHtml `<div class="cell" style="${this.getCellArea(cell)}"><slot name="${'cell-' + index}"></slot></div>`)} ${this.splitters.map((splitter) => uHtml `<div id="${this.getSplitterId(splitter)}" class="${'splitter splitter-' + splitter.orientation}" role="separator" onpointerdown="${(e) => this.onSplitterPointerDown(e, splitter)}" onpointermove="${(e) => this.onSplitterPointerMove(e, splitter)}" onpointerup="${(e) => this.onSplitterPointerUp(e, splitter)}"></div>`)}</div> ${this.htmlUnsafe(this.feedbackTemplateHtml ?? '')}`; }; /** The Cells of the current Layout, used to render them and their slots. */ cells = []; /** The splitters between the Cells of the current Layout. */ splitters = []; /** The splitter that is currently dragged, if any. */ draggedSplitter = null; constructor() { super('map-grid'); } get layout() { return this.state.layout; } get grid() { return this.getById('grid'); } connectedCallback() { super.connectedCallback(); this.render(); // The subscription on layout.type is called immediately, which builds the Grid. this.registerEvents(); } registerEvents() { this.subscribe('layout.type', () => this.applyLayout()); this.subscribe('layout.cells', () => this.applyCells()); this.subscribe('layout.sizes.columns', () => this.applySizes()); this.subscribe('layout.sizes.rows', () => this.applySizes()); } /** * Rebuilds the Cells and the splitters of the Grid for the current Layout, * then places the Components and applies the sizes. */ applyLayout() { const type = this.layout.type; this.cells = getLayoutCells(type); this.splitters = this.createSplitters(type); this.refreshRender(); this.applyCells(); this.applySizes(); } /** * Creates one splitter for each boundary between two columns and between two rows. * The columns and the rows keep the same sizes in every Cell of the Grid: a vertical splitter * goes along all the rows that have a Cell ending on its boundary, and a horizontal splitter * goes along all the columns. */ createSplitters(type) { const { columns, rows } = getGridSize(type); const cells = getLayoutCells(type); const splitters = []; for (let index = 0; index < columns - 1; index++) { const rowsAtBoundary = cells .filter((cell) => cell.column + cell.columnSpan === index + 1) .map((cell) => cell.row); splitters.push({ orientation: 'vertical', index: index, position: 0, from: Math.min(...rowsAtBoundary), to: Math.max(...rowsAtBoundary), start: 0, end: 0 }); } for (let index = 0; index < rows - 1; index++) { splitters.push({ orientation: 'horizontal', index: index, position: 0, from: 0, to: columns - 1, start: 0, end: 0 }); } return splitters; } getCellArea(cell) { return `grid-area: ${cell.row + 1} / ${cell.column + 1} / span 1 / span ${cell.columnSpan}`; } /** * Moves each Component into the Cell it is configured for, by setting the name of the slot * it must be displayed in. Components that are not placed in any Cell get no slot, * which means that they are not displayed at all. */ applyCells() { const cells = this.layout.cells; for (const component of AVAILABLE_CELL_COMPONENTS) { const element = this.querySelector(component.tagName); if (!element) { continue; } const cellIndex = cells.indexOf(component.id); if (cellIndex >= 0) { element.setAttribute('slot', `cell-${cellIndex}`); } else { element.removeAttribute('slot'); } } } applySizes() { if (!this.grid) { return; } const { columns, rows } = this.layout.sizes; this.applyGridSizes(columns, rows); } /** Applies the sizes of the columns and of the rows to the Grid and to the splitters. */ applyGridSizes(columns, rows) { this.grid.style.gridTemplateColumns = columns.map((size) => `${size}%`).join(' '); this.grid.style.gridTemplateRows = rows.map((size) => `${size}%`).join(' '); for (const splitter of this.splitters) { const isVertical = splitter.orientation === 'vertical'; const sizesAcross = isVertical ? columns : rows; const sizesAlong = isVertical ? rows : columns; splitter.position = this.sumOf(sizesAcross.slice(0, splitter.index + 1)); splitter.start = this.sumOf(sizesAlong.slice(0, splitter.from)); splitter.end = this.sumOf(sizesAlong.slice(0, splitter.to + 1)); this.applySplitterPosition(splitter); } } sumOf(sizes) { return sizes.reduce((sum, size) => sum + size, 0); } applySplitterPosition(splitter) { const element = this.getById(this.getSplitterId(splitter)); if (!element) { return; } // When the Layout changes, uHtml reuses the Elements of the previous splitters, which can have // the other orientation. The position of the other orientation has therefore to be removed, // otherwise it would override the size the splitter gets from its stylesheet. const isVertical = splitter.orientation === 'vertical'; element.style.left = isVertical ? `${splitter.position}%` : ''; element.style.top = isVertical ? '' : `${splitter.position}%`; element.style.setProperty('--start', `${splitter.start}%`); element.style.setProperty('--end', `${100 - splitter.end}%`); } getSplitterId(splitter) { return `splitter-${splitter.orientation}-${splitter.index}`; } onSplitterPointerDown(event, splitter) { event.preventDefault(); this.draggedSplitter = splitter; event.target.setPointerCapture(event.pointerId); } /** * Resizes the two columns (or the two rows) around the dragged splitter. * While dragging, the new sizes are only applied to the DOM: the state is updated * at the end of the drag, to prevent flooding it with intermediate values. */ onSplitterPointerMove(event, splitter) { if (this.draggedSplitter !== splitter) { return; } const gridBounds = this.grid.getBoundingClientRect(); const isVertical = splitter.orientation === 'vertical'; const position = isVertical ? ((event.clientX - gridBounds.left) / gridBounds.width) * 100 : ((event.clientY - gridBounds.top) / gridBounds.height) * 100; const sizes = this.getResizedSizes(splitter, position); if (!sizes) { return; } const { columns, rows } = this.layout.sizes; this.applyGridSizes(isVertical ? sizes : columns, isVertical ? rows : sizes); } onSplitterPointerUp(event, splitter) { if (this.draggedSplitter !== splitter) { return; } this.draggedSplitter = null; event.target.releasePointerCapture(event.pointerId); const sizes = this.getResizedSizes(splitter, splitter.position); if (sizes) { if (splitter.orientation === 'vertical') { this.layout.sizes.columns = sizes; } else { this.layout.sizes.rows = sizes; } } } /** * Computes the new sizes of the columns (or of the rows) when the splitter is moved * to the given position, in percent of the Grid. The two resized columns (or rows) are * kept between MIN_CELL_SIZE and MAX_CELL_SIZE, the other ones keep their size. */ getResizedSizes(splitter, position) { const sizes = [ ...(splitter.orientation === 'vertical' ? this.layout.sizes.columns : this.layout.sizes.rows) ]; const before = splitter.index; const after = splitter.index + 1; // Size the splitter can be dragged in, without taking the columns/rows that are not resized into account. const offset = sizes.slice(0, before).reduce((sum, size) => sum + size, 0); const available = sizes[before] + sizes[after]; let newSizeBefore = position - offset; newSizeBefore = Math.max(MIN_CELL_SIZE, Math.min(MAX_CELL_SIZE, newSizeBefore)); newSizeBefore = Math.min(newSizeBefore, available - MIN_CELL_SIZE); newSizeBefore = Math.max(newSizeBefore, available - MAX_CELL_SIZE); sizes[before] = newSizeBefore; sizes[after] = available - newSizeBefore; return sizes; } render() { super.render(); super.girafeTranslate(); } } export default MapGridComponent;