leaflet-html 0.2.2 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,15 +10,10 @@
10
10
  integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
11
11
  crossorigin=""
12
12
  />
13
- <script
14
- src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
15
- integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
16
- crossorigin=""
17
- ></script>
18
13
  <script type="importmap">
19
14
  {
20
15
  "imports": {
21
- "leaflet": "https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
16
+ "leaflet": "https://unpkg.com/leaflet@1.9.4/dist/leaflet-src.esm.js"
22
17
  }
23
18
  }
24
19
  </script>
@@ -9,15 +9,10 @@
9
9
  integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
10
10
  crossorigin=""
11
11
  />
12
- <script
13
- src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
14
- integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
15
- crossorigin=""
16
- ></script>
17
12
  <script type="importmap">
18
13
  {
19
14
  "imports": {
20
- "leaflet": "https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
15
+ "leaflet": "https://unpkg.com/leaflet@1.9.4/dist/leaflet-src.esm.js"
21
16
  }
22
17
  }
23
18
  </script>
@@ -10,15 +10,10 @@
10
10
  integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
11
11
  crossorigin=""
12
12
  />
13
- <script
14
- src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
15
- integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
16
- crossorigin=""
17
- ></script>
18
13
  <script type="importmap">
19
14
  {
20
15
  "imports": {
21
- "leaflet": "https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
16
+ "leaflet": "https://unpkg.com/leaflet@1.9.4/dist/leaflet-src.esm.js"
22
17
  }
23
18
  }
24
19
  </script>
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "leaflet-html",
3
3
  "type": "module",
4
- "version": "0.2.2",
4
+ "version": "0.3.1",
5
5
  "description": "Leaflet expressed in HTML",
6
6
  "source": "src/index.js",
7
7
  "main": "./dist/leaflet-html.cjs",
@@ -1,3 +1,4 @@
1
+ // @ts-check
1
2
  import { mapAddTo } from "./events.js";
2
3
 
3
4
  class LBaseLayers extends HTMLElement {
@@ -1,3 +1,6 @@
1
+ // @ts-check
2
+ /** @typedef {import("leaflet").Layer} Layer */
3
+ import { control } from "leaflet";
1
4
  import { mapAddTo } from "./events.js";
2
5
 
3
6
  class LControlLayers extends HTMLElement {
@@ -6,16 +9,18 @@ class LControlLayers extends HTMLElement {
6
9
  }
7
10
 
8
11
  connectedCallback() {
12
+ /** @type {{ [key: string]: Layer }} */
9
13
  const base = {};
14
+ /** @type {{ [key: string]: Layer }} */
10
15
  const overlay = {};
11
- const control = L.control.layers(base, overlay);
16
+ const controlLayers = control.layers(base, overlay);
12
17
 
13
18
  this.addEventListener(mapAddTo, (ev) => {
14
19
  const { type, name, layer } = ev.detail;
15
20
  if (type === "overlay") {
16
- control.addOverlay(layer, name);
21
+ controlLayers.addOverlay(layer, name);
17
22
  } else if (type === "base") {
18
- control.addBaseLayer(layer, name);
23
+ controlLayers.addBaseLayer(layer, name);
19
24
  }
20
25
  ev.preventDefault();
21
26
  });
@@ -24,7 +29,7 @@ class LControlLayers extends HTMLElement {
24
29
  cancelable: true,
25
30
  bubbles: true,
26
31
  detail: {
27
- layer: control,
32
+ layer: controlLayers,
28
33
  },
29
34
  });
30
35
  this.dispatchEvent(event);
package/src/l-geojson.js CHANGED
@@ -1,3 +1,5 @@
1
+ // @ts-check
2
+ import { geoJSON } from "leaflet";
1
3
  import { mapAddTo } from "./events.js";
2
4
 
3
5
  class LGeoJSON extends HTMLElement {
@@ -6,16 +8,19 @@ class LGeoJSON extends HTMLElement {
6
8
  }
7
9
 
8
10
  connectedCallback() {
9
- const layer = L.geoJSON(JSON.parse(this.getAttribute("geojson")));
10
- this.dispatchEvent(
11
- new CustomEvent(mapAddTo, {
12
- bubbles: true,
13
- cancelable: true,
14
- detail: {
15
- layer,
16
- },
17
- }),
18
- );
11
+ const value = this.getAttribute("geojson");
12
+ if (value !== null) {
13
+ const layer = geoJSON(JSON.parse(value));
14
+ this.dispatchEvent(
15
+ new CustomEvent(mapAddTo, {
16
+ bubbles: true,
17
+ cancelable: true,
18
+ detail: {
19
+ layer,
20
+ },
21
+ }),
22
+ );
23
+ }
19
24
  }
20
25
  }
21
26
 
@@ -1,3 +1,5 @@
1
+ // @ts-check
2
+ import { imageOverlay } from "leaflet";
1
3
  import { mapAddTo } from "./events.js";
2
4
 
3
5
  class LImageOverlay extends HTMLElement {
@@ -10,12 +12,20 @@ class LImageOverlay extends HTMLElement {
10
12
 
11
13
  connectedCallback() {
12
14
  const url = this.getAttribute("url");
13
- const bounds = JSON.parse(this.getAttribute("bounds"));
15
+ if (url === null) {
16
+ console.warn("attribute 'url' not set");
17
+ return;
18
+ }
19
+ let bounds = this.getAttribute("bounds");
20
+ if (bounds === null) {
21
+ console.warn("attribute 'bounds' not set");
22
+ return;
23
+ }
14
24
  const options = {
15
25
  opacity: parseFloat(this.getAttribute("opacity") || "1.0"),
16
26
  alt: this.getAttribute("alt") || "",
17
27
  };
18
- this.layer = L.imageOverlay(url, bounds, options);
28
+ this.layer = imageOverlay(url, JSON.parse(bounds), options);
19
29
  this.dispatchEvent(
20
30
  new CustomEvent(mapAddTo, {
21
31
  cancelable: true,
@@ -1,3 +1,4 @@
1
+ // @ts-check
1
2
  class LLatLngBounds extends HTMLElement {
2
3
  static observedAttributes = ["bounds"];
3
4
 
@@ -1,3 +1,5 @@
1
+ // @ts-check
2
+ import { layerGroup } from "leaflet";
1
3
  import { mapAddTo } from "./events.js";
2
4
 
3
5
  class LLayerGroup extends HTMLElement {
@@ -7,7 +9,7 @@ class LLayerGroup extends HTMLElement {
7
9
 
8
10
  connectedCallback() {
9
11
  const name = this.getAttribute("name");
10
- const group = L.layerGroup();
12
+ const group = layerGroup();
11
13
  const event = new CustomEvent(mapAddTo, {
12
14
  cancelable: true,
13
15
  bubbles: true,
@@ -26,9 +28,15 @@ class LLayerGroup extends HTMLElement {
26
28
  const observer = new MutationObserver(function (mutations) {
27
29
  mutations.forEach((mutation) => {
28
30
  mutation.removedNodes.forEach((node) => {
29
- const leafletId = node.getAttribute("leaflet-id");
30
- const layer = group.getLayer(leafletId);
31
- group.removeLayer(layer);
31
+ if (node instanceof HTMLElement) {
32
+ const leafletId = node.getAttribute("leaflet-id");
33
+ if (leafletId !== null) {
34
+ const layer = group.getLayer(parseInt(leafletId));
35
+ if (typeof layer !== "undefined") {
36
+ group.removeLayer(layer);
37
+ }
38
+ }
39
+ }
32
40
  });
33
41
  });
34
42
  });
package/src/l-map.js CHANGED
@@ -1,5 +1,7 @@
1
1
  // @ts-check
2
+ import * as L from "leaflet";
2
3
  import { layerRemove, mapAddTo } from "./events.js";
4
+ import LTileLayer from "./l-tile-layer.js";
3
5
 
4
6
  class LMap extends HTMLElement {
5
7
  constructor() {
@@ -8,8 +10,22 @@ class LMap extends HTMLElement {
8
10
  this.map = null;
9
11
  this.addEventListener("map:bounds", (ev) => {
10
12
  const { bounds, method } = ev.detail;
11
- this.map[method](bounds);
13
+ if (this.map !== null) {
14
+ this.map[method](bounds);
15
+ }
12
16
  });
17
+
18
+ // Observe removed l-tile-layers
19
+ const observer = new MutationObserver(function(mutations) {
20
+ mutations.forEach((mutation) => {
21
+ mutation.removedNodes.forEach((node) => {
22
+ if (node instanceof LTileLayer) {
23
+ this.map.removeLayer(node.layer)
24
+ }
25
+ })
26
+ })
27
+ })
28
+ observer.observe(this, { childList: true })
13
29
  }
14
30
 
15
31
  connectedCallback() {
@@ -25,7 +41,9 @@ class LMap extends HTMLElement {
25
41
  });
26
42
 
27
43
  this.addEventListener(layerRemove, (ev) => {
28
- this.map.remove(ev.detail.layer);
44
+ if (this.map !== null) {
45
+ this.map.removeLayer(ev.detail.layer);
46
+ }
29
47
  });
30
48
  }
31
49
  }
@@ -1,3 +1,4 @@
1
+ // @ts-check
1
2
  import { mapAddTo } from "./events.js";
2
3
 
3
4
  class LOverlayLayers extends HTMLElement {
package/src/l-popup.js CHANGED
@@ -1,3 +1,4 @@
1
+ // @ts-check
1
2
  import { popupAdd } from "./events.js";
2
3
 
3
4
  class LPopup extends HTMLElement {
@@ -1,18 +1,27 @@
1
+ // @ts-check
2
+ import { tileLayer } from "leaflet";
1
3
  import { mapAddTo } from "./events.js";
2
4
 
3
5
  class LTileLayer extends HTMLElement {
4
6
  constructor() {
5
7
  super();
8
+ this.layer = null
6
9
  }
7
10
 
8
11
  connectedCallback() {
9
12
  const name = this.getAttribute("name");
10
13
  const urlTemplate = this.getAttribute("url-template");
11
- const attribution = this.getAttribute("attribution");
12
- const options = { attribution };
13
- const layer = L.tileLayer(urlTemplate, options);
14
+ if (urlTemplate === null) {
15
+ return;
16
+ }
17
+ const options = {};
18
+ const key = "attribution";
19
+ if (this.hasAttribute(key)) {
20
+ options[key] = this.getAttribute(key);
21
+ }
22
+ this.layer = tileLayer(urlTemplate, options);
14
23
  const event = new CustomEvent(mapAddTo, {
15
- detail: { name, layer },
24
+ detail: { name, layer: this.layer },
16
25
  bubbles: true,
17
26
  });
18
27
  this.dispatchEvent(event);
@@ -1,3 +1,5 @@
1
+ // @ts-check
2
+ import { videoOverlay } from "leaflet";
1
3
  import { mapAddTo } from "./events.js";
2
4
 
3
5
  class LVideoOverlay extends HTMLElement {
@@ -15,7 +17,7 @@ class LVideoOverlay extends HTMLElement {
15
17
  muted: true,
16
18
  playsInline: true,
17
19
  };
18
- const layer = L.videoOverlay(url, bounds, options);
20
+ const layer = videoOverlay(url, bounds, options);
19
21
  this.dispatchEvent(
20
22
  new CustomEvent(mapAddTo, {
21
23
  cancelable: true,