UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

319 lines (317 loc) 18.1 kB
import { html as uHtml } from 'uhtml'; import GirafeResizableElement from '../../base/GirafeResizableElement'; import * as olExtent from 'ol/extent'; import { debounce } from '../../tools/utils/debounce'; import SelectionTabulatorManager from './tools/selectiontabulatormanager'; import CsvManager from '../../tools/export/csvmanager'; import MapManager from '../../tools/state/mapManager'; /** * Represents a selection grid component based on GridJs. * Display itself when it should be visible and have selected features. * To be visible, it has to be the defined selectionComponent. * @extends GirafeResizableElement */ class SelectionGridComponent extends GirafeResizableElement { constructor() { super('selectiongrid'); Object.defineProperty(this, "template", { enumerable: true, configurable: true, writable: true, value: () => { return uHtml `<style> *{font-family:Arial,sans-serif}.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}.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-grad2);background-color:var(--text-color-grad2);color:var(--bkg-color)}.gg-button:disabled{color:gray;cursor:not-allowed;background-color:#d3d3d3;border:none}.gg-input:disabled,.gg-select:disabled,.gg-textarea:disabled{color:gray;cursor:not-allowed;background-color:#d3d3d3}.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-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,.gg-select:hover,.gg-input:hover,.gg-textarea:hover,.gg-icon-button:hover{background-color:var(--bkg-color-grad1)}.gg-opacity{opacity:.5}.gg-opacity:hover{opacity:1;background-color:#0000}.gg-tabs{cursor:pointer;border-bottom:1px solid gray;justify-content:left;align-items:end;gap:1rem;height:2rem;margin:.5rem auto 1rem;display:flex}.gg-tab{cursor:pointer;color:var(--text-color);background:0 0;border:none;padding:.5rem}.gg-tab.active{border-bottom:solid 2px var(--text-color-grad2);font-weight:600}.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} </style><style> :host,#panel{background:var(--bkg-color);height:16rem;min-height:10rem}#panel{inset:0;overflow:hidden}#gutter{background:var(--bkg-color);cursor:row-resize;border:none;width:100%;height:6px;position:absolute;top:0;left:0}#close{color:#777;background-color:var(--bkg-color);cursor:pointer;border:none;width:1rem;height:1rem;margin:.5rem;padding:0;line-height:1rem;position:absolute;top:0;right:0}#close i:before{content:"uf00d"}#close:hover{color:#000;border-color:#000}#content{background:var(--bkg-color);flex-direction:column;margin:.5rem;padding:0;display:flex;position:absolute;inset:0;overflow:hidden}#header{flex:0;justify-content:space-between;display:flex}.selection-panel{margin-right:2rem}.tab{cursor:pointer;background-color:var(--bkg-color-grad2);border:1px solid #ccc;height:2rem;margin-bottom:.2rem;margin-right:.2rem;padding:0 1rem;line-height:2rem;display:inline-block}.tab.active{color:var(--text-color);background-color:var(--bkg-color)}#grid{flex:1;overflow:hidden}.geo-type{margin-right:.5rem}button{margin-left:.5rem}::-webkit-scrollbar{width:8px}::-webkit-scrollbar-thumb{background:#999}.tiny-margin{margin:.1rem}.tabulator-col[aria-sort=none] .tabulator-col-sorter i{color:#999}.tabulator-col[aria-sort=asc] .tabulator-col-sorter i{color:red}.tabulator-col[aria-sort=desc] .tabulator-col-sorter i{color:red;transform:scaleY(-1)}#tabulator{background-color:var(--bkg-color)}#tabulator .tabulator-header .tabulator-col-content{background-color:var(--bkg-color);color:var(--text-color)}#tabulator .tabulator-header .tabulator-col,#tabulator .tabulator-header .tabulator-col-row-handle{white-space:normal}#tabulator .tabulator-tableholder .tabulator-table .tabulator-row{background-color:var(--bkg-color);color:var(--text-color)}#tabulator .tabulator-tableholder .tabulator-table .tabulator-row:nth-child(2n){background-color:var(--bkg-color-grad1)}#tabulator .tabulator-tableholder .tabulator-table .tabulator-row.tabulator-selected{background-color:#9abcea}.tabulator-cell img{float:left;height:1.5rem;margin-right:.5rem}.tabulator-cell span{align-items:center;line-height:1.5rem;display:inline-flex;position:relative}.tabulator-cell span sup{position:absolute;top:-.3rem;right:-.5rem}@media (hover:hover) and (pointer:fine){#tabulator .tabulator-tableholder .tabulator-table .tabulator-row:hover{cursor:pointer;background-color:var(--bkg-color-grad2)}} </style> <link rel="stylesheet" href="styles/common.css" /> <link rel="stylesheet" href="lib/tabulator-tables/tabulator.min.css" /> <link rel="stylesheet" href="../../../node_modules/tabulator-tables/dist/css/tabulator.css" /> <div id="panel"> <div id="content"> <div id="header"> <div class="tab-panel"> ${this.getTabHeaders().map((th) => uHtml ` <button id="${th.id}" i18n="${th.text}" class="${th.active ? 'tab active' : 'tab'}" .active="${th.active}" onclick="${() => this.displayGrid(th.id, true)}"></button> `)} </div> <div class="selection-panel"> <button class="${this.showCsvButton ? 'gg-button' : 'hidden'}" onclick="${() => this.zoomToSelection()}" i18n="Zoom to"></button> <button class="gg-button" onclick="${() => this.selectAll()}" i18n="Select all"></button> <button class="gg-button" onclick="${() => this.selectNone()}" i18n="Select none"></button> <button class="gg-button" onclick="${() => this.invertSelection()}" i18n="Invert selection"></button> <button class="${this.showCsvButton ? 'gg-button' : 'hidden'}" onclick="${() => this.generateCSV()}" i18n="Generate CSV"></button> </div> </div> <div id="tabulator"></div> </div> <div id="gutter"></div> <button id="close" onclick="this.closePanel()"> <img id="close" alt="close-icon" src="icons/close.svg" /> </button> </div> `; } }); Object.defineProperty(this, "eventsCallbacks", { enumerable: true, configurable: true, writable: true, value: [] }); Object.defineProperty(this, "selectionTabulatorManager", { enumerable: true, configurable: true, writable: true, value: new SelectionTabulatorManager() }); Object.defineProperty(this, "csvManager", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "mapManager", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "isVisibleComponentSetup", { enumerable: true, configurable: true, writable: true, value: false }); Object.defineProperty(this, "debounceOnFeaturesSelected", { enumerable: true, configurable: true, writable: true, value: debounce(this.onFeaturesSelected.bind(this), 200) }); Object.defineProperty(this, "visible", { enumerable: true, configurable: true, writable: true, value: false }); Object.defineProperty(this, "currentTabId", { enumerable: true, configurable: true, writable: true, value: '' }); Object.defineProperty(this, "showCsvButton", { enumerable: true, configurable: true, writable: true, value: false }); this.csvManager = CsvManager.getInstance(); this.mapManager = MapManager.getInstance(); this.subscribe('selection.gridSelected', (_oldValue, newValue) => { this.showCsvButton = newValue; this.render(); }); this.addEventListener('resize-start', () => { this.selectionTabulatorManager.blockRedraw(); }); this.addEventListener('resize-end', () => { this.selectionTabulatorManager.restoreRedraw(); }); } connectedCallback() { this.render(); this.registerVisibilityEvents(); } /** * Render the component regarding its visibility. */ render() { if (this.visible) { this.renderComponent(); } else { this.renderEmptyComponent(); } } /** * @returns The current array of TabHeader objects. */ getTabHeaders() { return this.selectionTabulatorManager.tabHeaders; } /** * Activates the specified tab, renders the grid, and displays the grid for the specified id. */ displayGrid(id, replaceData = false) { if (replaceData) { this.selectionTabulatorManager.replaceData(id); } this.selectionTabulatorManager.activateTab(id); this.render(); this.selectionTabulatorManager.displayGrid(id); this.currentTabId = id; } /** * Clean the grid, closes the panel and deselect the selected features. */ closePanel() { this.state.interface.selectionComponentVisible = false; this.state.selection.selectedFeatures = []; this.showCsvButton = false; this.state.selection.gridSelected = false; super.clean(); } /** * Selects all rows in the grid. */ selectAll() { this.selectionTabulatorManager.table?.selectRow(); } /** * Deselects all rows in the grid. */ selectNone() { this.selectionTabulatorManager.table?.deselectRow(); } /** * Inverts the selection of all rows in the grid. */ invertSelection() { this.selectionTabulatorManager.table?.getRows().forEach((row) => { row.toggleSelect(); }); } /** * Zooms to the extent of the selected rows in the grid. */ zoomToSelection() { const extent = olExtent.createEmpty(); this.selectionTabulatorManager.table?.getRows().forEach((row) => { if (row.isSelected()) { const data = row.getData(); const geometry = data.geom ?? data.the_geom ?? data.geometry; olExtent.extend(extent, geometry.getExtent()); } }); this.mapManager.getMap().getView().fit(extent, { duration: 300 }); } /** * Generates a CSV file from the selected rows in the grid. */ generateCSV() { const columns = this.selectionTabulatorManager.data[this.currentTabId].columns.map((column) => { return { name: column }; }); const excludedColumns = ['geom', 'the_geom', 'geometry']; const filteredColumns = columns.filter((column) => !excludedColumns.includes(column.name)); this.csvManager.startDownload(this.selectionTabulatorManager.table?.getSelectedData() ?? [], filteredColumns, 'query-results.csv'); } /** * Renders the component by calling the necessary methods. * @private */ renderComponent() { super.render(); if (!this.isVisibleComponentSetup) { this.setupVisibleComponent(); } super.girafeTranslate(); } /** * Sets up the components state and side-kicks. * This must be called once at the first (visible) rendering. * @private */ setupVisibleComponent() { this.showCsvButton = false; this.state.selection.gridSelected = false; this.isVisibleComponentSetup = true; this.registerEvents(); } /** * Render a placeholder, not visible component on hide. * Removes event registration. * @private */ renderEmptyComponent() { this.unsubscribe(this.eventsCallbacks); this.eventsCallbacks.length = 0; this.isVisibleComponentSetup = false; this.renderEmpty(); } /** * Event about visibility that must be always listened by this component, even hidden. * @private */ registerVisibilityEvents() { this.subscribe('interface.selectionComponentVisible', (_oldValue, newValue) => this.togglePanel(newValue)); } /** * Listen events that must be listened if the component is visible. * @private */ registerEvents() { this.eventsCallbacks.push(this.subscribe('selection.selectedFeatures', (_oldFeatures, newFeatures) => { // Use debounce to avoid quickly closing the grid on selection change. this.debounceOnFeaturesSelected(newFeatures); })); } /** * Handles the selected features and updates the grid data accordingly or * close the panel if no data is selected. * @private */ onFeaturesSelected(features) { this.selectionTabulatorManager.tabHeaders = []; // No feature ? Close. if (this.isNullOrUndefined(features) || features.length <= 0) { this.closePanel(); return; } this.selectionTabulatorManager.featuresToGridData(features); const tabIds = Object.keys(this.selectionTabulatorManager.idTab); //define table element this.selectionTabulatorManager.setElement(this.shadow.querySelector('#tabulator')); // No tab ? Close the panel. if (!tabIds.length) { this.closePanel(); } // Otherwise, activate and display the first tab. this.displayGrid(tabIds[0]); } /** * Toggles the panel visibility. If visible, tries to display a grid with selected feature. * Can only be visible if the component is the wanted selection component. * @private */ togglePanel(visible) { if (this.state.interface.selectionComponent !== 'grid') { if (!this.visible) { return; } visible = false; } this.visible = visible; if (visible) { // Will be rendered after computing selected feature. this.onFeaturesSelected(this.state.selection.selectedFeatures); } else { this.render(); } } } export default SelectionGridComponent;