all repos — onyx @ c0a33feb7c8955acc019c1336d6992c79c07cd7d

minimal map annotation and location data sharing tool

src/onyx-scry.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
class Point implements L.LatLngLiteral {
  lat: number = 0.00;
  lng: number = 0.00;
}

enum OverlayType {
  POINT = 0,
  CIRCLE = 1,
  POLYGON = 2,
}

interface Overlay {
  name: string;
  desc: string;
  points: Point[];
  options: any;
}

class OverlayData implements Overlay {
  name: string;
  desc: string;
  points: Point[];
  options: any;
  type: OverlayType;
  
  constructor(type: OverlayType, name: string, desc: string, points: Point[], options: any) {
    this.type = type;
    this.name = name;
    this.desc = desc;
    this.points = points;
    this.options = options;
  }
}

abstract class OverlayBase implements Overlay {
  name: string;
  desc: string;
  points: Point[];
  options: any;
  protected self: any;
  
  constructor(name: string, desc: string, points: Point[], options: any) {
    this.name = name;
    this.desc = desc;
    this.points = points;
    this.options = options;
  }
  
  add(map: L.Map): void {
    this.self.addTo(map);
  }
  
  remove(map: L.Map): void {
    this.self.removeFrom(map);
  }
}


class Marker extends OverlayBase {
  
  constructor(name: string, desc: string, point: Point, options: any) {
    super(name, desc, [ point ], options);
    this.self = L.marker(point);
    this.self.bindPopup(`<h3>${name}</h3><p>${desc}</p>`);
  }
}

class Circle extends OverlayBase {

  constructor(name: string, desc: string, point: Point, options: any) {
    super(name, desc, [ point ], options);
    this.self = L.circle(point, options);
    this.self.bindPopup(`<h3>${name}</h3><p>${desc}</p>`);
  }
}

class Polygon extends OverlayBase {

  constructor(name: string, desc: string, points: Point[], options: any) {
    super(name, desc, points, options);    
    this.self = L.polygon(points, options);
  }
}

class OverlayState {
  markers: Marker[];
  circles: Circle[];
  polygons: Polygon[];
  
  constructor() {
    this.markers = [];
    this.circles = [];
    this.polygons = [];
  }
  
  static load(): OverlayState {
    const store = localStorage.getItem("overlay_state");
    if (store) {  
      const model = JSON.parse(store);
      return {
        markers: model.markers.map((m: OverlayData) => OverlayState.fromData(m)),
        circles: model.circles.map((c: OverlayData) => OverlayState.fromData(c)),
        polygons: model.polygons.map((p: OverlayData) => OverlayState.fromData(p)),
      } as OverlayState
    } else {
      return new OverlayState();
    }
  }
  
  static save(overlayState: OverlayState): void {
    localStorage.setItem("overlay_state", JSON.stringify({
      markers: overlayState.markers.map((m: OverlayBase) => OverlayState.toData(m)),
      circles: overlayState.circles.map((c: OverlayBase) => OverlayState.toData(c)),
      polygons: overlayState.polygons.map((p: OverlayBase) => OverlayState.toData(p)),
    }));
  }
  
  static clear(overlayState: OverlayState, map: L.Map): OverlayState {
    overlayState.markers.forEach((m: Marker) => m.remove(map));
    overlayState.circles.forEach((c: Circle) => c.remove(map));
    overlayState.polygons.forEach((p: Polygon) => p.remove(map));
    
    return new OverlayState();
  }
  
  private static toData(source: OverlayBase): OverlayData {
    let type = OverlayType.POINT;
    if (source.points.length > 1) {
      type = OverlayType.POLYGON;
    } else if (source.options.radius) {
      type = OverlayType.CIRCLE;
    }
    return new OverlayData(type, source.name, source.desc, source.points, source.options);
  }
  
  private static fromData(data: OverlayData): OverlayBase {
    switch(data.type) {
      case OverlayType.POINT:
        return new Marker(data.name, data.desc, data.points[0], data.options);
      case OverlayType.CIRCLE:
        return new Circle(data.name, data.desc, data.points[0], data.options);
      case OverlayType.POLYGON:
        return new Polygon(data.name, data.desc, data.points, data.options);
    }
  }


}class TileLayerWrapper {
  self: L.TileLayer;
  name: string;
  visible: boolean = false;
  
  constructor(name: string, self: L.TileLayer) {
    this.self = self;
    this.name = name;
  }
  
  static constructLayer(name: string, self: L.TileLayer): TileLayerWrapper {
    const wrapper = new TileLayerWrapper(name, self);
    TileLayerWrapper.layers.push(wrapper);
    return wrapper;
  }
  
  static getActiveLayer(): TileLayerWrapper | null {
    for (const l of TileLayerWrapper.layers) {
      if (l.visible == true) {
        return l;
      }
    }
    return null;
  }
  static layers: TileLayerWrapper[] = new Array<TileLayerWrapper>();
  static enableOnly(self: TileLayerWrapper, map: L.Map): void {
    for (const l of TileLayerWrapper.layers) {
      if (l.visible) {
        l.self.removeFrom(map);
        l.visible = false;
      }
      if (l.name == self.name) {
        l.self.addTo(map);
        l.visible = true;
      }
    }
  }
}class CreateOverlayModal {

  constructor() {
    const _this = this;
    const closeBtn = document.getElementById("createOverlay-closeBtn");
    if (closeBtn) {
      closeBtn.onclick = ()=>{_this.setVisible(false)};
    }
  }

  self(): HTMLElement | null {
    return document.getElementById("createOverlay-container");
  }
  
  title(): HTMLElement | null{
    return document.getElementById("createOverlay-title");
  }
  
  content(): HTMLElement | null {
    return document.getElementById("createOverlay-content");
  }
  
  submitBtn(): HTMLElement | null {
    return document.getElementById("createOverlay-submitBtn");
  }
  
  radiusContainer(): HTMLElement | null {
    return document.getElementById("radius-container");
  }
  
  nameField(): string { 
    return (document.getElementById("createOverlay-name") as HTMLInputElement)?.value ?? "";
  }
  
  descField(): string {
    return (document.getElementById("createOverlay-desc") as HTMLInputElement)?.value ?? "";
  }

  radiusField(): string {
    return (document.getElementById("createOverlay-radius") as HTMLInputElement)?.value ?? "";
  }

  visible(): boolean {
    return this.self()?.style.display != "none";
  }
  
  setVisible(v: boolean): void {
    const modal = this.self();
    if (modal) {
      modal.style.display = v ? "block" : "none";
    }
  }
  
  setState(state: OverlayType, args: any): void {
    const _this = this;
    const title = this.title()
    const radiusContainer = _this.radiusContainer();
    const radius = _this.radiusField();
    const name = _this.nameField();
    const desc = _this.descField();
    const submitBtn = _this.submitBtn();
    
    switch (state) {
      case OverlayType.POINT:
        if (title) {
          title.innerHTML = "Add Marker";
        }
        if (submitBtn) {
          submitBtn.onclick = () => {
            const point = new Marker(name, desc, args.latlng, {title: name, alt: name});
            point.add(args.map);
            args.overlays.markers.push(point);
            _this.setVisible(false);
          }
        }
        break;
      case OverlayType.CIRCLE:
        if (title) {
          title.innerHTML = "Add Circle";
        }
        if (radiusContainer) {
          radiusContainer.style.display = "block";
        }
        if (submitBtn) {
          submitBtn.onclick = () => {
            const circle = new Circle(name, desc, args.latlng, {radius: Number(radius) || 500});
            circle.add(args.map);
            args.overlays.circles.push(circle);
            _this.setVisible(false);
          }
        }
        break;
      case OverlayType.POLYGON:
        break;
    }
  }
}class MapHandler {
  map: L.Map;
  overlays: OverlayState;
  layers: TileLayerWrapper[];
  
  
  constructor(map: L.Map, overlays: OverlayState, layers: TileLayerWrapper[]) {
    this.map = map;
    this.overlays = overlays;
    this.layers = layers;
  }
}
function init(): void {
  let overlays: OverlayState = OverlayState.load() ?? new OverlayState(); 
  const map = L.map('map').setView([35.6653, -105.9507], 13);

  const streetLayer = TileLayerWrapper.constructLayer(
    "streetLayer",
    L.tileLayer(
      'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
      {
        maxZoom: 19,
        attribution: "street map tiles &copy; OpenStreetMap"
      }));
      
  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 tiles &copy; Esri"
      }));
      
  TileLayerWrapper.enableOnly(streetLayer, map);
  
  overlays.markers.forEach(m=>m.add(map));
  overlays.circles.forEach(m=>m.add(map));
  overlays.polygons.forEach(m=>m.add(map));
  
  const createOverlayModal = new CreateOverlayModal();

  const closeAllModals = (): void => {
    createOverlayModal.setVisible(false);
  }

  const resetMapClick = (): void => {
    try {
      const addPointBtn = document.getElementById("addPoint-btn");
      if (addPointBtn) {
        addPointBtn.classList.remove("activeBtn");
      }
      map.off("click", addMarkerHandler);

    } catch {}
    try {
      const addCircleBtn = document.getElementById("addCircle-btn");
      if (addCircleBtn) {
        addCircleBtn.classList.remove("activeBtn");
      }
      map.off("click", addCircleHandler);
    } catch {}
  }
  
  const addMarkerHandler = (e: any): void => {
    createOverlayModal.setVisible(true);
    createOverlayModal.setState(OverlayType.POINT, {
      latlng: e.latlng,
      map: map,
      overlays: overlays,
    });    
    resetMapClick();
  }
  
  const addCircleHandler = (e: any): void => {
    createOverlayModal.setVisible(true);
    createOverlayModal.setState(OverlayType.CIRCLE, {
      latlng: e.latlng,
      map: map,
      overlays: overlays,
    });    
    resetMapClick();
  }
  
  const addMarkerBtn = document.getElementById("addPoint-btn");
  if (addMarkerBtn) {
    addMarkerBtn.onclick = (e: any): void => {
      closeAllModals();
      resetMapClick()
      addMarkerBtn.classList.add("activeBtn");
      map.on("click", addMarkerHandler);
    };
  }  
  
  const addCircleBtn = document.getElementById("addCircle-btn");
  if (addCircleBtn) {
    addCircleBtn.onclick = (e: any): void => {
      closeAllModals();
      resetMapClick();
      addCircleBtn.classList.add("activeBtn");
      map.on("click", addCircleHandler);
    }
  }
  
  const saveBtn = document.getElementById("save-btn");
  if (saveBtn) {
    saveBtn.onclick = (e: any): void => {
      OverlayState.save(overlays);
    };
  }
  
  const clearBtn = document.getElementById("clear-btn");
  if (clearBtn) {
    clearBtn.onclick = (e: any): void => {
      overlays = OverlayState.clear(overlays, map);
    }
  }
  
  const tilesBtn = document.getElementById("tiles-btn");
  if (tilesBtn) {
    tilesBtn.onclick = (e: any): void => {
      if (TileLayerWrapper.getActiveLayer() == satelliteLayer) {
        TileLayerWrapper.enableOnly(streetLayer, map);
      } else {
        TileLayerWrapper.enableOnly(satelliteLayer, map);
      }
    };
  }
  
  const main = document.getElementById("app-container");
  if (main) {
    main.style.display = "initial";
  }
}

init();