2025-07-06 11:13:17 +03:00
|
|
|
import ViewMode, { ViewModeArgs } from "../view_mode.js";
|
2025-07-06 11:30:24 +03:00
|
|
|
import L from "leaflet";
|
2025-07-06 11:36:59 +03:00
|
|
|
import type { LatLng, Map } from "leaflet";
|
2025-07-06 11:47:37 +03:00
|
|
|
import SpacedUpdate from "../../../services/spaced_update.js";
|
|
|
|
|
import { t } from "../../../services/i18n.js";
|
2025-07-06 11:13:17 +03:00
|
|
|
|
|
|
|
|
const TPL = /*html*/`
|
|
|
|
|
<div class="geo-view">
|
2025-07-06 11:15:28 +03:00
|
|
|
<style>
|
|
|
|
|
.geo-view {
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
position: relative;
|
|
|
|
|
height: 100%;
|
|
|
|
|
}
|
2025-07-06 11:30:24 +03:00
|
|
|
|
|
|
|
|
.geo-map-container {
|
|
|
|
|
height: 100%;
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
}
|
2025-07-06 11:15:28 +03:00
|
|
|
</style>
|
|
|
|
|
|
2025-07-06 11:30:24 +03:00
|
|
|
<div class="geo-map-container"></div>
|
2025-07-06 11:13:17 +03:00
|
|
|
</div>`;
|
|
|
|
|
|
2025-07-06 11:36:59 +03:00
|
|
|
interface MapData {
|
|
|
|
|
view?: {
|
|
|
|
|
center?: LatLng | [number, number];
|
|
|
|
|
zoom?: number;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const DEFAULT_COORDINATES: [number, number] = [3.878638227135724, 446.6630455551659];
|
|
|
|
|
const DEFAULT_ZOOM = 2;
|
|
|
|
|
|
|
|
|
|
export default class GeoView extends ViewMode<MapData> {
|
2025-07-06 11:13:17 +03:00
|
|
|
|
|
|
|
|
private $root: JQuery<HTMLElement>;
|
2025-07-06 11:30:24 +03:00
|
|
|
private $container!: JQuery<HTMLElement>;
|
2025-07-06 11:36:59 +03:00
|
|
|
private map?: Map;
|
2025-07-06 11:47:37 +03:00
|
|
|
private spacedUpdate: SpacedUpdate;
|
2025-07-06 11:13:17 +03:00
|
|
|
|
|
|
|
|
constructor(args: ViewModeArgs) {
|
|
|
|
|
super(args, "geoMap");
|
|
|
|
|
this.$root = $(TPL);
|
2025-07-06 11:30:24 +03:00
|
|
|
this.$container = this.$root.find(".geo-map-container");
|
2025-07-06 11:47:37 +03:00
|
|
|
this.spacedUpdate = new SpacedUpdate(() => this.onSave(), 5_000);
|
2025-07-06 11:13:17 +03:00
|
|
|
args.$parent.append(this.$root);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async renderList() {
|
2025-07-06 11:30:24 +03:00
|
|
|
this.renderMap();
|
2025-07-06 11:13:17 +03:00
|
|
|
return this.$root;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 11:30:24 +03:00
|
|
|
async renderMap() {
|
|
|
|
|
const map = L.map(this.$container[0], {
|
|
|
|
|
worldCopyJump: true
|
|
|
|
|
});
|
|
|
|
|
L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
|
|
|
|
|
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
|
|
|
|
|
detectRetina: true
|
|
|
|
|
}).addTo(map);
|
|
|
|
|
|
|
|
|
|
this.map = map;
|
2025-07-06 11:36:59 +03:00
|
|
|
|
|
|
|
|
this.#onMapInitialized();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async #onMapInitialized() {
|
2025-07-06 11:47:37 +03:00
|
|
|
const map = this.map;
|
|
|
|
|
if (!map) {
|
|
|
|
|
throw new Error(t("geo-map.unable-to-load-map"));
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 11:36:59 +03:00
|
|
|
this.#restoreViewportAndZoom();
|
2025-07-06 11:47:37 +03:00
|
|
|
|
|
|
|
|
const updateFn = () => this.spacedUpdate.scheduleUpdate();
|
|
|
|
|
map.on("moveend", updateFn);
|
|
|
|
|
map.on("zoomend", updateFn);
|
|
|
|
|
// map.on("click", (e) => this.#onMapClicked(e));
|
2025-07-06 11:36:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async #restoreViewportAndZoom() {
|
|
|
|
|
const map = this.map;
|
|
|
|
|
if (!map) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parsedContent = await this.viewStorage.restore();
|
|
|
|
|
|
|
|
|
|
// Restore viewport position & zoom
|
|
|
|
|
const center = parsedContent?.view?.center ?? DEFAULT_COORDINATES;
|
|
|
|
|
const zoom = parsedContent?.view?.zoom ?? DEFAULT_ZOOM;
|
|
|
|
|
map.setView(center, zoom);
|
2025-07-06 11:30:24 +03:00
|
|
|
}
|
|
|
|
|
|
2025-07-06 11:47:37 +03:00
|
|
|
private onSave() {
|
|
|
|
|
const map = this.map;
|
|
|
|
|
let data: MapData = {};
|
|
|
|
|
if (map) {
|
|
|
|
|
data = {
|
|
|
|
|
view: {
|
|
|
|
|
center: map.getBounds().getCenter(),
|
|
|
|
|
zoom: map.getZoom()
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.viewStorage.store(data);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 11:15:28 +03:00
|
|
|
get isFullHeight(): boolean {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 11:13:17 +03:00
|
|
|
}
|