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-24 15:46:11 +03:00
|
|
|
import type { GPX, LatLng, Layer, LeafletMouseEvent, Map, Marker } 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 12:19:04 +03:00
|
|
|
import processNoteWithMarker, { processNoteWithGpxTrack } from "./markers.js";
|
2025-07-06 17:51:24 +03:00
|
|
|
import { hasTouchBar } from "../../../services/utils.js";
|
|
|
|
|
import toast from "../../../services/toast.js";
|
2025-07-06 18:25:44 +03:00
|
|
|
import { CommandListenerData, EventData } from "../../../components/app_context.js";
|
2025-07-07 18:04:00 +03:00
|
|
|
import { createNewNote, moveMarker, setupDragging } from "./editing.js";
|
2025-07-06 23:34:07 +03:00
|
|
|
import { openMapContextMenu } from "./context_menu.js";
|
2025-07-24 15:07:47 +03:00
|
|
|
import attributes from "../../../services/attributes.js";
|
2025-07-24 15:52:01 +03:00
|
|
|
import { DEFAULT_MAP_LAYER_NAME, MAP_LAYERS } from "./map_layer.js";
|
2025-07-06 11:13:17 +03:00
|
|
|
|
2025-07-06 11:36:59 +03:00
|
|
|
|
2025-07-06 17:51:24 +03:00
|
|
|
enum State {
|
|
|
|
|
Normal,
|
|
|
|
|
NewNote
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 11:36:59 +03:00
|
|
|
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 17:51:24 +03:00
|
|
|
private _state: State;
|
2025-07-06 18:25:44 +03:00
|
|
|
private ignoreNextZoomEvent?: boolean;
|
2025-07-06 11:13:17 +03:00
|
|
|
|
2025-07-06 12:12:59 +03:00
|
|
|
private currentMarkerData: Record<string, Marker>;
|
|
|
|
|
private currentTrackData: Record<string, GPX>;
|
|
|
|
|
|
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 12:12:59 +03:00
|
|
|
|
|
|
|
|
this.currentMarkerData = {};
|
|
|
|
|
this.currentTrackData = {};
|
2025-07-06 17:51:24 +03:00
|
|
|
this._state = State.Normal;
|
2025-07-06 12:12:59 +03:00
|
|
|
|
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() {
|
2025-09-03 23:17:35 +03:00
|
|
|
const layerName = this.parentNote.getLabelValue("map:style") ?? ;
|
2025-07-24 15:46:11 +03:00
|
|
|
|
|
|
|
|
|
2025-07-24 22:14:31 +03:00
|
|
|
if (this.parentNote.hasLabel("map:scale")) {
|
|
|
|
|
L.control.scale().addTo(map);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 15:46:11 +03:00
|
|
|
this.$container.toggleClass("dark", !!layerData.isDarkTheme);
|
|
|
|
|
|
2025-07-24 15:07:47 +03:00
|
|
|
layer.addTo(map);
|
2025-07-06 11:30:24 +03:00
|
|
|
|
|
|
|
|
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-07 19:04:47 +03:00
|
|
|
const isEditable = !this.isReadOnly;
|
2025-07-06 23:34:07 +03:00
|
|
|
map.on("click", (e) => this.#onMapClicked(e))
|
2025-07-07 19:04:47 +03:00
|
|
|
map.on("contextmenu", (e) => openMapContextMenu(this.parentNote.noteId, e, isEditable));
|
|
|
|
|
|
|
|
|
|
if (isEditable) {
|
|
|
|
|
setupDragging(this.$container, map, this.parentNote.noteId);
|
|
|
|
|
}
|
2025-07-06 12:12:59 +03:00
|
|
|
|
|
|
|
|
this.#reloadMarkers();
|
2025-07-06 18:25:44 +03:00
|
|
|
|
|
|
|
|
if (hasTouchBar) {
|
|
|
|
|
map.on("zoom", () => {
|
|
|
|
|
if (!this.ignoreNextZoomEvent) {
|
|
|
|
|
this.triggerCommand("refreshTouchBar");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.ignoreNextZoomEvent = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-07-06 11:36:59 +03:00
|
|
|
}
|
|
|
|
|
|
2025-07-06 12:12:59 +03:00
|
|
|
async #reloadMarkers() {
|
|
|
|
|
if (!this.map) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete all existing markers
|
|
|
|
|
for (const marker of Object.values(this.currentMarkerData)) {
|
|
|
|
|
marker.remove();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete all existing tracks
|
|
|
|
|
for (const track of Object.values(this.currentTrackData)) {
|
|
|
|
|
track.remove();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the new markers.
|
|
|
|
|
this.currentMarkerData = {};
|
2025-07-16 22:13:04 +03:00
|
|
|
const notes = await this.parentNote.getSubtreeNotes();
|
2025-07-07 19:04:47 +03:00
|
|
|
const draggable = !this.isReadOnly;
|
2025-07-06 17:54:13 +03:00
|
|
|
for (const childNote of notes) {
|
2025-07-06 12:12:59 +03:00
|
|
|
if (childNote.mime === "application/gpx+xml") {
|
2025-07-06 12:19:04 +03:00
|
|
|
const track = await processNoteWithGpxTrack(this.map, childNote);
|
|
|
|
|
this.currentTrackData[childNote.noteId] = track;
|
2025-07-06 12:12:59 +03:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (latLng) {
|
2025-07-07 19:04:47 +03:00
|
|
|
const marker = processNoteWithMarker(this.map, childNote, latLng, draggable);
|
2025-07-06 12:12:59 +03:00
|
|
|
this.currentMarkerData[childNote.noteId] = marker;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 17:51:24 +03:00
|
|
|
#changeState(newState: State) {
|
|
|
|
|
this._state = newState;
|
|
|
|
|
this.$container.toggleClass("placing-note", newState === State.NewNote);
|
|
|
|
|
if (hasTouchBar) {
|
|
|
|
|
this.triggerCommand("refreshTouchBar");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-14 22:52:37 +03:00
|
|
|
async onEntitiesReloaded({ loadResults }: EventData<"entitiesReloaded">) {
|
2025-07-06 17:51:24 +03:00
|
|
|
// If any of the children branches are altered.
|
|
|
|
|
if (loadResults.getBranchRows().find((branch) => branch.parentNoteId === this.parentNote.noteId)) {
|
|
|
|
|
this.#reloadMarkers();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If any of note has its location attribute changed.
|
|
|
|
|
// TODO: Should probably filter by parent here as well.
|
|
|
|
|
const attributeRows = loadResults.getAttributeRows();
|
2025-07-24 22:46:36 +03:00
|
|
|
if (attributeRows.find((at) => [LOCATION_ATTRIBUTE, "color", "iconClass"].includes(at.name ?? ""))) {
|
2025-07-06 17:51:24 +03:00
|
|
|
this.#reloadMarkers();
|
|
|
|
|
}
|
2025-07-24 15:07:47 +03:00
|
|
|
|
|
|
|
|
// Full reload if map layer is changed.
|
2025-07-24 22:18:30 +03:00
|
|
|
if (loadResults.getAttributeRows().some(attr => (attr.name?.startsWith("map:") && attributes.isAffecting(attr, this.parentNote)))) {
|
2025-07-24 15:07:47 +03:00
|
|
|
return true;
|
|
|
|
|
}
|
2025-07-06 17:51:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async geoMapCreateChildNoteEvent({ ntxId }: EventData<"geoMapCreateChildNote">) {
|
|
|
|
|
toast.showPersistent({
|
|
|
|
|
icon: "plus",
|
|
|
|
|
id: "geo-new-note",
|
|
|
|
|
title: "New note",
|
|
|
|
|
message: t("geo-map.create-child-note-instruction")
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.#changeState(State.NewNote);
|
|
|
|
|
|
|
|
|
|
const globalKeyListener: (this: Window, ev: KeyboardEvent) => any = (e) => {
|
|
|
|
|
if (e.key !== "Escape") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.#changeState(State.Normal);
|
|
|
|
|
|
|
|
|
|
window.removeEventListener("keydown", globalKeyListener);
|
|
|
|
|
toast.closePersistent("geo-new-note");
|
|
|
|
|
};
|
|
|
|
|
window.addEventListener("keydown", globalKeyListener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async #onMapClicked(e: LeafletMouseEvent) {
|
|
|
|
|
if (this._state !== State.NewNote) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toast.closePersistent("geo-new-note");
|
2025-07-06 23:34:07 +03:00
|
|
|
await createNewNote(this.parentNote.noteId, e);
|
2025-07-06 17:51:24 +03:00
|
|
|
this.#changeState(State.Normal);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 18:36:36 +03:00
|
|
|
deleteFromMapEvent({ noteId }: EventData<"deleteFromMap">) {
|
|
|
|
|
moveMarker(noteId, null);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 18:25:44 +03:00
|
|
|
buildTouchBarCommand({ TouchBar }: CommandListenerData<"buildTouchBar">) {
|
|
|
|
|
const map = this.map;
|
|
|
|
|
const that = this;
|
|
|
|
|
if (!map) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
new TouchBar.TouchBarSlider({
|
|
|
|
|
label: "Zoom",
|
|
|
|
|
value: map.getZoom(),
|
|
|
|
|
minValue: map.getMinZoom(),
|
|
|
|
|
maxValue: map.getMaxZoom(),
|
|
|
|
|
change(newValue) {
|
|
|
|
|
that.ignoreNextZoomEvent = true;
|
|
|
|
|
map.setZoom(newValue);
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
new TouchBar.TouchBarButton({
|
|
|
|
|
label: "New geo note",
|
|
|
|
|
click: () => this.triggerCommand("geoMapCreateChildNote"),
|
|
|
|
|
enabled: (this._state === State.Normal)
|
|
|
|
|
})
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 11:13:17 +03:00
|
|
|
}
|
2025-07-24 15:35:03 +03:00
|
|
|
|