-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from andrewgryan/feature/pane
Add support for l-pane custom element
- Loading branch information
Showing
12 changed files
with
321 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
+++ | ||
title = "Working with map panes" | ||
+++ | ||
|
||
## Demo | ||
|
||
This tutorial illustrates how to make effective use of Leaflet panes. | ||
|
||
<style> | ||
.leaflet-coastline-pane { | ||
z-index: 450; | ||
pointer-events: none; | ||
} | ||
</style> | ||
|
||
<l-map zoom="3" center="[55, 0]"> | ||
<l-tile-layer url-template="https://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png"></l-tile-layer> | ||
<l-pane name="coastline"> | ||
<l-tile-layer url-template="https://{s}.basemaps.cartocdn.com/light_only_labels/{z}/{x}/{y}.png"></l-tile-layer> | ||
</l-pane> | ||
<l-geojson id="countries"></l-geojson> | ||
</l-map> | ||
|
||
<script type="text/javascript" src="/leaflet-html/panes/eu-countries.js"></script> | ||
<script type="text/javascript" src="/panes/eu-countries.js"></script> | ||
<script> | ||
document.getElementById("countries").setAttribute("geojson", JSON.stringify(euCountries)) | ||
</script> | ||
|
||
Following the tutorial on the Leaflet JS website. | ||
|
||
### Mark-up | ||
|
||
The mark-up for a custom pane is relatively straight-forward. | ||
Add a `l-pane` tag with a `name` attribute to instruct `leaflet-html` to add a new pane. | ||
|
||
```html,hl_lines=6 10,linenos | ||
<!-- Example app using a Pane --> | ||
<l-map zoom="3" center="[55, 0]"> | ||
<l-tile-layer | ||
url-template="https://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png" | ||
></l-tile-layer> | ||
<l-pane name="coastline"> | ||
<l-tile-layer | ||
url-template="https://{s}.basemaps.cartocdn.com/light_only_labels/{z}/{x}/{y}.png" | ||
></l-tile-layer> | ||
</l-pane> | ||
<l-geojson id="countries"></l-geojson> | ||
</l-map> | ||
``` | ||
|
||
Lines 6 and 10 show the modifications required to place a layer into a pane. | ||
|
||
### Control the z-index and pointer events | ||
|
||
```css | ||
/* Style the element generated by Leaflet JS */ | ||
.leaflet-coastline-pane { | ||
z-index: 450; | ||
pointer-events: none; | ||
} | ||
``` | ||
|
||
### Sample data | ||
|
||
In a real application, the HTML will be server rendered with geoJSON. | ||
But for this short demo the following snippet of JS has been used. | ||
|
||
```js | ||
/** Sample data */ | ||
document | ||
.getElementById("countries") | ||
.setAttribute("geojson", JSON.stringify(euCountries)); | ||
``` | ||
|
||
## Conclusion | ||
|
||
Leaflet HTML supports custom panes for those times when controlling | ||
the z-index of a collection of layers is important. | ||
|
||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { paneConnected } from "./events.js"; | ||
|
||
export default class CustomElement extends HTMLElement { | ||
constructor() { | ||
super(); | ||
} | ||
|
||
connectedCallback() { | ||
this.dispatchEvent( | ||
new CustomEvent(paneConnected, { | ||
bubbles: true, | ||
cancelable: true, | ||
detail: { | ||
name: this.getAttribute("name"), | ||
}, | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// @vitest-environment happy-dom | ||
import "./index.js"; | ||
import { expect, it } from "vitest"; | ||
|
||
const createMapElement = (zoom, center) => { | ||
const root = document.createElement("l-map"); | ||
root.setAttribute("zoom", zoom.toString()); | ||
root.setAttribute("center", JSON.stringify(center)); | ||
return root; | ||
}; | ||
|
||
it("should support pane", () => { | ||
const layerName = "layer-name"; | ||
const root = createMapElement(0, [0, 0]); | ||
const pane = document.createElement("l-pane"); | ||
pane.setAttribute("name", layerName); | ||
root.appendChild(pane); | ||
document.body.appendChild(root); | ||
|
||
const actual = root.map.getPane(layerName); | ||
expect(actual).toBeDefined(); | ||
}); | ||
|
||
// Add minimum attributes to each Custom Element | ||
const fakeElement = (key) => { | ||
const el = document.createElement(key); | ||
switch (key) { | ||
case "l-image-overlay": | ||
el.setAttribute("url", "fake/url"); | ||
el.setAttribute("bounds", "[[0,0],[1,1]]"); | ||
break; | ||
case "l-marker": | ||
el.setAttribute("lat-lng", "[0, 0]"); | ||
break; | ||
case "l-tile-layer": | ||
el.setAttribute("url-template", "fake/{z}/{x}/{y}.png"); | ||
break; | ||
case "l-geojson": | ||
el.setAttribute("geojson", '{"type": "Feature", "properties": {}}'); | ||
break; | ||
} | ||
return el; | ||
}; | ||
|
||
// Check each layer supports custom pane | ||
it.each([["l-image-overlay"], ["l-marker"], ["l-tile-layer"], ["l-geojson"]])( | ||
"should capture %s in pane", | ||
(key) => { | ||
const root = createMapElement(0, [0, 0]); | ||
const pane = document.createElement("l-pane"); | ||
pane.setAttribute("name", "test-pane"); | ||
root.appendChild(pane); | ||
const layerEl = fakeElement(key); | ||
pane.appendChild(layerEl); | ||
document.body.appendChild(root); | ||
|
||
// Assert pane on map is the pane on the layer | ||
const actual = root.map.getPane("test-pane"); | ||
const expected = layerEl.layer.getPane(); | ||
expect(actual).toEqual(expected); | ||
} | ||
); | ||
|
||
// Check each layer supports custom pane | ||
it.each([ | ||
["l-image-overlay", "overlayPane"], | ||
["l-marker", "markerPane"], | ||
["l-tile-layer", "tilePane"], | ||
["l-geojson", "overlayPane"], | ||
])("should match default pane %s", (key, pane) => { | ||
const root = createMapElement(0, [0, 0]); | ||
const layerEl = fakeElement(key); | ||
root.appendChild(layerEl); | ||
document.body.appendChild(root); | ||
|
||
// Assert pane on map is the pane on the layer | ||
const actual = root.map.getPane(pane); | ||
const expected = layerEl.layer.getPane(); | ||
expect(actual).toEqual(expected); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters