diff --git a/src/l-map.js b/src/l-map.js index f73f264..22d3a66 100644 --- a/src/l-map.js +++ b/src/l-map.js @@ -55,6 +55,15 @@ class LMap extends HTMLElement { connectedCallback() { const options = { zoomControl: this.hasAttribute("zoom-control") }; + if (this.hasAttribute("max-zoom")) { + options.maxZoom = parseFloat(this.getAttribute("max-zoom")); + } + if (this.hasAttribute("min-zoom")) { + options.minZoom = parseFloat(this.getAttribute("min-zoom")); + } + if (this.hasAttribute("max-bounds")) { + options.maxBounds = JSON.parse(this.getAttribute("max-bounds")); + } if (this.hasAttribute("attribution-control")) { options["attributionControl"] = this.getAttribute("attribution-control").toLowerCase() === "true"; } diff --git a/src/l-map.test.js b/src/l-map.test.js index 77ee079..5c76f29 100644 --- a/src/l-map.test.js +++ b/src/l-map.test.js @@ -4,7 +4,7 @@ import { layerRemoved, layerConnected } from "./events" import { vi, it, expect } from "vitest"; import LTileLayer from "./l-tile-layer"; import LMap from "./l-map.js"; -import { map } from "leaflet"; +import { map, latLngBounds } from "leaflet"; it("should emit map:addTo event(s)", async () => { // Arrange: create a ... arrangement @@ -142,3 +142,22 @@ it("should remove attributionControl given attribution-control=true attribute", document.body.appendChild(el); expect(el.map.attributionControl).not.toBe(undefined); }) + +it("should set maxZoom and minZoom given max-zoom and min-zoom attribute", () => { + const el = document.createElement("l-map"); + el.setAttribute("zoom", "0"); + el.setAttribute("center", "[0,0]"); + el.setAttribute("max-zoom", "5"); + el.setAttribute("min-zoom", "0"); + document.body.appendChild(el); + expect(el.map.options).toEqual({ zoomControl: false, maxZoom: 5, minZoom: 0 }); +}) + +it("should set maxBounds given max-bounds attribute", () => { + const el = document.createElement("l-map"); + el.setAttribute("zoom", "0"); + el.setAttribute("center", "[0,0]"); + el.setAttribute("max-bounds", "[[0, 0], [1, 1]]"); + document.body.appendChild(el); + expect(el.map.options).toEqual({ zoomControl: false, maxBounds: latLngBounds([[0, 0], [1, 1]]) }); +})