@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.
@@ -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
  import { EventEmitter } from 'events';
3
3
  import { Control, DomEvent, GeoJSON, FeatureGroup, DivIcon, Marker, CircleMarker, ImageOverlay as ImageOverlay$1, Layer, Util, CRS, GridLayer, DomUtil, TileLayer as TileLayer$1, Map } from 'leaflet';
4
4
  import { debounce as debounce$1, defined } from '@nextgis/utils';
@@ -450,7 +450,9 @@ class GeoJsonAdapter extends BaseAdapter {
450
450
  getBounds() {
451
451
  if (this.layer) {
452
452
  const bounds = this.layer.getBounds();
453
- return boundsToArray(bounds);
453
+ if (bounds.isValid()) {
454
+ return boundsToArray(bounds);
455
+ }
454
456
  }
455
457
  }
456
458
  setOpacity(value) {
@@ -497,7 +499,7 @@ class GeoJsonAdapter extends BaseAdapter {
497
499
  }
498
500
  }
499
501
  }
500
- async _openPopup(def, options = {}, type, latlng) {
502
+ async _openPopup(def, options = {}, type, latLng) {
501
503
  var _a, _b;
502
504
  const { feature, layer } = def;
503
505
  const { minWidth, autoPan, maxWidth, closeButton: closeButton_, } = { minWidth: 300, ...options };
@@ -512,8 +514,9 @@ class GeoJsonAdapter extends BaseAdapter {
512
514
  this._removePopup(popup);
513
515
  }
514
516
  };
515
- const content = options.createPopupContent
516
- ? await options.createPopupContent({
517
+ let content;
518
+ if (options.createPopupContent) {
519
+ content = await options.createPopupContent({
517
520
  layer,
518
521
  feature,
519
522
  target: this,
@@ -521,17 +524,24 @@ class GeoJsonAdapter extends BaseAdapter {
521
524
  close,
522
525
  onClose,
523
526
  ...createFeaturePositionOptions(feature),
524
- })
525
- : options.popupContent;
527
+ });
528
+ }
529
+ else {
530
+ content = options.popupContent;
531
+ }
526
532
  if (content && layer) {
527
- popup = layer.bindPopup(content, {
533
+ const popupOptions = {
528
534
  minWidth,
529
535
  autoPan,
530
- maxWidth,
536
+ // maxWidth,
531
537
  closeButton,
532
538
  closeOnClick: false,
533
539
  autoClose: false,
534
- });
540
+ };
541
+ if (defined(maxWidth)) {
542
+ popupOptions.maxWidth = maxWidth;
543
+ }
544
+ popup = layer.bindPopup(content, popupOptions);
535
545
  const unselectOnClose = (_b = (_a = this.options.popupOptions) === null || _a === void 0 ? void 0 : _a.unselectOnClose) !== null && _b !== void 0 ? _b : true;
536
546
  if (unselectOnClose) {
537
547
  const p = layer.getPopup();
@@ -541,9 +551,7 @@ class GeoJsonAdapter extends BaseAdapter {
541
551
  }, this);
542
552
  }
543
553
  this._openedPopup.push([popup, _closeHandlers, def]);
544
- setTimeout(() => {
545
- popup.openPopup(latlng);
546
- }, 0);
554
+ popup.openPopup(latLng);
547
555
  }
548
556
  }
549
557
  _removePopup(popup) {
@@ -705,15 +713,19 @@ class GeoJsonAdapter extends BaseAdapter {
705
713
  }
706
714
  }
707
715
  this._handleMouseEvents(layer);
708
- if (popup) {
709
- this._openPopup({
710
- target: this,
711
- layer,
712
- feature,
713
- ...createFeaturePositionOptions(feature),
714
- }, popupOptions, 'api');
715
- }
716
- this._updateTooltip({ layer, feature });
716
+ // The timeout is needed to display the popup immediately when adding a layer to the map.
717
+ // Without a timeout, the layer may not yet have a _map object
718
+ setTimeout(() => {
719
+ if (popup) {
720
+ this._openPopup({
721
+ target: this,
722
+ layer,
723
+ feature,
724
+ ...createFeaturePositionOptions(feature),
725
+ }, popupOptions, 'api');
726
+ }
727
+ this._updateTooltip({ layer, feature });
728
+ });
717
729
  }
718
730
  }
719
731
  }
@@ -1308,6 +1320,15 @@ class OsmAdapter extends TileAdapter {
1308
1320
  }
1309
1321
  }
1310
1322
 
1323
+ function arrayToBoundsExpression(bounds) {
1324
+ const e = bounds;
1325
+ // top, left, bottom, right
1326
+ return [
1327
+ [e[3], e[0]],
1328
+ [e[1], e[2]],
1329
+ ];
1330
+ }
1331
+
1311
1332
  class LeafletMapAdapter$1 {
1312
1333
  constructor() {
1313
1334
  this.options = { target: 'map' };
@@ -1331,10 +1352,7 @@ class LeafletMapAdapter$1 {
1331
1352
  this.map = new Map(this.options.target, {
1332
1353
  attributionControl: false,
1333
1354
  zoomControl: false,
1334
- maxBounds: mb && [
1335
- [mb[1], mb[0]],
1336
- [mb[3], mb[2]],
1337
- ],
1355
+ maxBounds: mb ? arrayToBoundsExpression(mb) : undefined,
1338
1356
  maxZoom,
1339
1357
  minZoom,
1340
1358
  center: center && [center[1], center[0]],
@@ -1371,14 +1389,52 @@ class LeafletMapAdapter$1 {
1371
1389
  this.map.getContainer().style.cursor = cursor;
1372
1390
  }
1373
1391
  }
1374
- setView(lngLat, zoom) {
1375
- const [lng, lat] = lngLat;
1376
- if (this.map) {
1377
- if (typeof zoom === 'number') {
1378
- this.map.setView([lat, lng], zoom, { animate: false });
1392
+ setView(lngLatOrOpt, zoom) {
1393
+ const map = this.map;
1394
+ if (!map)
1395
+ return;
1396
+ if (Array.isArray(lngLatOrOpt)) {
1397
+ const lngLat = lngLatOrOpt;
1398
+ const [lng, lat] = lngLat;
1399
+ if (this.map) {
1400
+ if (typeof zoom === 'number') {
1401
+ this.map.setView([lat, lng], zoom, { animate: false });
1402
+ }
1403
+ else {
1404
+ this.setCenter([lng, lat]);
1405
+ }
1406
+ }
1407
+ }
1408
+ else {
1409
+ const { zoom, center, maxBounds, bounds, minZoom, maxZoom } = lngLatOrOpt;
1410
+ if (center && zoom !== undefined) {
1411
+ this.setView(center, zoom);
1379
1412
  }
1380
1413
  else {
1381
- this.setCenter([lng, lat]);
1414
+ if (zoom !== undefined) {
1415
+ this.setZoom(zoom);
1416
+ }
1417
+ if (center) {
1418
+ this.setCenter(center);
1419
+ }
1420
+ }
1421
+ if (bounds) {
1422
+ this.fitBounds(bounds);
1423
+ }
1424
+ if (maxBounds !== undefined) {
1425
+ if (maxBounds) {
1426
+ map.setMaxBounds(arrayToBoundsExpression(maxBounds));
1427
+ }
1428
+ else {
1429
+ // @ts-ignore `null` works for unset maxBounds, but not in typing
1430
+ map.setMaxBounds(null);
1431
+ }
1432
+ }
1433
+ if (maxZoom !== undefined) {
1434
+ map.setMaxZoom(maxZoom);
1435
+ }
1436
+ if (minZoom !== undefined) {
1437
+ map.setMinZoom(minZoom);
1382
1438
  }
1383
1439
  }
1384
1440
  }
@@ -1413,7 +1469,7 @@ class LeafletMapAdapter$1 {
1413
1469
  return [sw.lng, sw.lat, ne.lng, ne.lat];
1414
1470
  }
1415
1471
  // [west, south, east, north]
1416
- fitBounds(e, options = {}) {
1472
+ fitBounds(bounds, options = {}) {
1417
1473
  if (this.map) {
1418
1474
  const { maxZoom, offset, padding } = options;
1419
1475
  const opt = {};
@@ -1426,11 +1482,7 @@ class LeafletMapAdapter$1 {
1426
1482
  if (offset) {
1427
1483
  opt.padding = offset;
1428
1484
  }
1429
- // top, left, bottom, right
1430
- this.map.fitBounds([
1431
- [e[3], e[0]],
1432
- [e[1], e[2]],
1433
- ], opt);
1485
+ this.map.fitBounds(arrayToBoundsExpression(bounds), opt);
1434
1486
  }
1435
1487
  }
1436
1488
  getLayerAdapter(name) {