@mapcomponents/react-maplibre 0.1.87 → 0.1.88

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/components/MlFollowGps/MlFollowGps.stories.d.ts +2 -1
  3. package/dist/components/MlGeoJsonLayer/MlGeoJsonLayer.d.ts +8 -0
  4. package/dist/components/MlGeoJsonLayer/MlGeoJsonLayer.stories.d.ts +1 -0
  5. package/dist/components/MlLayer/MlLayer.d.ts +3 -2
  6. package/dist/components/MlScaleReference/MlScaleReference.stories.d.ts +3 -2
  7. package/dist/contexts/LayerContext.d.ts +15 -2
  8. package/dist/hooks/useAddImage/useaddImage.cy.d.ts +1 -0
  9. package/dist/hooks/useAddProtocol/useAddProtocol.stories.d.ts +9 -1
  10. package/dist/hooks/useFitLayerBounds.d.ts +8 -0
  11. package/dist/index.esm.js +812 -39
  12. package/dist/index.esm.js.map +1 -1
  13. package/dist/omt_styles/bright.d.ts +2495 -0
  14. package/dist/protocol_handlers/csv.d.ts +8 -0
  15. package/dist/protocol_handlers/mbtiles.d.ts +2 -2
  16. package/dist/protocol_handlers/osm.d.ts +11 -0
  17. package/dist/protocol_handlers/topojson.d.ts +9 -0
  18. package/dist/protocol_handlers/utils/getProtocolData.d.ts +1 -0
  19. package/dist/protocol_handlers/utils/protocolPathParser.d.ts +7 -0
  20. package/dist/protocol_handlers/xml.d.ts +10 -0
  21. package/dist/ui_components/AddLayerButton/AddLayerButton.d.ts +5 -0
  22. package/dist/ui_components/AddLayerButton/AddLayerPopup.d.ts +1 -0
  23. package/dist/ui_components/AddLayerButton/LayerConfigForms/LayerTypeForm.d.ts +1 -0
  24. package/dist/ui_components/AddLayerButton/LayerConfigForms/MbtilesLayerForm.d.ts +13 -0
  25. package/dist/ui_components/AddLayerButton/LayerConfigForms/ProtocolHandlerLayerForm.d.ts +10 -0
  26. package/dist/ui_components/AddLayerButton/LayerConfigForms/WmsLayerForm.d.ts +2 -1
  27. package/dist/ui_components/AddLayerButton/LayerConfigForms/utils/CSVOptionsFomular.d.ts +6 -0
  28. package/dist/ui_components/AddLayerButton/LayerConfigForms/utils/MbtilesLayerPropFormular.d.ts +7 -0
  29. package/dist/ui_components/AddLayerButton/LayerConfigForms/utils/OsmOptionsFomular.d.ts +6 -0
  30. package/dist/ui_components/DemoDescriptions.d.ts +12 -0
  31. package/dist/ui_components/LayerList/LayerListItemFactory.d.ts +2 -0
  32. package/package.json +6 -1
package/dist/index.esm.js CHANGED
@@ -62,6 +62,16 @@ var core = require('@dnd-kit/core');
62
62
  var modifiers = require('@dnd-kit/modifiers');
63
63
  var PlaylistAddIcon = require('@mui/icons-material/PlaylistAdd');
64
64
  var DynamicFeedIcon = require('@mui/icons-material/DynamicFeed');
65
+ var AddBoxIcon = require('@mui/icons-material/AddBox');
66
+ var IndeterminateCheckBoxIcon = require('@mui/icons-material/IndeterminateCheckBox');
67
+ var csv2geojson = require('csv2geojson');
68
+ var topojsonClient = require('topojson-client');
69
+ var osm2geojson = require('osm2geojson-lite');
70
+ var externParser = require('@tmcw/togeojson');
71
+ var initSqlJs = require('sql.js');
72
+ var pako = require('pako');
73
+ var KeyboardArrowDownIcon = require('@mui/icons-material/KeyboardArrowDown');
74
+ var KeyboardArrowUpIcon = require('@mui/icons-material/KeyboardArrowUp');
65
75
  var AppBar = require('@mui/material/AppBar');
66
76
  var Toolbar = require('@mui/material/Toolbar');
67
77
  var Menu = require('@mui/material/Menu');
@@ -146,6 +156,15 @@ var PolylineIcon__default = /*#__PURE__*/_interopDefaultLegacy(PolylineIcon);
146
156
  var d3__namespace = /*#__PURE__*/_interopNamespace(d3);
147
157
  var PlaylistAddIcon__default = /*#__PURE__*/_interopDefaultLegacy(PlaylistAddIcon);
148
158
  var DynamicFeedIcon__default = /*#__PURE__*/_interopDefaultLegacy(DynamicFeedIcon);
159
+ var AddBoxIcon__default = /*#__PURE__*/_interopDefaultLegacy(AddBoxIcon);
160
+ var IndeterminateCheckBoxIcon__default = /*#__PURE__*/_interopDefaultLegacy(IndeterminateCheckBoxIcon);
161
+ var csv2geojson__namespace = /*#__PURE__*/_interopNamespace(csv2geojson);
162
+ var osm2geojson__default = /*#__PURE__*/_interopDefaultLegacy(osm2geojson);
163
+ var externParser__namespace = /*#__PURE__*/_interopNamespace(externParser);
164
+ var initSqlJs__default = /*#__PURE__*/_interopDefaultLegacy(initSqlJs);
165
+ var pako__namespace = /*#__PURE__*/_interopNamespace(pako);
166
+ var KeyboardArrowDownIcon__default = /*#__PURE__*/_interopDefaultLegacy(KeyboardArrowDownIcon);
167
+ var KeyboardArrowUpIcon__default = /*#__PURE__*/_interopDefaultLegacy(KeyboardArrowUpIcon);
149
168
  var AppBar__default = /*#__PURE__*/_interopDefaultLegacy(AppBar);
150
169
  var Toolbar__default = /*#__PURE__*/_interopDefaultLegacy(Toolbar);
151
170
  var Menu__default = /*#__PURE__*/_interopDefaultLegacy(Menu);
@@ -947,7 +966,6 @@ function LayerContextProvider(props) {
947
966
  setSymbolLayers(symbolLayers);
948
967
  };
949
968
  React.useEffect(function () {
950
- console.log('layers', layers);
951
969
  if (layers.filter(function (el) { return !(el === null || el === void 0 ? void 0 : el.id); }).length) {
952
970
  var _layers = __spreadArray([], layers, true);
953
971
  _layers.forEach(function (el) {
@@ -2673,8 +2691,10 @@ var getDefaulLayerTypeByGeometry = function (geojson) {
2673
2691
  * @component
2674
2692
  */
2675
2693
  var MlGeoJsonLayer = function (props) {
2676
- var _a, _b;
2694
+ var _a, _b, _c;
2677
2695
  var layerType = props.type || getDefaulLayerTypeByGeometry(props.geojson);
2696
+ var layerId = props.layerId || 'MlGeoJsonLayer-' + uuid.v4();
2697
+ var labelLayerId = "label-".concat(layerId);
2678
2698
  // Use a useRef hook to reference the layer object to be able to access it later inside useEffect hooks
2679
2699
  useLayer({
2680
2700
  mapId: props.mapId,
@@ -2686,6 +2706,14 @@ var MlGeoJsonLayer = function (props) {
2686
2706
  onClick: props.onClick,
2687
2707
  onLeave: props.onLeave,
2688
2708
  });
2709
+ if (props.labelProp) {
2710
+ useLayer({
2711
+ mapId: props.mapId,
2712
+ layerId: labelLayerId,
2713
+ geojson: props.geojson,
2714
+ options: __assign(__assign({ type: 'symbol' }, ((props === null || props === void 0 ? void 0 : props.labelOptions) ? props.labelOptions : {})), { layout: __assign({ 'text-field': "{".concat(props.labelProp, "}") }, (((_c = props === null || props === void 0 ? void 0 : props.labelOptions) === null || _c === void 0 ? void 0 : _c.layout) ? props.labelOptions.layout : {})), paint: {} }),
2715
+ });
2716
+ }
2689
2717
  return React__default["default"].createElement(React__default["default"].Fragment, null);
2690
2718
  };
2691
2719
 
@@ -2880,7 +2908,7 @@ var MlMeasureTool = function (props) {
2880
2908
  React.useEffect(function () {
2881
2909
  if (currentFeatures[0]) {
2882
2910
  var result = props.measureType === 'polygon'
2883
- // turf area does actually return mm^2 contrary to the documentation stating it will return m^2
2911
+ // for "polyong" mode calculate km²
2884
2912
  ? (turf__namespace.area(currentFeatures[0]) / 1000000) * getUnitSquareMultiplier(props.unit)
2885
2913
  : turf__namespace.length(currentFeatures[0], { units: props.unit });
2886
2914
  if (typeof props.onChange === 'function') {
@@ -2891,10 +2919,16 @@ var MlMeasureTool = function (props) {
2891
2919
  }
2892
2920
  else {
2893
2921
  var label = 'm';
2894
- var value = result * 1000000;
2922
+ var value = result * 1000;
2923
+ if (props.measureType === 'polygon') {
2924
+ value = result * 1000000;
2925
+ }
2895
2926
  if (getUnitLabel(props.unit) === 'mi') {
2896
2927
  label = 'in';
2897
2928
  value = result * 63360;
2929
+ if (props.measureType === 'polygon') {
2930
+ value = result * 4014489599.4792;
2931
+ }
2898
2932
  }
2899
2933
  setDisplayValue({ value: value, label: label });
2900
2934
  }
@@ -2915,22 +2949,25 @@ MlMeasureTool.defaultProps = {
2915
2949
  unit: 'kilometers',
2916
2950
  };
2917
2951
 
2918
- var _path$1, _path2$1;
2952
+ var _g;
2919
2953
  function _extends$1() { _extends$1 = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$1.apply(this, arguments); }
2920
2954
  var SvgCompassNeedle = function SvgCompassNeedle(props) {
2921
2955
  return /*#__PURE__*/React__namespace.createElement("svg", _extends$1({
2922
- width: 9,
2956
+ width: 10,
2923
2957
  height: 40,
2924
- viewBox: "0 0 9 40",
2958
+ viewBox: "0 0 10 40",
2925
2959
  fill: "none",
2926
- xmlns: "http://www.w3.org/2000/svg"
2927
- }, props), _path$1 || (_path$1 = /*#__PURE__*/React__namespace.createElement("path", {
2928
- d: "M3.34715 4.52028C3.57452 3.46874 5.0746 3.46874 5.30197 4.52028L8.64912 20H0L3.34715 4.52028Z",
2929
- fill: "#CF003D"
2930
- })), _path2$1 || (_path2$1 = /*#__PURE__*/React__namespace.createElement("path", {
2931
- d: "M3.34715 35.4797C3.57452 36.5313 5.0746 36.5313 5.30197 35.4797L8.64912 20H0L3.34715 35.4797Z",
2932
- fill: "#D3DCE1"
2933
- })));
2960
+ id: "svg6"
2961
+ }, props), _g || (_g = /*#__PURE__*/React__namespace.createElement("g", {
2962
+ id: "g14",
2963
+ transform: "translate(0.67544,-1.25e-5)"
2964
+ }, /*#__PURE__*/React__namespace.createElement("path", {
2965
+ d: "m 3.34715,4.52028 c 0.22737,-1.05154 1.72745,-1.05154 1.95482,0 L 8.64912,20 H 0 Z",
2966
+ fill: "#cf003d"
2967
+ }), /*#__PURE__*/React__namespace.createElement("path", {
2968
+ d: "m 3.34715,35.4797 c 0.22737,1.0516 1.72745,1.0516 1.95482,0 L 8.64912,20 H 0 Z",
2969
+ fill: "#d3dcf0"
2970
+ }))));
2934
2971
  };
2935
2972
 
2936
2973
  var _circle, _path, _path2, _path3, _path4;
@@ -2983,7 +3020,13 @@ var CompassBox = material.styled(material.Box)(function (_a) {
2983
3020
  return (_b = {
2984
3021
  position: 'absolute',
2985
3022
  right: '-10px',
2986
- top: '-52px'
3023
+ top: '-52px',
3024
+ width: '52px',
3025
+ height: '52px',
3026
+ display: 'flex',
3027
+ flexDirection: 'row',
3028
+ justifyContent: 'center',
3029
+ alignItems: 'center'
2987
3030
  },
2988
3031
  _b[theme.breakpoints.down('md')] = {
2989
3032
  right: '0px',
@@ -3004,9 +3047,8 @@ var CompassBox = material.styled(material.Box)(function (_a) {
3004
3047
  var NeedleBox = material.styled(material.Box)(function (_a) {
3005
3048
  var theme = _a.theme;
3006
3049
  return ({
3007
- position: 'absolute',
3008
- right: '21.4px',
3009
- top: '6px',
3050
+ display: 'flex',
3051
+ flexDirection: 'row',
3010
3052
  'path:nth-of-type(2)': {
3011
3053
  fill: theme.palette.compass.compSouth,
3012
3054
  },
@@ -3062,7 +3104,7 @@ var MlNavigationCompass = function (props) {
3062
3104
  return (React__default["default"].createElement(React__default["default"].Fragment, null,
3063
3105
  React__default["default"].createElement(BoxStyled$2, { sx: __assign({}, props.style) },
3064
3106
  React__default["default"].createElement(CompassBox, { onClick: rotate, sx: __assign({}, props.backgroundStyle) },
3065
- React__default["default"].createElement(SvgCompassBackground, null),
3107
+ React__default["default"].createElement(SvgCompassBackground, { style: { position: 'absolute', top: 0, left: 0 } }),
3066
3108
  React__default["default"].createElement(NeedleBox, { onClick: rotate, sx: __assign({}, props.needleStyle) },
3067
3109
  React__default["default"].createElement(SvgCompassNeedle, { style: {
3068
3110
  transform: 'rotate(' + (bearing > 0 ? '-' + bearing : -1 * bearing) + 'deg)',
@@ -3145,9 +3187,9 @@ var MlLayer = function (props) {
3145
3187
  idPrefix: 'MlLayer-',
3146
3188
  layerId: props.layerId,
3147
3189
  mapId: props.mapId,
3148
- geojson: props.geojson,
3149
- options: __assign({ type: "background", paint: {
3150
- "background-color": "rgba(0,0,0,0)",
3190
+ geojson: props.geojson || undefined,
3191
+ options: __assign({ type: 'background', paint: {
3192
+ 'background-color': 'rgba(0,0,0,0)',
3151
3193
  } }, props.options),
3152
3194
  insertBeforeLayer: props.insertBeforeLayer,
3153
3195
  });
@@ -3432,7 +3474,7 @@ var MlLayerSwipe = function (props) {
3432
3474
  });
3433
3475
  }, [mapContext.mapIds, mapContext, props, onMove, mapExists]);
3434
3476
  var onDown = function (e) {
3435
- if (e.nativeEvent instanceof TouchEvent) {
3477
+ if ((window === null || window === void 0 ? void 0 : window.TouchEvent) && e.nativeEvent instanceof window.TouchEvent) {
3436
3478
  document.addEventListener('touchmove', onMove);
3437
3479
  document.addEventListener('touchend', onTouchEnd);
3438
3480
  }
@@ -7053,7 +7095,7 @@ var useAddProtocol = function (props) {
7053
7095
  return function () {
7054
7096
  maplibregl__default["default"].removeProtocol(props.protocol);
7055
7097
  };
7056
- }, []);
7098
+ }, [props]);
7057
7099
  return {};
7058
7100
  };
7059
7101
  useAddProtocol.defaultProps = {
@@ -7134,6 +7176,47 @@ var IconButtonStyled = material.styled(material.IconButton)({
7134
7176
  function LayerListItemFactory(props) {
7135
7177
  var _a, _b;
7136
7178
  var layerContext = useLayerContext();
7179
+ var mapHook = useMap({ mapId: undefined });
7180
+ //useCallback Hook
7181
+ function fitLayer(layer) {
7182
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
7183
+ var layerSource = layer.id && ((_b = (_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.getLayer(layer.id)) === null || _b === void 0 ? void 0 : _b.source)
7184
+ ? (_c = mapHook.map) === null || _c === void 0 ? void 0 : _c.getLayer(layer.id).source
7185
+ : undefined;
7186
+ var _bbox = null;
7187
+ switch (layer.type) {
7188
+ case 'geojson':
7189
+ if ((_d = layer.config) === null || _d === void 0 ? void 0 : _d.geojson) {
7190
+ (_e = mapHook.map) === null || _e === void 0 ? void 0 : _e.fitBounds(turf.bbox((_f = layer.config) === null || _f === void 0 ? void 0 : _f.geojson), props.fitBoundsOptions);
7191
+ }
7192
+ else {
7193
+ if (!layerSource) {
7194
+ return;
7195
+ }
7196
+ var _geojson = {
7197
+ type: 'FeatureCollection',
7198
+ features: (_g = mapHook.map) === null || _g === void 0 ? void 0 : _g.querySourceFeatures(layerSource),
7199
+ };
7200
+ if (_geojson.features.length === 0) {
7201
+ (_h = mapHook.map) === null || _h === void 0 ? void 0 : _h.zoomTo(1);
7202
+ _geojson.features = (_j = mapHook.map) === null || _j === void 0 ? void 0 : _j.querySourceFeatures(layerSource);
7203
+ }
7204
+ _bbox = turf.bbox(_geojson);
7205
+ }
7206
+ break;
7207
+ case 'vt':
7208
+ console.log('vt');
7209
+ break;
7210
+ case 'wms':
7211
+ _bbox = (_o = (_m = (_l = (_k = layer === null || layer === void 0 ? void 0 : layer.config) === null || _k === void 0 ? void 0 : _k.config) === null || _l === void 0 ? void 0 : _l.layers) === null || _m === void 0 ? void 0 : _m[0]) === null || _o === void 0 ? void 0 : _o.EX_GeographicBoundingBox;
7212
+ break;
7213
+ default:
7214
+ return;
7215
+ }
7216
+ if (_bbox) {
7217
+ (_p = mapHook.map) === null || _p === void 0 ? void 0 : _p.fitBounds(_bbox, props.fitBoundsOptions);
7218
+ }
7219
+ }
7137
7220
  var orderLayers = React.useMemo(function () {
7138
7221
  var layerIds = __spreadArray(__spreadArray([
7139
7222
  'order-background'
@@ -7179,7 +7262,7 @@ function LayerListItemFactory(props) {
7179
7262
  React__default["default"].createElement(core.DndContext, { collisionDetection: core.closestCenter, sensors: sensors, onDragEnd: function (event) { return dragEnd(event); }, modifiers: [modifiers.restrictToVerticalAxis] },
7180
7263
  React__default["default"].createElement(sortable.SortableContext, { items: layers, strategy: sortable.verticalListSortingStrategy },
7181
7264
  __spreadArray([], layers, true).map(function (layer, idx) {
7182
- var _a, _b;
7265
+ var _a, _b, _c, _d, _e;
7183
7266
  if (!(layer === null || layer === void 0 ? void 0 : layer.id))
7184
7267
  return null;
7185
7268
  switch (layer.type) {
@@ -7192,7 +7275,9 @@ function LayerListItemFactory(props) {
7192
7275
  React__default["default"].createElement(IconButtonStyled, { disabled: idx === 0, onClick: function () {
7193
7276
  layerContext.moveUp(layer.id || '');
7194
7277
  } },
7195
- React__default["default"].createElement(iconsMaterial.ArrowCircleUp, null))), setLayerState: function (layerConfig) {
7278
+ React__default["default"].createElement(iconsMaterial.ArrowCircleUp, null)),
7279
+ React__default["default"].createElement(IconButtonStyled, { onClick: function () { return fitLayer(layer); } },
7280
+ React__default["default"].createElement(iconsMaterial.CenterFocusWeak, null))), setLayerState: function (layerConfig) {
7196
7281
  return setLayers === null || setLayers === void 0 ? void 0 : setLayers(function (current) {
7197
7282
  var _layers = __spreadArray([], current, true);
7198
7283
  if (layerConfig === false) {
@@ -7223,7 +7308,8 @@ function LayerListItemFactory(props) {
7223
7308
  var _layers = __spreadArray([], current, true);
7224
7309
  if (typeof updateFunction === 'function') {
7225
7310
  _layers[idx].config.featureInfoActive =
7226
- updateFunction(((_a = _layers[idx].config) === null || _a === void 0 ? void 0 : _a.featureInfoActive) || false);
7311
+ updateFunction(((_a = _layers[idx].config) === null || _a === void 0 ? void 0 : _a.featureInfoActive) ||
7312
+ false);
7227
7313
  }
7228
7314
  return _layers;
7229
7315
  });
@@ -7235,7 +7321,33 @@ function LayerListItemFactory(props) {
7235
7321
  React__default["default"].createElement(IconButtonStyled, { disabled: idx === 0, onClick: function () {
7236
7322
  layerContext.moveUp(layer.id || '');
7237
7323
  } },
7238
- React__default["default"].createElement(iconsMaterial.ArrowCircleUp, null))) }))));
7324
+ React__default["default"].createElement(iconsMaterial.ArrowCircleUp, null)),
7325
+ React__default["default"].createElement(IconButtonStyled, { onClick: function () { return fitLayer(layer); } },
7326
+ React__default["default"].createElement(iconsMaterial.CenterFocusWeak, null))) }))));
7327
+ case 'vt':
7328
+ return (React__default["default"].createElement(React__default["default"].Fragment, { key: (layer === null || layer === void 0 ? void 0 : layer.id) + '_listItem' },
7329
+ React__default["default"].createElement(LayerListItem, { key: layer.id, name: (layer === null || layer === void 0 ? void 0 : layer.name) || (layer === null || layer === void 0 ? void 0 : layer.type) + ' layer' || 'unnamed layer', layerComponent: React__default["default"].createElement(MlVectorTileLayer, { layers: ((_c = layer === null || layer === void 0 ? void 0 : layer.config) === null || _c === void 0 ? void 0 : _c.layers) || [], key: layer.id, mapId: layer === null || layer === void 0 ? void 0 : layer.config.mapId, sourceOptions: (_d = layer === null || layer === void 0 ? void 0 : layer.config) === null || _d === void 0 ? void 0 : _d.sourceOptions, layerId: layer.id, url: (_e = layer === null || layer === void 0 ? void 0 : layer.config) === null || _e === void 0 ? void 0 : _e.url }), buttons: React__default["default"].createElement(React__default["default"].Fragment, null,
7330
+ React__default["default"].createElement(IconButtonStyled, { key: layer.id + '_button1', disabled: idx === layers.length - 1, onClick: function () {
7331
+ layerContext.moveDown(layer.id || '');
7332
+ } },
7333
+ React__default["default"].createElement(iconsMaterial.ArrowCircleDown, null)),
7334
+ React__default["default"].createElement(IconButtonStyled, { key: layer.id + '_button2', disabled: idx === 0, onClick: function () {
7335
+ layerContext.moveUp(layer.id || '');
7336
+ } },
7337
+ React__default["default"].createElement(iconsMaterial.ArrowCircleUp, null)),
7338
+ React__default["default"].createElement(IconButtonStyled, { onClick: function () { return fitLayer(layer); } },
7339
+ React__default["default"].createElement(iconsMaterial.CenterFocusWeak, null))), setLayerState: function (layerConfig) {
7340
+ return setLayers === null || setLayers === void 0 ? void 0 : setLayers(function (current) {
7341
+ var _layers = __spreadArray([], current, true);
7342
+ if (layerConfig === false) {
7343
+ _layers.splice(idx, 1);
7344
+ }
7345
+ else {
7346
+ _layers[idx].config = layerConfig;
7347
+ }
7348
+ return _layers;
7349
+ });
7350
+ }, configurable: true, showDeleteButton: true })));
7239
7351
  default:
7240
7352
  return null;
7241
7353
  }
@@ -7248,7 +7360,7 @@ LayerListItemFactory.defaultProps = {
7248
7360
  mapId: undefined,
7249
7361
  };
7250
7362
 
7251
- var types = [
7363
+ var types$1 = [
7252
7364
  'fill',
7253
7365
  'line',
7254
7366
  'circle',
@@ -7266,7 +7378,7 @@ function GeoJsonLayerForm(props) {
7266
7378
  React__default["default"].createElement(material.InputLabel, { id: "type-label" }, "Type"),
7267
7379
  React__default["default"].createElement(material.Select, { labelId: "type-label", value: config.type, label: "Type", onChange: function (ev) {
7268
7380
  return setConfig(function (current) { return (__assign(__assign({}, current), { type: ev.target.value })); });
7269
- } }, types.map(function (type) { return (React__default["default"].createElement(material.MenuItem, { key: type, value: type }, type)); }))),
7381
+ } }, types$1.map(function (type) { return (React__default["default"].createElement(material.MenuItem, { key: type, value: type }, type)); }))),
7270
7382
  React__default["default"].createElement(material.FormControl, { fullWidth: true },
7271
7383
  React__default["default"].createElement(material.Button, { variant: "contained", component: "label", sx: { marginTop: '10px' } },
7272
7384
  "Upload File",
@@ -7281,11 +7393,10 @@ function GeoJsonLayerForm(props) {
7281
7393
  React__default["default"].createElement(material.Button, { disabled: !configIsValid, onClick: function () { return props.onSubmit(config); } }, "Add"))));
7282
7394
  }
7283
7395
 
7284
- var layerTypes = ['geojson', 'wms'];
7285
7396
  var LayerTypeForm = function (props) {
7286
7397
  return (React__default["default"].createElement(React__default["default"].Fragment, null,
7287
- React__default["default"].createElement(material.DialogTitle, null, "Select a layer type"),
7288
- React__default["default"].createElement(material.List, null, layerTypes.map(function (type) { return (React__default["default"].createElement(material.ListItem, { disableGutters: true, key: type },
7398
+ React__default["default"].createElement(material.DialogTitle, null, props.layerTypes.length > 1 ? "Select a layer type" : "Load new layer"),
7399
+ React__default["default"].createElement(material.List, null, props.layerTypes.map(function (type, idx) { return (React__default["default"].createElement(material.ListItem, { disableGutters: true, key: idx },
7289
7400
  React__default["default"].createElement(material.ListItemButton, { autoFocus: true, onClick: function () {
7290
7401
  props.onSelect(type);
7291
7402
  } },
@@ -7314,18 +7425,667 @@ function WmsLayerForm(props) {
7314
7425
  React__default["default"].createElement(material.Button, { disabled: !configIsValid, onClick: function () { return props.onSubmit(config); } }, "Add"))));
7315
7426
  }
7316
7427
 
7428
+ var optionFields$1 = ['latfield', 'lonfield', 'delimiter'];
7429
+ function CSVOptionsFormular(props) {
7430
+ var _a = React.useState(false), open = _a[0], setOpen = _a[1];
7431
+ return (React__default["default"].createElement(React__default["default"].Fragment, null,
7432
+ React__default["default"].createElement(material.Typography, null, " Options "),
7433
+ React__default["default"].createElement(material.Button, { onClick: function () { return setOpen(!open); } }, open ? React__default["default"].createElement(IndeterminateCheckBoxIcon__default["default"], null) : React__default["default"].createElement(AddBoxIcon__default["default"], null)),
7434
+ React__default["default"].createElement(material.List, null, open &&
7435
+ (optionFields$1 === null || optionFields$1 === void 0 ? void 0 : optionFields$1.map(function (el) {
7436
+ return (React__default["default"].createElement(React__default["default"].Fragment, null,
7437
+ React__default["default"].createElement(material.ListItem, null,
7438
+ React__default["default"].createElement(material.Typography, null,
7439
+ " ",
7440
+ el,
7441
+ " ")),
7442
+ React__default["default"].createElement(material.ListItem, null,
7443
+ React__default["default"].createElement(material.TextField, { size: "small", onChange: function (ev) {
7444
+ var newObject = {};
7445
+ newObject[el] = ev.target.value;
7446
+ props.setter(newObject);
7447
+ } }))));
7448
+ })))));
7449
+ }
7450
+
7451
+ var optionFields = [
7452
+ 'completeFeature',
7453
+ 'allFeatures',
7454
+ 'renderTagged',
7455
+ 'excludeWay',
7456
+ 'suppressWay',
7457
+ ];
7458
+ function OsmOptionsFomular(props) {
7459
+ var _a = React.useState(false), open = _a[0], setOpen = _a[1];
7460
+ return (React__default["default"].createElement(React__default["default"].Fragment, null,
7461
+ React__default["default"].createElement(material.Typography, null, " Options "),
7462
+ React__default["default"].createElement(material.Button, { onClick: function () { return setOpen(!open); } }, open ? React__default["default"].createElement(IndeterminateCheckBoxIcon__default["default"], null) : React__default["default"].createElement(AddBoxIcon__default["default"], null)),
7463
+ React__default["default"].createElement(material.List, null, open &&
7464
+ (optionFields === null || optionFields === void 0 ? void 0 : optionFields.map(function (el) {
7465
+ return (React__default["default"].createElement(React__default["default"].Fragment, null,
7466
+ React__default["default"].createElement(material.ListItem, null,
7467
+ React__default["default"].createElement(material.Typography, null,
7468
+ " ",
7469
+ el,
7470
+ " "),
7471
+ React__default["default"].createElement(material.Checkbox, { onChange: function (ev) {
7472
+ var newObject = {};
7473
+ newObject[el] = ev.target.value === 'on' ? true : false;
7474
+ props.setter(newObject);
7475
+ } }))));
7476
+ })))));
7477
+ }
7478
+
7479
+ function protocolPathParser(url) {
7480
+ var test = url.split('?');
7481
+ var urlParts = test[0].split('://');
7482
+ var protocolId = urlParts[0];
7483
+ var csvUrl = urlParts.length > 2 ? urlParts[1] + '://' + urlParts[2] : urlParts[1];
7484
+ var csvParts = csvUrl.split('/');
7485
+ var filename = csvParts.join('/');
7486
+ var optionsString = decodeURI(test[1]);
7487
+ var options = Object.fromEntries(new URLSearchParams(optionsString));
7488
+ return {
7489
+ protocolId: protocolId,
7490
+ filename: filename,
7491
+ options: options,
7492
+ };
7493
+ }
7494
+
7495
+ function getProtocolData(path) {
7496
+ return __awaiter(this, void 0, void 0, function () {
7497
+ var response, rawData, error_1;
7498
+ return __generator(this, function (_a) {
7499
+ switch (_a.label) {
7500
+ case 0:
7501
+ _a.trys.push([0, 3, , 4]);
7502
+ return [4 /*yield*/, fetch(path)];
7503
+ case 1:
7504
+ response = _a.sent();
7505
+ return [4 /*yield*/, response.text()];
7506
+ case 2:
7507
+ rawData = _a.sent();
7508
+ return [2 /*return*/, rawData];
7509
+ case 3:
7510
+ error_1 = _a.sent();
7511
+ console.error('File could not be loaded: ', error_1);
7512
+ return [2 /*return*/, error_1];
7513
+ case 4: return [2 /*return*/];
7514
+ }
7515
+ });
7516
+ });
7517
+ }
7518
+
7519
+ function convertCsv(filename, options) {
7520
+ return __awaiter(this, void 0, void 0, function () {
7521
+ var geojson;
7522
+ return __generator(this, function (_a) {
7523
+ switch (_a.label) {
7524
+ case 0: return [4 /*yield*/, new Promise(function (resolve, reject) {
7525
+ var useOptions = options || {};
7526
+ var extension = filename.substring(filename.length - 3);
7527
+ if (extension === 'tsv') {
7528
+ options.delimiter = '\t';
7529
+ }
7530
+ getProtocolData(filename).then(function (rawData) {
7531
+ // Use the csv2geojson library to convert the CSV to GeoJSON
7532
+ csv2geojson__namespace.csv2geojson(rawData, useOptions, function (err, data) {
7533
+ if (err) {
7534
+ reject(err);
7535
+ }
7536
+ else {
7537
+ resolve(data);
7538
+ }
7539
+ });
7540
+ });
7541
+ })];
7542
+ case 1:
7543
+ geojson = _a.sent();
7544
+ return [2 /*return*/, geojson];
7545
+ }
7546
+ });
7547
+ });
7548
+ }
7549
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
7550
+ var CSVProtocolHandler = function (params, callback) {
7551
+ var parsedParams = protocolPathParser(params.url);
7552
+ convertCsv(parsedParams.filename, parsedParams.options).then(function (data) {
7553
+ if (data !== undefined) {
7554
+ callback(null, data, null, null);
7555
+ }
7556
+ else {
7557
+ callback(new Error('CSV not found'));
7558
+ }
7559
+ });
7560
+ return { cancel: function () { } };
7561
+ };
7562
+
7563
+ function reduceFeatures(geojson) {
7564
+ var newFeatures = [];
7565
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
7566
+ geojson.features.forEach(function (e) {
7567
+ if (!e.features) {
7568
+ newFeatures.push({
7569
+ type: e.type,
7570
+ geometry: e.geometry,
7571
+ properties: e.properties,
7572
+ });
7573
+ }
7574
+ else {
7575
+ e.features.forEach(function (el) {
7576
+ newFeatures.push({ type: el.type, geometry: el.geometry, properties: el.properties });
7577
+ });
7578
+ }
7579
+ });
7580
+ return newFeatures;
7581
+ }
7582
+ function convertTopojson(params) {
7583
+ return __awaiter(this, void 0, void 0, function () {
7584
+ var geojson;
7585
+ return __generator(this, function (_a) {
7586
+ switch (_a.label) {
7587
+ case 0: return [4 /*yield*/, new Promise(function (resolve) {
7588
+ var topoJsonData = {
7589
+ type: 'Topology',
7590
+ objects: { key: '' },
7591
+ arcs: []
7592
+ };
7593
+ getProtocolData(params.filename).then(function (rawData) {
7594
+ try {
7595
+ topoJsonData = JSON.parse(rawData);
7596
+ }
7597
+ catch (e) {
7598
+ throw 'Invalid TopoJson';
7599
+ }
7600
+ // Convert the data
7601
+ var result = {
7602
+ type: 'FeatureCollection',
7603
+ features: [],
7604
+ };
7605
+ if (topoJsonData.type === 'Topology' && topoJsonData.objects !== undefined) {
7606
+ // add the "fromObject" property in each topojson feature
7607
+ Object.keys(topoJsonData.objects).map(function (key) {
7608
+ var _a, _b, _c, _d, _e, _f, _g;
7609
+ if (((_a = topoJsonData.objects) === null || _a === void 0 ? void 0 : _a[key].type) === 'GeometryCollection') {
7610
+ (_c = (_b = topoJsonData.objects) === null || _b === void 0 ? void 0 : _b[key].geometries) === null || _c === void 0 ? void 0 : _c.forEach(function (e) { return (e.properties = __assign({ fromObject: key }, e.properties)); });
7611
+ }
7612
+ else if (((_d = topoJsonData === null || topoJsonData === void 0 ? void 0 : topoJsonData.objects) === null || _d === void 0 ? void 0 : _d[key]) &&
7613
+ ((_f = (_e = topoJsonData === null || topoJsonData === void 0 ? void 0 : topoJsonData.objects) === null || _e === void 0 ? void 0 : _e[key]) === null || _f === void 0 ? void 0 : _f.type) !== 'GeometryCollection') {
7614
+ topoJsonData.objects[key].properties = __assign({ fromObject: key }, (_g = topoJsonData.objects) === null || _g === void 0 ? void 0 : _g[key].properties);
7615
+ }
7616
+ });
7617
+ //convert the data into a geoJson object
7618
+ result = {
7619
+ type: 'FeatureCollection',
7620
+ features: Object.keys(topoJsonData.objects).map(function (key) {
7621
+ return topojsonClient.feature(topoJsonData, key);
7622
+ }),
7623
+ };
7624
+ result.features = reduceFeatures(result);
7625
+ }
7626
+ resolve(result);
7627
+ });
7628
+ })];
7629
+ case 1:
7630
+ geojson = _a.sent();
7631
+ return [2 /*return*/, geojson];
7632
+ }
7633
+ });
7634
+ });
7635
+ }
7636
+ var TopojsonProtocolHandler = function (params, callback) {
7637
+ var parsedParams = protocolPathParser(params.url);
7638
+ convertTopojson(parsedParams).then(function (data) {
7639
+ if (data !== undefined) {
7640
+ callback(null, data, null, null);
7641
+ }
7642
+ else {
7643
+ callback(new Error('Topojson not found'));
7644
+ }
7645
+ });
7646
+ return { cancel: function () { } };
7647
+ };
7648
+
7649
+ function convertOSM(params) {
7650
+ return __awaiter(this, void 0, void 0, function () {
7651
+ var options, geojson;
7652
+ return __generator(this, function (_a) {
7653
+ switch (_a.label) {
7654
+ case 0:
7655
+ options = params.options || {};
7656
+ return [4 /*yield*/, new Promise(function (resolve, reject) {
7657
+ getProtocolData(params.filename).then(function (rawData) {
7658
+ var newData = osm2geojson__default["default"](rawData, options);
7659
+ if (!newData) {
7660
+ reject('Conversion failed');
7661
+ }
7662
+ else {
7663
+ resolve(newData);
7664
+ }
7665
+ });
7666
+ })];
7667
+ case 1:
7668
+ geojson = _a.sent();
7669
+ return [2 /*return*/, geojson];
7670
+ }
7671
+ });
7672
+ });
7673
+ }
7674
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
7675
+ var OSMProtocolHandler = function (params, callback) {
7676
+ var parsedParams = protocolPathParser(params.url);
7677
+ convertOSM(parsedParams).then(function (data) {
7678
+ if (data !== undefined) {
7679
+ callback(null, data, null, null);
7680
+ }
7681
+ else {
7682
+ callback(new Error('OSM File not found'));
7683
+ }
7684
+ });
7685
+ return { cancel: function () { } };
7686
+ };
7687
+
7688
+ function convertXML(params) {
7689
+ return __awaiter(this, void 0, void 0, function () {
7690
+ var geojson;
7691
+ return __generator(this, function (_a) {
7692
+ switch (_a.label) {
7693
+ case 0: return [4 /*yield*/, new Promise(function (resolve, reject) {
7694
+ getProtocolData(params.filename).then(function (rawData) {
7695
+ var newData = function () {
7696
+ // use an extern converter for tcx files
7697
+ if (params.protocolId === 'tcx') {
7698
+ return externParser__namespace[params.protocolId](new DOMParser().parseFromString(rawData, 'text/xml'));
7699
+ // use the projects gpxConverter function for gpx and kml files
7700
+ }
7701
+ else {
7702
+ return toGeoJSON[params.protocolId](new DOMParser().parseFromString(rawData, 'text/xml'));
7703
+ }
7704
+ };
7705
+ if (!newData()) {
7706
+ reject('Conversion failed');
7707
+ }
7708
+ else {
7709
+ resolve(newData());
7710
+ }
7711
+ });
7712
+ })];
7713
+ case 1:
7714
+ geojson = _a.sent();
7715
+ return [2 /*return*/, geojson];
7716
+ }
7717
+ });
7718
+ });
7719
+ }
7720
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
7721
+ var XMLProtocolHandler = function (params, callback) {
7722
+ var parsedParams = protocolPathParser(params.url);
7723
+ convertXML(parsedParams).then(function (data) {
7724
+ if (data !== undefined) {
7725
+ callback(null, data, null, null);
7726
+ }
7727
+ else {
7728
+ callback(new Error('XML not found'));
7729
+ }
7730
+ });
7731
+ return { cancel: function () { } };
7732
+ };
7733
+
7734
+ var loadedMbtiles = {};
7735
+ var parseTileParams = function (url) {
7736
+ var urlParts = url.split('://');
7737
+ var mbtilesUrl = urlParts.length > 2 ? urlParts[1] + '://' + urlParts[2] : urlParts[1];
7738
+ var mbtilesParts = mbtilesUrl.split('/');
7739
+ var mbtilesPartsLength = mbtilesParts.length;
7740
+ var y = mbtilesParts.splice(mbtilesPartsLength - 1, 1)[0];
7741
+ var x = mbtilesParts.splice(mbtilesPartsLength - 2, 1)[0];
7742
+ var z = mbtilesParts.splice(mbtilesPartsLength - 3, 1)[0];
7743
+ var filename = mbtilesParts.join('/');
7744
+ return {
7745
+ filename: filename,
7746
+ z: z,
7747
+ x: x,
7748
+ y: y,
7749
+ };
7750
+ };
7751
+ // mbtiles files are sqlite databases. This function loads the database and returns a handler
7752
+ // to work with sqlite databases in javascript we need to use sql.js.
7753
+ // to make this work in your project make sure to copy sql-wasm.wasm to the file root of your public folder and
7754
+ // add the following config to the externals prop of your webpack config
7755
+ // {externals: { fs: 'fs' }};
7756
+ var getMbtilesDbHandler = function (_a) {
7757
+ var filename = _a.filename;
7758
+ return __awaiter(void 0, void 0, void 0, function () {
7759
+ var SQL, fetched, buf, db;
7760
+ return __generator(this, function (_b) {
7761
+ switch (_b.label) {
7762
+ case 0:
7763
+ if (!!loadedMbtiles[filename]) return [3 /*break*/, 4];
7764
+ return [4 /*yield*/, initSqlJs__default["default"]()];
7765
+ case 1:
7766
+ SQL = _b.sent();
7767
+ return [4 /*yield*/, fetch(filename)];
7768
+ case 2:
7769
+ fetched = _b.sent();
7770
+ return [4 /*yield*/, fetched.arrayBuffer()];
7771
+ case 3:
7772
+ buf = _b.sent();
7773
+ db = new SQL.Database(new Uint8Array(buf));
7774
+ loadedMbtiles[filename] = db;
7775
+ _b.label = 4;
7776
+ case 4: return [2 /*return*/, loadedMbtiles[filename]];
7777
+ }
7778
+ });
7779
+ });
7780
+ };
7781
+ /**
7782
+ * Example usage:
7783
+ * getBufferFromMbtiles({ filename: 'mbtiles/countries.mbtiles', z: '0', x: '0', y: '0' }).then(
7784
+ * (result) => {
7785
+ * console.log(result);
7786
+ * }
7787
+ * );
7788
+ */
7789
+ function getBufferFromMbtiles(params) {
7790
+ return __awaiter(this, void 0, void 0, function () {
7791
+ var db, query;
7792
+ return __generator(this, function (_a) {
7793
+ switch (_a.label) {
7794
+ case 0: return [4 /*yield*/, getMbtilesDbHandler(params)];
7795
+ case 1:
7796
+ db = _a.sent();
7797
+ query = 'SELECT tile_data FROM tiles WHERE zoom_level = ' +
7798
+ params.z +
7799
+ ' AND tile_column = ' +
7800
+ params.x +
7801
+ ' AND tile_row = ' +
7802
+ (Math.pow(2, parseInt(params.z)) - parseInt(params.y) - 1);
7803
+ return [2 /*return*/, new Promise(function (resolve, reject) {
7804
+ try {
7805
+ // some of the logic here was heavily inspired by
7806
+ // https://github.com/IsraelHikingMap/Site/blob/6aa2ec0cfb8891fa048b1d9e2a4fc7d4cbcc8c97/IsraelHiking.Web/src/application/services/database.service.ts
7807
+ var result = db.exec(query);
7808
+ if (result.length !== 1) {
7809
+ reject(new Error('Tile not found.'));
7810
+ return;
7811
+ }
7812
+ var resultData = result[0].values[0][0];
7813
+ var binData = void 0;
7814
+ var isGzipped = resultData[0] === 0x1f && resultData[1] === 0x8b;
7815
+ if (isGzipped) {
7816
+ binData = pako__namespace.inflate(resultData);
7817
+ }
7818
+ else {
7819
+ binData = resultData;
7820
+ }
7821
+ if (binData === null || binData === void 0 ? void 0 : binData.buffer) {
7822
+ resolve(binData.buffer);
7823
+ }
7824
+ else {
7825
+ reject(new Error('Tile not found.'));
7826
+ return;
7827
+ }
7828
+ }
7829
+ catch (error) {
7830
+ reject(error);
7831
+ }
7832
+ })];
7833
+ }
7834
+ });
7835
+ });
7836
+ }
7837
+ /**
7838
+ * Expects a tile url in the following format:
7839
+ *
7840
+ * 'mbtiles://mbtiles/countries.mbtiles/{z}/{x}/{y}'
7841
+ */
7842
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
7843
+ var mbTilesProtocolHandler = function (params, callback) {
7844
+ var parsedParams = parseTileParams(params.url);
7845
+ getBufferFromMbtiles(parsedParams).then(function (result) {
7846
+ if (result) {
7847
+ callback(null, result, null, null);
7848
+ }
7849
+ else {
7850
+ callback(new Error('Tile not found'));
7851
+ }
7852
+ });
7853
+ return { cancel: function () { } };
7854
+ };
7855
+
7856
+ var handlers = {
7857
+ csv: CSVProtocolHandler,
7858
+ topojson: TopojsonProtocolHandler,
7859
+ osm: OSMProtocolHandler,
7860
+ gpx: XMLProtocolHandler,
7861
+ kml: XMLProtocolHandler,
7862
+ tcx: XMLProtocolHandler,
7863
+ mbtiles: mbTilesProtocolHandler
7864
+ };
7865
+ var types = ['fill', 'line', 'circle'];
7866
+ function ProtocolHandlerLayerForm(props) {
7867
+ var _a = React.useState({ type: 'circle' }), config = _a[0], setConfig = _a[1];
7868
+ var _b = React.useState(), fileName = _b[0], setFileName = _b[1];
7869
+ var _c = React.useState(), filePath = _c[0], setFilePath = _c[1];
7870
+ var _d = React.useState({}), optionsObject = _d[0], setOptionsObject = _d[1];
7871
+ var mapHook = useMap({ mapId: props.mapId });
7872
+ var optionsURL = '?' + new URLSearchParams(optionsObject).toString();
7873
+ useAddProtocol({
7874
+ protocol: props.originType,
7875
+ handler: handlers[props.originType],
7876
+ });
7877
+ var configIsValid = React.useMemo(function () {
7878
+ if (!(config === null || config === void 0 ? void 0 : config.type))
7879
+ return false;
7880
+ if (filePath && fileName)
7881
+ return true;
7882
+ else
7883
+ return false;
7884
+ }, [config, filePath, fileName]);
7885
+ React.useEffect(function () {
7886
+ var _a, _b;
7887
+ if (typeof fileName !== 'undefined' && typeof filePath !== 'undefined') {
7888
+ if (!((_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.getSource(fileName)))
7889
+ (_b = mapHook.map) === null || _b === void 0 ? void 0 : _b.addSource(fileName, {
7890
+ type: 'geojson',
7891
+ data: optionsObject ? props.originType + '://' + filePath + optionsURL : props.originType + '://' + filePath,
7892
+ });
7893
+ config.options = { source: fileName };
7894
+ }
7895
+ return function () { };
7896
+ }, [fileName, mapHook.map, filePath]);
7897
+ //the temporally storage adress of the uploaded file will be revoked, after source and layer are loaded in the map
7898
+ React.useEffect(function () {
7899
+ var _a;
7900
+ if (filePath && fileName && ((_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.getLayer(fileName))) {
7901
+ URL.revokeObjectURL(filePath);
7902
+ }
7903
+ }, [fileName, filePath, mapHook.map]);
7904
+ function addOption(newObject) {
7905
+ var newOptions = __assign(__assign({}, optionsObject), newObject);
7906
+ return setOptionsObject(newOptions);
7907
+ }
7908
+ return (React__default["default"].createElement(React__default["default"].Fragment, null,
7909
+ React__default["default"].createElement(material.DialogTitle, null,
7910
+ "Layer from ",
7911
+ props.originType),
7912
+ React__default["default"].createElement(material.FormControl, { fullWidth: true },
7913
+ React__default["default"].createElement(material.InputLabel, { id: "type-label" }, "Type"),
7914
+ React__default["default"].createElement(material.Select, { labelId: "type-label", value: config.type, label: "Type", onChange: function (ev) {
7915
+ return setConfig(function (current) { return (__assign(__assign({}, current), { type: ev.target.value })); });
7916
+ } }, types.map(function (type) { return (React__default["default"].createElement(material.MenuItem, { key: type, value: type }, type)); }))),
7917
+ React__default["default"].createElement(material.FormControl, { fullWidth: true },
7918
+ React__default["default"].createElement(material.Button, { variant: "contained", component: "label", sx: { marginTop: '10px' } },
7919
+ "Select origin file",
7920
+ React__default["default"].createElement("input", { type: "file", hidden: true, accept: props.originType, onChange: function (ev) {
7921
+ var _a, _b, _c;
7922
+ setFileName((_a = ev.target.files) === null || _a === void 0 ? void 0 : _a[0].name);
7923
+ if ((_b = ev.target.files) === null || _b === void 0 ? void 0 : _b[0]) {
7924
+ var dataUrl = URL.createObjectURL((_c = ev.target.files) === null || _c === void 0 ? void 0 : _c[0]);
7925
+ setFilePath(dataUrl);
7926
+ }
7927
+ } })),
7928
+ props.originType === 'csv' && React__default["default"].createElement(CSVOptionsFormular, { setter: addOption }),
7929
+ props.originType === 'osm' && React__default["default"].createElement(OsmOptionsFomular, { setter: addOption })),
7930
+ React__default["default"].createElement(material.DialogActions, null,
7931
+ React__default["default"].createElement(material.Button, { onClick: props.onCancel }, "Cancel"),
7932
+ React__default["default"].createElement(material.Button, { disabled: !configIsValid, onClick: function () { return props.onSubmit(config); } }, "Add"))));
7933
+ }
7934
+
7935
+ var LayerSpecificationKeys = [
7936
+ 'id',
7937
+ 'type',
7938
+ 'metadata',
7939
+ 'source',
7940
+ 'source-layer',
7941
+ 'layout',
7942
+ 'paint',
7943
+ 'options',
7944
+ ];
7945
+ function MbtilesLayerPropFormular(props) {
7946
+ var _a = React.useState([]), layers = _a[0], setLayers = _a[1];
7947
+ var newLayer = {};
7948
+ var toJSON = ['paint', 'layout', 'options', 'metadata'];
7949
+ React.useEffect(function () {
7950
+ props.setter(layers);
7951
+ }, [layers]);
7952
+ var TextFields = function () {
7953
+ return (React__default["default"].createElement(React__default["default"].Fragment, null, LayerSpecificationKeys.map(function (key) {
7954
+ return (React__default["default"].createElement(React__default["default"].Fragment, { key: key + '_fragment' },
7955
+ React__default["default"].createElement(material.TextField, { label: key, onChange: function (ev) {
7956
+ return newLayer[key] = ev.target.value;
7957
+ } })));
7958
+ })));
7959
+ };
7960
+ function addLayer() {
7961
+ toJSON.map(function (key) {
7962
+ if (typeof newLayer[key] !== 'undefined') {
7963
+ try {
7964
+ newLayer[key] = JSON.parse(newLayer[key]);
7965
+ }
7966
+ catch (_a) {
7967
+ alert("Invalid JSON format, try again");
7968
+ }
7969
+ }
7970
+ });
7971
+ setLayers(function (current) {
7972
+ if (current.length > 0) {
7973
+ return __spreadArray([newLayer], current, true);
7974
+ }
7975
+ else {
7976
+ return [newLayer];
7977
+ }
7978
+ });
7979
+ }
7980
+ return (React__default["default"].createElement(React__default["default"].Fragment, null,
7981
+ React__default["default"].createElement(material.FormControl, { fullWidth: true },
7982
+ React__default["default"].createElement(TextFields, null)),
7983
+ React__default["default"].createElement(material.Button, { onClick: addLayer }, " Add")));
7984
+ }
7985
+
7986
+ function MbtilesLayerForm(props) {
7987
+ var _a = React__default["default"].useState(props.config), config = _a[0], setConfig = _a[1];
7988
+ var _b = React.useState(), fileName = _b[0], setFileName = _b[1];
7989
+ var _c = React.useState(), filePath = _c[0], setFilePath = _c[1];
7990
+ var _d = React.useState([]), layers = _d[0], setLayers = _d[1];
7991
+ var mapHook = useMap({ mapId: props.mapId });
7992
+ var _e = React.useState(false), expanded = _e[0], setExpanded = _e[1];
7993
+ console.log(layers);
7994
+ var LayersToCall = function () {
7995
+ return (React__default["default"].createElement(React__default["default"].Fragment, null, layers.map(function (el, idx) { return (React__default["default"].createElement(material.Typography, { variant: "body2", key: idx },
7996
+ idx + 1,
7997
+ ": ",
7998
+ JSON.stringify(el))); })));
7999
+ };
8000
+ /**
8001
+ * A Vector Tile layer configuration with a mbtile Protocol url will passed to the onComplete function of the addLayerButton.
8002
+ * In order to visdualize the file content, a mbtiles ProtocolHandler must be added to the map Instanz.
8003
+ * See the MapComponents AddLayerButton demo and the documentation of useAddProtocolHook to find out more about Protocol handlers.
8004
+ */
8005
+ /* example values:
8006
+ * id: 'countries',
8007
+ * type: 'fill',
8008
+ * 'source-layer': 'countries',
8009
+ * layout: {},
8010
+ * paint: { "fill-color": "#f9a5f5", "fill-opacity": 0.5 },
8011
+ */
8012
+ var configIsValid = React.useMemo(function () {
8013
+ if (!fileName)
8014
+ return false;
8015
+ return true;
8016
+ }, [fileName]);
8017
+ React.useEffect(function () {
8018
+ if (typeof fileName !== 'undefined' && typeof filePath !== 'undefined') {
8019
+ setConfig({
8020
+ url: 'mbtiles://' + filePath + '/{z}/{x}/{y}',
8021
+ layers: layers,
8022
+ layerId: fileName,
8023
+ sourceOptions: {
8024
+ type: 'vector',
8025
+ minzoom: 0,
8026
+ maxzoom: 1,
8027
+ },
8028
+ });
8029
+ }
8030
+ }, [fileName, mapHook.map, filePath, layers]);
8031
+ return (React__default["default"].createElement(React__default["default"].Fragment, null,
8032
+ React__default["default"].createElement(material.DialogTitle, null, " Layer from mbtiles file"),
8033
+ React__default["default"].createElement(material.DialogContent, null,
8034
+ React__default["default"].createElement(material.Button, { variant: "contained", component: "label", sx: { marginTop: '10px' } },
8035
+ "Select origin file",
8036
+ React__default["default"].createElement("input", { type: "file", hidden: true, accept: props.originType, onChange: function (ev) {
8037
+ var _a, _b, _c;
8038
+ setFileName((_a = ev.target.files) === null || _a === void 0 ? void 0 : _a[0].name);
8039
+ if ((_b = ev.target.files) === null || _b === void 0 ? void 0 : _b[0]) {
8040
+ var dataUrl = URL.createObjectURL((_c = ev.target.files) === null || _c === void 0 ? void 0 : _c[0]);
8041
+ setFilePath(dataUrl);
8042
+ }
8043
+ } })),
8044
+ React__default["default"].createElement(material.Accordion, { expanded: expanded },
8045
+ React__default["default"].createElement(material.AccordionSummary, { "aria-controls": "panel1d-content", id: "panel1d-header" },
8046
+ React__default["default"].createElement(material.Typography, null, "MB-Tile Layer properties"),
8047
+ React__default["default"].createElement(material.Button, { onClick: function () { return setExpanded(!expanded); } }, expanded ? React__default["default"].createElement(KeyboardArrowUpIcon__default["default"], null) : React__default["default"].createElement(KeyboardArrowDownIcon__default["default"], null))),
8048
+ React__default["default"].createElement(material.Typography, { variant: "body1" }, " Layers"),
8049
+ layers.length > 0 ? React__default["default"].createElement(LayersToCall, null) : React__default["default"].createElement(material.Typography, { variant: "body2" }, " 0 "),
8050
+ React__default["default"].createElement(MbtilesLayerPropFormular, { setter: setLayers }))),
8051
+ React__default["default"].createElement(material.DialogActions, null,
8052
+ React__default["default"].createElement(material.Button, { onClick: props.onCancel }, "Cancel"),
8053
+ React__default["default"].createElement(material.Button, { disabled: !configIsValid, onClick: function () { return props.onSubmit(config); } }, "Add"))));
8054
+ }
8055
+
7317
8056
  var AddLayerPopup = function (props) {
7318
8057
  var _a = React.useState(props === null || props === void 0 ? void 0 : props.config), layerConfig = _a[0], setLayerConfig = _a[1];
8058
+ var _b = React.useState(), originType = _b[0], setOriginType = _b[1];
8059
+ var layerTypes = props.layerTypes;
8060
+ var supportedProtocols = layerTypes.filter(function (el) { return el !== 'wms' && el !== 'geojson' && el !== 'mbtiles'; });
7319
8061
  var updateLayerType = function (type) {
7320
- setLayerConfig({ type: type, config: {} });
8062
+ setOriginType(type);
8063
+ if (supportedProtocols.includes(type)) {
8064
+ setLayerConfig({ type: 'geojson', config: {} });
8065
+ }
8066
+ else if (type === 'mbtiles') {
8067
+ setLayerConfig({ type: 'vt', config: { layers: [] } });
8068
+ }
8069
+ else {
8070
+ setLayerConfig({ type: type, config: {} });
8071
+ }
7321
8072
  };
7322
8073
  var handleCancel = function () {
7323
8074
  props.setOpen(false);
7324
8075
  setLayerConfig(undefined);
7325
8076
  };
8077
+ var ProtocolTypeFormulars = function () {
8078
+ return (React__default["default"].createElement(React__default["default"].Fragment, null, supportedProtocols.map(function (el, idx) {
8079
+ return (React__default["default"].createElement(React__default["default"].Fragment, null, (layerConfig === null || layerConfig === void 0 ? void 0 : layerConfig.type) === 'geojson' && originType === el && (React__default["default"].createElement(ProtocolHandlerLayerForm, { key: idx, originType: el, onSubmit: function (config) {
8080
+ var _a;
8081
+ (_a = props === null || props === void 0 ? void 0 : props.onComplete) === null || _a === void 0 ? void 0 : _a.call(props, __assign(__assign({}, layerConfig), { config: config, type: 'geojson' }));
8082
+ handleCancel();
8083
+ }, onCancel: handleCancel }))));
8084
+ })));
8085
+ };
7326
8086
  return (React__default["default"].createElement(material.Dialog, { open: props.open, onClose: handleCancel, PaperProps: { sx: { padding: '20px' } } },
7327
- !(layerConfig === null || layerConfig === void 0 ? void 0 : layerConfig.type) && React__default["default"].createElement(LayerTypeForm, { onSelect: updateLayerType }),
7328
- (layerConfig === null || layerConfig === void 0 ? void 0 : layerConfig.type) === 'geojson' && (React__default["default"].createElement(GeoJsonLayerForm, { onSubmit: function (config) {
8087
+ !(layerConfig === null || layerConfig === void 0 ? void 0 : layerConfig.type) && React__default["default"].createElement(LayerTypeForm, { onSelect: updateLayerType, layerTypes: layerTypes }),
8088
+ (layerConfig === null || layerConfig === void 0 ? void 0 : layerConfig.type) === 'geojson' && originType === 'geojson' && (React__default["default"].createElement(GeoJsonLayerForm, { onSubmit: function (config) {
7329
8089
  var _a;
7330
8090
  (_a = props === null || props === void 0 ? void 0 : props.onComplete) === null || _a === void 0 ? void 0 : _a.call(props, __assign(__assign({}, layerConfig), { config: config }));
7331
8091
  handleCancel();
@@ -7334,16 +8094,29 @@ var AddLayerPopup = function (props) {
7334
8094
  var _a;
7335
8095
  (_a = props === null || props === void 0 ? void 0 : props.onComplete) === null || _a === void 0 ? void 0 : _a.call(props, __assign(__assign({}, layerConfig), { config: config }));
7336
8096
  handleCancel();
7337
- }, onCancel: handleCancel }))));
8097
+ }, onCancel: handleCancel })),
8098
+ (layerConfig === null || layerConfig === void 0 ? void 0 : layerConfig.type) === 'vt' && originType !== undefined && (React__default["default"].createElement(MbtilesLayerForm, { config: layerConfig, originType: originType, onSubmit: function (config) {
8099
+ var _a;
8100
+ if (layerConfig) {
8101
+ (_a = props === null || props === void 0 ? void 0 : props.onComplete) === null || _a === void 0 ? void 0 : _a.call(props, __assign(__assign({}, layerConfig), { config: config }));
8102
+ handleCancel();
8103
+ }
8104
+ }, onCancel: handleCancel })),
8105
+ React__default["default"].createElement(ProtocolTypeFormulars, { key: 'protocol' })));
7338
8106
  };
7339
8107
  AddLayerPopup.defaultProps = {};
7340
8108
 
7341
8109
  var AddLayerButton = function (props) {
7342
8110
  var _a = React__default["default"].useState(false), popupOpen = _a[0], setPopupOpen = _a[1];
8111
+ var layerTypes = props.layerTypes || ['geojson', 'wms', 'mbtiles', 'csv', 'topojson', 'osm', 'gpx', 'kml', 'tcx'];
8112
+ layerTypes.includes('mbtiles') && useAddProtocol({
8113
+ protocol: 'mbtiles',
8114
+ handler: mbTilesProtocolHandler,
8115
+ });
7343
8116
  return (React__default["default"].createElement(React__default["default"].Fragment, null,
7344
8117
  React__default["default"].createElement(material.Button, { variant: "contained", sx: __assign({ marginTop: '10px' }, props.sx), onClick: function () { return setPopupOpen(true); } },
7345
8118
  React__default["default"].createElement(PlaylistAddIcon__default["default"], null)),
7346
- React__default["default"].createElement(AddLayerPopup, { open: popupOpen, setOpen: setPopupOpen, onComplete: props === null || props === void 0 ? void 0 : props.onComplete })));
8119
+ React__default["default"].createElement(AddLayerPopup, { open: popupOpen, setOpen: setPopupOpen, onComplete: props === null || props === void 0 ? void 0 : props.onComplete, layerTypes: layerTypes })));
7347
8120
  };
7348
8121
  AddLayerButton.defaultProps = {};
7349
8122