@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
750 lines (745 loc) • 34.8 kB
JavaScript
import { html as uHtml } from 'uhtml';
import Collection from 'ol/Collection';
import Feature from 'ol/Feature';
import VectorSource from 'ol/source/Vector';
import VectorLayer from 'ol/layer/Vector';
import { LineString, MultiLineString, MultiPolygon, Point, MultiPoint, Polygon } from 'ol/geom';
import { Style, Icon, Stroke, Fill } from 'ol/style';
import { buffer, getWidth, getHeight, getCenter, containsExtent } from 'ol/extent';
import GirafeColorPicker from '../../tools/utils/girafecolorpicker';
import PinIcon from './images/pin.svg';
import LayerIcon from './images/layer.svg';
import LayerGroupIcon from './images/layergroup.svg';
import SearchIcon from './images/search.svg';
import PaintbrushIcon from './images/paintbrush.svg';
import GirafeHTMLElement from '../../base/GirafeHTMLElement';
import MapManager from '../../tools/state/mapManager';
import LayerManager from '../../tools/layers/layermanager';
import { parseCoordinates } from '../../tools/geometrytools';
import ThemesHelper from '../../tools/themes/themeshelper';
import ThemeLayer from '../../models/layers/themelayer';
class SearchComponent extends GirafeHTMLElement {
constructor() {
super('search');
Object.defineProperty(this, "template", {
enumerable: true,
configurable: true,
writable: true,
value: () => {
return uHtml `<style>
*{font-family:Arial,sans-serif}.hidden{display:none}.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>
.hidden{display:none}#searchbox{background-color:var(--bkg-color);border:1px solid #cfd6dd;border-radius:4px;flex-direction:row;width:100%;height:2.25rem;display:flex}#search{-webkit-appearance:none;width:auto;height:2.25rem;color:var(--text-color);background:0 0;border:0;outline:none;-webkit-flex:auto;-ms-flex:auto;flex:auto;margin:0 0 0 .5rem;padding-left:.8rem;font-size:1rem}.search-icon,.close-icon,.select-color-icon{width:14px;color:var(--text-color);padding:0 1rem}.close-button,.select-color-button{cursor:pointer;background-color:#0000;border:none;padding:0}#results{z-index:1000;background-color:var(--bkg-color);scrollbar-width:thin;width:36rem;max-height:31rem;margin-top:.1rem;margin-bottom:.5rem;position:absolute;top:3.5rem;overflow-x:hidden}.result,.title{width:34rem;padding:.3rem 1rem;line-height:1.3rem;display:inline-block}.title{color:var(--text-color);padding-top:1.5rem;font-size:1.3rem;font-weight:600;line-height:2.5rem}.result{cursor:pointer;text-align:left;width:100%;color:var(--text-color);background:0 0;border:none;border-top:1px solid #ccc}.title img{width:14px;margin-right:1rem}.title span{text-transform:uppercase}.result.selected{color:var(--text-color);background-color:#aaa}#no-result{background:var(--warning-color);opacity:.75;visibility:hidden;pointer-events:none;border-radius:6px;width:fit-content;margin:auto;padding:.5rem 0;font-size:1rem;position:absolute;top:3.5rem;left:0;right:0}#no-result>span{padding:1.3rem}@keyframes fadeOut{0%{opacity:1}80%{opacity:1}to{opacity:0}}.shown-then-fadeout{animation:2s ease-in-out forwards fadeOut;visibility:visible}
</style>
<link rel="stylesheet" href="lib/vanilla-picker/vanilla-picker.csp.css" />
<link rel="stylesheet" href="styles/girafecolorpicker.css" />
<div id="searchbox">
<input
tip="search_tip"
id="search"
class="gg-input"
length="20"
maxlength="1000"
autocomplete="off"
autocorrect="off"
i18n="Search..."
placeholder="Search..."
oninput="${(e) => this.doSearchDebounced(e)}"
onfocusin="${() => this.onFocusIn()}"
onfocusout="${() => this.onFocusOut()}"
onkeydown="${(e) => this.onKeyDown(e)}" />
<img class="${this.allResults.length > 0 ? 'hidden' : 'search-icon'}" alt="search-icon" src="${this.searchIcon}" />
<button class="${this.allResults.length > 0 ? 'close-button' : 'hidden'}" onclick="${() => this.clearSearch(true)}">
<img class="close-icon" alt="close-icon" src="icons/close.svg" />
</button>
<button
tip="change_search_result_color"
id="colorPickerBtn"
class="${this.paintSearchResults ? 'select-color-button' : 'hidden'}">
<img class="select-color-icon" alt="select-color-icon" src="${this.paintbrushIcon}" />
</button>
<div
id="results"
class="${Object.keys(this.groupedResults).length === 0 || this.forceHide ? 'hidden' : ''}"
onmousemove="${() => this.onMouseMove()}">
${Object.keys(this.groupedResults).map(group => uHtml `
<div class="title">
<img alt="result icon" src="${this.getIcon(group)}" />
<span i18n="${group}"></span>
</div>
${this.groupedResults[group].map(result => uHtml `
<button
class="${result.selected ? 'result selected' : 'result'}"
onmousedown="${() => this.onMouseDown()}"
onclick="${() => this.onSelect(result)}"
onmouseover="${() => this.onMouseOver(result)}"
onmouseleave="${() => this.onMouseLeave()}">
${result.properties?.label}
</button>
`)} `)}
</div>
<div id="no-result" class="${this.showNoResultWarning ? 'shown-then-fadeout' : ''}">
<span i18n="${'No match found'}"></span>
</div>
</div>
`;
}
});
Object.defineProperty(this, "searchIcon", {
enumerable: true,
configurable: true,
writable: true,
value: SearchIcon
});
Object.defineProperty(this, "paintbrushIcon", {
enumerable: true,
configurable: true,
writable: true,
value: PaintbrushIcon
});
Object.defineProperty(this, "themesHelper", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "layerManager", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "map", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "previewFeaturesCollection", {
enumerable: true,
configurable: true,
writable: true,
value: new Collection()
});
Object.defineProperty(this, "previewTheme", {
enumerable: true,
configurable: true,
writable: true,
value: null
});
Object.defineProperty(this, "previewGeoLayer", {
enumerable: true,
configurable: true,
writable: true,
value: null
});
Object.defineProperty(this, "maxExtent", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "ignoreBlur", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
Object.defineProperty(this, "groupedResults", {
enumerable: true,
configurable: true,
writable: true,
value: {}
});
Object.defineProperty(this, "allResults", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
Object.defineProperty(this, "forceHide", {
enumerable: true,
configurable: true,
writable: true,
value: true
});
Object.defineProperty(this, "searchTermPlaceholder", {
enumerable: true,
configurable: true,
writable: true,
value: '###SEARCHTERM###'
});
Object.defineProperty(this, "searchLangPlaceholder", {
enumerable: true,
configurable: true,
writable: true,
value: '###SEARCHLANG###'
});
Object.defineProperty(this, "COORD_REGEX", {
enumerable: true,
configurable: true,
writable: true,
value: /^(\d+[.,]?\d*)\s*[,;/\s]\s*(\d+[.,]?\d*)$/
});
Object.defineProperty(this, "focusedResultIndex", {
enumerable: true,
configurable: true,
writable: true,
value: -1
});
Object.defineProperty(this, "focusedResult", {
enumerable: true,
configurable: true,
writable: true,
value: null
});
Object.defineProperty(this, "selectedResult", {
enumerable: true,
configurable: true,
writable: true,
value: null
});
Object.defineProperty(this, "searchBox", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "paintSearchResults", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "defaultSearchStrokeColor", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "defaultSearchFillColor", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "abortController", {
enumerable: true,
configurable: true,
writable: true,
value: new AbortController()
});
Object.defineProperty(this, "showNoResultWarning", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
// Keeping track of the last input timeout
Object.defineProperty(this, "ongoingSearchTimeoutId", {
enumerable: true,
configurable: true,
writable: true,
value: 0
});
this.themesHelper = ThemesHelper.getInstance();
this.layerManager = LayerManager.getInstance();
this.map = MapManager.getInstance().getMap();
this.defaultSearchStrokeColor = this.configManager.Config.search.defaultStrokeColor;
this.defaultSearchFillColor = this.configManager.Config.search.defaultFillColor;
this.createPreviewLayer();
}
createPreviewLayer() {
this.configManager.loadConfig().then(() => {
this.paintSearchResults = this.configManager.Config.search.paintSearchResults;
this.maxExtent = this.configManager.Config.map.maxExtent?.split(',').map(Number);
this.previewGeoLayer = new VectorLayer({
properties: {
addToPrintedLayers: true
},
source: new VectorSource({
features: this.previewFeaturesCollection
})
});
this.updatePreviewLayerStyle(this.defaultSearchFillColor, this.defaultSearchStrokeColor);
this.map.addLayer(this.previewGeoLayer);
this.previewGeoLayer.setZIndex(1010);
this.initColorPicker();
});
}
onMouseDown() {
this.ignoreBlur = true;
}
onFocusIn() {
this.forceHide = false;
super.render();
}
onFocusOut() {
if (!this.ignoreBlur) {
this.forceHide = true;
super.render();
}
this.ignoreBlur = false;
}
render() {
super.render();
this.searchBox = this.shadowRoot?.getElementById('search');
}
connectedCallback() {
this.loadConfig().then(() => {
this.render();
super.girafeTranslate();
});
}
clearSearch(purge = false) {
if (purge) {
if (this.searchBox) {
this.searchBox.value = '';
}
}
this.forceHide = false;
this.groupedResults = {};
this.allResults = [];
this.clearPreview();
this.focusedResultIndex = -1;
this.focusedResult = null;
super.render();
}
async doSearch(e) {
this.showNoResultWarning = false;
// Cancel any previous search
this.abortController.abort();
// Create a new controller for the new request
const currentAbortController = new AbortController();
this.abortController = currentAbortController;
const target = e.target;
const term = target.value.trim();
this.clearSearch();
if (this.COORD_REGEX.test(term)) {
this.displayCoordinates(term);
return;
}
if (term.length > 0) {
const url = this.configManager.Config.search.url
.replace(this.searchTermPlaceholder, term)
.replace(this.searchLangPlaceholder, this.state.language);
try {
const response = await fetch(url, { signal: this.abortController.signal });
const data = await response.json();
// If the search term is at least two charecter but yieds no result, a warning
// box is displayed for 2 seconds and then fades out (CSS)
if (data.features.length === 0 && term.length >= 2) {
this.showNoResultWarning = true;
}
this.displayResults(data);
}
catch (error) {
if (error instanceof DOMException && error.name === 'AbortError') {
// Request was aborted, ignore the error
console.debug('Multiple parallel search: previous request was aborted.');
return;
}
}
}
}
/**
* Debounce the fetch call to API to prevent sending request at every stroke.
* @param e
*/
async doSearchDebounced(e) {
if (this.ongoingSearchTimeoutId !== 0) {
clearTimeout(this.ongoingSearchTimeoutId);
}
// The original even cannot be passed because event objects are ephemeral and
// not designed to be used asynchronously (the target property would be undefined)
const syntheticEvent = {
target: { value: e.target && 'value' in e.target ? e.target.value : undefined }
};
this.ongoingSearchTimeoutId = window.setTimeout(() => {
this.ongoingSearchTimeoutId = 0;
this.doSearch(syntheticEvent);
}, 300);
}
/**
* Will render the result of the search with coordinates
* @param term typed string
*/
displayCoordinates(term) {
const matches = this.COORD_REGEX.exec(term);
const coord1 = Number.parseFloat(matches[1].replace(',', '.'));
const coord2 = Number.parseFloat(matches[2].replace(',', '.'));
const current_srid = this.map.getView().getProjection().getCode();
const [east_coord, north_coord] = parseCoordinates([coord1, coord2], this.maxExtent, current_srid);
// Don't show result if no corresponding coordinates were parsed
if (!east_coord || !north_coord) {
return;
}
const result = {
bbox: [east_coord, north_coord, east_coord, north_coord],
geometry: {
type: 'Point',
coordinates: [east_coord, north_coord]
},
properties: {
label: `${coord1} ${coord2}`,
layer_name: 'recenter_map'
}
};
this.allResults = [result];
this.groupedResults.recenter_map = [result];
super.render();
super.girafeTranslate();
}
displayResults(results) {
// First, group the results
for (const result of results.features) {
// results.features.forEach((result) => {
let type = 'Unknown layer type';
if (result.properties) {
if (result.properties.layer_name) {
type = result.properties.layer_name;
}
else if (result.properties.actions[0].action === 'add_theme') {
type = 'add_theme';
}
else if (result.properties.actions[0].action === 'add_group') {
type = 'add_group';
}
else if (result.properties.actions[0].action === 'add_layer') {
type = 'add_layer';
}
}
let resultList;
if (type in this.groupedResults) {
resultList = this.groupedResults[type];
}
else {
resultList = [];
this.groupedResults[type] = resultList;
}
resultList.push(result);
}
// Manage a flat list with all results
this.allResults = Object.values(this.groupedResults).flat();
// And then rerender the results
super.render();
super.girafeTranslate();
}
getIcon(searchGroup) {
switch (searchGroup) {
case 'Group':
return LayerGroupIcon;
case 'Layer':
return LayerIcon;
default:
return PinIcon;
}
}
onMouseOver(result) {
this.focusResult(result);
}
onMouseLeave() {
// Clear preview search result, only if the result was not selected
if (this.selectedResult === null) {
this.clearPreview();
}
}
focusResultFromIndex() {
const result = this.allResults[this.focusedResultIndex];
this.focusResult(result);
}
focusResult(result) {
// Clear old selection and preview
this.clearPreview();
if (this.focusedResult) {
this.focusedResult.selected = false;
}
// Set new selected object, and activate preview
this.focusedResultIndex = this.allResults.findIndex((r) => r === result);
this.focusedResult = this.allResults[this.focusedResultIndex];
this.focusedResult.selected = true;
this.render();
this.preview(result);
// Scroll to selected div
const resultHtmlElement = this.shadow.querySelectorAll('.result')[this.focusedResultIndex];
resultHtmlElement.scrollIntoView({ block: 'nearest' });
}
preview(result) {
if (result.bbox && this.configManager.Config.search.objectPreview) {
// Result with geometry
if (result.geometry) {
this.addFeatureToPreview(result.geometry);
this.updatePreviewLayerStyle();
}
}
if (result.properties?.actions[0].action === 'add_layer' && this.configManager.Config.search.layerPreview) {
const layer = this.themesHelper.findLayerByName(result.properties?.actions[0].data);
const clonedTheme = this.getMinimalClonedThemeForLayer(layer);
if (!this.isAlreadyPresent(clonedTheme)) {
// Preview layer
clonedTheme.order = 0;
clonedTheme.isExpanded = true;
this.previewTheme = clonedTheme;
this.state.layers.layersList.push(clonedTheme);
this.layerManager.toggleGroupOrTheme(clonedTheme, 'on');
}
}
}
isAlreadyPresent(theme) {
return this.state.layers.layersList.some((t) => t.id === theme.id);
}
getMinimalClonedThemeForLayer(layer) {
const hierarchy = this.getHierarchyFromLayer(layer);
const theme = hierarchy[0];
const clone = theme.clone();
// Remove unnecessary clones
let children = clone.children;
for (let i = 1; i < hierarchy.length; ++i) {
this.removeOthers(children, hierarchy[i]);
children[0].isExpanded = true;
children = children[0].children;
}
return clone;
}
removeOthers(layers, keep) {
for (let i = layers.length - 1; i >= 0; --i) {
if (layers[i].id !== keep.id) {
layers.splice(i, 1);
}
}
}
getHierarchyFromLayer(layer) {
if (layer instanceof ThemeLayer) {
return [layer];
}
if (!layer.parent) {
throw new Error('A group or a layer should always have a parent.');
}
const parents = this.getHierarchyFromLayer(layer.parent);
return [...parents, layer];
}
addFeatureToPreview(geometry) {
switch (geometry.type) {
case 'Point': {
const feature = new Feature(new Point(geometry.coordinates));
this.previewFeaturesCollection.push(feature);
return;
}
case 'MultiPoint': {
const feature = new Feature(new MultiPoint(geometry.coordinates));
this.previewFeaturesCollection.push(feature);
return;
}
case 'MultiLineString': {
const feature = new Feature(new MultiLineString(geometry.coordinates));
this.previewFeaturesCollection.push(feature);
return;
}
case 'LineString': {
const feature = new Feature(new LineString(geometry.coordinates));
this.previewFeaturesCollection.push(feature);
return;
}
case 'Polygon': {
const feature = new Feature(new Polygon(geometry.coordinates));
this.previewFeaturesCollection.push(feature);
return;
}
case 'MultiPolygon': {
const feature = new Feature(new MultiPolygon(geometry.coordinates));
this.previewFeaturesCollection.push(feature);
return;
}
case 'GeometryCollection': {
for (const geom of geometry.geometries) {
this.addFeatureToPreview(geom);
}
return;
}
default:
throw new Error('Geometry type of search result is not being supported.');
}
}
clearPreview() {
// Clear preview search result
this.previewFeaturesCollection.clear();
// Clear preview layer
if (this.previewTheme) {
this.layerManager.toggle(this.previewTheme, 'off');
const index = this.state.layers.layersList.findIndex((l) => l.treeItemId === this.previewTheme?.treeItemId);
if (index >= 0) {
this.state.layers.layersList.splice(index, 1);
}
else {
console.warn('Error while removing preview layer.');
}
this.previewTheme = null;
}
}
onSelect(result) {
this.selectedResult = result;
this.ignoreBlur = false;
this.forceHide = true;
this.previewTheme = null;
super.render();
if (result.bbox) {
// Result with geometry
this.zoomTo(result.bbox);
}
else {
let clonedTheme;
if (result.properties?.actions[0].action === 'add_theme') {
const theme = this.themesHelper.findThemeByName(result.properties?.actions[0].data);
clonedTheme = theme.clone();
}
else if (result.properties?.actions[0].action === 'add_group') {
const group = this.themesHelper.findGroupByName(result.properties?.actions[0].data);
clonedTheme = this.getMinimalClonedThemeForLayer(group);
}
else if (result.properties?.actions[0].action === 'add_layer') {
const layer = this.themesHelper.findLayerByName(result.properties?.actions[0].data);
clonedTheme = this.getMinimalClonedThemeForLayer(layer);
}
else {
console.warn('Unsupported result type');
}
if (clonedTheme && !this.isAlreadyPresent(clonedTheme)) {
clonedTheme.order = 0;
this.state.layers.layersList.push(clonedTheme);
}
}
this.onFocusOut();
// Update searchbox with result
if (this.searchBox && result.properties) {
this.searchBox.value = result.properties.label;
}
}
zoomTo(extent) {
// We create a buffer around the extent from 50% of the width/height
const bufferValue = Math.max((getWidth(extent) * 50) / 100, (getHeight(extent) * 50) / 100);
const bufferedExtent = buffer(extent, bufferValue);
const minResolution = this.configManager.Config.search.minResolution;
const currentResolution = this.map.getView().getResolution();
const currentExtent = this.map.getView().calculateExtent();
if (minResolution) {
if (currentResolution > minResolution) {
// If we are in a bigger resolution as the minimal one,
// Zoom to object with minResolution
MapManager.getInstance().zoomToExtent(bufferedExtent, minResolution);
}
else if (!containsExtent(currentExtent, extent)) {
// Else, if the extent is NOT already within the current extent of the map
// We keep the current resolution, and just pan to object
this.state.position.center = getCenter(extent);
}
}
// Otherwise, if the searched object is already in the current map extent
// We do nothing
}
onMouseMove() {
// if the mouse moves, we activate the hover effect
const results = this.shadowRoot?.querySelectorAll('.result');
if (!results) {
return;
}
for (const result of results) {
result.classList.remove('active');
const htmlResult = result;
htmlResult.style.removeProperty('background-color');
}
}
onKeyDown(e) {
// clear search on escape
if (e.key === 'Escape') {
this.clearSearch(true);
}
// automatic re-open search results on enter
else if (this.forceHide) {
if (e.key === 'Enter') {
this.onFocusIn();
}
}
// navigate through search results
else if (!this.forceHide) {
this.navigateToResult(e);
}
}
navigateToResult(e) {
switch (e.key) {
case 'ArrowDown':
if (this.focusedResultIndex < this.allResults.length - 1) {
this.focusedResultIndex += 1;
this.focusResultFromIndex();
}
break;
case 'ArrowUp':
if (this.focusedResultIndex > 0) {
this.focusedResultIndex -= 1;
this.focusResultFromIndex();
}
break;
case 'Enter':
if (this.focusedResultIndex >= 0) {
this.onSelect(this.allResults[this.focusedResultIndex]);
}
break;
}
}
initColorPicker() {
super.render();
const colorPicker = this.shadowRoot?.getElementById('colorPickerBtn');
if (colorPicker) {
const fillPicker = new GirafeColorPicker({
parent: colorPicker,
color: this.configManager.Config.search.defaultStrokeColor,
popup: 'right'
});
fillPicker.onChange = (color) => {
// The fill color should be the selected color with a bit more transparency
const fillColor = [color.rgba[0], color.rgba[1], color.rgba[2], color.rgba[3] / 2];
this.updatePreviewLayerStyle(fillColor, color.hex);
};
}
}
updatePreviewLayerStyle(fillColor, strokeColor) {
if (!this.previewGeoLayer) {
return;
}
// Only update style if new colors were provided via color picker or default colors have changed
if (this.defaultColorHasChanged()) {
this.defaultSearchFillColor = this.configManager.Config.search.defaultFillColor;
this.defaultSearchStrokeColor = this.configManager.Config.search.defaultStrokeColor;
}
const strokeColorWithFallback = strokeColor ?? this.defaultSearchStrokeColor;
this.previewGeoLayer.setStyle(new Style({
stroke: new Stroke({
color: strokeColorWithFallback,
width: this.configManager.Config.search.defaultStrokeWidth
}),
fill: new Fill({ color: fillColor ?? this.defaultSearchFillColor }),
image: new Icon({
anchor: [0.5, 1],
anchorXUnits: 'fraction',
anchorYUnits: 'fraction',
src: this.getColoredPinIcon(strokeColorWithFallback),
scale: 0.3
})
}));
}
defaultColorHasChanged() {
return (this.defaultSearchFillColor !== this.configManager.Config.search.defaultFillColor ||
this.defaultSearchStrokeColor !== this.configManager.Config.search.defaultStrokeColor);
}
getColoredPinIcon(hexColor) {
const pin = `<svg xmlns="http://www.w3.org/2000/svg"
width="120"
height="120"
style="fill: ${hexColor};"
viewBox="0 0 384 512">
<path d="M215.7 499.2C267 435 384 279.4 384 192C384 86 298 0 192 0S0 86 0 192c0 87.4 117 243 168.3
307.2c12.3 15.3 35.1 15.3 47.4 0zM192 128a64 64 0 1 1 0 128 64 64 0 1 1 0-128z"/>
</svg>`;
return `data:image/svg+xml;utf8,${encodeURIComponent(pin)}`;
}
}
export default SearchComponent;