qwc2
Version:
QGIS Web Client
372 lines (370 loc) • 20.8 kB
JavaScript
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 { 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 CoordinatesUtils from '../utils/CoordinatesUtils';
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.editConfigs).forEach(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
mapName = _ref2[0],
editConfigs = _ref2[1];
Object.entries(editConfigs).forEach(function (_ref3) {
var _this$props$filter$fi;
var _ref4 = _slicedToArray(_ref3, 2),
layerName = _ref4[0],
editConfig = _ref4[1];
if (!editConfig.geomType) {
// Skip geometryless datasets
return;
}
var match = LayerUtils.searchLayer(_this.props.layers, 'wms_name', mapName, 'name', layerName);
var mapScale = MapUtils.computeForZoom(_this.props.map.scales, _this.props.map.zoom);
if (!match || !LayerUtils.sublayerVisible(match.layer, match.path) || !LayerUtils.layerScaleInRange(match.sublayer, mapScale)) {
return;
}
var layerOrder = match.layer.params.LAYERS.split(",");
++pendingRequests;
var scale = Math.round(MapUtils.computeForZoom(_this.props.map.scales, _this.props.map.zoom));
_this.props.iface.getFeature(editConfig, pos, _this.props.map.projection, scale, 96, function (featureCollection) {
_this.setState(function (state) {
var pickedFeatures = state.pickedFeatures;
if (!isEmpty(featureCollection === null || featureCollection === void 0 ? void 0 : featureCollection.features)) {
pickedFeatures = Object.fromEntries(Object.entries(_objectSpread(_objectSpread({}, state.pickedFeatures), featureCollection.features.reduce(function (res, feature) {
return _objectSpread(_objectSpread({}, res), {}, _defineProperty({}, mapName + "#" + layerName + "#" + feature.id, feature));
}, {}))).sort(function (a, b) {
var partsA = a[0].split("#");
var partsB = b[0].split("#");
var diff = layerOrder.indexOf(partsB[1]) - layerOrder.indexOf(partsA[1]);
return diff === 0 ? partsA[1].localeCompare(partsB[1]) : diff;
}));
}
return {
pickedFeatures: pickedFeatures,
pendingRequests: state.pendingRequests - 1
};
});
}, (_this$props$filter$fi = _this.props.filter.filterParams) === null || _this$props$filter$fi === void 0 ? void 0 : _this$props$filter$fi[match.sublayer.name], _this.props.filter.filterGeom);
});
});
_this.setState({
pendingRequests: pendingRequests,
pickedFeatures: {},
selectedFeature: ""
});
});
_defineProperty(_this, "setSelectedFeature", function (selectedFeature) {
if (selectedFeature) {
var _selectedFeature$spli = selectedFeature.split("#", 2),
_selectedFeature$spli2 = _slicedToArray(_selectedFeature$spli, 2),
mapName = _selectedFeature$spli2[0],
layerName = _selectedFeature$spli2[1];
var editConfig = _this.props.editConfigs[mapName][layerName];
_this.props.setEditContext('FeatureForm', {
action: 'Pick',
feature: _this.state.pickedFeatures[selectedFeature],
changed: false,
mapPrefix: mapName,
editConfig: editConfig
});
} else {
_this.props.clearEditContext('FeatureForm');
}
_this.setState({
selectedFeature: selectedFeature
});
});
_defineProperty(_this, "onWindowClose", function () {
if (!_this.props.editContext.changed) {
_this.clearResults();
if (_this.props.exitTaskOnResultsClose) {
_this.props.setCurrentTask(null);
}
}
});
_defineProperty(_this, "clearResults", function () {
_this.props.clearEditContext('FeatureForm');
_this.setState(FeatureForm.defaultState);
});
_defineProperty(_this, "updatePickedFeatures", function (newfeature) {
_this.setState(function (state) {
return {
pickedFeatures: Object.entries(state.pickedFeatures).reduce(function (res, _ref5) {
var _ref6 = _slicedToArray(_ref5, 2),
key = _ref6[0],
feature = _ref6[1];
res[key] = feature.id === newfeature.id ? newfeature : feature;
return res;
}, {})
};
});
});
_this.state = FeatureForm.defaultState;
return _this;
}
_inherits(FeatureForm, _React$Component);
return _createClass(FeatureForm, [{
key: "componentDidMount",
value: function componentDidMount() {
if (this.props.enabled) {
this.props.setEditContext('FeatureForm', {
action: 'Pick'
});
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
if (this.props.enabled && this.props.theme && !prevProps.theme) {
var startupParams = this.props.startupParams;
var haveIc = ["1", "true"].includes((startupParams.ic || "").toLowerCase());
var c = (startupParams.c || "").split(/[;,]/g).map(function (x) {
return parseFloat(x) || 0;
});
if (haveIc && c.length === 2) {
var mapCrs = this.props.theme.mapCrs;
this.queryFeatures(CoordinatesUtils.reproject(c, startupParams.crs || mapCrs, mapCrs));
}
} else if (this.props.theme !== prevProps.theme || !this.props.enabled && prevProps.enabled) {
this.clearResults();
}
var isAllowedContext = [null, "FeatureForm"].includes(this.props.currentEditContext);
if (this.props.enabled && this.state.pendingRequests === 0 && isAllowedContext && !this.props.editContext.changed) {
var clickPoint = this.queryPoint(prevProps);
if (clickPoint) {
this.queryFeatures(clickPoint);
}
}
if (this.state.pendingRequests === 0 && prevState.pendingRequests > 0) {
// Select first result
this.setSelectedFeature(Object.keys(this.state.pickedFeatures)[0]);
}
}
}, {
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"
}, /*#__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"
}, /*#__PURE__*/React.createElement("span", {
className: "identify-body-message"
}, LocaleUtils.tr("featureform.noresults")));
} else {
var featureText = LocaleUtils.tr("featureform.feature");
var attributeForm = null;
if (this.props.editContext.feature) {
var _this$props$layers$fi;
var translations = (_this$props$layers$fi = this.props.layers.find(function (layer) {
return layer.wms_name === _this2.props.editContext.mapPrefix;
})) === null || _this$props$layers$fi === void 0 ? void 0 : _this$props$layers$fi.translations;
attributeForm = /*#__PURE__*/React.createElement(AttributeForm, {
editContext: this.props.editContext,
iface: this.props.iface,
onCommit: this.updatePickedFeatures,
translations: translations
});
}
body = /*#__PURE__*/React.createElement("div", {
className: "feature-query-body"
}, Object.keys(this.state.pickedFeatures).length > 1 ? /*#__PURE__*/React.createElement("div", {
className: "feature-query-selection"
}, /*#__PURE__*/React.createElement("select", {
onChange: function onChange(ev) {
return _this2.setSelectedFeature(ev.target.value);
},
value: this.state.selectedFeature
}, Object.entries(this.state.pickedFeatures).map(function (_ref7) {
var _ref9, _ref10, _match$layer$translat, _match$layer$translat2, _match$sublayer;
var _ref8 = _slicedToArray(_ref7, 2),
id = _ref8[0],
feature = _ref8[1];
var _id$split = id.split("#"),
_id$split2 = _slicedToArray(_id$split, 3),
mapName = _id$split2[0],
layerName = _id$split2[1],
featureId = _id$split2[2];
var editConfig = _this2.props.editConfigs[mapName][layerName];
var match = LayerUtils.searchLayer(_this2.props.layers, 'wms_name', mapName, 'name', layerName);
var layerTitle = (_ref9 = (_ref10 = (_match$layer$translat = (_match$layer$translat2 = match.layer.translations) === null || _match$layer$translat2 === void 0 || (_match$layer$translat2 = _match$layer$translat2.layertree) === null || _match$layer$translat2 === void 0 ? void 0 : _match$layer$translat2[layerName]) !== null && _match$layer$translat !== void 0 ? _match$layer$translat : editConfig.layerTitle) !== null && _ref10 !== void 0 ? _ref10 : match === null || match === void 0 || (_match$sublayer = match.sublayer) === null || _match$sublayer === void 0 ? void 0 : _match$sublayer.title) !== null && _ref9 !== void 0 ? _ref9 : layerName;
var featureName = editConfig.displayField ? feature.properties[editConfig.displayField] : featureText + " " + featureId;
return /*#__PURE__*/React.createElement("option", {
key: id,
value: id
}, layerTitle + ": " + featureName);
}))) : null, attributeForm);
}
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,
editConfigs: PropTypes.object,
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,
startupParams: PropTypes.object,
theme: PropTypes.object
});
_defineProperty(FeatureForm, "defaultProps", {
clearResultsOnClose: true,
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,
editConfigs: state.layers.editConfigs,
theme: state.theme.current,
startupParams: state.localConfig.startupParams
};
}, {
setCurrentTask: setCurrentTask,
clearEditContext: clearEditContext,
setEditContext: setEditContext
})(FeatureForm);
});