refactor(geo_map): move marker processing to dedicated method

This commit is contained in:
Elian Doran 2025-02-01 21:46:11 +02:00
parent bd638b689f
commit 211b557920
No known key found for this signature in database

View File

@ -159,9 +159,7 @@ export default class GeoMapTypeWidget extends TypeWidget {
} }
async #reloadMarkers() { async #reloadMarkers() {
const map = this.geoMapWidget.map; if (!this.note) {
if (!this.note || !map) {
return; return;
} }
@ -173,20 +171,28 @@ export default class GeoMapTypeWidget extends TypeWidget {
// Add the new markers. // Add the new markers.
this.currentMarkerData = {}; this.currentMarkerData = {};
const childNotes = await this.note.getChildNotes(); const childNotes = await this.note.getChildNotes();
const L = this.L;
for (const childNote of childNotes) { for (const childNote of childNotes) {
const latLng = childNote.getAttributeValue("label", LOCATION_ATTRIBUTE); const latLng = childNote.getAttributeValue("label", LOCATION_ATTRIBUTE);
if (!latLng) { if (latLng) {
continue; this.#processNoteWithMarker(childNote, latLng);
}
}
}
#processNoteWithMarker(note: FNote, latLng: string) {
const map = this.geoMapWidget.map;
if (!map) {
return;
} }
const [ lat, lng ] = latLng.split(",", 2).map((el) => parseFloat(el)); const [ lat, lng ] = latLng.split(",", 2).map((el) => parseFloat(el));
const L = this.L;
const icon = L.divIcon({ const icon = L.divIcon({
html: `\ html: `\
<img class="icon" src="${asset_path}/node_modules/leaflet/dist/images/marker-icon.png" /> <img class="icon" src="${asset_path}/node_modules/leaflet/dist/images/marker-icon.png" />
<img class="icon-shadow" src="${asset_path}/node_modules/leaflet/dist/images/marker-shadow.png" /> <img class="icon-shadow" src="${asset_path}/node_modules/leaflet/dist/images/marker-shadow.png" />
<span class="bx ${childNote.getIcon()}"></span> <span class="bx ${note.getIcon()}"></span>
<span class="title-label">${childNote.title}</span>`, <span class="title-label">${note.title}</span>`,
iconSize: [ 25, 41 ], iconSize: [ 25, 41 ],
iconAnchor: [ 12, 41 ] iconAnchor: [ 12, 41 ]
}) })
@ -199,22 +205,21 @@ export default class GeoMapTypeWidget extends TypeWidget {
}) })
.addTo(map) .addTo(map)
.on("moveend", e => { .on("moveend", e => {
this.moveMarker(childNote.noteId, (e.target as Marker).getLatLng()); this.moveMarker(note.noteId, (e.target as Marker).getLatLng());
}); });
marker.on("contextmenu", (e) => { marker.on("contextmenu", (e) => {
openContextMenu(childNote.noteId, e.originalEvent); openContextMenu(note.noteId, e.originalEvent);
}); });
const el = marker.getElement(); const el = marker.getElement();
if (el) { if (el) {
const $el = $(el); const $el = $(el);
$el.attr("data-href", `#${childNote.noteId}`); $el.attr("data-href", `#${note.noteId}`);
note_tooltip.setupElementTooltip($($el)); note_tooltip.setupElementTooltip($($el));
} }
this.currentMarkerData[childNote.noteId] = marker; this.currentMarkerData[note.noteId] = marker;
}
} }
#changeState(newState: State) { #changeState(newState: State) {