UNPKG

qwc2

Version:
276 lines (275 loc) 14.3 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 _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 ReactDOM from 'react-dom'; import { connect } from 'react-redux'; import PropTypes from 'prop-types'; import { setEditContext } from '../actions/editing'; import { LayerRole, addLayerFeatures, removeLayer } from '../actions/layers'; import { getFeatureTemplate } from '../utils/EditingUtils'; import LocaleUtils from '../utils/LocaleUtils'; import MapUtils from '../utils/MapUtils'; import AttributeForm from './AttributeForm'; import MeasureSwitcher from './MeasureSwitcher'; import { BottomToolPortalContext } from './PluginsContainer'; import './style/LinkFeatureForm.css'; var LinkFeatureForm = /*#__PURE__*/function (_React$Component) { function LinkFeatureForm() { var _this; _classCallCheck(this, LinkFeatureForm); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, LinkFeatureForm, [].concat(args)); _defineProperty(_this, "state", { editContext: {}, pickedFeatures: null, highlightedFeature: null }); _defineProperty(_this, "changeMeasurementState", function (diff) { var editContext = _this.props.editing.contexts[_this.props.editContextId]; _this.props.setEditContext(_this.props.editContextId, { measurements: _objectSpread(_objectSpread({}, editContext.measurements), diff) }); }); _defineProperty(_this, "childPickQuery", function (coordinate) { var scale = Math.round(MapUtils.computeForZoom(_this.props.map.scales, _this.props.map.zoom)); _this.props.iface.getFeature(_this.props.editConfig, coordinate, _this.props.map.projection, scale, 96, function (featureCollection) { var features = featureCollection ? featureCollection.features : null; if (features && features.length === 1) { if (!_this.props.pickFilter) { _this.props.finished(features[0]); } else { var newFeature = _this.props.pickFilter(features[0]); if (newFeature) { _this.props.finished(newFeature); } } } else { _this.setState({ pickedFeatures: features }); } }); }); _defineProperty(_this, "finish", function () { var editContext = _this.props.editing.contexts[_this.props.editContextId]; _this.props.finished(editContext.feature); }); _defineProperty(_this, "hoverFeature", function (feature) { var layer = { id: _this.props.editContextId + "-pick-selection", role: LayerRole.SELECTION }; _this.props.addLayerFeatures(layer, [feature], true); _this.setState({ highlightedFeature: feature.id }); }); _defineProperty(_this, "unhoverFeature", function (feature) { if (_this.state.highlightedFeature === feature.id) { _this.props.removeLayer(_this.props.editContextId + "-pick-selection"); _this.setState({ highlightedFeature: null }); } }); _defineProperty(_this, "pickFeatureSelected", function (feature) { _this.unhoverFeature(feature); if (!_this.props.pickFilter) { _this.props.finished(feature); } else { var newFeature = _this.props.pickFilter(feature); if (newFeature) { _this.props.finished(newFeature); } } }); _defineProperty(_this, "onDiscard", function () { var editContext = _this.props.editing.contexts[_this.props.editContextId]; if (editContext.action === "Draw") { // Discarded draw = cancel _this.props.finished(null); } }); return _this; } _inherits(LinkFeatureForm, _React$Component); return _createClass(LinkFeatureForm, [{ key: "componentDidMount", value: function componentDidMount() { var _this2 = this; if (this.props.action === 'Edit') { if (this.props.feature) { this.props.setEditContext(this.props.editContextId, { action: 'Pick', feature: this.props.feature, geomType: this.props.editConfig.geomType, editConfig: this.props.editConfig, mapPrefix: this.props.mapPrefix }); } else { this.props.iface.getFeatureById(this.props.editConfig, this.props.featureId, this.props.map.projection, function (result) { if (result) { _this2.props.setEditContext(_this2.props.editContextId, { action: 'Pick', feature: result, geomType: _this2.props.editConfig.geomType, editConfig: _this2.props.editConfig, mapPrefix: _this2.props.mapPrefix }); } }); } } else if (this.props.action === 'Create') { var featureSkel = _objectSpread({ type: "Feature", properties: {} }, this.props.feature); getFeatureTemplate(this.props.editConfig, featureSkel, this.props.iface, this.props.mapPrefix, this.props.map.projection, function (feature) { _this2.props.setEditContext(_this2.props.editContextId, { action: 'Draw', geomType: _this2.props.editConfig.geomType, feature: feature, editConfig: _this2.props.editConfig, mapPrefix: _this2.props.mapPrefix }); }); } else if (this.props.action === 'Pick') { this.props.setEditContext(this.props.editContextId, { editConfig: this.props.editConfig, mapPrefix: this.props.mapPrefix }); } } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { // Handle drawPick var editContext = this.props.editing.contexts[this.props.editContextId]; if (editContext && editContext.action === null && this.props.map.click && this.props.map.click !== prevProps.map.click) { this.childPickQuery(this.props.map.click.coordinate); } } }, { key: "render", value: function render() { var _this3 = this; var editContext = this.props.editing.contexts[this.props.editContextId]; if (!editContext) { return null; } if (editContext.action === null) { // Picking return /*#__PURE__*/React.createElement("div", { className: "link-feature-form" }, !this.state.pickedFeatures ? /*#__PURE__*/React.createElement("div", { className: "link-feature-form-hint" }, /*#__PURE__*/React.createElement("span", null, LocaleUtils.tr("linkfeatureform.pickhint"))) : /*#__PURE__*/React.createElement("div", { className: "link-feature-form-feature-list" }, this.state.pickedFeatures.map(function (feature) { var _feature$properties$_; return /*#__PURE__*/React.createElement("div", { key: feature.id, onClick: function onClick() { return _this3.pickFeatureSelected(feature); }, onMouseOut: function onMouseOut() { return _this3.unhoverFeature(feature); }, onMouseOver: function onMouseOver() { return _this3.hoverFeature(feature); } }, (_feature$properties$_ = feature.properties[_this3.props.displayField]) !== null && _feature$properties$_ !== void 0 ? _feature$properties$_ : feature.id); })), /*#__PURE__*/React.createElement("div", { className: "link-feature-form-close" }, /*#__PURE__*/React.createElement("button", { className: "button", disabled: editContext.changed, onClick: this.finish }, LocaleUtils.tr("common.cancel")))); } else if (editContext.feature) { var _editContext$feature; var drawing = editContext.action === 'Draw' && !editContext.feature.geometry && this.props.editConfig.geomType; return [/*#__PURE__*/React.createElement("div", { className: "link-feature-form", key: "LinkFeatureForm" }, drawing ? /*#__PURE__*/React.createElement("div", { className: "link-feature-form-hint" }, /*#__PURE__*/React.createElement("span", null, LocaleUtils.tr("linkfeatureform.drawhint"))) : /*#__PURE__*/React.createElement(AttributeForm, { editContext: editContext, hideDelete: this.props.hideDelete, iface: this.props.iface, onDiscard: this.onDiscard, readOnly: this.props.readOnly, translations: this.props.translations }), /*#__PURE__*/React.createElement("div", { className: "link-feature-form-close" }, /*#__PURE__*/React.createElement("button", { className: "button", disabled: editContext.changed, onClick: this.finish }, drawing ? LocaleUtils.tr("common.cancel") : LocaleUtils.tr("common.close")))), drawing || (_editContext$feature = editContext.feature) !== null && _editContext$feature !== void 0 && _editContext$feature.geometry ? /*#__PURE__*/ReactDOM.createPortal(/*#__PURE__*/React.createElement(MeasureSwitcher, { changeMeasureState: this.changeMeasurementState, geomType: editContext.geomType, iconSize: "large", measureState: editContext.measurements }), this.context) : null]; } else { return null; } } }]); }(React.Component); _defineProperty(LinkFeatureForm, "contextType", BottomToolPortalContext); _defineProperty(LinkFeatureForm, "propTypes", { action: PropTypes.string, addLayerFeatures: PropTypes.func, displayField: PropTypes.string, editConfig: PropTypes.object, editContextId: PropTypes.string, editing: PropTypes.object, feature: PropTypes.object, featureId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), finished: PropTypes.func, hideDelete: PropTypes.bool, iface: PropTypes.object, map: PropTypes.object, mapPrefix: PropTypes.string, pickFilter: PropTypes.func, readOnly: PropTypes.bool, removeLayer: PropTypes.func, setEditContext: PropTypes.func, translations: PropTypes.object }); export default connect(function (state) { return { editing: state.editing, map: state.map }; }, { addLayerFeatures: addLayerFeatures, removeLayer: removeLayer, setEditContext: setEditContext })(LinkFeatureForm);