qwc2
Version:
QGIS Web Client
173 lines (171 loc) • 9.06 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 _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 2016-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 PropTypes from 'prop-types';
import { changeZoomLevel, zoomToExtent, zoomToPoint } from '../actions/map';
import { setCurrentTask } from '../actions/task';
import MapButton from '../components/MapButton';
import MapSelection from '../components/MapSelection';
import LocaleUtils from '../utils/LocaleUtils';
import ThemeUtils from '../utils/ThemeUtils';
/**
* Map button for zooming the map.
*
* Two specific plugins exist: ZoomInPlugin and ZoomOutPlugin, which are instances of ZoomButton for the respective zoom directions.
*/
var ZoomButton = /*#__PURE__*/function (_React$Component) {
function ZoomButton(props) {
var _this;
_classCallCheck(this, ZoomButton);
_this = _callSuper(this, ZoomButton, [props]);
_defineProperty(_this, "state", {
disabled: false
});
_defineProperty(_this, "buttonClicked", function () {
if (_this.props.enableZoomByBoxSelection) {
_this.props.setCurrentTask(_this.props.currentTask === _this.task ? null : _this.task);
} else if (!_this.state.disabled) {
_this.props.changeZoomLevel(_this.props.currentZoom + _this.props.direction);
}
});
_defineProperty(_this, "updateZoom", function (geom) {
var zoomBox = geom.coordinates[0];
if (_this.props.direction > 0) {
_this.props.zoomToExtent(zoomBox, _this.props.mapCrs);
} else {
var center = [0.5 * (zoomBox[0] + zoomBox[2]), 0.5 * (zoomBox[1] + zoomBox[3])];
var zoom = Math.max(0, _this.props.currentZoom + _this.props.direction);
_this.props.zoomToPoint(center, zoom, _this.props.mapCrs);
}
});
_this.task = props.direction > 0 ? "ZoomIn" : "ZoomOut";
return _this;
}
_inherits(ZoomButton, _React$Component);
return _createClass(ZoomButton, [{
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
if (prevProps.currentZoom !== this.props.currentZoom || prevProps.maxZoom !== this.props.maxZoom) {
if (this.props.direction > 0) {
this.setState({
disabled: this.props.currentZoom >= this.props.maxZoom
});
} else if (this.props.direction < 0) {
this.setState({
disabled: this.props.currentZoom <= 0
});
}
}
if (this.props.currentTask === this.task && this.props.click !== prevProps.click) {
var _this$props$click;
var point = (_this$props$click = this.props.click) === null || _this$props$click === void 0 ? void 0 : _this$props$click.coordinate;
if (point) {
var zoom = Math.max(0, this.props.currentZoom + this.props.direction);
this.props.zoomToPoint(point, zoom, this.mapCrs);
}
}
}
}, {
key: "render",
value: function render() {
var _this2 = this;
if (!ThemeUtils.themeFlagsAllowed(this.props.theme, this.props.themeFlagWhitelist, this.props.themeFlagBlacklist)) {
return null;
}
var defaultPosition = this.props.direction > 0 ? 4 : 3;
var position = this.props.position >= 0 ? this.props.position : defaultPosition;
var tooltip = this.props.direction > 0 ? LocaleUtils.tr("tooltip.zoomin") : LocaleUtils.tr("tooltip.zoomout");
var active = this.props.enableZoomByBoxSelection && this.props.currentTask === this.task;
return [/*#__PURE__*/React.createElement(MapButton, {
active: active,
disabled: this.state.disabled,
icon: this.props.direction > 0 ? "plus" : "minus",
key: this.task + "Button",
onClick: this.buttonClicked,
position: position,
tooltip: tooltip
}), this.props.currentTask === this.task ? /*#__PURE__*/React.createElement(MapSelection, {
active: true,
cursor: this.props.direction > 0 ? "zoom-in" : "zoom-out",
geomType: "DragBox",
geometryChanged: function geometryChanged(geom) {
return _this2.updateZoom(geom);
},
key: "MapSelection"
}) : null];
}
}]);
}(React.Component);
_defineProperty(ZoomButton, "propTypes", {
changeZoomLevel: PropTypes.func,
click: PropTypes.object,
currentTask: PropTypes.string,
currentZoom: PropTypes.number,
direction: PropTypes.number,
/** Enable zoom in or out by box selection. */
enableZoomByBoxSelection: PropTypes.bool,
mapCrs: PropTypes.string,
maxZoom: PropTypes.number,
/** The position slot index of the map button, from the bottom (0: bottom slot). */
position: PropTypes.number,
setCurrentTask: PropTypes.func,
theme: PropTypes.object,
/** Omit the button in themes matching one of these flags. */
themeFlagBlacklist: PropTypes.arrayOf(PropTypes.string),
/** Only show the button in themes matching one of these flags. */
themeFlagWhitelist: PropTypes.arrayOf(PropTypes.string),
zoomToExtent: PropTypes.func,
zoomToPoint: PropTypes.func
});
export var ZoomInPlugin = connect(function (state) {
return {
click: state.map.click,
currentTask: state.task.id,
currentZoom: state.map.zoom,
maxZoom: state.map.resolutions.length - 1,
direction: +1,
mapCrs: state.map.projection,
theme: state.theme.current
};
}, {
changeZoomLevel: changeZoomLevel,
setCurrentTask: setCurrentTask,
zoomToExtent: zoomToExtent,
zoomToPoint: zoomToPoint
})(ZoomButton);
export var ZoomOutPlugin = connect(function (state) {
return {
click: state.map.click || {},
currentTask: state.task.id,
currentZoom: state.map.zoom,
maxZoom: state.map.resolutions.length - 1,
direction: -1,
mapCrs: state.map.projection,
theme: state.theme.current
};
}, {
changeZoomLevel: changeZoomLevel,
setCurrentTask: setCurrentTask,
zoomToExtent: zoomToExtent,
zoomToPoint: zoomToPoint
})(ZoomButton);