Skip to content

Instantly share code, notes, and snippets.

@gtramontina
Created September 22, 2023 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gtramontina/ff58345cb9cc53398945fd25a4ddbd3c to your computer and use it in GitHub Desktop.
Save gtramontina/ff58345cb9cc53398945fd25a4ddbd3c to your computer and use it in GitHub Desktop.
leaflet-dom-event-bridge.js
L.Map.addInitHook(function () {
const dom = this.getContainer();
const forward = (e) =>
dom.dispatchEvent(
new CustomEvent(`map:${e.type}`, {
detail: {
originalEvent: e,
zoom: this.getZoom(),
center: this.getCenter(),
bounds: {
northEast: this.getBounds().getNorthEast(),
southWest: this.getBounds().getSouthWest(),
},
},
}),
);
dom._leaflet_map = this;
this.on("unload", () => {
dom._leaflet_map = undefined;
});
this.on("baselayerchange", forward);
this.on("overlayadd", forward);
this.on("overlayremove", forward);
this.on("layerremove", forward);
this.on("zoomlevelschange", forward);
this.on("resize", forward);
this.on("unload", forward);
this.on("viewreset", forward);
this.on("load", forward);
this.on("zoomstart", forward);
this.on("movestart", forward);
this.on("zoom", forward);
this.on("move", forward);
this.on("zoomend", forward);
this.on("moveend", forward);
this.on("popupopen", forward);
this.on("popupclose", forward);
this.on("autopanstart", forward);
this.on("tooltipopen", forward);
this.on("tooltipclose", forward);
this.on("locationerror", forward);
this.on("locationfound", forward);
this.on("click", forward);
this.on("dblclick", forward);
this.on("mousedown", forward);
this.on("mouseup", forward);
this.on("mouseover", forward);
this.on("mouseout", forward);
this.on("mousemove", forward);
this.on("contextmenu", forward);
this.on("keypress", forward);
this.on("keydown", forward);
this.on("keyup", forward);
this.on("preclick", forward);
this.on("zoomanim", forward);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment