UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

314 lines (312 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{color:var(--link-color)}a:visited{color:var(--link-color)}@keyframes spin{from{transform:rotate(0)}to{transform:rotate(360deg)}}@keyframes spin-wait{0%{transform:rotate(0)}7%{transform:rotate(360deg)}100%{transform:rotate(360deg)}}.gg-spin{animation-name:spin;animation-duration:2s;animation-iteration-count:infinite;animation-timing-function:linear}.gg-spin-wait{animation-name:spin-wait;animation-duration:10s;animation-iteration-count:infinite;animation-timing-function:linear}::-webkit-scrollbar{width:5px}::-webkit-scrollbar-thumb{background:#999}.gg-button,.gg-input,.gg-select,.gg-textarea{background-color:var(--bkg-color);color:var(--text-color);outline:0;border-radius:3px;border:var(--app-standard-border);display:inline-block;box-sizing:border-box;padding:0 0 0 .5rem;margin:0;cursor:pointer}.gg-label{background-color:var(--bkg-color);color:var(--text-color);border:none;display:inline-block;padding:0;margin:0}.gg-button,.gg-input,.gg-label,.gg-select{min-height:calc(var(--app-standard-height)/ 1.5);max-height:calc(var(--app-standard-height)/ 1.5);line-height:calc(var(--app-standard-height) / 1.5)}.gg-textarea{padding:.5rem;height:6rem}.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{background-color:#d3d3d3;color:grey;border:none;cursor:not-allowed}.gg-button>img{vertical-align:middle}.gg-icon-button{border:none;background-color:transparent;display:flex;flex-direction:column;color:var(--text-color);padding:0;align-items:center;justify-content:center;cursor:pointer}.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{margin:0;width:calc(var(--app-standard-height) - 1.5rem)}.gg-big-withtext span{font-size:.9rem;font-variant:small-caps;padding:0 1rem}.gg-medium,.gg-medium-withtext{flex-direction:row;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)}.gg-medium img{margin:0;width:calc(var(--app-standard-height)/ 2.4)}.gg-medium-withtext img{margin-left:.5rem;width:calc(var(--app-standard-height)/ 2.4)}.gg-medium-withtext span{font-size:.9rem;padding:0 1rem;padding-left:.5rem}.gg-small,.gg-small-withtext{flex-direction:row;min-width:calc(var(--app-standard-height)/ 2);min-height:calc(var(--app-standard-height)/ 2);max-height:calc(var(--app-standard-height)/ 2)}.gg-small img{margin:0;width:calc(var(--app-standard-height)/ 3)}.gg-small-withtext img{margin-left:.5rem;width:calc(var(--app-standard-height)/ 3)}.gg-small-withtext span{font-size:.9rem;padding:0 .5rem;padding-left:.3rem}.gg-button:hover,.gg-icon-button:hover,.gg-input:hover,.gg-select:hover,.gg-textarea:hover{background-color:var(--bkg-color-grad1)}.gg-opacity{opacity:.5}.gg-opacity:hover{background-color:transparent;opacity:1}.gg-tabs{display:flex;justify-content:left;margin:.5rem auto 0;border-bottom:1px solid grey;cursor:pointer;height:2rem;align-items:end;gap:1rem;margin-bottom:1rem}.gg-tab{border:none;background:0 0;cursor:pointer;padding:.5rem;color:var(--text-color)}.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{border:none;background-color:transparent;display:flex;flex-direction:column;color:var(--text-color)}.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(1)}.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{margin:.3rem;height:2rem}.girafe-button-large span{line-height:2rem;height:2rem;margin:.3rem}.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{width:100%;text-align:left;overflow:hidden;text-overflow:ellipsis}.girafe-button-tiny{width:1rem;height:1rem;align-items:center;padding:0}.girafe-button-tiny img{overflow:hidden} </style><style> #panel,:host{min-height:10rem;height:16rem;background:var(--bkg-color)}#panel{top:0;left:0;right:0;bottom:0;overflow:hidden}#gutter{border:none;height:6px;width:100%;background:var(--bkg-color);position:absolute;top:0;left:0;cursor:row-resize}#close{border:none;color:#777;padding:0;margin:.5rem;width:1rem;height:1rem;line-height:1rem;position:absolute;top:0;right:0;background-color:var(--bkg-color);cursor:pointer}#close i:before{content:'\uf00d'}#close:hover{border-color:#000;color:#000}#content{position:absolute;top:0;left:0;right:0;bottom:0;background:var(--bkg-color);padding:0;margin:.5rem;overflow:hidden;display:flex;flex-direction:column}#header{flex:0;display:flex;justify-content:space-between}.selection-panel{margin-right:2rem}.tab{border:solid 1px #ccc;display:inline-block;height:2rem;line-height:2rem;padding:0 1rem;margin-right:.2rem;margin-bottom:.2rem;cursor:pointer;background-color:var(--bkg-color-grad2)}.tab.active{color:var(--text-color);background-color:var(--bkg-color)}#grid{overflow:hidden;flex:1}.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{height:1.5rem;float:left;margin-right:.5rem}.tabulator-cell span{line-height:1.5rem;display:inline-flex;align-items:center;position:relative}.tabulator-cell span sup{position:absolute;right:-.5rem;top:-.3rem}@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() { this.visible ? this.renderComponent() : this.renderComponentEmpty(); } /** * @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 */ renderComponentEmpty() { 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;