commented app.js

This commit is contained in:
2026-04-19 13:44:08 +02:00
parent 1714e33fa7
commit c8f4832a95

View File

@@ -111,7 +111,7 @@ L.control.scale({
L.control.fullscreen({ L.control.fullscreen({
position: 'topright', position: 'topright',
title: 'Vollbild', title: 'Vollbild',
titleCancel: 'Vollbild beenden' titleCancel: 'Vollbild deaktivieren'
}).addTo(map); }).addTo(map);
// Geocoder Address Search // Geocoder Address Search
@@ -242,11 +242,11 @@ map.pm.addControls({
rotateMode: false rotateMode: false
}); });
// When a Shape is drawn, capture the Geometry and open the Create Modal // Captures drawn Geometry and opens the Create Modal
map.on('pm:create', function (e) { map.on('pm:create', function (e) {
var geojson = e.layer.toGeoJSON().geometry; var geojson = e.layer.toGeoJSON().geometry;
// Determine Geometry Type and normalize to simple Types // Determines drawn Geometry Type and normalizes to simple Types
if (e.shape === 'Marker') { if (e.shape === 'Marker') {
drawnGeometry = { type: 'Point', coordinates: geojson.coordinates }; drawnGeometry = { type: 'Point', coordinates: geojson.coordinates };
drawnGeomType = 'point'; drawnGeomType = 'point';
@@ -257,22 +257,22 @@ map.on('pm:create', function (e) {
drawnGeometry = { type: 'Polygon', coordinates: geojson.coordinates }; drawnGeometry = { type: 'Polygon', coordinates: geojson.coordinates };
drawnGeomType = 'polygon'; drawnGeomType = 'polygon';
} else { } else {
// Unsupported Shape — remove from Map and exit // removes unsupported Objects
map.removeLayer(e.layer); map.removeLayer(e.layer);
return; return;
} }
// Remove the drawn Layer — it will be re-added after API Confirmation // Removes the drawn Layer, which will be re-added after Moderation Confirmation
map.removeLayer(e.layer); map.removeLayer(e.layer);
// Check if User is logged in // Checks if User is logged in
if (!currentUser) { if (!currentUser) {
Swal.fire('Bitte anmelden', 'Sie müssen sich anmelden, um Beiträge zu erstellen.', 'info'); Swal.fire('Bitte anmelden', 'Sie sollten sich anmelden, um Beiträge hinzuzufügen oder zu bearbeiten.', 'info');
showLoginModal(); showLoginModal();
return; return;
} }
// Populate hidden Fields and open Create Modal // Populates hidden Fields and opens Create Modal
document.getElementById('create-geom').value = JSON.stringify(drawnGeometry); document.getElementById('create-geom').value = JSON.stringify(drawnGeometry);
document.getElementById('create-geom-type').value = drawnGeomType; document.getElementById('create-geom-type').value = drawnGeomType;
document.getElementById('create-modal').style.display = 'flex'; document.getElementById('create-modal').style.display = 'flex';
@@ -302,11 +302,11 @@ function apiCall(data, callback) {
}) })
.catch(function (error) { .catch(function (error) {
console.error('API Error:', error); console.error('API Error:', error);
Swal.fire('Verbindungsfehler', 'Die Verbindung zum Server ist fehlgeschlagen.', 'error'); Swal.fire('Verbindungsfehler', 'Verbindung zum Server fehlgeschlagen.', 'error');
}); });
} }
// Load all Contributions from API and display on Map // Loads all Contributions from API and displays Contributions on Map
function loadContributions() { function loadContributions() {
apiCall({ action: 'read', municipality_id: MUNICIPALITY.id }, function (data) { apiCall({ action: 'read', municipality_id: MUNICIPALITY.id }, function (data) {
if (data.error) { if (data.error) {
@@ -316,13 +316,13 @@ function loadContributions() {
contributionsData = data.features || []; contributionsData = data.features || [];
// Remove existing Layer if present // Removes existing Layer if present
if (contributionsLayer) { if (contributionsLayer) {
map.removeLayer(contributionsLayer); map.removeLayer(contributionsLayer);
layerControl.removeLayer(contributionsLayer); layerControl.removeLayer(contributionsLayer);
} }
// Create new GeoJSON Layer // Creates new GeoJSON Layer
contributionsLayer = L.geoJSON(data, { contributionsLayer = L.geoJSON(data, {
pointToLayer: stylePoint, pointToLayer: stylePoint,
style: styleLinePolygon, style: styleLinePolygon,
@@ -370,20 +370,20 @@ function styleLinePolygon(feature) {
// ===================================================================== // =====================================================================
// Block 9: Feature Popups (Read, Vote, Edit, Delete) // Block 9: Feature Popups for Read, Edit, Delete and Vote
// ===================================================================== // =====================================================================
function bindFeaturePopup(feature, layer) { function bindFeaturePopup(feature, layer) {
var props = feature.properties; var props = feature.properties;
var cat = CATEGORIES[props.category] || CATEGORIES.other; var cat = CATEGORIES[props.category] || CATEGORIES.other;
// Format Date // Formats Date
var date = new Date(props.created_at); var date = new Date(props.created_at);
var dateStr = date.toLocaleDateString('de-DE', { var dateStr = date.toLocaleDateString('de-DE', {
day: '2-digit', month: '2-digit', year: 'numeric' day: '2-digit', month: '2-digit', year: 'numeric'
}); });
// Build Popup HTML // Builds Popup on Click
var html = '' + var html = '' +
'<div class="popup-detail">' + '<div class="popup-detail">' +
'<span class="popup-detail-category">' + cat.icon + ' ' + cat.label + '</span>' + '<span class="popup-detail-category">' + cat.icon + ' ' + cat.label + '</span>' +
@@ -409,7 +409,7 @@ function bindFeaturePopup(feature, layer) {
layer.bindPopup(html, { maxWidth: 320, minWidth: 240 }); layer.bindPopup(html, { maxWidth: 320, minWidth: 240 });
// Tooltip on Hover // Builds Tooltip on Hover
layer.bindTooltip(cat.icon + ' ' + escapeHtml(props.title), { layer.bindTooltip(cat.icon + ' ' + escapeHtml(props.title), {
direction: 'top', direction: 'top',
offset: [0, -10] offset: [0, -10]
@@ -421,7 +421,7 @@ function bindFeaturePopup(feature, layer) {
// Block 10: CRUD Operations // Block 10: CRUD Operations
// ===================================================================== // =====================================================================
// CREATE Submit new Contribution from Modal // CREATE: Submits new Contributions from Modal
function submitCreate() { function submitCreate() {
var category = document.getElementById('create-category').value; var category = document.getElementById('create-category').value;
var title = document.getElementById('create-title').value.trim(); var title = document.getElementById('create-title').value.trim();
@@ -429,7 +429,7 @@ function submitCreate() {
var geom = document.getElementById('create-geom').value; var geom = document.getElementById('create-geom').value;
var geomType = document.getElementById('create-geom-type').value; var geomType = document.getElementById('create-geom-type').value;
// Validate // Validates
if (!category) { if (!category) {
Swal.fire('Kategorie fehlt', 'Bitte wählen Sie eine Kategorie aus.', 'warning'); Swal.fire('Kategorie fehlt', 'Bitte wählen Sie eine Kategorie aus.', 'warning');
return; return;
@@ -458,13 +458,13 @@ function submitCreate() {
return; return;
} }
Swal.fire('Eingereicht!', 'Ihr Beitrag wurde erfolgreich eingereicht und wird nach Prüfung veröffentlicht.', 'success'); Swal.fire('Eingereicht!', 'Ihr Beitrag wurde erfolgreich eingereicht und wird nach Prüfung durch das Moderationsteam veröffentlicht.', 'success');
closeCreateModal(); closeCreateModal();
loadContributions(); loadContributions();
}); });
} }
// Cancel Create close Modal and clear Form // Cancels Create, closes Modal and clears Form
function cancelCreate() { function cancelCreate() {
closeCreateModal(); closeCreateModal();
} }
@@ -480,9 +480,9 @@ function closeCreateModal() {
drawnGeomType = null; drawnGeomType = null;
} }
// UPDATE Edit an existing Contribution // UPDATE: Edit existing Contributions
function editContribution(contributionId) { function editContribution(contributionId) {
// Find Contribution in local Data // Finds Contribution in local Data
var contribution = contributionsData.find(function (f) { var contribution = contributionsData.find(function (f) {
return f.properties.contribution_id === contributionId; return f.properties.contribution_id === contributionId;
}); });