UNPKG

qwc2-lts

Version:
335 lines (333 loc) 18.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 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 _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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 _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } 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 2017-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 { connect } from 'react-redux'; import isEmpty from 'lodash.isempty'; import PropTypes from 'prop-types'; import { setEditContext, clearEditContext } from '../actions/editing'; import { LayerRole } from '../actions/layers'; import { setCurrentTask } from '../actions/task'; import AttributeForm from '../components/AttributeForm'; import ResizeableWindow from '../components/ResizeableWindow'; import TaskBar from '../components/TaskBar'; import ConfigUtils from '../utils/ConfigUtils'; import EditingInterface from '../utils/EditingInterface'; import LayerUtils from '../utils/LayerUtils'; import LocaleUtils from '../utils/LocaleUtils'; import MapUtils from '../utils/MapUtils'; import './style/FeatureForm.css'; /** * Displays queried feature attributes in a form. * * The attribute form is generated from the QGIS attribute form configuration. * * This plugin queries the feature via the editing service specified by * `editServiceUrl` in `config.json` (by default the `qwc-data-service`), rather than over WMS * GetFeatureInfo like the `Identify` plugin. * * You need to create and permit `Data` resources for datasets which should be queryable via `FeatureForm`. * If the dataset it editable, the feature attributes can be edited directly in the displayed form. * * Can be used as default identify tool by setting `"identifyTool": "FeatureForm"` in `config.json`. */ var FeatureForm = /*#__PURE__*/function (_React$Component) { function FeatureForm(props) { var _this; _classCallCheck(this, FeatureForm); _this = _callSuper(this, FeatureForm, [props]); _defineProperty(_this, "queryPoint", function (prevProps) { if (_this.props.click.button !== 0 || _this.props.click === prevProps.click || (_this.props.click.features || []).find(function (feature) { return feature.id === 'startupposmarker'; })) { return null; } var searchMarker = (_this.props.click.features || []).find(function (feature) { return feature.id === 'searchmarker'; }); if (searchMarker && searchMarker.geometry.type === "Point") { return searchMarker.geometry.coordinates; } return _this.props.click.coordinate; }); _defineProperty(_this, "queryFeatures", function (pos) { var pendingRequests = 0; Object.entries(_this.props.theme.editConfig || {}).forEach(function (_ref) { var _this$props$filter$fi; var _ref2 = _slicedToArray(_ref, 2), layerId = _ref2[0], editConfig = _ref2[1]; if (!editConfig.geomType) { // Skip geometryless datasets return; } var path = []; var sublayer = null; var mapScale = MapUtils.computeForZoom(_this.props.map.scales, _this.props.map.zoom); var layer = _this.props.layers.find(function (l) { return l.role === LayerRole.THEME && (sublayer = LayerUtils.searchSubLayer(l, 'name', layerId, path)); }); if (!layer || !sublayer || !LayerUtils.sublayerVisible(layer, path) || !LayerUtils.layerScaleInRange(sublayer, mapScale)) { return; } var layerOrder = layer.params.LAYERS.split(","); ++pendingRequests; var editDataset = editConfig.editDataset || layerId; var scale = Math.round(MapUtils.computeForZoom(_this.props.map.scales, _this.props.map.zoom)); _this.props.iface.getFeature(editDataset, pos, _this.props.map.projection, scale, 96, function (featureCollection) { if (featureCollection && !isEmpty(featureCollection.features)) { _this.setState(function (state) { var newPickedFeatures = Object.fromEntries(Object.entries(_objectSpread(_objectSpread({}, state.pickedFeatures), featureCollection.features.reduce(function (res, feature) { return _objectSpread(_objectSpread({}, res), {}, _defineProperty({}, layerId + "::" + feature.id, feature)); }, {}))).sort(function (a, b) { var partsA = a[0].split("::"); var partsB = b[0].split("::"); var diff = layerOrder.indexOf(partsB[0]) - layerOrder.indexOf(partsA[0]); return diff === 0 ? partsA[1].localeCompare(partsB[1]) : diff; })); var selectedFeature = state.pendingRequests <= 1 && !state.selectedFeature ? Object.keys(newPickedFeatures)[0] : ""; return { pickedFeatures: newPickedFeatures, pendingRequests: state.pendingRequests - 1, selectedFeature: selectedFeature }; }); } else { _this.setState(function (state) { var _Object$keys$; var selectedFeature = state.pendingRequests <= 1 && !state.selectedFeature ? (_Object$keys$ = Object.keys(state.pickedFeatures)[0]) !== null && _Object$keys$ !== void 0 ? _Object$keys$ : "" : ""; return { pendingRequests: state.pendingRequests - 1, selectedFeature: selectedFeature }; }); } }, (_this$props$filter$fi = _this.props.filter.filterParams) === null || _this$props$filter$fi === void 0 ? void 0 : _this$props$filter$fi[sublayer.name], _this.props.filter.filterGeom); }); _this.setState({ pendingRequests: pendingRequests, pickedFeatures: {}, selectedFeature: "" }); }); _defineProperty(_this, "setSelectedFeature", function (ev) { _this.setState({ selectedFeature: ev.target.value }); }); _defineProperty(_this, "onWindowClose", function () { _this.clearResults(); if (_this.props.exitTaskOnResultsClose) { _this.props.setCurrentTask(null); } }); _defineProperty(_this, "clearResults", function () { if (!_this.props.editContext.changed) { _this.setState(FeatureForm.defaultState); } }); _this.state = FeatureForm.defaultState; return _this; } _inherits(FeatureForm, _React$Component); return _createClass(FeatureForm, [{ key: "componentDidUpdate", value: function componentDidUpdate(prevProps, prevState) { if (this.props.enabled && !prevProps.enabled) { this.props.setEditContext('FeatureForm', { action: 'Pick' }); } var isCurrentContext = this.props.editContext.id === this.props.currentEditContext; if (this.props.enabled && isCurrentContext && !this.props.editContext.changed && this.state.pendingRequests === 0) { var clickPoint = this.queryPoint(prevProps); if (clickPoint) { this.queryFeatures(clickPoint); } } if (this.props.enabled && this.state.selectedFeature !== prevState.selectedFeature) { var feature = this.state.pickedFeatures ? this.state.pickedFeatures[this.state.selectedFeature] : null; var curLayerId = this.state.selectedFeature.split("::")[0]; var curConfig = this.props.theme.editConfig[curLayerId] || {}; var editPermissions = curConfig.permissions || {}; this.props.setEditContext('FeatureForm', { action: 'Pick', feature: feature, changed: false, geomType: curConfig.geomType || null, geomReadOnly: editPermissions.updatable === false }); } if (!this.props.enabled && prevProps.enabled) { this.props.clearEditContext('FeatureForm'); this.setState(FeatureForm.defaultState); } } }, { key: "render", value: function render() { var _this2 = this; var resultWindow = null; if (this.state.pickedFeatures !== null) { var body = null; if (this.state.pendingRequests > 0) { body = /*#__PURE__*/React.createElement("div", { className: "feature-query-body", role: "body" }, /*#__PURE__*/React.createElement("span", { className: "identify-body-message" }, LocaleUtils.tr("featureform.querying"))); } else if (isEmpty(this.state.pickedFeatures)) { body = /*#__PURE__*/React.createElement("div", { className: "feature-query-body", role: "body" }, /*#__PURE__*/React.createElement("span", { className: "identify-body-message" }, LocaleUtils.tr("featureform.noresults"))); } else { var featureText = LocaleUtils.tr("featureform.feature"); var curLayerId = this.state.selectedFeature.split("::")[0]; var curConfig = this.props.theme.editConfig[curLayerId]; body = /*#__PURE__*/React.createElement("div", { className: "feature-query-body", role: "body" }, Object.keys(this.state.pickedFeatures).length > 1 ? /*#__PURE__*/React.createElement("div", { className: "feature-query-selection" }, /*#__PURE__*/React.createElement("select", { onChange: this.setSelectedFeature, value: this.state.selectedFeature }, Object.entries(this.state.pickedFeatures).map(function (_ref3) { var _ref4 = _slicedToArray(_ref3, 2), id = _ref4[0], feature = _ref4[1]; var _id$split = id.split("::"), _id$split2 = _slicedToArray(_id$split, 2), layerId = _id$split2[0], featureId = _id$split2[1]; var editConfig = _this2.props.theme.editConfig[layerId]; var match = LayerUtils.searchLayer(_this2.props.layers, 'name', editConfig.layerName, [LayerRole.THEME]); var layerName = match ? match.sublayer.title : editConfig.layerName; var featureName = editConfig.displayField ? feature.properties[editConfig.displayField] : featureText + " " + featureId; return /*#__PURE__*/React.createElement("option", { key: id, value: id }, layerName + ": " + featureName); }))) : null, this.props.editContext.feature ? /*#__PURE__*/React.createElement(AttributeForm, { editConfig: curConfig, editContext: this.props.editContext, iface: this.props.iface }) : null); } resultWindow = /*#__PURE__*/React.createElement(ResizeableWindow, { dockable: this.props.geometry.side, icon: "featureform", initialHeight: this.props.geometry.initialHeight, initialWidth: this.props.geometry.initialWidth, initialX: this.props.geometry.initialX, initialY: this.props.geometry.initialY, initiallyDocked: this.props.geometry.initiallyDocked, key: "FeatureForm", onClose: this.onWindowClose, title: LocaleUtils.tr("featureform.title") }, body); } return [resultWindow, /*#__PURE__*/React.createElement(TaskBar, { key: "FeatureFormTaskBar", task: "FeatureForm" }, function () { return { body: LocaleUtils.tr("infotool.clickhelpPoint") }; })]; } }]); }(React.Component); _defineProperty(FeatureForm, "propTypes", { clearEditContext: PropTypes.func, click: PropTypes.object, currentEditContext: PropTypes.string, editContext: PropTypes.object, enabled: PropTypes.bool, /** Whether to clear the task when the results window is closed. */ exitTaskOnResultsClose: PropTypes.bool, filter: PropTypes.object, /** Default window geometry with size, position and docking status. Positive position values (including '0') are related to top (InitialY) and left (InitialX), negative values (including '-0') to bottom (InitialY) and right (InitialX). */ geometry: PropTypes.shape({ initialWidth: PropTypes.number, initialHeight: PropTypes.number, initialX: PropTypes.number, initialY: PropTypes.number, initiallyDocked: PropTypes.bool, side: PropTypes.string }), iface: PropTypes.object, layers: PropTypes.array, map: PropTypes.object, setCurrentTask: PropTypes.func, setEditContext: PropTypes.func, theme: PropTypes.object }); _defineProperty(FeatureForm, "defaultProps", { geometry: { initialWidth: 320, initialHeight: 480, initialX: 0, initialY: 0, initiallyDocked: false, side: 'left' } }); _defineProperty(FeatureForm, "defaultState", { pendingRequests: 0, pickedFeatures: null, selectedFeature: "" }); export default (function () { var iface = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : EditingInterface; return connect(function (state) { var enabled = state.task.id === "FeatureForm" || state.task.identifyEnabled && ConfigUtils.getConfigProp("identifyTool", state.theme.current, "Identify") === "FeatureForm"; return { click: state.map.click || { modifiers: {} }, enabled: enabled, editContext: state.editing.contexts.FeatureForm || {}, currentEditContext: state.editing.currentContext, iface: iface, layers: state.layers.flat, filter: state.layers.filter, map: state.map, theme: state.theme.current }; }, { setCurrentTask: setCurrentTask, clearEditContext: clearEditContext, setEditContext: setEditContext })(FeatureForm); });