New "Karte" tab: a tiled zoom/pan map of the game world with a category sidebar (groups, per-category marker counts, show all / hide all, search) and marker popups, plus tactic layers drawn on top of it. Markers are placed by clicking the map (map.manage) and live in freely editable two-level categories, each with its own emoji and colour. A tactic (map.tactics) is a separate drawing layer that can be toggled on and off: rally points, dashed routes, areas and text notes in any colour, finished by double click, an explicit button or Enter, removed by clicking them. Coordinates are stored as a fraction of the map's width/height rather than in pixels, so re-tiling the map at a different resolution never moves anything. The map data lives entirely in the website database - the Discord bot is not involved. tools/make_map_tiles.php slices a high-resolution image into the zoom pyramid locally; the resulting folder is uploaded by FTP and picked up automatically, one region per folder. Leaflet 1.9.4 is vendored under assets/vendor/ so there is still no CDN and no build step. New permissions map.view / map.manage / map.tactics and four new tables - existing installs have to add both once (see README). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
891 lines
37 KiB
JavaScript
891 lines
37 KiB
JavaScript
(function () {
|
|
'use strict';
|
|
|
|
var page = document.querySelector('.map-page');
|
|
var dataEl = document.getElementById('map-data');
|
|
if (!page || !dataEl || typeof L === 'undefined') return;
|
|
|
|
var data = JSON.parse(dataEl.textContent);
|
|
var manifest = data.manifest;
|
|
var mapKey = page.getAttribute('data-map-key');
|
|
var canManage = page.getAttribute('data-can-manage') === '1';
|
|
var canTactics = page.getAttribute('data-can-tactics') === '1';
|
|
var csrfToken = (document.querySelector('meta[name="csrf-token"]') || {}).content || '';
|
|
|
|
var statusEl = document.getElementById('map-status');
|
|
|
|
function postJson(payload) {
|
|
payload.csrf_token = csrfToken;
|
|
payload.map_key = mapKey;
|
|
return fetch('/map_action.php', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(payload)
|
|
}).then(function (response) { return response.json(); });
|
|
}
|
|
|
|
function setStatus(text, isError) {
|
|
if (!statusEl) return;
|
|
statusEl.textContent = text || '';
|
|
statusEl.className = 'map-status text-muted' + (isError ? ' error' : '');
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Karte: gekacheltes Bild ohne Geo-Bezug, deshalb CRS.Simple. Alle Marker
|
|
// liegen als 0..1-Anteil der Bildbreite/-hoehe vor und werden hier ueber
|
|
// die maximale Zoomstufe in Leaflet-Koordinaten uebersetzt - dadurch bleiben
|
|
// sie auch dann richtig, wenn die Karte spaeter neu gekachelt wird.
|
|
// ---------------------------------------------------------------------
|
|
var map = L.map('map-canvas', {
|
|
crs: L.CRS.Simple,
|
|
minZoom: manifest.min_zoom,
|
|
maxZoom: manifest.max_zoom,
|
|
zoomControl: true,
|
|
attributionControl: false,
|
|
maxBoundsViscosity: 0.85,
|
|
doubleClickZoom: false,
|
|
// Die Zoomstufen der Kachel-Pyramide sind Zweierpotenzen. Ohne Zwischenstufen
|
|
// koennte die Startansicht die Flaeche nur selten gut ausnutzen (die naechst
|
|
// groessere Stufe passt meist nicht mehr hinein), deshalb Viertelschritte.
|
|
zoomSnap: 0.25,
|
|
zoomDelta: 0.5
|
|
});
|
|
|
|
function toLatLng(x, y) {
|
|
return map.unproject([x * manifest.width, y * manifest.height], manifest.max_zoom);
|
|
}
|
|
|
|
function toNormalized(latlng) {
|
|
var point = map.project(latlng, manifest.max_zoom);
|
|
return [
|
|
Math.min(1, Math.max(0, point.x / manifest.width)),
|
|
Math.min(1, Math.max(0, point.y / manifest.height))
|
|
];
|
|
}
|
|
|
|
// Rund um das Kartenbild liegt noch Leerraum (aufgefuellte Randkacheln). Ein Klick
|
|
// dorthin wuerde durch das Klemmen in toNormalized() still an den Bildrand rutschen -
|
|
// deshalb solche Klicks lieber ablehnen als das Objekt woanders abzulegen.
|
|
function isInsideMap(latlng) {
|
|
var point = map.project(latlng, manifest.max_zoom);
|
|
return point.x >= 0 && point.x <= manifest.width
|
|
&& point.y >= 0 && point.y <= manifest.height;
|
|
}
|
|
|
|
var bounds = L.latLngBounds(toLatLng(0, 1), toLatLng(1, 0));
|
|
map.setMaxBounds(bounds.pad(0.15));
|
|
|
|
L.tileLayer(manifest.url_template, {
|
|
tileSize: manifest.tile_size,
|
|
minZoom: manifest.min_zoom,
|
|
maxZoom: manifest.max_zoom,
|
|
bounds: bounds,
|
|
noWrap: true,
|
|
// Randkacheln jenseits des Bildes gibt es nicht - statt eines kaputten
|
|
// Bildes soll dort einfach nichts stehen.
|
|
errorTileUrl: 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
|
|
}).addTo(map);
|
|
|
|
function resetView() {
|
|
map.fitBounds(bounds, { padding: [8, 8] });
|
|
}
|
|
resetView();
|
|
|
|
var resetBtn = document.getElementById('map-reset-view');
|
|
if (resetBtn) resetBtn.addEventListener('click', resetView);
|
|
|
|
// Positionsanzeige (praktisch, um Koordinaten durchzugeben)
|
|
var coordsEl = document.getElementById('map-coords');
|
|
if (coordsEl) {
|
|
map.on('mousemove', function (e) {
|
|
var pos = toNormalized(e.latlng);
|
|
coordsEl.textContent = (pos[0] * 100).toFixed(1) + ' / ' + (pos[1] * 100).toFixed(1);
|
|
});
|
|
map.on('mouseout', function () { coordsEl.textContent = ''; });
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Marker
|
|
// ---------------------------------------------------------------------
|
|
var categoriesById = {};
|
|
data.categories.forEach(function (category) { categoriesById[category.id] = category; });
|
|
|
|
var markerLayers = {}; // category_id -> L.LayerGroup
|
|
var markerById = {}; // marker_id -> {data, layer, group}
|
|
var visibleCategories = {};
|
|
var searchTerm = '';
|
|
|
|
function categoryIcon(category) {
|
|
return L.divIcon({
|
|
className: 'map-marker-icon',
|
|
html: '<span class="map-marker-pin" style="background:' + (category ? category.color : '#c9a227') + '">'
|
|
+ '<span class="map-marker-glyph">' + (category && category.icon ? category.icon : '') + '</span></span>',
|
|
iconSize: [26, 26],
|
|
iconAnchor: [13, 13],
|
|
popupAnchor: [0, -14]
|
|
});
|
|
}
|
|
|
|
function escapeHtml(value) {
|
|
return String(value == null ? '' : value)
|
|
.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
|
.replace(/"/g, '"').replace(/'/g, ''');
|
|
}
|
|
|
|
function popupHtml(marker) {
|
|
var category = categoriesById[marker.category_id];
|
|
var html = '<div class="map-popup">'
|
|
+ '<strong class="map-popup-title">' + escapeHtml(marker.name) + '</strong>'
|
|
+ '<div class="map-popup-category">' + escapeHtml(category ? category.icon + ' ' + category.name : '') + '</div>';
|
|
if (marker.description) {
|
|
html += '<div class="map-popup-text">' + escapeHtml(marker.description).replace(/\n/g, '<br>') + '</div>';
|
|
}
|
|
if (marker.author) {
|
|
html += '<div class="map-popup-author">von ' + escapeHtml(marker.author) + '</div>';
|
|
}
|
|
if (canManage) {
|
|
html += '<button type="button" class="map-popup-edit" data-marker-id="' + marker.id + '">Bearbeiten</button>';
|
|
}
|
|
return html + '</div>';
|
|
}
|
|
|
|
function layerForCategory(categoryId) {
|
|
if (!markerLayers[categoryId]) {
|
|
markerLayers[categoryId] = L.layerGroup();
|
|
if (visibleCategories[categoryId] !== false) {
|
|
markerLayers[categoryId].addTo(map);
|
|
}
|
|
}
|
|
return markerLayers[categoryId];
|
|
}
|
|
|
|
function addMarker(markerData) {
|
|
var category = categoriesById[markerData.category_id];
|
|
var layer = L.marker(toLatLng(markerData.x, markerData.y), {
|
|
icon: categoryIcon(category),
|
|
draggable: false,
|
|
title: markerData.name
|
|
});
|
|
layer.bindPopup(popupHtml(markerData));
|
|
layer.addTo(layerForCategory(markerData.category_id));
|
|
|
|
markerById[markerData.id] = { data: markerData, layer: layer };
|
|
return layer;
|
|
}
|
|
|
|
data.markers.forEach(addMarker);
|
|
|
|
function removeMarker(markerId) {
|
|
var entry = markerById[markerId];
|
|
if (!entry) return;
|
|
var layer = markerLayers[entry.data.category_id];
|
|
if (layer) layer.removeLayer(entry.layer);
|
|
delete markerById[markerId];
|
|
}
|
|
|
|
// Sichtbarkeit: Kategorie-Haken UND Suchbegriff muessen passen.
|
|
function applyFilters() {
|
|
Object.keys(markerLayers).forEach(function (categoryId) {
|
|
var wanted = visibleCategories[categoryId] !== false;
|
|
var layer = markerLayers[categoryId];
|
|
if (wanted && !map.hasLayer(layer)) layer.addTo(map);
|
|
if (!wanted && map.hasLayer(layer)) map.removeLayer(layer);
|
|
});
|
|
|
|
if (searchTerm === '') {
|
|
Object.keys(markerById).forEach(function (id) {
|
|
var entry = markerById[id];
|
|
var element = entry.layer.getElement();
|
|
if (element) element.classList.remove('map-marker-filtered');
|
|
});
|
|
return;
|
|
}
|
|
|
|
Object.keys(markerById).forEach(function (id) {
|
|
var entry = markerById[id];
|
|
var category = categoriesById[entry.data.category_id];
|
|
var haystack = (entry.data.name + ' ' + (entry.data.description || '') + ' '
|
|
+ (category ? category.name : '')).toLowerCase();
|
|
var element = entry.layer.getElement();
|
|
if (!element) return;
|
|
element.classList.toggle('map-marker-filtered', haystack.indexOf(searchTerm) === -1);
|
|
});
|
|
}
|
|
|
|
document.querySelectorAll('.map-category-checkbox').forEach(function (checkbox) {
|
|
visibleCategories[checkbox.value] = checkbox.checked;
|
|
checkbox.addEventListener('change', function () {
|
|
visibleCategories[checkbox.value] = checkbox.checked;
|
|
syncGroupCheckbox(checkbox.closest('.map-group'));
|
|
applyFilters();
|
|
});
|
|
});
|
|
|
|
function syncGroupCheckbox(group) {
|
|
if (!group) return;
|
|
var groupBox = group.querySelector('.map-group-checkbox');
|
|
var boxes = group.querySelectorAll('.map-category-checkbox');
|
|
if (!groupBox || boxes.length === 0) return;
|
|
var checked = 0;
|
|
boxes.forEach(function (box) { if (box.checked) checked++; });
|
|
groupBox.checked = checked > 0;
|
|
groupBox.indeterminate = checked > 0 && checked < boxes.length;
|
|
}
|
|
|
|
document.querySelectorAll('.map-group-checkbox').forEach(function (groupBox) {
|
|
groupBox.addEventListener('change', function () {
|
|
var group = groupBox.closest('.map-group');
|
|
group.querySelectorAll('.map-category-checkbox').forEach(function (box) {
|
|
box.checked = groupBox.checked;
|
|
visibleCategories[box.value] = box.checked;
|
|
});
|
|
groupBox.indeterminate = false;
|
|
applyFilters();
|
|
});
|
|
});
|
|
|
|
document.querySelectorAll('.map-group-toggle').forEach(function (toggle) {
|
|
toggle.addEventListener('click', function () {
|
|
var group = toggle.closest('.map-group');
|
|
var collapsed = group.classList.toggle('collapsed');
|
|
toggle.setAttribute('aria-expanded', collapsed ? 'false' : 'true');
|
|
});
|
|
});
|
|
|
|
function setAllCategories(checked) {
|
|
document.querySelectorAll('.map-category-checkbox').forEach(function (box) {
|
|
box.checked = checked;
|
|
visibleCategories[box.value] = checked;
|
|
});
|
|
document.querySelectorAll('.map-group-checkbox').forEach(function (box) {
|
|
box.checked = checked;
|
|
box.indeterminate = false;
|
|
});
|
|
applyFilters();
|
|
}
|
|
|
|
var showAllBtn = document.getElementById('map-show-all');
|
|
var hideAllBtn = document.getElementById('map-hide-all');
|
|
if (showAllBtn) showAllBtn.addEventListener('click', function () { setAllCategories(true); });
|
|
if (hideAllBtn) hideAllBtn.addEventListener('click', function () { setAllCategories(false); });
|
|
|
|
var searchInput = document.getElementById('map-search');
|
|
if (searchInput) {
|
|
searchInput.addEventListener('input', function () {
|
|
searchTerm = searchInput.value.trim().toLowerCase();
|
|
applyFilters();
|
|
|
|
// Kategorien, die nicht zur Suche passen, in der Sidebar ausblenden …
|
|
document.querySelectorAll('.map-category').forEach(function (row) {
|
|
var name = (row.getAttribute('data-name') || '').toLowerCase();
|
|
row.hidden = searchTerm !== '' && name.indexOf(searchTerm) === -1
|
|
&& !hasMatchingMarker(row.getAttribute('data-category-id'));
|
|
});
|
|
|
|
// … und Gruppen, von denen dadurch nichts mehr uebrig bleibt, gleich mit.
|
|
document.querySelectorAll('.map-group').forEach(function (group) {
|
|
var visible = Array.prototype.some.call(
|
|
group.querySelectorAll('.map-category'),
|
|
function (row) { return !row.hidden; }
|
|
);
|
|
group.hidden = searchTerm !== '' && !visible;
|
|
});
|
|
});
|
|
}
|
|
|
|
function hasMatchingMarker(categoryId) {
|
|
return Object.keys(markerById).some(function (id) {
|
|
var entry = markerById[id];
|
|
if (String(entry.data.category_id) !== String(categoryId)) return false;
|
|
return (entry.data.name + ' ' + (entry.data.description || '')).toLowerCase().indexOf(searchTerm) !== -1;
|
|
});
|
|
}
|
|
|
|
// ---- Sidebar-Reiter ----
|
|
document.querySelectorAll('.map-tab').forEach(function (tab) {
|
|
tab.addEventListener('click', function () {
|
|
document.querySelectorAll('.map-tab').forEach(function (t) { t.classList.remove('active'); });
|
|
tab.classList.add('active');
|
|
document.querySelectorAll('.map-panel').forEach(function (panel) {
|
|
panel.hidden = panel.getAttribute('data-panel') !== tab.getAttribute('data-panel');
|
|
});
|
|
});
|
|
});
|
|
|
|
var mapSelect = document.getElementById('map-select');
|
|
if (mapSelect) {
|
|
mapSelect.addEventListener('change', function () {
|
|
window.location.href = '/map.php?map=' + encodeURIComponent(mapSelect.value);
|
|
});
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Modals
|
|
// ---------------------------------------------------------------------
|
|
function openModal(id) {
|
|
var modal = document.getElementById(id);
|
|
if (modal) modal.hidden = false;
|
|
}
|
|
function closeModal(id) {
|
|
var modal = document.getElementById(id);
|
|
if (!modal) return;
|
|
modal.hidden = true;
|
|
var feedback = modal.querySelector('.map-feedback');
|
|
if (feedback) feedback.textContent = '';
|
|
}
|
|
document.querySelectorAll('.modal-overlay').forEach(function (overlay) {
|
|
var closeBtn = overlay.querySelector('.modal-close');
|
|
if (closeBtn) closeBtn.addEventListener('click', function () { overlay.hidden = true; });
|
|
overlay.addEventListener('click', function (e) {
|
|
if (e.target === overlay) overlay.hidden = true;
|
|
});
|
|
});
|
|
|
|
function feedback(formId, text, isError) {
|
|
var form = document.getElementById(formId);
|
|
if (!form) return;
|
|
var el = form.querySelector('.map-feedback');
|
|
if (!el) return;
|
|
el.textContent = text || '';
|
|
el.className = 'map-feedback text-muted' + (isError ? ' error' : '');
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Marker setzen/bearbeiten
|
|
// ---------------------------------------------------------------------
|
|
var markerForm = document.getElementById('map-marker-form');
|
|
var markerModalTitle = document.getElementById('map-marker-modal-title');
|
|
var markerDeleteBtn = document.getElementById('map-marker-delete');
|
|
var placingMarker = false;
|
|
|
|
var markerToolBtn = document.getElementById('map-tool-marker');
|
|
if (markerToolBtn) {
|
|
markerToolBtn.addEventListener('click', function () {
|
|
placingMarker = !placingMarker;
|
|
markerToolBtn.classList.toggle('active', placingMarker);
|
|
document.getElementById('map-canvas').classList.toggle('map-placing', placingMarker);
|
|
setStatus(placingMarker ? 'Klicke auf die Karte, um den Marker zu setzen (Esc bricht ab).' : '');
|
|
});
|
|
}
|
|
|
|
function openMarkerForm(marker, position) {
|
|
if (!markerForm) return;
|
|
markerForm.marker_id.value = marker ? marker.id : '';
|
|
markerForm.name.value = marker ? marker.name : '';
|
|
markerForm.description.value = marker ? (marker.description || '') : '';
|
|
markerForm.x.value = marker ? marker.x : position[0];
|
|
markerForm.y.value = marker ? marker.y : position[1];
|
|
if (marker) markerForm.category_id.value = String(marker.category_id);
|
|
markerModalTitle.textContent = marker ? 'Marker bearbeiten' : 'Marker anlegen';
|
|
markerDeleteBtn.hidden = !marker;
|
|
openModal('map-marker-modal');
|
|
markerForm.name.focus();
|
|
}
|
|
|
|
map.on('click', function (e) {
|
|
if (!drawingShape && (!placingMarker || !canManage)) return;
|
|
|
|
if (!isInsideMap(e.latlng)) {
|
|
setStatus('Dieser Punkt liegt außerhalb der Karte.', true);
|
|
return;
|
|
}
|
|
|
|
if (drawingShape) { handleDrawClick(e); return; }
|
|
openMarkerForm(null, toNormalized(e.latlng));
|
|
});
|
|
|
|
document.addEventListener('keydown', function (e) {
|
|
var inField = /^(INPUT|TEXTAREA|SELECT)$/.test((e.target.tagName || '').toUpperCase());
|
|
|
|
if (e.key === 'Escape') {
|
|
if (placingMarker && markerToolBtn) markerToolBtn.click();
|
|
if (drawingShape) cancelDrawing();
|
|
return;
|
|
}
|
|
if (e.key === 'Enter' && !inField && drawingShape) {
|
|
e.preventDefault();
|
|
finishShape();
|
|
}
|
|
});
|
|
|
|
// Popup-Buttons entstehen erst beim Oeffnen des Popups, deshalb delegiert.
|
|
document.addEventListener('click', function (e) {
|
|
var editBtn = e.target.closest('.map-popup-edit');
|
|
if (!editBtn) return;
|
|
var entry = markerById[editBtn.getAttribute('data-marker-id')];
|
|
if (entry) {
|
|
map.closePopup();
|
|
openMarkerForm(entry.data);
|
|
}
|
|
});
|
|
|
|
if (markerForm) {
|
|
markerForm.addEventListener('submit', function (e) {
|
|
e.preventDefault();
|
|
feedback('map-marker-form', 'Wird gespeichert …');
|
|
|
|
postJson({
|
|
action: 'marker_save',
|
|
marker_id: markerForm.marker_id.value,
|
|
category_id: markerForm.category_id.value,
|
|
name: markerForm.name.value,
|
|
description: markerForm.description.value,
|
|
x: parseFloat(markerForm.x.value),
|
|
y: parseFloat(markerForm.y.value)
|
|
}).then(function (result) {
|
|
if (result.status !== 'success') {
|
|
feedback('map-marker-form', result.message, true);
|
|
return;
|
|
}
|
|
window.location.reload();
|
|
}).catch(function () {
|
|
feedback('map-marker-form', 'Verbindung fehlgeschlagen.', true);
|
|
});
|
|
});
|
|
|
|
document.getElementById('map-marker-cancel').addEventListener('click', function () {
|
|
closeModal('map-marker-modal');
|
|
});
|
|
|
|
markerDeleteBtn.addEventListener('click', function () {
|
|
if (!window.confirm('Diesen Marker wirklich löschen?')) return;
|
|
postJson({ action: 'marker_delete', marker_id: markerForm.marker_id.value })
|
|
.then(function (result) {
|
|
if (result.status !== 'success') {
|
|
feedback('map-marker-form', result.message, true);
|
|
return;
|
|
}
|
|
removeMarker(markerForm.marker_id.value);
|
|
closeModal('map-marker-modal');
|
|
window.location.reload();
|
|
});
|
|
});
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Kategorien
|
|
// ---------------------------------------------------------------------
|
|
var categoryForm = document.getElementById('map-category-form');
|
|
var categoryDeleteBtn = document.getElementById('map-category-delete');
|
|
|
|
var seedBtn = document.getElementById('map-seed-categories');
|
|
if (seedBtn) {
|
|
seedBtn.addEventListener('click', function () {
|
|
seedBtn.disabled = true;
|
|
postJson({ action: 'category_seed' }).then(function (result) {
|
|
if (result.status === 'success') {
|
|
window.location.reload();
|
|
} else {
|
|
seedBtn.disabled = false;
|
|
setStatus(result.message, true);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
var addCategoryBtn = document.getElementById('map-add-category');
|
|
if (addCategoryBtn) {
|
|
addCategoryBtn.addEventListener('click', function () {
|
|
categoryForm.category_id.value = '';
|
|
categoryForm.name.value = '';
|
|
categoryForm.icon.value = '';
|
|
categoryForm.color.value = '#c9a227';
|
|
categoryForm.parent_id.value = '0';
|
|
document.getElementById('map-category-modal-title').textContent = 'Kategorie anlegen';
|
|
categoryDeleteBtn.hidden = true;
|
|
openModal('map-category-modal');
|
|
});
|
|
}
|
|
|
|
document.querySelectorAll('.map-category-edit').forEach(function (btn) {
|
|
btn.addEventListener('click', function () {
|
|
categoryForm.category_id.value = btn.getAttribute('data-category-id');
|
|
categoryForm.name.value = btn.getAttribute('data-name') || '';
|
|
categoryForm.icon.value = btn.getAttribute('data-icon') || '';
|
|
categoryForm.color.value = btn.getAttribute('data-color') || '#c9a227';
|
|
categoryForm.parent_id.value = btn.getAttribute('data-parent-id') || '0';
|
|
document.getElementById('map-category-modal-title').textContent = 'Kategorie bearbeiten';
|
|
categoryDeleteBtn.hidden = false;
|
|
openModal('map-category-modal');
|
|
});
|
|
});
|
|
|
|
if (categoryForm) {
|
|
categoryForm.addEventListener('submit', function (e) {
|
|
e.preventDefault();
|
|
feedback('map-category-form', 'Wird gespeichert …');
|
|
postJson({
|
|
action: 'category_save',
|
|
category_id: categoryForm.category_id.value,
|
|
parent_id: categoryForm.parent_id.value,
|
|
name: categoryForm.name.value,
|
|
icon: categoryForm.icon.value,
|
|
color: categoryForm.color.value
|
|
}).then(function (result) {
|
|
if (result.status !== 'success') {
|
|
feedback('map-category-form', result.message, true);
|
|
return;
|
|
}
|
|
window.location.reload();
|
|
});
|
|
});
|
|
|
|
document.getElementById('map-category-cancel').addEventListener('click', function () {
|
|
closeModal('map-category-modal');
|
|
});
|
|
|
|
categoryDeleteBtn.addEventListener('click', function () {
|
|
if (!window.confirm('Kategorie mitsamt allen darin enthaltenen Markern löschen?')) return;
|
|
postJson({ action: 'category_delete', category_id: categoryForm.category_id.value })
|
|
.then(function (result) {
|
|
if (result.status !== 'success') {
|
|
feedback('map-category-form', result.message, true);
|
|
return;
|
|
}
|
|
window.location.reload();
|
|
});
|
|
});
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Taktiken: eigene Zeichenebene je Taktik (Punkte, Routen, Flaechen, Notizen)
|
|
// ---------------------------------------------------------------------
|
|
var tacticLayers = {}; // tactic_id -> L.FeatureGroup
|
|
var tacticsById = {};
|
|
var activeTacticId = null;
|
|
var drawingShape = null; // aktuell gezeichneter Typ
|
|
var drawPoints = [];
|
|
var drawPreview = null;
|
|
|
|
data.tactics.forEach(function (tactic) {
|
|
tacticsById[tactic.id] = tactic;
|
|
tacticLayers[tactic.id] = L.featureGroup();
|
|
tactic.shapes.forEach(function (shape) { drawShape(tactic.id, shape); });
|
|
});
|
|
|
|
function shapeLatLngs(shape) {
|
|
return shape.points.map(function (point) { return toLatLng(point[0], point[1]); });
|
|
}
|
|
|
|
function drawShape(tacticId, shape) {
|
|
var group = tacticLayers[tacticId];
|
|
if (!group) return;
|
|
var latlngs = shapeLatLngs(shape);
|
|
var layer;
|
|
|
|
if (shape.type === 'route') {
|
|
layer = L.polyline(latlngs, { color: shape.color, weight: 4, opacity: 0.9, dashArray: '10 6' });
|
|
} else if (shape.type === 'area') {
|
|
layer = L.polygon(latlngs, { color: shape.color, weight: 3, opacity: 0.9, fillOpacity: 0.2 });
|
|
} else if (shape.type === 'text') {
|
|
layer = L.marker(latlngs[0], {
|
|
icon: L.divIcon({
|
|
className: 'map-shape-text',
|
|
html: '<span style="border-color:' + shape.color + '">' + escapeHtml(shape.label) + '</span>',
|
|
iconSize: null
|
|
})
|
|
});
|
|
} else {
|
|
layer = L.marker(latlngs[0], {
|
|
icon: L.divIcon({
|
|
className: 'map-shape-point',
|
|
html: '<span class="map-shape-dot" style="background:' + shape.color + '"></span>'
|
|
+ (shape.label ? '<span class="map-shape-label">' + escapeHtml(shape.label) + '</span>' : ''),
|
|
iconSize: null,
|
|
iconAnchor: [9, 9]
|
|
})
|
|
});
|
|
}
|
|
|
|
if (shape.label && shape.type !== 'text' && shape.type !== 'point') {
|
|
layer.bindTooltip(shape.label, { permanent: false, sticky: true });
|
|
}
|
|
|
|
layer.shapeId = shape.id;
|
|
layer.on('click', function (e) {
|
|
if (!canTactics || activeTacticId !== tacticId || drawingShape) return;
|
|
L.DomEvent.stopPropagation(e);
|
|
if (window.confirm('Objekt "' + (shape.label || shape.type) + '" löschen?')) {
|
|
postJson({ action: 'shape_delete', shape_id: shape.id }).then(function (result) {
|
|
if (result.status === 'success') {
|
|
group.removeLayer(layer);
|
|
setStatus('Objekt gelöscht.');
|
|
} else {
|
|
setStatus(result.message, true);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
group.addLayer(layer);
|
|
}
|
|
|
|
document.querySelectorAll('.map-tactic-checkbox').forEach(function (checkbox) {
|
|
checkbox.addEventListener('change', function () {
|
|
var group = tacticLayers[checkbox.value];
|
|
if (!group) return;
|
|
if (checkbox.checked) {
|
|
group.addTo(map);
|
|
} else {
|
|
map.removeLayer(group);
|
|
}
|
|
});
|
|
});
|
|
|
|
// ---- Taktik anlegen/bearbeiten ----
|
|
var tacticForm = document.getElementById('map-tactic-form');
|
|
var tacticDeleteBtn = document.getElementById('map-tactic-delete');
|
|
var tacticTools = document.getElementById('map-tactic-tools');
|
|
var tacticActiveName = document.getElementById('map-tactic-active-name');
|
|
|
|
var addTacticBtn = document.getElementById('map-add-tactic');
|
|
if (addTacticBtn) {
|
|
addTacticBtn.addEventListener('click', function () {
|
|
tacticForm.tactic_id.value = '';
|
|
tacticForm.name.value = '';
|
|
tacticForm.description.value = '';
|
|
document.getElementById('map-tactic-modal-title').textContent = 'Taktik anlegen';
|
|
tacticDeleteBtn.hidden = true;
|
|
openModal('map-tactic-modal');
|
|
tacticForm.name.focus();
|
|
});
|
|
}
|
|
|
|
document.querySelectorAll('.map-tactic-edit').forEach(function (btn) {
|
|
btn.addEventListener('click', function () {
|
|
var row = btn.closest('.map-tactic');
|
|
var tactic = tacticsById[row.getAttribute('data-tactic-id')];
|
|
if (!tactic) return;
|
|
tacticForm.tactic_id.value = tactic.id;
|
|
tacticForm.name.value = tactic.name;
|
|
tacticForm.description.value = tactic.description || '';
|
|
document.getElementById('map-tactic-modal-title').textContent = 'Taktik bearbeiten';
|
|
tacticDeleteBtn.hidden = false;
|
|
openModal('map-tactic-modal');
|
|
});
|
|
});
|
|
|
|
if (tacticForm) {
|
|
tacticForm.addEventListener('submit', function (e) {
|
|
e.preventDefault();
|
|
feedback('map-tactic-form', 'Wird gespeichert …');
|
|
postJson({
|
|
action: 'tactic_save',
|
|
tactic_id: tacticForm.tactic_id.value,
|
|
name: tacticForm.name.value,
|
|
description: tacticForm.description.value
|
|
}).then(function (result) {
|
|
if (result.status !== 'success') {
|
|
feedback('map-tactic-form', result.message, true);
|
|
return;
|
|
}
|
|
// Eine frisch angelegte Taktik ist noch nicht im DOM - dafuer neu laden
|
|
// und danach direkt in den Bearbeiten-Modus springen.
|
|
if (!tacticForm.tactic_id.value) {
|
|
window.location.href = '/map.php?map=' + encodeURIComponent(mapKey) + '&edit=' + result.tactic_id;
|
|
return;
|
|
}
|
|
closeModal('map-tactic-modal');
|
|
startTacticEditing(parseInt(tacticForm.tactic_id.value, 10));
|
|
var row = document.querySelector('.map-tactic[data-tactic-id="' + tacticForm.tactic_id.value + '"] .map-tactic-name');
|
|
if (row) row.textContent = tacticForm.name.value;
|
|
});
|
|
});
|
|
|
|
document.getElementById('map-tactic-cancel').addEventListener('click', function () {
|
|
closeModal('map-tactic-modal');
|
|
});
|
|
|
|
tacticDeleteBtn.addEventListener('click', function () {
|
|
if (!window.confirm('Diese Taktik mitsamt allen Objekten löschen?')) return;
|
|
postJson({ action: 'tactic_delete', tactic_id: tacticForm.tactic_id.value })
|
|
.then(function (result) {
|
|
if (result.status !== 'success') {
|
|
feedback('map-tactic-form', result.message, true);
|
|
return;
|
|
}
|
|
window.location.reload();
|
|
});
|
|
});
|
|
}
|
|
|
|
function startTacticEditing(tacticId) {
|
|
if (!canTactics || !tacticsById[tacticId]) return;
|
|
activeTacticId = tacticId;
|
|
|
|
// Beim Bearbeiten muss die Ebene sichtbar sein, sonst zeichnet man ins Leere.
|
|
var checkbox = document.querySelector('.map-tactic-checkbox[value="' + tacticId + '"]');
|
|
if (checkbox && !checkbox.checked) {
|
|
checkbox.checked = true;
|
|
checkbox.dispatchEvent(new Event('change'));
|
|
}
|
|
if (!map.hasLayer(tacticLayers[tacticId])) tacticLayers[tacticId].addTo(map);
|
|
|
|
document.querySelectorAll('.map-tactic').forEach(function (row) {
|
|
row.classList.toggle('editing', row.getAttribute('data-tactic-id') === String(tacticId));
|
|
});
|
|
|
|
if (tacticTools) tacticTools.hidden = false;
|
|
if (tacticActiveName) tacticActiveName.textContent = tacticsById[tacticId].name;
|
|
setStatus('Taktik wird bearbeitet - Werkzeug wählen und auf die Karte klicken. Klick auf ein Objekt löscht es.');
|
|
}
|
|
|
|
function stopTacticEditing() {
|
|
activeTacticId = null;
|
|
cancelDrawing();
|
|
if (tacticTools) tacticTools.hidden = true;
|
|
document.querySelectorAll('.map-tactic').forEach(function (row) { row.classList.remove('editing'); });
|
|
setStatus('');
|
|
}
|
|
|
|
var tacticCloseBtn = document.getElementById('map-tactic-close');
|
|
if (tacticCloseBtn) tacticCloseBtn.addEventListener('click', stopTacticEditing);
|
|
|
|
document.querySelectorAll('#map-tactic-tools .map-tool[data-shape]').forEach(function (btn) {
|
|
btn.addEventListener('click', function () {
|
|
var shape = btn.getAttribute('data-shape');
|
|
if (drawingShape === shape) { cancelDrawing(); return; }
|
|
cancelDrawing();
|
|
drawingShape = shape;
|
|
drawPoints = [];
|
|
btn.classList.add('active');
|
|
document.getElementById('map-canvas').classList.add('map-placing');
|
|
if (finishBtn) finishBtn.hidden = shape !== 'route' && shape !== 'area';
|
|
setStatus(shape === 'route' || shape === 'area'
|
|
? 'Punkte anklicken, Doppelklick beendet die Form (Esc bricht ab).'
|
|
: 'Auf die Karte klicken, um das Objekt zu setzen (Esc bricht ab).');
|
|
});
|
|
});
|
|
|
|
function cancelDrawing() {
|
|
drawingShape = null;
|
|
drawPoints = [];
|
|
if (drawPreview) { map.removeLayer(drawPreview); drawPreview = null; }
|
|
document.querySelectorAll('#map-tactic-tools .map-tool').forEach(function (b) { b.classList.remove('active'); });
|
|
document.getElementById('map-canvas').classList.remove('map-placing');
|
|
if (finishBtn) finishBtn.hidden = true;
|
|
}
|
|
|
|
function currentColor() {
|
|
var picker = document.getElementById('map-shape-color');
|
|
return picker ? picker.value : '#c9a227';
|
|
}
|
|
|
|
function handleDrawClick(e) {
|
|
if (!activeTacticId) return;
|
|
var position = toNormalized(e.latlng);
|
|
|
|
if (drawingShape === 'point' || drawingShape === 'text') {
|
|
askForLabel(drawingShape, [position]);
|
|
return;
|
|
}
|
|
|
|
drawPoints.push(position);
|
|
var latlngs = drawPoints.map(function (p) { return toLatLng(p[0], p[1]); });
|
|
if (drawPreview) map.removeLayer(drawPreview);
|
|
drawPreview = drawingShape === 'area'
|
|
? L.polygon(latlngs, { color: currentColor(), weight: 2, dashArray: '4 4', fillOpacity: 0.12 })
|
|
: L.polyline(latlngs, { color: currentColor(), weight: 3, dashArray: '4 4' });
|
|
drawPreview.addTo(map);
|
|
}
|
|
|
|
// Route/Flaeche abschliessen: per Doppelklick, ueber den Button oder mit Enter.
|
|
// Doppelklick allein reicht nicht - auf Touch-Geraeten loest Doppeltippen kein
|
|
// zuverlaessiges dblclick aus.
|
|
function finishShape() {
|
|
if (!drawingShape || (drawingShape !== 'route' && drawingShape !== 'area')) return;
|
|
if (drawPoints.length < 2) {
|
|
setStatus('Bitte mindestens zwei Punkte setzen.', true);
|
|
return;
|
|
}
|
|
askForLabel(drawingShape, drawPoints.slice());
|
|
}
|
|
|
|
map.on('dblclick', function (e) {
|
|
if (!drawingShape || (drawingShape !== 'route' && drawingShape !== 'area')) return;
|
|
L.DomEvent.stop(e);
|
|
finishShape();
|
|
});
|
|
|
|
var finishBtn = document.getElementById('map-shape-finish');
|
|
if (finishBtn) finishBtn.addEventListener('click', finishShape);
|
|
|
|
// Beschriftung/Farbe kommen aus einem eigenen Popup statt aus window.prompt -
|
|
// dieselbe Optik wie die uebrigen Dialoge der Seite.
|
|
var shapeForm = document.getElementById('map-shape-form');
|
|
var pendingShape = null;
|
|
|
|
function askForLabel(type, points) {
|
|
pendingShape = { type: type, points: points };
|
|
var labels = { point: 'Punkt beschriften', route: 'Route beschriften', area: 'Fläche beschriften', text: 'Notiz eingeben' };
|
|
document.getElementById('map-shape-modal-title').textContent = labels[type] || 'Objekt beschriften';
|
|
// Nur die Notiz besteht aus ihrem Text - bei allen anderen ist er optional.
|
|
document.getElementById('map-shape-label-hint').textContent = type === 'text' ? '(erforderlich)' : '(optional)';
|
|
shapeForm.label.value = '';
|
|
shapeForm.color.value = currentColor();
|
|
openModal('map-shape-modal');
|
|
shapeForm.label.focus();
|
|
}
|
|
|
|
if (shapeForm) {
|
|
shapeForm.addEventListener('submit', function (e) {
|
|
e.preventDefault();
|
|
if (!pendingShape) return;
|
|
|
|
var label = shapeForm.label.value.trim();
|
|
if (pendingShape.type === 'text' && label === '') {
|
|
feedback('map-shape-form', 'Bitte einen Text für die Notiz eingeben.', true);
|
|
return;
|
|
}
|
|
|
|
saveShape(pendingShape.type, label, pendingShape.points, shapeForm.color.value);
|
|
pendingShape = null;
|
|
closeModal('map-shape-modal');
|
|
cancelDrawing();
|
|
});
|
|
|
|
document.getElementById('map-shape-cancel').addEventListener('click', function () {
|
|
pendingShape = null;
|
|
closeModal('map-shape-modal');
|
|
cancelDrawing();
|
|
});
|
|
|
|
// Auch Schliessen ueber das X oder einen Klick neben das Popup bricht das
|
|
// angefangene Zeichnen ab, damit kein Werkzeug aktiv haengen bleibt.
|
|
document.getElementById('map-shape-modal').addEventListener('click', function (e) {
|
|
if (e.target.id === 'map-shape-modal' || e.target.closest('.modal-close')) {
|
|
pendingShape = null;
|
|
cancelDrawing();
|
|
}
|
|
});
|
|
}
|
|
|
|
function saveShape(type, label, points, color) {
|
|
postJson({
|
|
action: 'shape_save',
|
|
tactic_id: activeTacticId,
|
|
type: type,
|
|
label: label,
|
|
color: color,
|
|
points: points
|
|
}).then(function (result) {
|
|
if (result.status !== 'success') {
|
|
setStatus(result.message, true);
|
|
return;
|
|
}
|
|
var shape = { id: result.shape_id, type: type, label: label, color: color, points: points };
|
|
tacticsById[activeTacticId].shapes.push(shape);
|
|
drawShape(activeTacticId, shape);
|
|
setStatus('Objekt gespeichert.');
|
|
}).catch(function () {
|
|
setStatus('Verbindung fehlgeschlagen.', true);
|
|
});
|
|
}
|
|
|
|
// Nach dem Anlegen einer Taktik landet man per ?edit=<id> direkt im Bearbeiten-Modus.
|
|
var editParam = new URLSearchParams(window.location.search).get('edit');
|
|
if (editParam) {
|
|
document.querySelector('.map-tab[data-panel="tactics"]').click();
|
|
startTacticEditing(parseInt(editParam, 10));
|
|
}
|
|
})();
|