Skip to content

Commit

Permalink
feat: <my-map> - support child <marker> elements, etc (#44)(#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
nfreear committed Oct 28, 2023
1 parent 2e2c874 commit b00866b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 32 deletions.
18 changes: 10 additions & 8 deletions demo/my-map.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

<my-map
lang="en-Test"
lat="51.505"
long="-0.09"
latlng="51.505,-0.09"
zoom="13"
geojson="./data/landmarks.geo.json"
>
<marker latlng="51.505, -0.09" hidden>Hello, I'm a popup!</marker>
<i latlng="51.507,-0.094" data-fa-id="star" hidden>A 2nd popup</i>

<h1> my-map Web Component </h1>

<p> Easily embed a map powered by <a href="https://leafletjs.com/">Leaflet.js</a>
Expand All @@ -24,11 +26,12 @@ <h1> my-map Web Component </h1>

<my-code>
&lt;my-map
lat="51.505"
long="-0.09"
latlng="51.505,-0.09"
zoom="13"
geojson="./data/landmarks.geo.json"
>
&lt;marker latlng="51.505, -0.09" hidden>Hello, I'm a popup!&lt;/marker>

&lt;p> Map description &lt;/p>
&lt;/my-map>
</my-code>
Expand All @@ -41,14 +44,13 @@ <h1> my-map Web Component </h1>
<script type="module">
const myMap = document.querySelector('my-map');

const L = await myMap.getLeaflet();
const MAP = await myMap.getMap();
const { L, map } = await myMap.getLeafletMap();

L.popup()
.setLatLng([51.513, -0.09])
.setContent('I am a standalone popup.').openOn(MAP);
.setContent('I am a standalone popup.').openOn(map);

console.debug('>> Leaflet:', L.version, MAP, L);
console.debug('>> Leaflet:', L.version, map, L);
</script>

<pre>
Expand Down
18 changes: 16 additions & 2 deletions packages/my-map/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

[![Node.js CI][ci-img]][ci]
[![NPM package][npm-img]][npm]
[![Leaflet 1.9.4][leaflet-img]][leaflet]

# my-map #

Easily embed a map powered by [Leaflet][].
Easily embed a map powered by [Leaflet][]. Includes [accessibility][myp] and [translation][myp] Leaflet plugins.

* [nfreear.github.io/elements/demo][demo]

Expand All @@ -13,13 +14,24 @@ Easily embed a map powered by [Leaflet][].
Available on the [Unpkg][] CDN:

```html
<my-map></my-map>
<my-map
latlng="51.505,-0.09"
zoom="13"
geojson="./data/landmarks.geo.json"
>
<marker latlng="51.505, -0.09" hidden>Hello, I'm a popup!</marker>

<p> My map description ... </p>
</my-map>

<script src="https://unpkg.com/my-map@1" type="module" ></script>
```

License: [MIT][].

[ci]: https://github.com/nfreear/elements/actions/workflows/node.js.yml
[ci-img]: https://github.com/nfreear/elements/actions/workflows/node.js.yml/badge.svg
[leaflet-img]: https://img.shields.io/badge/leaflet-1.9.4-green.svg?style=flat
[demo]: https://nfreear.github.io/elements/demo/
[mit]: https://github.com/nfreear/elements/blob/main/LICENSE.txt
[npm]: https://www.npmjs.com/package/my-map
Expand All @@ -30,3 +42,5 @@ Available on the [Unpkg][] CDN:
[skypack]: https://cdn.skypack.dev
"A JavaScript Delivery Network for modern web apps"
[leaflet]: https://leafletjs.com/
[myp]: https://github.com/nfreear/leaflet.plugins
"A collection of accessibility and localisation/ translation plugins for Leaflet"
1 change: 1 addition & 0 deletions packages/my-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "1.5.1",
"type": "module",
"main": "index.js",
"module": "index.js",
"author": "Nick Freear",
"license": "MIT",
"homepage": "https://nfreear.github.io/elements/demo/",
Expand Down
78 changes: 56 additions & 22 deletions src/components/MyMapElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const { fetch } = window;
const LEAFLET_CDN_LIBS = [
'https://unpkg.com/[email protected]/dist/leaflet.js',
'https://unpkg.com/[email protected]/Leaflet.i18n.js',
'https://unpkg.com/leaflet.translate@0.3.0/Leaflet.translate.js',
'https://unpkg.com/leaflet.a11y@0.3.0/Leaflet.a11y.js'
'https://unpkg.com/leaflet.translate@0.6.0/Leaflet.translate.js',
'https://unpkg.com/leaflet.a11y@0.6.0/Leaflet.a11y.js'
];

// Some defaults.
Expand Down Expand Up @@ -63,7 +63,7 @@ const TEMPLATE = `
border-radius: 5px;
}
</style>
<div id="desc" class="map-desc"><slot> Description of the map. </slot></div>
<div id="desc" class="map-desc"><slot><p> Description of the map. </p></slot></div>
<div id="my-map" role="region" aria-describedby="desc">
<p class="my-loading">Loading map…</p>
</div>
Expand All @@ -75,32 +75,51 @@ export class MyMapElement extends MyElement {
return 'my-map';
}

get lang () { return this.getAttribute('lang') || ''; }
get latLng () { return this.getLatLong(this); }

/* async getMap () {
return await this._whenReady(() => this.$$.map, 'getMap');
} */
get zoom () { return parseInt(this.getAttribute('zoom') || DEF.zoom); }

getLatLong (elem) {
const str = elem.getAttribute('latlng') || `${DEF.lat}, ${DEF.long}`;
const parts = str.split(/, ?/) || [0, 0];
return this._leaflet.latLng(parseFloat(parts[0]), parseFloat(parts[1]));
// return [parseFloat(parts[0]), parseFloat(parts[1])];
}

/** The CSS selector to use to query for HTML `<marker>` elements.
* @default '[latlng]' - Any HTML element with a `latlng` attribute.
*/
get markerSelector () {
return this.getAttribute('marker-selector') || '[latlng]';
}

/** The name of a marker JS constructor available on the `L` global.
* @default 'Marker'
*/
get markerClass () {
return this.getAttribute('marker-class') || 'Marker';
}

/** Asynchronously get references to Leaflet and the map object.
* @return { L, map } (Promise)
*/
async getLeafletMap () {
await this._whenReady(() => this.$$.map && this.$$.L, 'getMap');
await this._whenReady(() => this.$$.map && this.$$.L, 'getLeafletMap');
return { L: this.$$.L, map: this.$$.map };
}

async connectedCallback () {
this.$$ = {};

const lat = parseFloat(this.getAttribute('lat') || DEF.lat);
const long = parseFloat(this.getAttribute('long') || DEF.long);
const zoom = parseInt(this.getAttribute('zoom') || DEF.zoom);
// const caption = this.getAttribute('caption') || 'A caption for the map.';
const geojson = this.getAttribute('geojson') || null; // GeoJSON URL is relative to the HTML page!
const attribution = null;

const ATTRS = { lat, long, zoom, geojson, tileUrl: this.tileUrl, attribution };
const ATTRS = { latLng: {}, zoom: this.zoom, geojson, tileUrl: this.tileUrl, attribution };

await this._initialize(ATTRS);

console.debug('my-map:', this.$$.L.version, this.$$, this);
console.debug('<my-map>:', this.$$.L.version, this.$$, this);
}

async _initialize (attr) {
Expand All @@ -117,7 +136,7 @@ export class MyMapElement extends MyElement {
L.translate.load(this.lang);
}

const map = L.map(mapElem, { a11yPlugin: true }).setView([attr.lat, attr.long], attr.zoom);
const map = L.map(mapElem, { a11yPlugin: true }).setView(this.latLng, attr.zoom);

const tiles = L.tileLayer(attr.tileUrl, {
apikey: this.apiKey,
Expand All @@ -131,7 +150,7 @@ export class MyMapElement extends MyElement {
}); */

this.$$ = {
...attr, map, mapElem, tiles, L
...attr, latLng: this.latLng, map, mapElem, tiles, L
};

if (attr.geojson) {
Expand All @@ -146,7 +165,7 @@ export class MyMapElement extends MyElement {
}
}

// Was: this._accessibilityFixes();
this._queryAddChildMarkers();

const PATH = this.shadowRoot.querySelector('.leaflet-overlay-pane path');
PATH && PATH.setAttribute('part', 'path');
Expand Down Expand Up @@ -185,6 +204,27 @@ export class MyMapElement extends MyElement {
;
}

/** Construct a marker object.
*/
marker (latLng, options = {}) {
return new this._leaflet[this.markerClass](latLng, options);
}

_queryAddChildMarkers () {
const markerElems = this.querySelectorAll(this.markerSelector) || [];
const markers = [...markerElems].map((markerEl, idx) => {
const latLng = this.getLatLong(markerEl);
const popupContent = markerEl.textContent;
const options = { ...markerEl.dataset };
const marker = this.marker(latLng, options).addTo(this.$$.map);
if (popupContent) {
marker.bindPopup(popupContent);
}
return { latLng, popupContent, options, marker };
});
console.debug('queryAddChildMarkers:', markers);
}

async loadGeoJson (geojson) {
const resp = await fetch(geojson); // './data/landmarks.geo.json');
const geoJsonFeatures = await resp.json();
Expand Down Expand Up @@ -245,12 +285,6 @@ export class MyMapElement extends MyElement {
shadowUrl: 'https://unpkg.com/[email protected]/dist/images/marker-shadow.png'
});
}

_accessibilityFixes () {
}

_accessibilityFixPopup (ev) {
}
}

MyMapElement.define();
Expand Down

0 comments on commit b00866b

Please sign in to comment.