fix: refactor journey management and add backend integration
Co-authored-by: aider (ollama_chat/qwen3:30b-a3b-instruct-2507-q4_K_M) <aider@aider.chat>
This commit is contained in:
2 files changed
+392
-249
No files matched your search
@@ -1,90 +1,76 @@
|
||||
let map;
|
||||
let journeys = [];
|
||||
let currentJourney = {
|
||||
id: Date.now(),
|
||||
name: "Untitled Journey",
|
||||
description: "",
|
||||
markers: [],
|
||||
path: null
|
||||
};
|
||||
let currentMarkerBeingEdited = null;
|
||||
let isCreatingJourney = true;
|
||||
// /Volumes/Data/Code/FHGR/Frontend/js/map.js
|
||||
// Add the createMarker function to the window object
|
||||
window.createMarker = function(lngLat, content = {}) {
|
||||
const marker = new maplibregl.Marker({
|
||||
color: '#3887be',
|
||||
draggable: true
|
||||
})
|
||||
.setLngLat(lngLat)
|
||||
.addTo(map);
|
||||
|
||||
function saveJourneyToLocalStorage() {
|
||||
localStorage.setItem('journeys', JSON.stringify(journeys));
|
||||
}
|
||||
|
||||
function loadJourneysFromLocalStorage() {
|
||||
const stored = localStorage.getItem('journeys');
|
||||
if (stored) {
|
||||
journeys = JSON.parse(stored);
|
||||
}
|
||||
}
|
||||
|
||||
// Function to create a marker at a lngLat and add to the map
|
||||
function createMarker(lngLat) {
|
||||
const markerElement = document.createElement('div');
|
||||
markerElement.className = 'marker';
|
||||
markerElement.innerHTML = '<i class="fas fa-map-marker"></i>';
|
||||
|
||||
const marker = new maplibregl.Marker(markerElement)
|
||||
.setLngLat(lngLat)
|
||||
.addTo(map);
|
||||
|
||||
marker.id = Date.now();
|
||||
marker.content = {
|
||||
title: '',
|
||||
date: '',
|
||||
text: '',
|
||||
images: [],
|
||||
videoUrl: ''
|
||||
};
|
||||
|
||||
// Add a popup
|
||||
// Create popup with marker content
|
||||
const popup = new maplibregl.Popup({ offset: 25 })
|
||||
.setHTML('<strong>New Marker</strong>');
|
||||
.setHTML(`<strong>${content.title || 'Untitled'}</strong>`);
|
||||
|
||||
marker.setPopup(popup);
|
||||
|
||||
// When the marker is clicked, open the editor
|
||||
markerElement.addEventListener('click', () => {
|
||||
marker.getElement().addEventListener('click', () => {
|
||||
openMarkerEditor(marker);
|
||||
});
|
||||
|
||||
// Add marker to current journey
|
||||
const markerData = {
|
||||
id: Date.now(),
|
||||
lngLat: lngLat.toArray(),
|
||||
content: content
|
||||
};
|
||||
|
||||
// Add marker to current journey
|
||||
currentJourney.markers.push(marker);
|
||||
updateJourneyPath();
|
||||
return marker;
|
||||
}
|
||||
|
||||
function openMarkerEditor(marker) {
|
||||
currentMarkerBeingEdited = marker;
|
||||
document.getElementById('marker-title').value = marker.content.title || '';
|
||||
document.getElementById('marker-date').value = marker.content.date || '';
|
||||
document.getElementById('marker-text').value = marker.content.text || '';
|
||||
document.getElementById('video-url').value = marker.content.videoUrl || '';
|
||||
document.getElementById('marker-coords').textContent =
|
||||
`${marker.getLngLat().lng.toFixed(4)}, ${marker.getLngLat().lat.toFixed(4)}`;
|
||||
|
||||
// Update image review
|
||||
const imagePreview = document.getElementById('image-preview');
|
||||
imagePreview.innerHTML = '';
|
||||
if (marker.content.images && marker.content.images.length > 0) {
|
||||
marker.content.images.forEach(img => {
|
||||
const imgEl = document.createElement('img');
|
||||
imgEl.src = img;
|
||||
imagePreview.appendChild(imgEl);
|
||||
|
||||
// Add marker to the markers list
|
||||
const markersContainer = document.getElementById('markers-container');
|
||||
const markerElement = document.createElement('div');
|
||||
markerElement.className = 'marker-item';
|
||||
markerElement.innerHTML = `
|
||||
<div class="marker-title">${content.title || 'Untitled'}</div>
|
||||
<div class="marker-coords">${lngLat.lng.toFixed(4)}, ${lngLat.lat.toFixed(4)}</div>
|
||||
`;
|
||||
|
||||
// Add click event to marker item
|
||||
markerElement.addEventListener('click', function() {
|
||||
// Center map on marker
|
||||
map.flyTo({
|
||||
center: lngLat,
|
||||
zoom: 10
|
||||
});
|
||||
}
|
||||
|
||||
// Open marker editor
|
||||
openMarkerEditor(marker);
|
||||
});
|
||||
|
||||
markersContainer.appendChild(markerElement);
|
||||
|
||||
return marker;
|
||||
};
|
||||
|
||||
document.getElementById('marker-modal').style.display = 'block';
|
||||
}
|
||||
|
||||
function closeMarkerEditor() {
|
||||
document.getElementById('marker-modal').style.display = 'none';
|
||||
currentMarkerBeingEdited = null;
|
||||
}
|
||||
|
||||
// Function to update the journey path
|
||||
// Update the updateJourneyPath function to handle cases where markers array is empty
|
||||
function updateJourneyPath() {
|
||||
if (!currentJourney.markers || currentJourney.markers.length === 0) {
|
||||
// If no markers, clear the path
|
||||
map.getSource('journey-path').setData({
|
||||
type: 'Feature',
|
||||
properties: {},
|
||||
geometry: {
|
||||
type: 'LineString',
|
||||
coordinates: []
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const coordinates = currentJourney.markers.map(marker => marker.getLngLat().toArray());
|
||||
|
||||
map.getSource('journey-path').setData({
|
||||
@@ -96,126 +82,3 @@ function updateJourneyPath() {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
map = new maplibregl.Map({
|
||||
container: 'map',
|
||||
style: 'https://demotiles.maplibre.org/style.json',
|
||||
center: [0, 20],
|
||||
zoom: 2
|
||||
});
|
||||
|
||||
// Add journey path layer
|
||||
map.on('load', function() {
|
||||
map.addSource('journey-path', {
|
||||
type: 'geojson',
|
||||
data: {
|
||||
type: 'Feature',
|
||||
properties: {},
|
||||
geometry: {
|
||||
type: 'LineString',
|
||||
coordinates: []
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
map.addLayer({
|
||||
id: 'journey-path',
|
||||
type: 'line',
|
||||
source: 'journey-path',
|
||||
layout: {
|
||||
'line-join': 'round',
|
||||
'line-cap': 'round'
|
||||
},
|
||||
paint: {
|
||||
'line-color': '#3887be',
|
||||
'line-width': 5
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Close editor events
|
||||
document.getElementById('close-modal').addEventListener('click', closeMarkerEditor);
|
||||
document.getElementById('cancel-marker').addEventListener('click', closeMarkerEditor);
|
||||
|
||||
// Save marker event
|
||||
document.getElementById('save-marker').addEventListener('click', function() {
|
||||
if (!currentMarkerBeingEdited) return;
|
||||
|
||||
// Update marker content
|
||||
currentMarkerBeingEdited.content.title = document.getElementById('marker-title').value;
|
||||
currentMarkerBeingEdited.content.date = document.getElementById('marker-date').value;
|
||||
currentMarkerBeingEdited.content.text = document.getElementById('marker-text').value;
|
||||
currentMarkerBeingEdited.content.videoUrl = document.getElementById('video-url').value;
|
||||
|
||||
// Update the popup
|
||||
currentMarkerBeingEdited.getPopup().setHTML(
|
||||
`<strong>${currentMarkerBeingEdited.content.title || 'Untitled'}</strong>`
|
||||
);
|
||||
|
||||
closeMarkerEditor();
|
||||
});
|
||||
|
||||
// Delete marker event
|
||||
document.getElementById('delete-marker').addEventListener('click', function() {
|
||||
if (!currentMarkerBeingEdited) return;
|
||||
|
||||
// Remove from map
|
||||
currentMarkerBeingEdited.remove();
|
||||
|
||||
// Remove from currentJourney.markers
|
||||
const index = currentJourney.markers.findIndex(m => m.id === currentMarkerBeingEdited.id);
|
||||
if (index !== -1) {
|
||||
currentJourney.markers.splice(index, 1);
|
||||
}
|
||||
|
||||
updateJourneyPath(); // Update path after marker deletion
|
||||
|
||||
closeMarkerEditor();
|
||||
});
|
||||
|
||||
// Add marker on map click
|
||||
map.on('click', (e) => {
|
||||
if (isCreatingJourney) {
|
||||
createMarker(e.lngLat);
|
||||
}
|
||||
});
|
||||
|
||||
// Save journey button
|
||||
document.getElementById('save-journey').addEventListener('click', function() {
|
||||
currentJourney.name = document.getElementById('journey-title').value;
|
||||
currentJourney.description = document.getElementById('journey-description').value;
|
||||
|
||||
// Add the current journey to the journeys array if not already added
|
||||
const existingJourneyIndex = journeys.findIndex(j => j.id === currentJourney.id);
|
||||
if (existingJourneyIndex === -1) {
|
||||
journeys.push({
|
||||
id: currentJourney.id,
|
||||
name: currentJourney.name,
|
||||
description: currentJourney.description,
|
||||
markers: currentJourney.markers.map(marker => ({
|
||||
id: marker.id,
|
||||
lngLat: marker.getLngLat(),
|
||||
content: marker.content
|
||||
}))
|
||||
});
|
||||
} else {
|
||||
journeys[existingJourneyIndex] = {
|
||||
...journeys[existingJourneyIndex],
|
||||
name: currentJourney.name,
|
||||
description: currentJourney.description,
|
||||
markers: currentJourney.markers.map(marker => ({
|
||||
id: marker.id,
|
||||
lngLat: marker.getLngLat(),
|
||||
content: marker.content
|
||||
}))
|
||||
};
|
||||
}
|
||||
|
||||
saveJourneyToLocalStorage();
|
||||
alert('Journey saved!');
|
||||
});
|
||||
|
||||
// Load journeys on start
|
||||
loadJourneysFromLocalStorage();
|
||||
});
|
||||
Reference in new issue
Block a user