LuminaWebsite/assets/map/js/controllers/LocationController.js
Maaxxs c869f93152 Map: share locations and pick them for calendar entries
- Map links (/map.php?karte=…&x=…&y=…&zoom=…&ort=…) open the region at that spot with a pin
- "Share location" button on the map: click a spot, optional name, copy the link
- Calendar entries: "Pick location on the map" opens the map in a window, "Use this location" stores the meeting point
- Map links in comments, captions and descriptions render as "📍 Region – Location" and do not count as external links for moderation

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

263 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Orte auf der Karte verlinken:
* - Link oeffnen: /map.php?karte=verteron&x=1234&y=2345&zoom=5&ort=Treffpunkt oeffnet die Karte an
* der Stelle und setzt dort einen Pin (map.php reicht die Parameter an diesen Viewer weiter).
* - Ort teilen (📍-Knopf): auf die Karte klicken, Namen eingeben, Link kopieren.
* - Ort auswaehlen (?pick=1): die Karte laeuft in einem Fenster der Website (z.B. Kalender);
* "Diesen Ort uebernehmen" schickt den Ort per postMessage an die Seite zurueck.
*/
class LocationController {
constructor(mapController) {
this.map = mapController;
this.view = mapController.view;
this.params = new URLSearchParams(window.location.search);
this.pickMode = this.params.get('pick') === '1';
this.active = false; // Klick auf die Karte setzt gerade einen Ort
this.pin = null; // {x, y} in Weltkoordinaten
this.label = '';
this.buildUi();
this.view.onPinUpdate = () => this.updatePin();
this.map.onTap = (clientX, clientY) => this.handleTap(clientX, clientY);
this.map.onMapChanged = this.chain(this.map.onMapChanged, () => this.clearPin());
// Mausklick (ohne Ziehen) auf die Karte
let down = null;
this.view.canvas.addEventListener('mousedown', e => { down = { x: e.clientX, y: e.clientY }; });
this.view.canvas.addEventListener('mouseup', e => {
if (down && Math.hypot(e.clientX - down.x, e.clientY - down.y) < 5) this.handleTap(e.clientX, e.clientY);
down = null;
});
if (this.pickMode) {
document.body.classList.add('pick-mode');
this.start();
}
}
/** Zwei Callbacks hintereinander (TacticController haengt sich auch an onMapChanged). */
chain(first, second) {
return (...args) => {
if (first) first(...args);
second(...args);
};
}
/** Karte aus dem Link (?karte=...), sonst null. */
requestedMap() {
const slug = this.params.get('karte');
return slug && this.map.mapData.maps.some(m => m.slug === slug) ? slug : null;
}
/** Nach dem Laden der Karte aus dem Link: an die Stelle springen und den Pin setzen. */
applyLink() {
const x = parseFloat(this.params.get('x'));
const y = parseFloat(this.params.get('y'));
if (!this.requestedMap() || !isFinite(x) || !isFinite(y)) return;
const zoom = parseInt(this.params.get('zoom'), 10);
this.view.zoom = Math.min(CONFIG.MAX_ZOOM, Math.max(CONFIG.MIN_ZOOM, isFinite(zoom) ? zoom : CONFIG.MIN_ZOOM + 3));
this.view.centerWorldX = x;
this.view.centerWorldY = y;
this.setPin(x, y, (this.params.get('ort') || '').slice(0, 60));
this.map.render();
if (this.pickMode) this.showPanel();
}
buildUi() {
const overlay = document.getElementById('ui-overlay');
// 📍-Knopf in der Zoom-Leiste
const button = document.createElement('button');
button.id = 'btn-share-location';
button.title = 'Ort teilen - auf die Karte klicken und den Link kopieren';
button.textContent = '📍';
button.addEventListener('click', () => (this.active ? this.stop() : this.start()));
document.getElementById('zoom-controls').appendChild(button);
this.button = button;
// Hinweis oben: "Klicke auf die Karte"
this.banner = document.createElement('div');
this.banner.id = 'loc-banner';
this.banner.className = 'panel hidden';
this.banner.innerHTML = '<span>📍 Klicke auf die Karte, um einen Ort zu markieren</span>';
if (!this.pickMode) {
const cancel = document.createElement('button');
cancel.type = 'button';
cancel.textContent = 'Abbrechen';
cancel.addEventListener('click', () => this.stop());
this.banner.appendChild(cancel);
}
overlay.appendChild(this.banner);
// Pin mit Beschriftung
this.pinEl = document.createElement('div');
this.pinEl.id = 'map-pin';
this.pinEl.className = 'hidden';
this.pinEl.innerHTML = '<span class="pin-label"></span><span class="pin-dot"></span>';
document.getElementById('map-container').appendChild(this.pinEl);
// Fenster mit Name und Link bzw. "Uebernehmen"
this.panel = document.createElement('div');
this.panel.id = 'loc-panel';
this.panel.className = 'panel hidden';
this.panel.innerHTML =
'<button type="button" class="loc-close" title="Schließen" aria-label="Schließen">✕</button>'
+ '<h3>' + (this.pickMode ? 'Ort auswählen' : 'Ort teilen') + '</h3>'
+ '<label><span>Name des Ortes <span class="loc-optional">(optional)</span></span>'
+ '<input type="text" id="loc-name" maxlength="60" placeholder="z.B. Treffpunkt am Portal"></label>'
+ '<div class="loc-where" id="loc-where"></div>'
+ (this.pickMode
? '<button type="button" id="loc-use" class="loc-primary">✓ Diesen Ort übernehmen</button>'
: '<div class="loc-link-row"><input type="text" id="loc-link" readonly aria-label="Link">'
+ '<button type="button" id="loc-copy" class="loc-primary">Link kopieren</button></div>')
+ '<p class="loc-hint">' + (this.pickMode ? 'Woanders klicken, um den Ort zu ändern.' : 'Den Link kannst du in Terminen, Kommentaren oder im Discord einfügen.') + '</p>';
overlay.appendChild(this.panel);
this.nameInput = this.panel.querySelector('#loc-name');
this.nameInput.addEventListener('input', () => {
this.label = this.nameInput.value.trim();
this.updatePin();
this.updateLink();
});
// Tasten wie H oder +/- sollen beim Tippen nicht die Karte steuern
this.nameInput.addEventListener('keydown', e => e.stopPropagation());
this.panel.querySelector('.loc-close').addEventListener('click', () => {
this.panel.classList.add('hidden');
if (!this.pickMode) this.clearPin();
});
if (this.pickMode) {
this.panel.querySelector('#loc-use').addEventListener('click', () => this.sendPick());
} else {
this.panel.querySelector('#loc-copy').addEventListener('click', () => this.copyLink());
}
}
start() {
this.active = true;
this.button.classList.add('active');
this.banner.classList.remove('hidden');
this.view.canvas.classList.add('picking');
}
stop() {
this.active = false;
this.button.classList.remove('active');
this.banner.classList.add('hidden');
this.view.canvas.classList.remove('picking');
}
/** Klick/Tipp auf die Karte - true, wenn er hier verarbeitet wurde. */
handleTap(clientX, clientY) {
if (!this.active || !this.map.mapData.currentMap) return false;
const rect = this.view.canvas.getBoundingClientRect();
const world = this.view.screenToWorld(clientX - rect.left, clientY - rect.top);
const x = Math.round(world.x);
const y = Math.round(world.y);
if (x < CONFIG.WORLD_MIN_X || x > CONFIG.WORLD_MAX_X || y < CONFIG.WORLD_MIN_Y || y > CONFIG.WORLD_MAX_Y) return true;
// Name vorschlagen: der naechste Marker in der Naehe (auch ausgeblendete Kategorien)
const nearby = this.nearestMarkerName(clientX - rect.left, clientY - rect.top);
this.setPin(x, y, this.nameInput.value.trim() || nearby);
this.showPanel();
if (!this.pickMode) this.stop();
return true;
}
nearestMarkerName(screenX, screenY) {
let best = '';
let bestDist = 40;
for (const marker of this.map.mapData.markers) {
const p = this.view.worldToScreen(marker.worldX, marker.worldY);
const dist = Math.hypot(p.x - screenX, p.y - screenY);
if (dist < bestDist) {
bestDist = dist;
best = marker.iconText || marker.nt || marker.name || '';
}
}
return String(best).slice(0, 60);
}
setPin(x, y, label) {
this.pin = { x, y };
this.label = label || '';
this.nameInput.value = this.label;
this.updatePin();
this.updateLink();
}
clearPin() {
this.pin = null;
this.pinEl.classList.add('hidden');
}
updatePin() {
if (!this.pin) return;
const p = this.view.worldToScreen(this.pin.x, this.pin.y);
this.pinEl.style.left = p.x + 'px';
this.pinEl.style.top = p.y + 'px';
this.pinEl.querySelector('.pin-label').textContent = this.label;
this.pinEl.querySelector('.pin-label').hidden = this.label === '';
this.pinEl.classList.remove('hidden');
}
showPanel() {
this.panel.classList.remove('hidden');
this.updateLink();
if (!this.pickMode) this.nameInput.focus();
}
mapName() {
return this.map.mapData.currentMap ? this.map.mapData.currentMap.name : '';
}
/** Pfad des Links, z.B. /map.php?karte=verteron&x=1&y=2&zoom=5&ort=Portal */
linkPath() {
if (!this.pin || !this.map.mapData.currentMap) return '';
const params = new URLSearchParams({
karte: this.map.mapData.currentMap.slug,
x: String(this.pin.x),
y: String(this.pin.y),
zoom: String(Math.max(this.view.zoom, CONFIG.MIN_ZOOM + 2)),
});
if (this.label) params.set('ort', this.label);
return '/map.php?' + params.toString();
}
updateLink() {
const where = this.panel.querySelector('#loc-where');
where.textContent = this.pin ? '🗺️ ' + this.mapName() + (this.label ? ' – ' + this.label : '') : '';
const link = this.panel.querySelector('#loc-link');
if (link) link.value = this.pin ? window.location.origin + this.linkPath() : '';
}
copyLink() {
const link = this.panel.querySelector('#loc-link');
const done = () => this.toast('Link kopiert - einfach in Termin, Kommentar oder Discord einfügen.');
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(link.value).then(done, () => { link.select(); document.execCommand('copy'); done(); });
} else {
link.select();
document.execCommand('copy');
done();
}
}
sendPick() {
if (!this.pin) return;
window.parent.postMessage({
type: 'lumina-map-pick',
url: this.linkPath(),
label: this.mapName() + (this.label ? ' – ' + this.label : ''),
}, window.location.origin);
}
toast(text) {
const toast = document.getElementById('toast');
if (!toast) return;
toast.textContent = text;
toast.classList.remove('hidden');
clearTimeout(this.toastTimer);
this.toastTimer = setTimeout(() => toast.classList.add('hidden'), 2500);
}
}