LuminaWebsite/assets/map/js/services/TacticService.js
Maaxxs 1278859f30 Karte: tactics in steps drawn on the map, show/hide all categories
The category panel gets "Alle anzeigen" / "Alle verstecken" buttons.

Tactics belong to a region and are split into steps, each its own drawing
layer: pen, arrow, line, area, circle, labelled point, text and eraser, with
colour, stroke width and undo. A new step can start empty or as a copy of the
current one, earlier steps can be shown faded underneath, and steps can be
renamed, reordered and deleted. Viewing needs map.view, editing map.tactics.

Drawings are stored per shape in world coordinates (the same as the markers)
through tactics_api.php in three new tables; the map page offers to create
them when they are missing. The viewer's own code only gains two hooks
(view change, map change) and the show/hide-all method.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 07:21:45 +02:00

61 lines
2 KiB
JavaScript

/**
* TacticService - Laden und Speichern der Taktiken ueber die Website (tactics_api.php).
*
* Der Viewer laeuft im iframe von map.php auf derselben Domain: Anmeldung (Session-Cookie)
* und CSRF-Token kommen von der umgebenden Seite. Ausserhalb der Website - etwa als
* lokale Datei - gibt es den Endpunkt nicht, dann bleiben die Taktiken ausgeblendet.
*/
class TacticService {
constructor() {
this.endpoint = '/tactics_api.php';
}
csrfToken() {
try {
const meta = window.parent.document.querySelector('meta[name="csrf-token"]');
return meta ? meta.content : '';
} catch (e) {
return '';
}
}
async list(slug) {
const response = await fetch(`${this.endpoint}?map=${encodeURIComponent(slug)}`, {
credentials: 'same-origin',
headers: { 'Accept': 'application/json' }
});
return this.parse(response);
}
async send(action, payload = {}) {
const response = await fetch(this.endpoint, {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
body: JSON.stringify({ ...payload, action, csrf_token: this.csrfToken() })
});
return this.parse(response);
}
async parse(response) {
const text = await response.text();
let data;
try {
data = JSON.parse(text);
} catch (e) {
const error = new Error(text && response.status !== 404
? text.replace(/<[^>]*>/g, ' ').trim().slice(0, 200)
: 'Taktiken sind hier nicht verfügbar.');
error.unavailable = response.status === 404;
throw error;
}
if (data.status !== 'success') {
const error = new Error(data.message || 'Unbekannter Fehler.');
error.data = data;
throw error;
}
return data;
}
}