@nextgis/leaflet-map-adapter 1.8.1 → 1.9.0

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.
package/lib/index.d.ts CHANGED
@@ -20,6 +20,7 @@ import { Map as Map_2 } from 'leaflet';
20
20
  import type { MapAdapter } from '@nextgis/webmap';
21
21
  import type { MapControl } from '@nextgis/webmap';
22
22
  import type { MapOptions } from '@nextgis/webmap';
23
+ import type { ViewOptions } from '@nextgis/webmap';
23
24
 
24
25
  declare class AttributionControl extends Control.Attribution {
25
26
  constructor(options: AttributionControlOptions & ControlOptions);
@@ -52,12 +53,13 @@ declare class LeafletMapAdapter implements MapAdapter<Map_2, any, Control> {
52
53
  getControlContainer(): HTMLElement;
53
54
  setCursor(cursor: string): void;
54
55
  setView(lngLat: LngLatArray, zoom?: number): void;
56
+ setView(options: ViewOptions): void;
55
57
  setCenter(lngLat: LngLatArray): void;
56
58
  getCenter(): LngLatArray | undefined;
57
59
  setZoom(zoom: number): void;
58
60
  getZoom(): number | undefined;
59
61
  getBounds(): LngLatBoundsArray | undefined;
60
- fitBounds(e: LngLatBoundsArray, options?: FitOptions): void;
62
+ fitBounds(bounds: LngLatBoundsArray, options?: FitOptions): void;
61
63
  getLayerAdapter(name: string): Type<LayerAdapter<L.Map, any, any>>;
62
64
  createControl(control: MapControl, options: CreateControlOptions): L.Control;
63
65
  createButtonControl(options: ButtonControlOptions): L.Control;
@@ -1,4 +1,4 @@
1
- /** Bundle of @nextgis/leaflet-map-adapter; version: 1.8.1; author: NextGIS */
1
+ /** Bundle of @nextgis/leaflet-map-adapter; version: 1.9.0; author: NextGIS */
2
2
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
3
3
 
4
4
  var events = {exports: {}};
@@ -15038,7 +15038,9 @@ class GeoJsonAdapter extends BaseAdapter {
15038
15038
  getBounds() {
15039
15039
  if (this.layer) {
15040
15040
  const bounds = this.layer.getBounds();
15041
- return boundsToArray(bounds);
15041
+ if (bounds.isValid()) {
15042
+ return boundsToArray(bounds);
15043
+ }
15042
15044
  }
15043
15045
  }
15044
15046
  setOpacity(value) {
@@ -15085,7 +15087,7 @@ class GeoJsonAdapter extends BaseAdapter {
15085
15087
  }
15086
15088
  }
15087
15089
  }
15088
- async _openPopup(def, options = {}, type, latlng) {
15090
+ async _openPopup(def, options = {}, type, latLng) {
15089
15091
  var _a, _b;
15090
15092
  const { feature, layer } = def;
15091
15093
  const { minWidth, autoPan, maxWidth, closeButton: closeButton_, } = { minWidth: 300, ...options };
@@ -15100,8 +15102,9 @@ class GeoJsonAdapter extends BaseAdapter {
15100
15102
  this._removePopup(popup);
15101
15103
  }
15102
15104
  };
15103
- const content = options.createPopupContent
15104
- ? await options.createPopupContent({
15105
+ let content;
15106
+ if (options.createPopupContent) {
15107
+ content = await options.createPopupContent({
15105
15108
  layer,
15106
15109
  feature,
15107
15110
  target: this,
@@ -15109,17 +15112,24 @@ class GeoJsonAdapter extends BaseAdapter {
15109
15112
  close,
15110
15113
  onClose,
15111
15114
  ...createFeaturePositionOptions(feature),
15112
- })
15113
- : options.popupContent;
15115
+ });
15116
+ }
15117
+ else {
15118
+ content = options.popupContent;
15119
+ }
15114
15120
  if (content && layer) {
15115
- popup = layer.bindPopup(content, {
15121
+ const popupOptions = {
15116
15122
  minWidth,
15117
15123
  autoPan,
15118
- maxWidth,
15124
+ // maxWidth,
15119
15125
  closeButton,
15120
15126
  closeOnClick: false,
15121
15127
  autoClose: false,
15122
- });
15128
+ };
15129
+ if (defined(maxWidth)) {
15130
+ popupOptions.maxWidth = maxWidth;
15131
+ }
15132
+ popup = layer.bindPopup(content, popupOptions);
15123
15133
  const unselectOnClose = (_b = (_a = this.options.popupOptions) === null || _a === void 0 ? void 0 : _a.unselectOnClose) !== null && _b !== void 0 ? _b : true;
15124
15134
  if (unselectOnClose) {
15125
15135
  const p = layer.getPopup();
@@ -15129,9 +15139,7 @@ class GeoJsonAdapter extends BaseAdapter {
15129
15139
  }, this);
15130
15140
  }
15131
15141
  this._openedPopup.push([popup, _closeHandlers, def]);
15132
- setTimeout(() => {
15133
- popup.openPopup(latlng);
15134
- }, 0);
15142
+ popup.openPopup(latLng);
15135
15143
  }
15136
15144
  }
15137
15145
  _removePopup(popup) {
@@ -15293,15 +15301,19 @@ class GeoJsonAdapter extends BaseAdapter {
15293
15301
  }
15294
15302
  }
15295
15303
  this._handleMouseEvents(layer);
15296
- if (popup) {
15297
- this._openPopup({
15298
- target: this,
15299
- layer,
15300
- feature,
15301
- ...createFeaturePositionOptions(feature),
15302
- }, popupOptions, 'api');
15303
- }
15304
- this._updateTooltip({ layer, feature });
15304
+ // The timeout is needed to display the popup immediately when adding a layer to the map.
15305
+ // Without a timeout, the layer may not yet have a _map object
15306
+ setTimeout(() => {
15307
+ if (popup) {
15308
+ this._openPopup({
15309
+ target: this,
15310
+ layer,
15311
+ feature,
15312
+ ...createFeaturePositionOptions(feature),
15313
+ }, popupOptions, 'api');
15314
+ }
15315
+ this._updateTooltip({ layer, feature });
15316
+ });
15305
15317
  }
15306
15318
  }
15307
15319
  }
@@ -15877,6 +15889,15 @@ class OsmAdapter extends TileAdapter {
15877
15889
  }
15878
15890
  }
15879
15891
 
15892
+ function arrayToBoundsExpression(bounds) {
15893
+ const e = bounds;
15894
+ // top, left, bottom, right
15895
+ return [
15896
+ [e[3], e[0]],
15897
+ [e[1], e[2]],
15898
+ ];
15899
+ }
15900
+
15880
15901
  class LeafletMapAdapter$1 {
15881
15902
  constructor() {
15882
15903
  this.options = { target: 'map' };
@@ -15900,10 +15921,7 @@ class LeafletMapAdapter$1 {
15900
15921
  this.map = new leafletSrc.exports.Map(this.options.target, {
15901
15922
  attributionControl: false,
15902
15923
  zoomControl: false,
15903
- maxBounds: mb && [
15904
- [mb[1], mb[0]],
15905
- [mb[3], mb[2]],
15906
- ],
15924
+ maxBounds: mb ? arrayToBoundsExpression(mb) : undefined,
15907
15925
  maxZoom,
15908
15926
  minZoom,
15909
15927
  center: center && [center[1], center[0]],
@@ -15940,14 +15958,52 @@ class LeafletMapAdapter$1 {
15940
15958
  this.map.getContainer().style.cursor = cursor;
15941
15959
  }
15942
15960
  }
15943
- setView(lngLat, zoom) {
15944
- const [lng, lat] = lngLat;
15945
- if (this.map) {
15946
- if (typeof zoom === 'number') {
15947
- this.map.setView([lat, lng], zoom, { animate: false });
15961
+ setView(lngLatOrOpt, zoom) {
15962
+ const map = this.map;
15963
+ if (!map)
15964
+ return;
15965
+ if (Array.isArray(lngLatOrOpt)) {
15966
+ const lngLat = lngLatOrOpt;
15967
+ const [lng, lat] = lngLat;
15968
+ if (this.map) {
15969
+ if (typeof zoom === 'number') {
15970
+ this.map.setView([lat, lng], zoom, { animate: false });
15971
+ }
15972
+ else {
15973
+ this.setCenter([lng, lat]);
15974
+ }
15975
+ }
15976
+ }
15977
+ else {
15978
+ const { zoom, center, maxBounds, bounds, minZoom, maxZoom } = lngLatOrOpt;
15979
+ if (center && zoom !== undefined) {
15980
+ this.setView(center, zoom);
15948
15981
  }
15949
15982
  else {
15950
- this.setCenter([lng, lat]);
15983
+ if (zoom !== undefined) {
15984
+ this.setZoom(zoom);
15985
+ }
15986
+ if (center) {
15987
+ this.setCenter(center);
15988
+ }
15989
+ }
15990
+ if (bounds) {
15991
+ this.fitBounds(bounds);
15992
+ }
15993
+ if (maxBounds !== undefined) {
15994
+ if (maxBounds) {
15995
+ map.setMaxBounds(arrayToBoundsExpression(maxBounds));
15996
+ }
15997
+ else {
15998
+ // @ts-ignore `null` works for unset maxBounds, but not in typing
15999
+ map.setMaxBounds(null);
16000
+ }
16001
+ }
16002
+ if (maxZoom !== undefined) {
16003
+ map.setMaxZoom(maxZoom);
16004
+ }
16005
+ if (minZoom !== undefined) {
16006
+ map.setMinZoom(minZoom);
15951
16007
  }
15952
16008
  }
15953
16009
  }
@@ -15982,7 +16038,7 @@ class LeafletMapAdapter$1 {
15982
16038
  return [sw.lng, sw.lat, ne.lng, ne.lat];
15983
16039
  }
15984
16040
  // [west, south, east, north]
15985
- fitBounds(e, options = {}) {
16041
+ fitBounds(bounds, options = {}) {
15986
16042
  if (this.map) {
15987
16043
  const { maxZoom, offset, padding } = options;
15988
16044
  const opt = {};
@@ -15995,11 +16051,7 @@ class LeafletMapAdapter$1 {
15995
16051
  if (offset) {
15996
16052
  opt.padding = offset;
15997
16053
  }
15998
- // top, left, bottom, right
15999
- this.map.fitBounds([
16000
- [e[3], e[0]],
16001
- [e[1], e[2]],
16002
- ], opt);
16054
+ this.map.fitBounds(arrayToBoundsExpression(bounds), opt);
16003
16055
  }
16004
16056
  }
16005
16057
  getLayerAdapter(name) {