all repos — onyx @ c80313085e7c6f7569fb3d81500a1b5b761aae78

minimal map annotation and location data sharing tool

src/40-handlers.ts (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
class MapHandler {
  map: L.Map;
  overlays: OverlayState;
  layers: TileLayerWrapper[];
  modals: ModalCollection;
  
  static instance: MapHandler | null = null;
  
  private constructor(map: L.Map, overlays: OverlayState, layers: TileLayerWrapper[], modals: ModalCollection) {
    this.map = map;
    this.overlays = overlays;
    this.layers = layers;
    this.modals = modals;
  }
  
  static init(map: L.Map, overlays: OverlayState, layers: TileLayerWrapper[], modals: ModalCollection): void {
    if (!MapHandler.instance) {
      MapHandler.instance = new MapHandler(map, overlays, layers, modals);
    }
  }

  static setButtonClick(btnId: string, handler: any): void {
      const button = document.getElementById(btnId);
      if (button) {
        button.onclick = handler;
      }
  }
  
  static resetMapClick(): void {
    const self = MapHandler.instance;
    if (self) {
      try {
        const addPointBtn = document.getElementById("addPoint-btn");
        if (addPointBtn) {
          addPointBtn.classList.remove("activeBtn");
        }
        self.map.off("click", this.addMarker);
      } catch {}
      try {
        const addCircleBtn = document.getElementById("addCircle-btn");
        if (addCircleBtn) {
          addCircleBtn.classList.remove("activeBtn");
        }
        self.map.off("click", this.addCircle);
      } catch {}
    }
  }
  
  static addMarker(e: any): void {
    const self = MapHandler.instance;
    if (self) {
      self.modals.createOverlay.setVisible(true);
      self.modals.createOverlay.setState(OverlayType.POINT, {
        latlng: e.latlng,
        map: self.map,
        overlays: self.overlays,
      });    
      MapHandler.resetMapClick();
    }
  }
  
  static addCircle(e: any): void {
    const self = MapHandler.instance;
    if (self) {
      self.modals.createOverlay.setVisible(true);
      self.modals.createOverlay.setState(OverlayType.CIRCLE, {
        latlng: e.latlng,
        map: self.map,
        overlays: self.overlays,
      });    
      MapHandler.resetMapClick();
    }
  }
  
  static circleCollect(e: any): void {
    const self = MapHandler.instance;
    if (self) {
      self.modals.closeAll();
      MapHandler.resetMapClick();
      (e.target as HTMLElement).classList.add("activeBtn");
      self.map.on("click", MapHandler.addCircle);
    }
  }
  
  static markerCollect(e: any): void {
    const self = MapHandler.instance;
    if (self) {
      self.modals.closeAll();
      MapHandler.resetMapClick();
      (e.target as HTMLElement).classList.add("activeBtn");
      self.map.on("click", MapHandler.addMarker);
    }
  }
  
  static overlaySave(e: any): void {
    const self = MapHandler.instance;
    if (self) {
      OverlayState.save(self.overlays);
    }
  }
  
  static overlayClear(e: any): void {
    const self = MapHandler.instance;
    if (self) {
      self.overlays = OverlayState.clear(self.overlays, self.map);
    }
  }
  
  static swapTiles(e: any): void {
    const self = MapHandler.instance;
    if (self) {
      if (TileLayerWrapper.getActiveLayer() == "satelliteLayer") {
        TileLayerWrapper.enableOnly("streetLayer", self.map);
      } else {
        TileLayerWrapper.enableOnly("satelliteLayer", self.map);
      }
    }
  }
}