UNPKG

qwc2

Version:
264 lines 14.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 _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 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 2015 GeoSolutions Sas * 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 ol from 'openlayers'; import PropTypes from 'prop-types'; import { refreshLayer, setLayerLoading } from '../../actions/layers'; import { zoomToExtent } from '../../actions/map'; import LayerUtils from '../../utils/LayerUtils'; import MapUtils from '../../utils/MapUtils'; import Signal from '../../utils/Signal'; import LayerRegistry from './layers/index'; export var OlLayerAdded = new Signal(); export var OlLayerUpdated = new Signal(); var OlLayer = /*#__PURE__*/function (_React$Component) { function OlLayer(props) { var _this; _classCallCheck(this, OlLayer); _this = _callSuper(this, OlLayer, [props]); _defineProperty(_this, "makeOptions", function (options) { var _options$opacity, _options$visibility; var projection = options.srs || options.crs || options.projection || _this.props.projection; return _objectSpread(_objectSpread({}, options), {}, { projection: projection, opacity: (_options$opacity = options.opacity) !== null && _options$opacity !== void 0 ? _options$opacity : 255, visibility: (_options$visibility = options.visibility) !== null && _options$visibility !== void 0 ? _options$visibility : true, minResolution: typeof options.minScale === 'number' ? MapUtils.getResolutionsForScales([options.minScale], projection)[0] : undefined, maxResolution: typeof options.maxScale === 'number' ? MapUtils.getResolutionsForScales([options.maxScale], projection)[0] : undefined }); }); _defineProperty(_this, "createLayer", function (options) { if (options.type === 'group') { _this.layer = new ol.layer.Group({ zIndex: _this.props.zIndex }); _this.layer.setLayers(new ol.Collection(options.items.map(function (item) { var layerCreator = LayerRegistry[item.type]; if (layerCreator) { var sublayer = layerCreator.create(_this.makeOptions(item), _this.props.map); sublayer.set('id', options.id + "#" + item.name); return sublayer; } else { return null; } }).filter(function (x) { return x; }))); } else { var layerCreator = LayerRegistry[options.type]; if (layerCreator) { _this.layer = layerCreator.create(options, _this.props.map); } } if (_this.layer) { _this.layer.set('id', options.id); // WMS layer handles visibility and opacity internally if (options.type !== "wms") { _this.layer.setVisible(_this.layer.get("empty") !== true && options.visibility); _this.layer.setOpacity(options.opacity / 255.0); } _this.layer.setZIndex(_this.props.zIndex); _this.addLayer(_this.layer, options); var refreshInterval = LayerUtils.getLayerRefreshInterval(options); if (refreshInterval > 0 && options.visibility) { _this.updateInterval = setInterval(function () { _this.props.refreshLayer(function (layer) { return layer.id === options.id; }); }, refreshInterval); } } }); _defineProperty(_this, "updateLayer", function (newOptions, oldOptions) { // optimization to avoid to update the layer if not necessary if (newOptions === oldOptions) { return; } var layerCreator = LayerRegistry[_this.props.options.type]; if (layerCreator && layerCreator.update) { layerCreator.update(_this.layer, newOptions, oldOptions, _this.props.map); OlLayerUpdated.notify(_this.layer); } var oldRefreshInterval = LayerUtils.getLayerRefreshInterval(oldOptions); var newRefreshInterval = LayerUtils.getLayerRefreshInterval(newOptions); if (oldRefreshInterval !== newRefreshInterval || oldOptions.visibility !== newOptions.visibility) { clearInterval(_this.updateInterval); _this.updateInterval = null; if (newRefreshInterval && newOptions.visibility) { _this.updateInterval = setInterval(function () { _this.props.refreshLayer(function (layer) { return layer.id === newOptions.id; }); }, newRefreshInterval); } } }); _defineProperty(_this, "addLayer", function (layer, options) { _this.props.map.addLayer(layer); OlLayerAdded.notify(layer); layer.on('prerender', function (event) { var ctx = event.context; ctx.save(); ctx.beginPath(); if (_this.props.swipe !== null && _this.props.swipe !== undefined) { var width = ctx.canvas.width * (_this.props.swipe / 100); ctx.rect(0, 0, width, ctx.canvas.height); ctx.clip(); } }); layer.on('postrender', function (event) { event.context.restore(); }); if (options.zoomToExtent && options.bbox && options.bbox.bounds) { _this.props.zoomToExtent(options.bbox.bounds, options.bbox.crs); } var sublayers = {}; if (layer instanceof ol.layer.Group) { layer.getLayers().forEach(function (sublayer) { sublayers[options.id + "#" + sublayer.get('id')] = sublayer; }); } else { sublayers[options.id] = layer; } Object.entries(sublayers).map(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), id = _ref2[0], sublayer = _ref2[1]; if (sublayer.getSource() && sublayer.getSource().getImageLoadFunction) { sublayer.getSource().on('imageloadstart', function () { _this.props.setLayerLoading(id, true); }); sublayer.getSource().on('imageloadend', function () { _this.props.setLayerLoading(id, false); }); sublayer.getSource().on('imageloaderror', function () { _this.props.setLayerLoading(id, false); }); } else if (sublayer.getSource() && sublayer.getSource().getTileLoadFunction) { sublayer.getSource().on('tileloadstart', function () { if (_this.tilestoload === 0) { _this.props.setLayerLoading(id, true); } _this.tilestoload++; }); sublayer.getSource().on('tileloadend', function () { _this.tilestoload--; if (_this.tilestoload === 0) { _this.props.setLayerLoading(id, false); } }); sublayer.getSource().on('tileloaderror', function () { _this.tilestoload--; if (_this.tilestoload === 0) { _this.props.setLayerLoading(id, false); } }); } else if (sublayer.getSource() && sublayer.getSource() instanceof ol.source.Vector && sublayer.getSource().getUrl()) { sublayer.getSource().on('featuresloadstart', function () { _this.props.setLayerLoading(id, true); }); sublayer.getSource().on('featuresloadend', function () { _this.props.setLayerLoading(id, false); }); sublayer.getSource().on('featuresloaderror', function () { _this.props.setLayerLoading(id, false); }); } }); }); _this.layer = null; _this.updateInterval = null; return _this; } _inherits(OlLayer, _React$Component); return _createClass(OlLayer, [{ key: "componentDidMount", value: function componentDidMount() { this.tilestoload = 0; this.createLayer(this.makeOptions(this.props.options)); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { if (!this.layer) { return; } var newOptions = this.makeOptions(this.props.options); var oldOptions = this.makeOptions(prevProps.options); this.updateLayer(newOptions, oldOptions); // WMS layer handles visibility and opacity internally if (newOptions.type !== "wms") { this.layer.setVisible(newOptions.visibility); this.layer.setOpacity(newOptions.opacity / 255.0); } this.layer.setZIndex(this.props.zIndex); if (this.props.swipe !== prevProps.swipe) { this.props.map.render(); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { if (this.layer && this.props.map) { this.props.map.removeLayer(this.layer); } clearInterval(this.updateInterval); } }, { key: "render", value: function render() { var layerCreator = LayerRegistry[this.props.options.type]; if (layerCreator && layerCreator.render) { // NOTE: required for Google Maps layer return layerCreator.render(this.props.options, this.props.map, this.layer); } return null; } }]); }(React.Component); _defineProperty(OlLayer, "propTypes", { map: PropTypes.object, options: PropTypes.object, projection: PropTypes.string, refreshLayer: PropTypes.func, setLayerLoading: PropTypes.func, swipe: PropTypes.number, zIndex: PropTypes.number, zoomToExtent: PropTypes.func }); export default connect(function () { return {}; }, { setLayerLoading: setLayerLoading, refreshLayer: refreshLayer, zoomToExtent: zoomToExtent })(OlLayer);