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 17:51:24 +03:00
|
|
|
import type { GPX, LatLng, 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 12:12:59 +03:00
|
|
|
import froca from "../../../services/froca.js";
|
2025-07-06 17:51:24 +03:00
|
|
|
import { hasTouchBar } from "../../../services/utils.js";
|
|
|
|
|
import toast from "../../../services/toast.js";
|
|
|
|
|
import { EventData } from "../../../components/app_context.js";
|
|
|
|
|
import dialog from "../../../services/dialog.js";
|
|
|
|
|
import server from "../../../services/server.js";
|
|
|
|
|
import attributes from "../../../services/attributes.js";
|
|
|
|
|
import { moveMarker } from "./editing.js";
|
|
|
|
|
|
|
|
|
|
// TODO: Deduplicate
|
|
|
|
|
interface CreateChildResponse {
|
|
|
|
|
note: {
|
|
|
|
|
noteId: string;
|
|
|
|
|
};
|
|
|
|
|
}
|
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 12:12:59 +03:00
|
|
|
|
|
|
|
|
.leaflet-pane {
|
|
|
|
|
z-index: 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.geo-map-container.placing-note {
|
|
|
|
|
cursor: crosshair;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.geo-map-container .marker-pin {
|
|
|
|
|
position: relative;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.geo-map-container .leaflet-div-icon {
|
|
|
|
|
position: relative;
|
|
|
|
|
background: transparent;
|
|
|
|
|
border: 0;
|
|
|
|
|
overflow: visible;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.geo-map-container .leaflet-div-icon .icon-shadow {
|
|
|
|
|
position: absolute;
|
|
|
|
|
top: 0;
|
|
|
|
|
left: 0;
|
|
|
|
|
z-index: -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.geo-map-container .leaflet-div-icon .bx {
|
|
|
|
|
position: absolute;
|
|
|
|
|
top: 3px;
|
|
|
|
|
left: 2px;
|
|
|
|
|
background-color: white;
|
|
|
|
|
color: black;
|
|
|
|
|
padding: 2px;
|
|
|
|
|
border-radius: 50%;
|
|
|
|
|
font-size: 17px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.geo-map-container .leaflet-div-icon .title-label {
|
|
|
|
|
display: block;
|
|
|
|
|
position: absolute;
|
|
|
|
|
top: 100%;
|
|
|
|
|
left: 50%;
|
|
|
|
|
transform: translateX(-50%);
|
|
|
|
|
font-size: 0.75rem;
|
|
|
|
|
height: 1rem;
|
|
|
|
|
color: black;
|
|
|
|
|
width: 100px;
|
|
|
|
|
text-align: center;
|
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
|
text-shadow: -1px -1px 0 white, 1px -1px 0 white, -1px 1px 0 white, 1px 1px 0 white;
|
|
|
|
|
white-space: no-wrap;
|
|
|
|
|
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;
|
2025-07-06 12:22:51 +03:00
|
|
|
export const LOCATION_ATTRIBUTE = "geolocation";
|
2025-07-06 12:12:59 +03:00
|
|
|
const CHILD_NOTE_ICON = "bx bx-pin";
|
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
|
|
|
|
2025-07-06 12:12:59 +03:00
|
|
|
private args: ViewModeArgs;
|
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 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");
|
2025-07-06 12:12:59 +03:00
|
|
|
this.args = args;
|
2025-07-06 11:13:17 +03:00
|
|
|
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() {
|
|
|
|
|
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);
|
2025-07-06 17:51:24 +03:00
|
|
|
map.on("click", (e) => this.#onMapClicked(e));
|
2025-07-06 12:12:59 +03:00
|
|
|
|
|
|
|
|
this.#reloadMarkers();
|
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 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-06 17:54:13 +03:00
|
|
|
const notes = await this.parentNote.getChildNotes();
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const latLng = childNote.getAttributeValue("label", LOCATION_ATTRIBUTE);
|
|
|
|
|
if (latLng) {
|
|
|
|
|
const marker = processNoteWithMarker(this.map, childNote, latLng);
|
|
|
|
|
this.currentMarkerData[childNote.noteId] = marker;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 11:15:28 +03:00
|
|
|
get isFullHeight(): boolean {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onEntitiesReloaded({ loadResults }: EventData<"entitiesReloaded">): boolean | void {
|
|
|
|
|
// 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();
|
|
|
|
|
if (attributeRows.find((at) => [LOCATION_ATTRIBUTE, "color"].includes(at.name ?? ""))) {
|
|
|
|
|
this.#reloadMarkers();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
const title = await dialog.prompt({ message: t("relation_map.enter_title_of_new_note"), defaultValue: t("relation_map.default_new_note_title") });
|
|
|
|
|
|
|
|
|
|
if (title?.trim()) {
|
|
|
|
|
const { note } = await server.post<CreateChildResponse>(`notes/${this.parentNote.noteId}/children?target=into`, {
|
|
|
|
|
title,
|
|
|
|
|
content: "",
|
|
|
|
|
type: "text"
|
|
|
|
|
});
|
|
|
|
|
attributes.setLabel(note.noteId, "iconClass", CHILD_NOTE_ICON);
|
|
|
|
|
moveMarker(note.noteId, e.latlng);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.#changeState(State.Normal);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 11:13:17 +03:00
|
|
|
}
|