qwc2
Version:
QGIS Web Client
226 lines (224 loc) • 13.1 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 _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 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 ol from 'openlayers';
import PropTypes from 'prop-types';
import { setEditContext } from '../../actions/editing';
import LocationRecorder from '../../components/LocationRecorder';
import FeatureStyles from "../../utils/FeatureStyles";
/**
* Editing support for the map component.
*/
var EditingSupport = /*#__PURE__*/function (_React$Component) {
function EditingSupport(props) {
var _this;
_classCallCheck(this, EditingSupport);
_this = _callSuper(this, EditingSupport, [props]);
_defineProperty(_this, "state", {
showRecordLocation: false
});
_defineProperty(_this, "editStyle", function () {
var geometryFunction = function geometryFunction(feature) {
if (feature.getGeometry().getType() === "Point") {
return new ol.geom.MultiPoint([feature.getGeometry().getCoordinates()]);
} else if (feature.getGeometry().getType() === "LineString") {
return new ol.geom.MultiPoint(feature.getGeometry().getCoordinates());
} else if (feature.getGeometry().getType() === "Polygon") {
return new ol.geom.MultiPoint(feature.getGeometry().getCoordinates()[0]);
} else if (feature.getGeometry().getType() === "MultiPoint") {
return feature.getGeometry();
} else if (feature.getGeometry().getType() === "MultiLineString") {
return new ol.geom.MultiPoint(feature.getGeometry().getCoordinates()[0]);
} else if (feature.getGeometry().getType() === "MultiPolygon") {
return new ol.geom.MultiPoint(feature.getGeometry().getCoordinates()[0][0]);
}
return feature.getGeometry();
};
return [FeatureStyles.interaction(_this.props.editContext.geometryStyle), FeatureStyles.interactionVertex(_objectSpread({
geometryFunction: geometryFunction
}, _this.props.editContext.vertexStyle))];
});
_defineProperty(_this, "createLayer", function () {
var source = new ol.source.Vector();
_this.layer = new ol.layer.Vector({
source: source,
zIndex: 1000000,
style: _this.editStyle()
});
_this.props.map.addLayer(_this.layer);
});
_defineProperty(_this, "addDrawInteraction", function () {
_this.reset();
_this.createLayer();
var geomType = _this.props.editContext.geomType.replace(/Z$/, '');
var drawInteraction = new ol.interaction.Draw({
stopClick: true,
type: geomType,
source: _this.layer.getSource(),
condition: function condition(event) {
return event.originalEvent.buttons === 1;
},
style: _this.editStyle()
});
drawInteraction.on('drawstart', function (evt) {
_this.currentFeature = evt.feature;
}, _this);
drawInteraction.on('drawend', function () {
_this.setState({
showRecordLocation: false
});
_this.commitCurrentFeature();
_this.props.map.removeInteraction(drawInteraction);
_this.interaction = null;
}, _this);
_this.props.map.addInteraction(drawInteraction);
_this.interaction = drawInteraction;
_this.setState({
showRecordLocation: ["Point", "LineString", "MultiPoint", "MultiLineString"].includes(geomType)
});
});
_defineProperty(_this, "addEditInteraction", function () {
_this.reset();
_this.createLayer();
var format = new ol.format.GeoJSON();
_this.currentFeature = format.readFeature(_this.props.editContext.feature);
_this.layer.getSource().addFeature(_this.currentFeature);
var modifyInteraction = new ol.interaction.Modify({
features: new ol.Collection([_this.currentFeature]),
condition: function condition(event) {
return event.originalEvent.buttons === 1;
},
deleteCondition: function deleteCondition(event) {
// delete vertices on SHIFT + click
if (event.type === "pointerdown" && ol.events.condition.shiftKeyOnly(event)) {
_this.props.map.setIgnoreNextClick(true);
}
return ol.events.condition.shiftKeyOnly(event) && ol.events.condition.singleClick(event);
},
style: FeatureStyles.sketchInteraction()
});
modifyInteraction.on('modifyend', function () {
_this.commitCurrentFeature();
}, _this);
modifyInteraction.setActive(_this.props.editContext.geomType && _this.props.editContext.permissions.updatable && !_this.props.editContext.geomReadOnly && !_this.props.editContext.geomNonZeroZ);
_this.props.map.addInteraction(modifyInteraction);
_this.interaction = modifyInteraction;
});
_defineProperty(_this, "commitCurrentFeature", function () {
if (!_this.currentFeature) {
return;
}
var format = new ol.format.GeoJSON();
var feature = format.writeFeatureObject(_this.currentFeature);
if (_this.props.editContext.feature) {
feature = _objectSpread(_objectSpread({}, _this.props.editContext.feature), {}, {
geometry: feature.geometry
});
}
var _addZCoordinateIfNeeded = function addZCoordinateIfNeeded(entry) {
return Array.isArray(entry[0]) ? entry.map(_addZCoordinateIfNeeded) : [].concat(_toConsumableArray(entry.slice(0, 2)), [0]);
};
if (_this.props.editContext.geomType.endsWith('Z')) {
feature.geometry.coordinates = _addZCoordinateIfNeeded(feature.geometry.coordinates);
}
_this.props.setEditContext(_this.props.editContext.id, {
feature: feature,
changed: true
});
});
_defineProperty(_this, "reset", function () {
if (_this.interaction) {
_this.props.map.removeInteraction(_this.interaction);
}
_this.interaction = null;
_this.currentFeature = null;
if (_this.layer) {
_this.props.map.removeLayer(_this.layer);
}
_this.layer = null;
});
_this.interaction = null;
_this.layer = null;
_this.currentFeature = null;
return _this;
}
_inherits(EditingSupport, _React$Component);
return _createClass(EditingSupport, [{
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
if (this.props.editContext === prevProps.editContext) {
// pass
} else if (this.props.editContext.action === 'Pick' && this.props.editContext.feature) {
// If a feature without geometry was picked, enter draw mode, otherwise enter edit mode
if (!this.props.editContext.feature.geometry && this.props.editContext.geomType) {
this.addDrawInteraction();
} else {
this.addEditInteraction();
}
} else if (this.props.editContext.action === 'Draw' && this.props.editContext.geomType) {
// Usually, draw mode starts without a feature, but draw also can start with a pre-set geometry
if (!(this.props.editContext.feature || {}).geometry || prevProps.editContext.geomType !== this.props.editContext.geomType) {
this.addDrawInteraction();
} else if ((this.props.editContext.feature || {}).geometry) {
this.addEditInteraction();
}
} else {
this.reset();
}
}
}, {
key: "render",
value: function render() {
if (this.state.showRecordLocation && this.props.editContext.geomType) {
var geomType = this.props.editContext.geomType.replace(/Z$/, '');
return /*#__PURE__*/React.createElement(LocationRecorder, {
drawInteraction: this.interaction,
geomType: geomType,
key: "LocationRecorder",
map: this.props.map
});
}
return null;
}
}]);
}(React.Component);
_defineProperty(EditingSupport, "propTypes", {
editContext: PropTypes.object,
map: PropTypes.object,
setEditContext: PropTypes.func
});
export default connect(function (state) {
return {
editContext: state.editing.contexts[state.editing.currentContext] || {}
};
}, {
setEditContext: setEditContext
})(EditingSupport);