@nextgis/leaflet-map-adapter 3.1.0 → 4.0.0-alpha.1

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.
@@ -1,8 +1,10 @@
1
- /** Bundle of @nextgis/leaflet-map-adapter; version: 3.1.0; author: NextGIS */
1
+ /** Bundle of @nextgis/leaflet-map-adapter; version: 4.0.0-alpha.1; author: NextGIS */
2
+ import { LeafletControlContainer, createControl, createButtonControl, createToggleControl } from '@nextgis/leaflet-control-container';
2
3
  import { EventEmitter } from 'events';
3
- import { Control, DomEvent, GeoJSON, DivIcon, Marker, CircleMarker, FeatureGroup, ImageOverlay as ImageOverlay$1, Layer, Util, GridLayer, DomUtil, TileLayer as TileLayer$1, Map, latLngBounds, latLng } from 'leaflet';
4
+ import { Control, GeoJSON, DomEvent, DivIcon, Marker, CircleMarker, FeatureGroup, ImageOverlay as ImageOverlay$1, Layer, Util, GridLayer, DomUtil, TileLayer as TileLayer$1, Map, latLngBounds, latLng } from 'leaflet';
4
5
  import { defined, debounce as debounce$1, isObject, updateUrlParams } from '@nextgis/utils';
5
6
  import { isPaintCallback, isPaint } from '@nextgis/paint';
7
+ import { getLayerRequestOptions } from '@nextgis/webmap';
6
8
  import Queue from '@nextgis/queue';
7
9
 
8
10
  class AttributionControl extends Control.Attribution {
@@ -22,81 +24,6 @@ class AttributionControl extends Control.Attribution {
22
24
  }
23
25
  }
24
26
 
25
- function createControl(control, options = {}, map) {
26
- const C = Control.extend({
27
- onAdd() {
28
- const element = document.createElement("div");
29
- const content = control.onAdd(map);
30
- element.classList.add("leaflet-control");
31
- if (options.bar) {
32
- element.classList.add("leaflet-bar");
33
- }
34
- if (options.addClass) {
35
- element.classList.add(options.addClass);
36
- }
37
- if (!options.margin && !options.bar) {
38
- element.style.marginBottom = "0px";
39
- element.style.marginTop = "0px";
40
- element.style.marginLeft = "0px";
41
- element.style.marginRight = "0px";
42
- }
43
- if (content) {
44
- element.appendChild(content);
45
- }
46
- DomEvent.disableClickPropagation(element);
47
- return element;
48
- },
49
- onRemove() {
50
- control.onRemove();
51
- }
52
- });
53
- return new C();
54
- }
55
-
56
- function createButtonControl(options, map) {
57
- const link = document.createElement("a");
58
- const title = options.title || "";
59
- link.href = "#";
60
- link.title = title;
61
- link.setAttribute("role", "button");
62
- link.setAttribute("aria-label", title);
63
- if (options.addClass) {
64
- link.classList.add(options.addClass);
65
- }
66
- DomEvent.disableClickPropagation(link);
67
- DomEvent.on(link, "click", DomEvent.stop);
68
- if (options.html instanceof HTMLElement) {
69
- link.appendChild(options.html);
70
- } else if (typeof options.html === "string") {
71
- link.innerHTML = options.html;
72
- }
73
- const onClick = (e) => {
74
- e.stopPropagation();
75
- options.onClick();
76
- };
77
- return createControl(
78
- {
79
- onAdd() {
80
- if (options.onClick !== void 0) {
81
- link.addEventListener("click", onClick);
82
- }
83
- return link;
84
- },
85
- onRemove() {
86
- const parent = link.parentNode;
87
- if (parent) {
88
- parent.removeChild(link);
89
- }
90
- if (options.onClick !== void 0) {
91
- link.removeEventListener("click", onClick);
92
- }
93
- }
94
- },
95
- { bar: true },
96
- map
97
- );
98
- }
99
-
100
27
  function boundsToArray(bounds) {
101
28
  const ne = bounds.getNorthEast();
102
29
  const sw = bounds.getSouthWest();
@@ -1091,34 +1018,38 @@ class GeoJsonAdapter extends BaseAdapter {
1091
1018
  function callAjax({
1092
1019
  src,
1093
1020
  headers,
1094
- withCredentials
1021
+ withCredentials,
1022
+ request
1095
1023
  }) {
1096
- let abortFunc = () => {
1097
- };
1098
- const promise = new Promise((resolve, reject) => {
1099
- const xhr = new XMLHttpRequest();
1100
- xhr.open("GET", src);
1101
- xhr.responseType = "arraybuffer";
1102
- for (const h in headers) {
1103
- xhr.setRequestHeader(h, headers[h]);
1104
- }
1105
- if (withCredentials) {
1106
- xhr.withCredentials = withCredentials;
1107
- }
1108
- xhr.onload = function() {
1109
- const arrayBufferView = new Uint8Array(this.response);
1110
- const blob = new Blob([arrayBufferView], { type: "image/png" });
1111
- const urlCreator = window.URL || window.webkitURL;
1112
- const imageUrl = urlCreator.createObjectURL(blob);
1113
- resolve(imageUrl);
1114
- };
1115
- xhr.onerror = reject;
1116
- xhr.send();
1117
- abortFunc = () => {
1118
- xhr.abort();
1119
- resolve("");
1120
- };
1024
+ const layerRequest = getLayerRequestOptions({
1025
+ headers,
1026
+ request,
1027
+ withCredentials
1028
+ });
1029
+ const requestHeaders = (layerRequest == null ? void 0 : layerRequest.headers) || {};
1030
+ const controller = new AbortController();
1031
+ const promise = fetch(src, {
1032
+ cache: layerRequest == null ? void 0 : layerRequest.cache,
1033
+ credentials: layerRequest == null ? void 0 : layerRequest.credentials,
1034
+ headers: requestHeaders,
1035
+ signal: controller.signal
1036
+ }).then((response) => {
1037
+ if (!response.ok) {
1038
+ throw new Error(response.statusText);
1039
+ }
1040
+ return response.blob();
1041
+ }).then((blob) => {
1042
+ const urlCreator = window.URL || window.webkitURL;
1043
+ return urlCreator.createObjectURL(blob);
1044
+ }).catch((er) => {
1045
+ if (er.name === "AbortError") {
1046
+ return "";
1047
+ }
1048
+ throw er;
1121
1049
  });
1050
+ const abortFunc = () => {
1051
+ controller.abort();
1052
+ };
1122
1053
  return [promise, abortFunc];
1123
1054
  }
1124
1055
 
@@ -1140,7 +1071,7 @@ class ImageOverlay extends ImageOverlay$1 {
1140
1071
  }
1141
1072
  _initImage() {
1142
1073
  super._initImage();
1143
- const { headers, withCredentials } = this.options;
1074
+ const { headers, request, withCredentials } = this.options;
1144
1075
  const img = this._image;
1145
1076
  const src = img.src;
1146
1077
  img.src = "";
@@ -1148,6 +1079,7 @@ class ImageOverlay extends ImageOverlay$1 {
1148
1079
  const [promise, abortFunc] = callAjax({
1149
1080
  src,
1150
1081
  headers,
1082
+ request,
1151
1083
  withCredentials
1152
1084
  });
1153
1085
  promise.then((imgUrl) => {
@@ -1256,6 +1188,7 @@ class ImageLayer extends Layer {
1256
1188
  opacity: 0,
1257
1189
  pane: this.options.pane,
1258
1190
  headers: this.options.headers,
1191
+ request: this.options.request,
1259
1192
  withCredentials: this.options.withCredentials
1260
1193
  });
1261
1194
  overlay.addTo(this._map);
@@ -1380,15 +1313,17 @@ class ImageAdapter extends BaseAdapter {
1380
1313
  const url = options && options.url;
1381
1314
  if (url) {
1382
1315
  const updateWmsParamsFromOpt = options.updateWmsParams;
1383
- this.layer = new ImageLayer(url, __spreadValues$4(__spreadValues$4(__spreadValues$4({
1316
+ const request = getLayerRequestOptions(options);
1317
+ this.layer = new ImageLayer(url, __spreadProps$3(__spreadValues$4(__spreadValues$4(__spreadValues$4({
1384
1318
  pane: this.pane,
1385
- headers: options.headers,
1386
1319
  setViewDelay: options.setViewDelay
1387
1320
  }, __spreadProps$3(__spreadValues$4({
1388
1321
  transparent: true
1389
1322
  }, options), {
1390
1323
  opacity: (_a = options.opacity) != null ? _a : void 0
1391
- })), options.params), options.nativeOptions));
1324
+ })), options.params), options.nativeOptions), {
1325
+ request
1326
+ }));
1392
1327
  if (updateWmsParamsFromOpt) {
1393
1328
  const updateWmsParams = this.layer.updateWmsParams;
1394
1329
  this.layer.updateWmsParams = function(map) {
@@ -1443,7 +1378,8 @@ function makeRemote(Base) {
1443
1378
  const [promise, abortFunc] = callAjax({
1444
1379
  src,
1445
1380
  withCredentials: this.options.withCredentials,
1446
- headers: this.options.headers
1381
+ headers: this.options.headers,
1382
+ request: this.options.request
1447
1383
  });
1448
1384
  promise.then((response) => {
1449
1385
  tile.src = response;
@@ -1534,10 +1470,10 @@ class TileAdapter extends BaseAdapter {
1534
1470
  tileLayerOptions.subdomains = opt.subdomains;
1535
1471
  }
1536
1472
  let layer;
1537
- if (opt.headers || opt.withCredentials) {
1473
+ const request = getLayerRequestOptions(opt);
1474
+ if (request) {
1538
1475
  layer = new TileLayer(url, __spreadProps$2(__spreadValues$3({}, tileLayerOptions), {
1539
- headers: opt.headers,
1540
- withCredentials: opt.withCredentials,
1476
+ request,
1541
1477
  setViewDelay: opt.setViewDelay
1542
1478
  }));
1543
1479
  } else {
@@ -1642,10 +1578,10 @@ class WmsAdapter extends BaseAdapter {
1642
1578
  transparent: true
1643
1579
  }, options.nativeOptions);
1644
1580
  let layer;
1645
- if (opt.headers || opt.withCredentials) {
1581
+ const request = getLayerRequestOptions(opt);
1582
+ if (request) {
1646
1583
  layer = new WmsLayer(url, __spreadProps(__spreadValues$1({}, layerOptions), {
1647
- headers: opt.headers,
1648
- withCredentials: opt.withCredentials
1584
+ request
1649
1585
  }));
1650
1586
  } else {
1651
1587
  layer = new TileLayer$1.WMS(url, layerOptions);
@@ -1688,6 +1624,7 @@ const _LeafletMapAdapter = class _LeafletMapAdapter {
1688
1624
  __publicField(this, "emitter", new EventEmitter());
1689
1625
  __publicField(this, "map");
1690
1626
  __publicField(this, "_resizeObserver");
1627
+ __publicField(this, "_controlContainer");
1691
1628
  __publicField(this, "_unselectCb", []);
1692
1629
  __publicField(this, "_universalEvents", [
1693
1630
  "move",
@@ -1726,6 +1663,7 @@ const _LeafletMapAdapter = class _LeafletMapAdapter {
1726
1663
  this._updateMinZoomForBounds(minZoom);
1727
1664
  this._watchSizeChangeToUpdateMinZoom();
1728
1665
  const defPane = this.map.createPane("order-0");
1666
+ this._controlContainer = new LeafletControlContainer(this.map);
1729
1667
  this.map._addUnselectCb = (def) => {
1730
1668
  this._addUnselectCb(def);
1731
1669
  };
@@ -1738,12 +1676,19 @@ const _LeafletMapAdapter = class _LeafletMapAdapter {
1738
1676
  if (this.map) {
1739
1677
  this.map.remove();
1740
1678
  }
1679
+ if (this._controlContainer) {
1680
+ this._controlContainer.detach();
1681
+ this._controlContainer = void 0;
1682
+ }
1741
1683
  this._stopWatchSizeChangeToUpdateMinZoom();
1742
1684
  }
1743
1685
  getContainer() {
1744
1686
  return this.map && this.map.getContainer();
1745
1687
  }
1746
1688
  getControlContainer() {
1689
+ if (this._controlContainer) {
1690
+ return this._controlContainer.getContainer();
1691
+ }
1747
1692
  const controlContainer = this.map && this.map._controlContainer;
1748
1693
  if (controlContainer) {
1749
1694
  return controlContainer;
@@ -1857,16 +1802,17 @@ const _LeafletMapAdapter = class _LeafletMapAdapter {
1857
1802
  createButtonControl(options) {
1858
1803
  return createButtonControl(options, this);
1859
1804
  }
1860
- addControl(control, position) {
1861
- control.options.position = position.replace("-", "");
1862
- if (this.map) {
1863
- this.map.addControl(control);
1864
- return control;
1805
+ createToggleControl(options) {
1806
+ return createToggleControl(options, this);
1807
+ }
1808
+ addControl(control, position, options) {
1809
+ if (this._controlContainer) {
1810
+ return this._controlContainer.addControl(control, position, options);
1865
1811
  }
1866
1812
  }
1867
1813
  removeControl(control) {
1868
- if (this.map) {
1869
- this.map.removeControl(control);
1814
+ if (this._controlContainer) {
1815
+ this._controlContainer.removeControl(control);
1870
1816
  }
1871
1817
  }
1872
1818
  removeLayer(layer) {