Notes/apps/client/src/widgets/geo_map.ts

59 lines
1.4 KiB
TypeScript
Raw Normal View History

import type { Map } from "leaflet";
import L from "leaflet";
import "leaflet/dist/leaflet.css";
2025-01-20 18:45:56 +02:00
import NoteContextAwareWidget from "./note_context_aware_widget.js";
const TPL = /*html*/`\
2025-01-20 18:45:56 +02:00
<div class="geo-map-widget">
2025-01-20 19:18:29 +02:00
<style>
.note-detail-geo-map,
.geo-map-widget,
.geo-map-container {
height: 100%;
overflow: hidden;
}
.leaflet-top,
.leaflet-bottom {
z-index: 900;
}
2025-01-20 19:18:29 +02:00
</style>
<div class="geo-map-container"></div>
2025-03-02 20:47:57 +01:00
</div>`;
2025-01-20 19:18:29 +02:00
export type Leaflet = typeof L;
2025-03-02 20:47:57 +01:00
export type InitCallback = (L: Leaflet) => void;
2025-01-20 23:27:32 +02:00
2025-01-20 18:45:56 +02:00
export default class GeoMapWidget extends NoteContextAwareWidget {
map?: Map;
$container!: JQuery<HTMLElement>;
2025-01-20 23:27:32 +02:00
private initCallback?: InitCallback;
2025-01-20 23:27:32 +02:00
constructor(widgetMode: "type", initCallback?: InitCallback) {
2025-01-20 18:45:56 +02:00
super();
this.initCallback = initCallback;
2025-01-20 18:45:56 +02:00
}
doRender() {
2025-01-20 19:18:29 +02:00
this.$widget = $(TPL);
this.$container = this.$widget.find(".geo-map-container");
2025-01-20 19:18:29 +02:00
const map = L.map(this.$container[0], {
worldCopyJump: true
});
2025-01-20 19:20:59 +02:00
this.map = map;
if (this.initCallback) {
this.initCallback(L);
}
2025-01-20 19:20:59 +02:00
L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
detectRetina: true
}).addTo(map);
2025-01-20 18:45:56 +02:00
}
}