UNPKG

qwc2

Version:
260 lines (259 loc) 13.7 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /** * Copyright 2024 Sourcepole AG * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. */ import React from 'react'; import axios from 'axios'; import isEmpty from 'lodash.isempty'; import PropTypes from 'prop-types'; import { v4 as uuidv4 } from 'uuid'; import LocaleUtils from '../../utils/LocaleUtils'; import MiscUtils from '../../utils/MiscUtils'; import { SearchResultType } from '../../utils/SearchProviders'; import VectorLayerUtils from '../../utils/VectorLayerUtils'; import Icon from '../Icon'; import InputContainer from './InputContainer'; import Spinner from './Spinner'; import './style/SearchWidget.css'; var SearchWidget = /*#__PURE__*/function (_React$Component) { function SearchWidget(props) { var _this; _classCallCheck(this, SearchWidget); _this = _callSuper(this, SearchWidget, [props]); _defineProperty(_this, "state", { text: '', reqId: null, results: [], pending: 0, active: false }); _defineProperty(_this, "renderResults", function () { return /*#__PURE__*/React.createElement("div", { className: "search-widget-results", onMouseDown: _this.setPreventBlur }, _this.state.results.filter(function (group) { var _group$type; return _this.props.resultTypeFilter.includes((_group$type = group.type) !== null && _group$type !== void 0 ? _group$type : SearchResultType.PLACE); }).map(function (group) { var _group$title; return /*#__PURE__*/React.createElement("div", { className: "search-widget-results-group", key: group.id, onMouseDown: MiscUtils.killEvent }, /*#__PURE__*/React.createElement("div", { className: "search-widget-results-group-title" }, /*#__PURE__*/React.createElement("span", null, (_group$title = group.title) !== null && _group$title !== void 0 ? _group$title : LocaleUtils.tr(group.titlemsgid))), group.items.map(function (item) { item.text = (item.label !== undefined ? item.label : item.text || '').replace(/<\/?\w+\s*\/?>/g, ''); return /*#__PURE__*/React.createElement("div", { className: "search-widget-results-group-item", key: item.id, onClick: function onClick() { return _this.resultSelected(group, item); }, title: item.text }, item.text); })); })); }); _defineProperty(_this, "setPreventBlur", function () { _this.preventBlur = true; setTimeout(function () { _this.preventBlur = false; return false; }, 100); }); _defineProperty(_this, "textChanged", function (ev) { _this.setState({ text: ev.target.value, reqId: null, results: [], pending: 0 }); clearTimeout(_this.searchTimeout); if (!ev.target.value) { _this.props.resultSelected(null); } else { _this.searchTimeout = setTimeout(_this.startSearch, 250); } }); _defineProperty(_this, "onBlur", function () { if (!_this.preventBlur) { clearTimeout(_this.searchTimeout); _this.props.onBlur(); _this.setState({ active: false }); } }); _defineProperty(_this, "onFocus", function (ev) { ev.target.select(); _this.props.onFocus(); _this.setState({ active: true }); }); _defineProperty(_this, "onKeyDown", function (ev) { if (ev.key === 'Enter') { _this.startSearch(); } else if (ev.key === 'Escape') { ev.target.blur(); } }); _defineProperty(_this, "startSearch", function () { clearTimeout(_this.searchTimeout); var reqId = uuidv4(); _this.setState({ reqId: reqId, results: [], pending: _this.props.searchProviders.length }); _this.props.searchProviders.forEach(function (provider) { var searchParams = _objectSpread(_objectSpread({ lang: LocaleUtils.lang(), cfgParams: provider.cfgParams }, provider.params), _this.props.searchParams); provider.onSearch(_this.state.text, searchParams, function (response) { _this.setState(function (state) { if (state.reqId !== reqId) { return {}; } return { results: [].concat(_toConsumableArray(state.results), _toConsumableArray(response.results.map(function (group) { return _objectSpread(_objectSpread({}, group), {}, { provider: provider }); }))), pending: state.pending - 1 }; }); }, axios); }); }); _defineProperty(_this, "resultSelected", function (group, item) { if (!item.geometry && group.provider.getResultGeometry) { group.provider.getResultGeometry(item, function (response) { var _item$x, _response$center, _item$y, _response$center2, _item$crs; _this.props.resultSelected(_objectSpread(_objectSpread({}, item), {}, { feature: response ? VectorLayerUtils.reprojectFeature(response.feature, response.crs, item.crs) : null, x: (_item$x = item.x) !== null && _item$x !== void 0 ? _item$x : response === null || response === void 0 || (_response$center = response.center) === null || _response$center === void 0 ? void 0 : _response$center[0], y: (_item$y = item.y) !== null && _item$y !== void 0 ? _item$y : response === null || response === void 0 || (_response$center2 = response.center) === null || _response$center2 === void 0 ? void 0 : _response$center2[1], crs: (_item$crs = item.crs) !== null && _item$crs !== void 0 ? _item$crs : response === null || response === void 0 ? void 0 : response.crs })); }); } else { _this.props.resultSelected(_objectSpread(_objectSpread({}, item), {}, { feature: item.geometry ? { type: "Feature", geometry: item.geometry } : null })); } if (_this.input) { _this.input.blur(); } }); _defineProperty(_this, "clear", function () { _this.setState({ results: [], text: "" }); _this.props.resultSelected(null); }); _this.searchTimeout = null; _this.preventBlur = false; _this.state.text = props.value; _this.input = null; return _this; } _inherits(SearchWidget, _React$Component); return _createClass(SearchWidget, [{ key: "componentDidMount", value: function componentDidMount() { this.componentDidUpdate({}); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { if (this.props.value !== prevProps.value) { this.setState({ text: this.props.value }); } } }, { key: "render", value: function render() { var _this$props$placehold, _this2 = this; return /*#__PURE__*/React.createElement("div", { className: "search-widget-container" }, /*#__PURE__*/React.createElement(InputContainer, null, /*#__PURE__*/React.createElement("input", { onBlur: this.onBlur, onChange: this.textChanged, onFocus: this.onFocus, onKeyDown: this.onKeyDown, placeholder: (_this$props$placehold = this.props.placeholder) !== null && _this$props$placehold !== void 0 ? _this$props$placehold : LocaleUtils.tr("search.search"), ref: function ref(el) { _this2.input = el; }, role: "input", type: "text", value: this.state.text }), this.state.pending > 0 ? /*#__PURE__*/React.createElement(Spinner, { role: "suffix" }) : /*#__PURE__*/React.createElement(Icon, { icon: "clear", onClick: this.clear, role: "suffix" })), (!isEmpty(this.state.results) || this.state.pending > 0) && this.state.active ? this.renderResults() : null); } }]); }(React.Component); _defineProperty(SearchWidget, "propTypes", { className: PropTypes.string, onBlur: PropTypes.func, onFocus: PropTypes.func, placeholder: PropTypes.string, queryGeometries: PropTypes.bool, resultSelected: PropTypes.func.isRequired, resultTypeFilter: PropTypes.array, searchParams: PropTypes.shape({ mapcrs: PropTypes.string.isRequired, displaycrs: PropTypes.string.isRequired }), searchProviders: PropTypes.array, value: PropTypes.string }); _defineProperty(SearchWidget, "defaultProps", { onBlur: function onBlur() {}, onFocus: function onFocus() {}, resultTypeFilter: [SearchResultType.PLACE], searchParams: {}, searchProviders: [] }); export { SearchWidget as default };