-
Notifications
You must be signed in to change notification settings - Fork 2
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 #35 from mo-analameira/feature/add-marker-cluster-…
…group First iteration of l-marker-cluster-group
- Loading branch information
Showing
5 changed files
with
153 additions
and
2 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
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,63 @@ | ||
// @ts-check | ||
import "leaflet.markercluster"; | ||
import { layerConnected } from "./events.js"; | ||
import LLayer from "./l-layer.js"; | ||
|
||
class LMarkerClusterGroup extends LLayer { | ||
static observedAttributes = [ | ||
"show-coverage-on-hover", | ||
"icon-options" | ||
]; | ||
|
||
constructor() { | ||
super(); | ||
this.layer = null; | ||
} | ||
|
||
connectedCallback() { | ||
const name = this.getAttribute("name"); | ||
const iconOptions = this.getAttribute("icon-options"); | ||
this.layer = L.markerClusterGroup({ | ||
showCoverageOnHover: this.hasAttribute("show-coverage-on-hover"), | ||
iconCreateFunction: this._createIconCreateFunction(iconOptions) | ||
}); | ||
|
||
const event = new CustomEvent(layerConnected, { | ||
cancelable: true, | ||
bubbles: true, | ||
detail: { | ||
layer: this.layer, | ||
name | ||
} | ||
}); | ||
this.dispatchEvent(event); | ||
|
||
this.addEventListener(layerConnected, (ev) => { | ||
ev.stopPropagation(); | ||
this.layer.addLayer(ev.detail.layer); | ||
}); | ||
} | ||
|
||
_createIconCreateFunction(iconOptions) { | ||
if (iconOptions) { | ||
let { size, content, className } = JSON.parse(iconOptions); | ||
|
||
return (cluster) => { | ||
const resolvedSize = this._callClusterFunction(cluster, size); | ||
const resolvedContent = this._callClusterFunction(cluster, content); | ||
|
||
return L.divIcon({ | ||
html: resolvedContent, | ||
className: className, | ||
iconSize: new L.Point(resolvedSize, resolvedSize) | ||
}); | ||
}; | ||
} | ||
} | ||
|
||
_callClusterFunction(cluster, body) { | ||
return Function("cluster", `"use strict";return (${body})`)(cluster); | ||
} | ||
} | ||
|
||
export default LMarkerClusterGroup; |
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,85 @@ | ||
// @vitest-environment happy-dom | ||
import "leaflet"; | ||
import "leaflet.markercluster"; | ||
import { it, expect } from "vitest"; | ||
import { layerConnected } from "./events"; | ||
import "./index"; | ||
|
||
it("should cover l-marker-cluster-group", async () => { | ||
const el = document.createElement("l-marker-cluster-group"); | ||
el.setAttribute("name", "Marker Cluster"); | ||
el.setAttribute("show-coverage-on-hover", "true"); | ||
|
||
let promise = new Promise((resolve) => { | ||
el.addEventListener(layerConnected, (ev) => { | ||
resolve(ev.detail); | ||
}); | ||
}); | ||
document.body.appendChild(el); | ||
|
||
const actual = await promise; | ||
const expected = { | ||
name: "Marker Cluster", | ||
layer: L.markerClusterGroup({ showCoverageOnHover: true }) | ||
}; | ||
|
||
expect(_removePropertiesToIgnore(actual)).toEqual(_removePropertiesToIgnore(expected)); | ||
}); | ||
|
||
it("should register layers", async () => { | ||
const el = document.createElement("l-marker-cluster-group"); | ||
const marker = document.createElement("l-marker"); | ||
marker.setAttribute("lat-lng", "[0,0]"); | ||
el.appendChild(marker); | ||
|
||
let promise = new Promise((resolve) => { | ||
el.addEventListener(layerConnected, (ev) => { | ||
resolve(ev.detail); | ||
}); | ||
}); | ||
document.body.appendChild(el); | ||
|
||
const actual = await promise; | ||
const expected = { | ||
name: null, | ||
layer: L.markerClusterGroup({ showCoverageOnHover: false }).addLayer(L.marker(new L.LatLng(0, 0))) | ||
}; | ||
|
||
expect(_removePropertiesToIgnore(actual)).toEqual(_removePropertiesToIgnore(expected)); | ||
}); | ||
|
||
it("should create icon function using icon-options", async () => { | ||
const el = document.createElement("l-marker-cluster-group"); | ||
const iconOptions = { | ||
"size": "Math.max(40, cluster.getChildCount() / 2)", | ||
"content": "\"<div>\" + cluster.getChildCount() + \"</div>\"", | ||
"className": "mavis-marker-cluster" | ||
}; | ||
el.setAttribute("icon-options", JSON.stringify(iconOptions)); | ||
|
||
let promise = new Promise((resolve) => { | ||
el.addEventListener(layerConnected, (ev) => { | ||
resolve(ev.detail); | ||
}); | ||
}); | ||
document.body.appendChild(el); | ||
|
||
const mockCluster = { getChildCount: () => 100 }; | ||
const actual = await promise; | ||
const expectedClusterIcon = L.divIcon({ | ||
html: "<div>100</div>", | ||
className: "mavis-marker-cluster", | ||
iconSize: new L.Point(50, 50) | ||
}); | ||
|
||
expect(actual.layer.options.iconCreateFunction(mockCluster)).toEqual(expectedClusterIcon); | ||
}); | ||
|
||
function _removePropertiesToIgnore(element) { | ||
element.layer._leaflet_id = null; | ||
element.layer._featureGroup._eventParents = null; | ||
element.layer._nonPointGroup._eventParents = null; | ||
element.layer._needsClustering.forEach(e => e._leaflet_id = null) | ||
|
||
return element; | ||
} |