onyx/src/99-onyx-scry.ts

126 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-08-13 20:33:44 +00:00
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,
2022-08-14 04:18:00 +00:00
attribution: "street map tiles © OpenStreetMap"
2022-08-13 20:33:44 +00:00
}));
const satelliteLayer = TileLayerWrapper.constructLayer(
"satelliteLayer",
L.tileLayer(
'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
{
maxZoom: 19,
2022-08-14 04:18:00 +00:00
attribution: "satellite tiles © Esri"
2022-08-13 20:33:44 +00:00
}));
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));
2022-08-14 04:18:00 +00:00
const createOverlayModal = new CreateOverlayModal();
const closeAllModals = (): void => {
createOverlayModal.setVisible(false);
}
const resetMapClick = (): void => {
try {
2022-08-14 04:18:00 +00:00
const addPointBtn = document.getElementById("addPoint-btn");
if (addPointBtn) {
addPointBtn.classList.remove("activeBtn");
}
map.off("click", addMarkerHandler);
2022-08-14 04:18:00 +00:00
} catch {}
try {
2022-08-14 04:18:00 +00:00
const addCircleBtn = document.getElementById("addCircle-btn");
if (addCircleBtn) {
addCircleBtn.classList.remove("activeBtn");
}
map.off("click", addCircleHandler);
} catch {}
}
const addMarkerHandler = (e: any): void => {
2022-08-14 04:18:00 +00:00
createOverlayModal.setVisible(true);
createOverlayModal.setState(OverlayType.POINT, {
2022-08-13 20:33:44 +00:00
latlng: e.latlng,
map: map,
overlays: overlays,
});
2022-08-14 04:18:00 +00:00
resetMapClick();
2022-08-13 20:33:44 +00:00
}
const addCircleHandler = (e: any): void => {
2022-08-14 04:18:00 +00:00
createOverlayModal.setVisible(true);
createOverlayModal.setState(OverlayType.CIRCLE, {
latlng: e.latlng,
map: map,
overlays: overlays,
});
2022-08-14 04:18:00 +00:00
resetMapClick();
}
2022-08-13 20:33:44 +00:00
const addMarkerBtn = document.getElementById("addPoint-btn");
if (addMarkerBtn) {
addMarkerBtn.onclick = (e: any): void => {
2022-08-14 04:18:00 +00:00
closeAllModals();
resetMapClick()
2022-08-14 04:18:00 +00:00
addMarkerBtn.classList.add("activeBtn");
map.on("click", addMarkerHandler);
2022-08-13 20:33:44 +00:00
};
}
const addCircleBtn = document.getElementById("addCircle-btn");
if (addCircleBtn) {
addCircleBtn.onclick = (e: any): void => {
2022-08-14 04:18:00 +00:00
closeAllModals();
resetMapClick();
2022-08-14 04:18:00 +00:00
addCircleBtn.classList.add("activeBtn");
map.on("click", addCircleHandler);
}
}
2022-08-13 20:33:44 +00:00
const saveBtn = document.getElementById("save-btn");
if (saveBtn) {
saveBtn.onclick = (e: any): void => {
2022-08-13 20:33:44 +00:00
OverlayState.save(overlays);
};
}
const clearBtn = document.getElementById("clear-btn");
if (clearBtn) {
clearBtn.onclick = (e: any): void => {
2022-08-13 20:33:44 +00:00
overlays = OverlayState.clear(overlays, map);
}
}
const tilesBtn = document.getElementById("tiles-btn");
if (tilesBtn) {
tilesBtn.onclick = (e: any): void => {
2022-08-13 20:33:44 +00:00
if (TileLayerWrapper.getActiveLayer() == satelliteLayer) {
TileLayerWrapper.enableOnly(streetLayer, map);
} else {
TileLayerWrapper.enableOnly(satelliteLayer, map);
}
};
}
2022-08-14 04:18:00 +00:00
const main = document.getElementById("app-container");
if (main) {
main.style.display = "initial";
}
2022-08-13 20:33:44 +00:00
}
init();