@russss/maplibregl-layer-switcher 2.2.2 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -21,7 +21,7 @@ import { LayerSwitcher } from '@russss/maplibregl-layer-switcher'
21
21
  You can also import some basic styles:
22
22
 
23
23
  ```css
24
- @import '@russss/maplibregl-layer-switcher/layerswitcher.css';
24
+ @import '@russss/maplibregl-layer-switcher/maplibregl-layer-switcher.css';
25
25
  ```
26
26
 
27
27
  Now define your layers. Each layer has a short identifier which will be used in the URL hash,
@@ -110,5 +110,4 @@ The URLHash object doesn't currently support tilt and rotation parameters.
110
110
 
111
111
  ## Development
112
112
 
113
- Vite can be used to test the library (using index.html and index.ts in the root directory) but isn't
114
- used to build the published version.
113
+ Vite is used to build and test the library - to test, run `npm start` (which uses index.html and index.ts in the root directory).
package/dist/data.d.ts ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * A layer in the layer switcher.
3
+ */
4
+ export declare class Layer {
5
+ id: string;
6
+ prefix: string;
7
+ title: string;
8
+ groupId?: string;
9
+ enabled: boolean;
10
+ /**
11
+ * An item in the layer switcher.
12
+ *
13
+ * @param id alphanumeric identifier for the layer. This is included in the URL hash so should be as short
14
+ * as possible. Must be unique among all layers, or an error will be thrown.
15
+ * @param title name of the layer as shown in the layer switcher.
16
+ * @param prefix prefix of the layer in the map style to match.
17
+ * @param groupId when group id is set the layer will be part of a radio button group.
18
+ * @param enabled whether the layer is enabled by default.
19
+ */
20
+ constructor(id: string, title: string, prefix: string, enabled?: boolean);
21
+ constructor(id: string, title: string, prefix: string, groupId: string, enabled?: boolean);
22
+ }
23
+ /**
24
+ * A group of layers shown in the layer switcher.
25
+ */
26
+ export declare class LayerGroup {
27
+ layers: Layer[];
28
+ title: string;
29
+ /**
30
+ * A group of layers shown in the layer switcher.
31
+ *
32
+ * @param title name of the group to be shown.
33
+ * @param layers list of layers in the group.
34
+ */
35
+ constructor(title: string, layers: Layer[]);
36
+ }
37
+ //# sourceMappingURL=data.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"data.d.ts","sourceRoot":"","sources":["../src/data.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,KAAK;IAChB,EAAE,EAAE,MAAM,CAAA;IACV,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;IACb,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,OAAO,CAAQ;IAExB;;;;;;;;;OASG;gBACS,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO;gBAC5D,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO;CAa1F;AAED;;GAEG;AACH,qBAAa,UAAU;IACrB,MAAM,EAAE,KAAK,EAAE,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;IAEb;;;;;OAKG;gBACS,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE;CAI3C"}
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./layerswitcher-CezhM7p2.cjs");function t(e){let t=e.replace(`#`,``).split(`/`),n={additional:{}};if(t.length<3)return n;n.layers=``,n.zoom=+t[0],n.center=[+t[2],+t[1]];for(let e=3;e<t.length;e++){let r=t[e],i=r.match(/^(\w+)=(.*)$/);i?(n.additional=n.additional||{},n.additional[i[1]]=i[2]):n.layers=r}return n}function n(e){if(!e.zoom||!e.center)return``;let t=Math.round(e.zoom*100)/100,n=10**Math.ceil((t*Math.LN2+Math.log(512/360/.5))/Math.LN10),r=Math.round(e.center[0]*n)/n,i=`#${t}/${Math.round(e.center[1]*n)/n}/${r}`;if(e.layers&&(i+=`/`+e.layers),e.additional)for(let t in e.additional)i+=`/`+t+`=`+e.additional[t];return i}var r=class{constructor(e){this.layerSwitcher=e,this.additional={},this.handlers={}}enable(e){this._map=e,this.layerSwitcher.urlhash=this,this._onHashChange(window.location.hash),e.on(`moveend`,()=>{this._updateHash()}),window.addEventListener(`hashchange`,()=>{this._onHashChange(window.location.hash)},!1)}registerHandler(e,t){this.handlers[e]=t}setParameter(e,t){this.additional[e]!==t&&(t===null?delete this.additional[e]:this.additional[e]=t,this._updateHash())}_fireParameterChange(e,t){this.handlers[e]&&this.handlers[e](t)}_onHashChange(e){let n=t(e),r=[...Object.keys(n.additional),...Object.keys(this.additional)];for(let e of r)this.additional[e]&&!n.additional[e]?(delete this.additional[e],this._fireParameterChange(e,null)):n.additional[e]&&this.additional[e]!==n.additional[e]&&(this.additional[e]=n.additional[e],this._fireParameterChange(e,n.additional[e]));this._map?.isStyleLoaded()&&n.center&&n.zoom&&this._map.jumpTo({center:n.center,zoom:n.zoom}),this.layerSwitcher&&n.layers!==void 0&&this.layerSwitcher.setURLString(n.layers)}_updateHash(){let e=this.getHashString();try{window.history.replaceState(window.history.state,``,e)}catch(e){console.log(e)}}getHashString(){if(!this._map)throw Error(`getHashString called before map initialised`);let{lng:e,lat:t}=this._map.getCenter(),r={center:[e,t],zoom:this._map.getZoom(),additional:this.additional};return this.layerSwitcher&&(r.layers=this.layerSwitcher.getURLString()),n(r)}init(e){e.hash=!1;let t=window.location.hash.replace(`#`,``).split(`/`);return t.length>=3&&(e.center=[+t[2],+t[1]],e.zoom=+t[0]),e}};exports.Layer=e.n,exports.LayerGroup=e.r,exports.LayerSwitcher=e.t,exports.URLHash=r;
@@ -0,0 +1,5 @@
1
+ import { default as LayerSwitcher } from './layerswitcher';
2
+ import { default as URLHash } from './urlhash';
3
+ import { Layer, LayerGroup } from './data';
4
+ export { LayerSwitcher, URLHash, Layer, LayerGroup };
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,aAAa,MAAM,iBAAiB,CAAA;AAC3C,OAAO,OAAO,MAAM,WAAW,CAAA;AAC/B,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAA;AAE1C,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,71 @@
1
+ import { n as e, r as t, t as n } from "./layerswitcher-B7ForKVG.js";
2
+ //#region src/urlhash.ts
3
+ function r(e) {
4
+ let t = e.replace("#", "").split("/"), n = { additional: {} };
5
+ if (t.length < 3) return n;
6
+ n.layers = "", n.zoom = +t[0], n.center = [+t[2], +t[1]];
7
+ for (let e = 3; e < t.length; e++) {
8
+ let r = t[e], i = r.match(/^(\w+)=(.*)$/);
9
+ i ? (n.additional = n.additional || {}, n.additional[i[1]] = i[2]) : n.layers = r;
10
+ }
11
+ return n;
12
+ }
13
+ function i(e) {
14
+ if (!e.zoom || !e.center) return "";
15
+ let t = Math.round(e.zoom * 100) / 100, n = 10 ** Math.ceil((t * Math.LN2 + Math.log(512 / 360 / .5)) / Math.LN10), r = Math.round(e.center[0] * n) / n, i = `#${t}/${Math.round(e.center[1] * n) / n}/${r}`;
16
+ if (e.layers && (i += "/" + e.layers), e.additional) for (let t in e.additional) i += "/" + t + "=" + e.additional[t];
17
+ return i;
18
+ }
19
+ var a = class {
20
+ constructor(e) {
21
+ this.layerSwitcher = e, this.additional = {}, this.handlers = {};
22
+ }
23
+ enable(e) {
24
+ this._map = e, this.layerSwitcher.urlhash = this, this._onHashChange(window.location.hash), e.on("moveend", () => {
25
+ this._updateHash();
26
+ }), window.addEventListener("hashchange", () => {
27
+ this._onHashChange(window.location.hash);
28
+ }, !1);
29
+ }
30
+ registerHandler(e, t) {
31
+ this.handlers[e] = t;
32
+ }
33
+ setParameter(e, t) {
34
+ this.additional[e] !== t && (t === null ? delete this.additional[e] : this.additional[e] = t, this._updateHash());
35
+ }
36
+ _fireParameterChange(e, t) {
37
+ this.handlers[e] && this.handlers[e](t);
38
+ }
39
+ _onHashChange(e) {
40
+ let t = r(e), n = [...Object.keys(t.additional), ...Object.keys(this.additional)];
41
+ for (let e of n) this.additional[e] && !t.additional[e] ? (delete this.additional[e], this._fireParameterChange(e, null)) : t.additional[e] && this.additional[e] !== t.additional[e] && (this.additional[e] = t.additional[e], this._fireParameterChange(e, t.additional[e]));
42
+ this._map?.isStyleLoaded() && t.center && t.zoom && this._map.jumpTo({
43
+ center: t.center,
44
+ zoom: t.zoom
45
+ }), this.layerSwitcher && t.layers !== void 0 && this.layerSwitcher.setURLString(t.layers);
46
+ }
47
+ _updateHash() {
48
+ let e = this.getHashString();
49
+ try {
50
+ window.history.replaceState(window.history.state, "", e);
51
+ } catch (e) {
52
+ console.log(e);
53
+ }
54
+ }
55
+ getHashString() {
56
+ if (!this._map) throw Error("getHashString called before map initialised");
57
+ let { lng: e, lat: t } = this._map.getCenter(), n = {
58
+ center: [e, t],
59
+ zoom: this._map.getZoom(),
60
+ additional: this.additional
61
+ };
62
+ return this.layerSwitcher && (n.layers = this.layerSwitcher.getURLString()), i(n);
63
+ }
64
+ init(e) {
65
+ e.hash = !1;
66
+ let t = window.location.hash.replace("#", "").split("/");
67
+ return t.length >= 3 && (e.center = [+t[2], +t[1]], e.zoom = +t[0]), e;
68
+ }
69
+ };
70
+ //#endregion
71
+ export { e as Layer, t as LayerGroup, n as LayerSwitcher, a as URLHash };