maplibre-gl-layer 0.2.2 → 0.2.3

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
@@ -42,6 +42,9 @@ const layerControl = new LayerControl({
42
42
  }
43
43
  ]
44
44
  })
45
+ layerControl.on('layerchange', (layer) => {
46
+ console.log('Layer changed to:', layer.name)
47
+ })
45
48
  map.addControl(layerControl)
46
49
  ```
47
50
 
@@ -16,9 +16,11 @@ export declare class LayerControl implements IControl {
16
16
  private addedSources;
17
17
  private addedLayers;
18
18
  private layerDialog;
19
+ private layerChangeCallback;
19
20
  constructor(options: LayerControlOptions);
20
21
  onAdd(map: Map): HTMLElement;
21
22
  onRemove(): void;
23
+ on(type: string, callback: (layer: Layer) => void): void;
22
24
  private getPosition;
23
25
  private createButton;
24
26
  private changeLayer;
@@ -1,11 +1,11 @@
1
- var u = Object.defineProperty;
2
- var g = (c, e, t) => e in c ? u(c, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[e] = t;
3
- var a = (c, e, t) => g(c, typeof e != "symbol" ? e + "" : e, t);
4
- class m {
1
+ var g = Object.defineProperty;
2
+ var m = (c, e, t) => e in c ? g(c, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[e] = t;
3
+ var s = (c, e, t) => m(c, typeof e != "symbol" ? e + "" : e, t);
4
+ class b {
5
5
  constructor(e, t) {
6
- a(this, "dialog");
7
- a(this, "options");
8
- a(this, "layerChangeCallback", () => {
6
+ s(this, "dialog");
7
+ s(this, "options");
8
+ s(this, "layerChangeCallback", () => {
9
9
  });
10
10
  this.options = e, this.dialog = document.createElement("dialog"), this.dialog.classList.add("maplibregl-ctrl-layer-dialog"), document.body.appendChild(this.dialog), this.dialog.addEventListener("click", () => {
11
11
  this.dialog.close();
@@ -42,17 +42,19 @@ class m {
42
42
  t === "top-right" ? (l = `${i.y}px`, o = `${i.x - this.dialog.clientWidth - 12}px`) : t === "top-left" ? (l = `${i.y}px`, o = `${i.x + i.width + 12}px`) : t === "bottom-right" ? (l = `${i.y - this.dialog.clientHeight + i.height}px`, o = `${i.x - this.dialog.clientWidth - 12}px`) : t === "bottom-left" && (l = `${i.y - this.dialog.clientHeight + i.height}px`, o = `${i.x + i.width + 12}px`), this.dialog.style.setProperty("top", l), this.dialog.style.setProperty("left", o);
43
43
  }
44
44
  }
45
- class b {
45
+ class L {
46
46
  constructor(e) {
47
- a(this, "map", null);
48
- a(this, "options");
49
- a(this, "container");
50
- a(this, "currentLayer");
51
- a(this, "currentInitStyle", null);
52
- a(this, "addedSources", {});
53
- a(this, "addedLayers", []);
54
- a(this, "layerDialog");
55
- this.options = e, this.container = document.createElement("div"), this.container.classList.add("maplibregl-ctrl", "maplibregl-ctrl-group"), this.currentLayer = e.layers.find((t) => t.checked) || e.layers[0], this.layerDialog = new m(this.options, this.currentLayer), this.layerDialog.on("layerchange", (t) => {
47
+ s(this, "map", null);
48
+ s(this, "options");
49
+ s(this, "container");
50
+ s(this, "currentLayer");
51
+ s(this, "currentInitStyle", null);
52
+ s(this, "addedSources", {});
53
+ s(this, "addedLayers", []);
54
+ s(this, "layerDialog");
55
+ s(this, "layerChangeCallback", () => {
56
+ });
57
+ this.options = e, this.container = document.createElement("div"), this.container.classList.add("maplibregl-ctrl", "maplibregl-ctrl-group"), this.currentLayer = e.layers.find((t) => t.checked) || e.layers[0], this.layerDialog = new b(this.options, this.currentLayer), this.layerDialog.on("layerchange", (t) => {
56
58
  this.changeLayer(t), this.layerDialog.close();
57
59
  });
58
60
  }
@@ -69,6 +71,9 @@ class b {
69
71
  var e;
70
72
  (e = this.container.parentNode) == null || e.removeChild(this.container);
71
73
  }
74
+ on(e, t) {
75
+ e === "layerchange" && (this.layerChangeCallback = t);
76
+ }
72
77
  getPosition() {
73
78
  if (!this.map) return null;
74
79
  const e = this.map._controlPositions;
@@ -81,39 +86,40 @@ class b {
81
86
  return t.classList.add("maplibregl-ctrl-icon"), e.appendChild(t), e;
82
87
  }
83
88
  changeLayer(e) {
89
+ var u;
84
90
  if (!this.map) return;
85
91
  const t = this.currentInitStyle.layers, i = this.map.getStyle().layers, l = i.filter(
86
- (r) => !t.some((s) => s.id === r.id)
92
+ (a) => !t.some((r) => r.id === a.id)
87
93
  );
88
94
  this.addedLayers = [...this.addedLayers, ...l];
89
95
  const o = t.filter(
90
- (r) => !i.some((s) => s.id === r.id)
96
+ (a) => !i.some((r) => r.id === a.id)
91
97
  );
92
98
  this.addedLayers = this.addedLayers.filter(
93
- (r) => !o.some((s) => s.id === r.id)
94
- ), this.addedLayers.forEach((r) => {
95
- const s = i.find((y) => y.id === r.id);
96
- r.filter = s == null ? void 0 : s.filter;
99
+ (a) => !o.some((r) => r.id === a.id)
100
+ ), this.addedLayers.forEach((a) => {
101
+ const r = i.find((y) => y.id === a.id);
102
+ a.filter = r == null ? void 0 : r.filter;
97
103
  });
98
104
  const d = this.currentInitStyle.sources, n = this.map.getStyle().sources, h = {};
99
- Object.entries(n).forEach(([r, s]) => {
100
- Object.keys(d).includes(r) || (h[r] = s);
105
+ Object.entries(n).forEach(([a, r]) => {
106
+ Object.keys(d).includes(a) || (h[a] = r);
101
107
  }), this.addedSources = { ...this.addedSources, ...h }, Object.keys(d).filter(
102
- (r) => !Object.keys(n).includes(r)
103
- ).forEach((r) => {
104
- delete this.addedSources[r];
108
+ (a) => !Object.keys(n).includes(a)
109
+ ).forEach((a) => {
110
+ delete this.addedSources[a];
105
111
  }), this.map.setStyle(e.style, {
106
- transformStyle: (r, s) => {
112
+ transformStyle: (a, r) => {
107
113
  const y = {
108
- ...s,
109
- sources: { ...s.sources, ...this.addedSources },
110
- layers: s.layers.concat(this.addedLayers)
114
+ ...r,
115
+ sources: { ...r.sources, ...this.addedSources },
116
+ layers: r.layers.concat(this.addedLayers)
111
117
  };
112
118
  return this.currentInitStyle = y, y;
113
119
  }
114
- });
120
+ }), this.currentLayer = e, (u = this.layerChangeCallback) == null || u.call(this, e);
115
121
  }
116
122
  }
117
123
  export {
118
- b as LayerControl
124
+ L as LayerControl
119
125
  };
@@ -1 +1 @@
1
- (function(o,l){typeof exports=="object"&&typeof module<"u"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(o=typeof globalThis<"u"?globalThis:o||self,l(o["maplibre-gl-layer"]={}))})(this,function(o){"use strict";var f=Object.defineProperty;var b=(o,l,h)=>l in o?f(o,l,{enumerable:!0,configurable:!0,writable:!0,value:h}):o[l]=h;var n=(o,l,h)=>b(o,typeof l!="symbol"?l+"":l,h);class l{constructor(e,t){n(this,"dialog");n(this,"options");n(this,"layerChangeCallback",()=>{});this.options=e,this.dialog=document.createElement("dialog"),this.dialog.classList.add("maplibregl-ctrl-layer-dialog"),document.body.appendChild(this.dialog),this.dialog.addEventListener("click",()=>{this.dialog.close()});const i=document.createElement("div");i.classList.add("maplibregl-ctrl-layer-dialog-content"),i.addEventListener("click",a=>{a.stopPropagation()}),this.dialog.appendChild(i),this.options.layers.forEach(a=>{const d=document.createElement("div");d.classList.add("maplibregl-ctrl-layer-dialog-item");const p=document.createElement("label");d.appendChild(p);const c=document.createElement("input");c.setAttribute("type","radio"),c.setAttribute("name","layer"),t.name===a.name&&c.setAttribute("checked","checked"),c.addEventListener("change",()=>{var g;(g=this.layerChangeCallback)==null||g.call(this,a)}),p.appendChild(c);const u=document.createElement("span");u.textContent=a.name,p.appendChild(u),i.appendChild(d)})}open(e,t=null){this.dialog.showModal(),this.setPosition(e,t)}close(){this.dialog.close()}on(e,t){e==="layerchange"&&(this.layerChangeCallback=t)}setPosition(e,t){const i=e.getBoundingClientRect();let a="",d="";t==="top-right"?(a=`${i.y}px`,d=`${i.x-this.dialog.clientWidth-12}px`):t==="top-left"?(a=`${i.y}px`,d=`${i.x+i.width+12}px`):t==="bottom-right"?(a=`${i.y-this.dialog.clientHeight+i.height}px`,d=`${i.x-this.dialog.clientWidth-12}px`):t==="bottom-left"&&(a=`${i.y-this.dialog.clientHeight+i.height}px`,d=`${i.x+i.width+12}px`),this.dialog.style.setProperty("top",a),this.dialog.style.setProperty("left",d)}}class h{constructor(e){n(this,"map",null);n(this,"options");n(this,"container");n(this,"currentLayer");n(this,"currentInitStyle",null);n(this,"addedSources",{});n(this,"addedLayers",[]);n(this,"layerDialog");this.options=e,this.container=document.createElement("div"),this.container.classList.add("maplibregl-ctrl","maplibregl-ctrl-group"),this.currentLayer=e.layers.find(t=>t.checked)||e.layers[0],this.layerDialog=new l(this.options,this.currentLayer),this.layerDialog.on("layerchange",t=>{this.changeLayer(t),this.layerDialog.close()})}onAdd(e){this.map=e,this.currentInitStyle=this.map.getStyle(),this.currentInitStyle||this.map.once("style.load",()=>{this.map&&(this.currentInitStyle=this.map.getStyle())});const t=this.createButton();return t.addEventListener("click",()=>{this.layerDialog.open(t,this.getPosition())}),this.container.appendChild(t),this.container}onRemove(){var e;(e=this.container.parentNode)==null||e.removeChild(this.container)}getPosition(){if(!this.map)return null;const e=this.map._controlPositions;return e["top-left"].querySelector(".maplibregl-ctrl-layer")?"top-left":e["top-right"].querySelector(".maplibregl-ctrl-layer")?"top-right":e["bottom-left"].querySelector(".maplibregl-ctrl-layer")?"bottom-left":e["bottom-right"].querySelector(".maplibregl-ctrl-layer")?"bottom-right":null}createButton(){const e=document.createElement("button");e.classList.add("maplibregl-ctrl-layer"),e.setAttribute("title","Change map style"),e.setAttribute("aria-label","Change map style");const t=document.createElement("i");return t.classList.add("maplibregl-ctrl-icon"),e.appendChild(t),e}changeLayer(e){if(!this.map)return;const t=this.currentInitStyle.layers,i=this.map.getStyle().layers,a=i.filter(r=>!t.some(s=>s.id===r.id));this.addedLayers=[...this.addedLayers,...a];const d=t.filter(r=>!i.some(s=>s.id===r.id));this.addedLayers=this.addedLayers.filter(r=>!d.some(s=>s.id===r.id)),this.addedLayers.forEach(r=>{const s=i.find(y=>y.id===r.id);r.filter=s==null?void 0:s.filter});const p=this.currentInitStyle.sources,c=this.map.getStyle().sources,u={};Object.entries(c).forEach(([r,s])=>{Object.keys(p).includes(r)||(u[r]=s)}),this.addedSources={...this.addedSources,...u},Object.keys(p).filter(r=>!Object.keys(c).includes(r)).forEach(r=>{delete this.addedSources[r]}),this.map.setStyle(e.style,{transformStyle:(r,s)=>{const y={...s,sources:{...s.sources,...this.addedSources},layers:s.layers.concat(this.addedLayers)};return this.currentInitStyle=y,y}})}}o.LayerControl=h,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
1
+ (function(n,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(n=typeof globalThis<"u"?globalThis:n||self,a(n["maplibre-gl-layer"]={}))})(this,function(n){"use strict";var b=Object.defineProperty;var L=(n,a,h)=>a in n?b(n,a,{enumerable:!0,configurable:!0,writable:!0,value:h}):n[a]=h;var o=(n,a,h)=>L(n,typeof a!="symbol"?a+"":a,h);class a{constructor(e,t){o(this,"dialog");o(this,"options");o(this,"layerChangeCallback",()=>{});this.options=e,this.dialog=document.createElement("dialog"),this.dialog.classList.add("maplibregl-ctrl-layer-dialog"),document.body.appendChild(this.dialog),this.dialog.addEventListener("click",()=>{this.dialog.close()});const i=document.createElement("div");i.classList.add("maplibregl-ctrl-layer-dialog-content"),i.addEventListener("click",s=>{s.stopPropagation()}),this.dialog.appendChild(i),this.options.layers.forEach(s=>{const d=document.createElement("div");d.classList.add("maplibregl-ctrl-layer-dialog-item");const y=document.createElement("label");d.appendChild(y);const c=document.createElement("input");c.setAttribute("type","radio"),c.setAttribute("name","layer"),t.name===s.name&&c.setAttribute("checked","checked"),c.addEventListener("change",()=>{var g;(g=this.layerChangeCallback)==null||g.call(this,s)}),y.appendChild(c);const u=document.createElement("span");u.textContent=s.name,y.appendChild(u),i.appendChild(d)})}open(e,t=null){this.dialog.showModal(),this.setPosition(e,t)}close(){this.dialog.close()}on(e,t){e==="layerchange"&&(this.layerChangeCallback=t)}setPosition(e,t){const i=e.getBoundingClientRect();let s="",d="";t==="top-right"?(s=`${i.y}px`,d=`${i.x-this.dialog.clientWidth-12}px`):t==="top-left"?(s=`${i.y}px`,d=`${i.x+i.width+12}px`):t==="bottom-right"?(s=`${i.y-this.dialog.clientHeight+i.height}px`,d=`${i.x-this.dialog.clientWidth-12}px`):t==="bottom-left"&&(s=`${i.y-this.dialog.clientHeight+i.height}px`,d=`${i.x+i.width+12}px`),this.dialog.style.setProperty("top",s),this.dialog.style.setProperty("left",d)}}class h{constructor(e){o(this,"map",null);o(this,"options");o(this,"container");o(this,"currentLayer");o(this,"currentInitStyle",null);o(this,"addedSources",{});o(this,"addedLayers",[]);o(this,"layerDialog");o(this,"layerChangeCallback",()=>{});this.options=e,this.container=document.createElement("div"),this.container.classList.add("maplibregl-ctrl","maplibregl-ctrl-group"),this.currentLayer=e.layers.find(t=>t.checked)||e.layers[0],this.layerDialog=new a(this.options,this.currentLayer),this.layerDialog.on("layerchange",t=>{this.changeLayer(t),this.layerDialog.close()})}onAdd(e){this.map=e,this.currentInitStyle=this.map.getStyle(),this.currentInitStyle||this.map.once("style.load",()=>{this.map&&(this.currentInitStyle=this.map.getStyle())});const t=this.createButton();return t.addEventListener("click",()=>{this.layerDialog.open(t,this.getPosition())}),this.container.appendChild(t),this.container}onRemove(){var e;(e=this.container.parentNode)==null||e.removeChild(this.container)}on(e,t){e==="layerchange"&&(this.layerChangeCallback=t)}getPosition(){if(!this.map)return null;const e=this.map._controlPositions;return e["top-left"].querySelector(".maplibregl-ctrl-layer")?"top-left":e["top-right"].querySelector(".maplibregl-ctrl-layer")?"top-right":e["bottom-left"].querySelector(".maplibregl-ctrl-layer")?"bottom-left":e["bottom-right"].querySelector(".maplibregl-ctrl-layer")?"bottom-right":null}createButton(){const e=document.createElement("button");e.classList.add("maplibregl-ctrl-layer"),e.setAttribute("title","Change map style"),e.setAttribute("aria-label","Change map style");const t=document.createElement("i");return t.classList.add("maplibregl-ctrl-icon"),e.appendChild(t),e}changeLayer(e){var m;if(!this.map)return;const t=this.currentInitStyle.layers,i=this.map.getStyle().layers,s=i.filter(l=>!t.some(r=>r.id===l.id));this.addedLayers=[...this.addedLayers,...s];const d=t.filter(l=>!i.some(r=>r.id===l.id));this.addedLayers=this.addedLayers.filter(l=>!d.some(r=>r.id===l.id)),this.addedLayers.forEach(l=>{const r=i.find(p=>p.id===l.id);l.filter=r==null?void 0:r.filter});const y=this.currentInitStyle.sources,c=this.map.getStyle().sources,u={};Object.entries(c).forEach(([l,r])=>{Object.keys(y).includes(l)||(u[l]=r)}),this.addedSources={...this.addedSources,...u},Object.keys(y).filter(l=>!Object.keys(c).includes(l)).forEach(l=>{delete this.addedSources[l]}),this.map.setStyle(e.style,{transformStyle:(l,r)=>{const p={...r,sources:{...r.sources,...this.addedSources},layers:r.layers.concat(this.addedLayers)};return this.currentInitStyle=p,p}}),this.currentLayer=e,(m=this.layerChangeCallback)==null||m.call(this,e)}}n.LayerControl=h,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maplibre-gl-layer",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "A layer control for MapLibre GL JS",
5
5
  "type": "module",
6
6
  "files": [