all repos — onyx @ 1a0497887a424251f799a123c70ce3dfdef40b37

minimal map annotation and location data sharing tool

src/99-onyx.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
const helpLink = "<br>ONYX v0.3.0 [ <a target='_blank' href='https://nilfm.cc/git/onyx/about/LICENSE'>license</a> | <a target='_blank' href='https://nilfm.cc/git/onyx/about'>manual</a> ]";


function init(): void {
  let overlays: OverlayState = new OverlayState();
  try {
    overlays = OverlayState.load();
  } catch {
    alert("Error reading saved data; initializing with empty data.");
  }
  const map = L.map('map').fitWorld();
  
  const streetLayer = TileLayerWrapper.constructLayer(
    "streetLayer",
    L.tileLayer(
      'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
      {
        maxZoom: 19,
        attribution: "street map data &copy; OpenStreetMap contributors" + helpLink
      }));
      
  const satelliteLayer = TileLayerWrapper.constructLayer(
    "satelliteLayer",
    L.tileLayer(
      'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
      {
        maxZoom: 19,
        attribution: "satellite data &copy; Esri" + helpLink
      }));
      
  TileLayerWrapper.enableOnly("streetLayer", map);
  
  overlays.markers.forEach(m=>m.add(map));
  overlays.circles.forEach(m=>m.add(map));
  overlays.paths.forEach(m=>m.add(map));
  overlays.polyline.add(map);

  const modals = new ModalCollection(
    new CreateOverlayModal(),
    new CancelModal(),
    new OKCancelModal(),
    new InfoModal(),
    new OverlayManagementModal(),
    new ImportExportModal());

  MapHandler.init(map, overlays, TileLayerWrapper.layers, modals);
  
  MapHandler.setButtonClick("home-btn", MapHandler.goHome);
  MapHandler.setButtonClick("addPoint-btn", MapHandler.markerCollect);
  MapHandler.setButtonClick("addCircle-btn", MapHandler.circleCollect);
  MapHandler.setButtonClick("addPolygon-btn", MapHandler.polygonCollect);
  
  MapHandler.setButtonClick("tiles-btn", MapHandler.swapTiles);  
  MapHandler.setButtonClick("save-btn", MapHandler.overlaySave);
  MapHandler.setButtonClick("clear-btn", MapHandler.overlayClear);
  MapHandler.setButtonClick("restore-btn", MapHandler.overlayReset);
  MapHandler.setButtonClick("menu-btn", MapHandler.toggleMenu);
  
  MapHandler.setButtonClick("set-home-btn", MapHandler.setHome);
  MapHandler.setButtonClick("export-all-btn", MapHandler.exportAll);
  MapHandler.setButtonClick("import-export-cancel-btn", MapHandler.closeImportExport);
  MapHandler.setButtonClick("import-btn", MapHandler.import);
  
  map.on("locationfound", MapHandler.setHome);
  
  map.on("locationerror", ()=> {
    const info = modals.info;
    info.setMsg("Could not get location data");
    info.setVisible(true);
  });
  
  // the menu doesn't open on the first click unless we do this first... not sure why
  modals.closeAll();
  
  const homeData = localStorage.getItem("home");
  if (homeData) {
    const home = <Point>JSON.parse(homeData);
    map.setView(home, 13);
  } else {
    const okCancel = modals.okCancel;
    const okBtn = okCancel.okBtn();
    if (okBtn) {
      okBtn.onclick = () => {
        modals.closeAll();
        map.locate({setView: true, maxZoom: 13});     
      }
    }
    const cancelBtn = okCancel.cancelBtn();
    if (cancelBtn) {
      cancelBtn.onclick = () => {
        modals.closeAll();
      }
    }
    okCancel.setMsg("Would you like to use location data to set Home?");
    okCancel.setVisible(true);
  }
}

init();