LuminaWebsite/assets/map/js/controllers/LocationController.js
Maaxxs 342301b082 Consilium: guests see only their shared tactic and go back to the session list when the share ends
Asked for: when the share for the Consilium ends, everyone from the Consilium is sent back to the live overview
(so far only the live view ended and the map stayed open); during the session they may not switch maps or see the
categories - really only the tactic.

- map_frame.php: Consilium guests get the map viewer only for a running shared session (?karte=...&taktik=...),
  otherwise the whole page goes back to the session list; the viewer gets LUMINA_GUEST.
- Viewer guest mode (body.guest-mode): no map picker, no markers or categories (not even loaded), no location
  sharing, no tactic list - only the tactic of the session (tactics_api.php ?taktik=).
- When the share ends (relay event, tactic gone, no ticket), TacticController.endGuestSession() sends the top page
  to map.php?beendet=stopped|ended with a note why; the page around the map also checks every 15 s.
- Calendar: no location picker for guests, meeting points are plain text for them (map_link_html()).
- README, patch note text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 02:04:24 +02:00

271 lines
12 KiB
JavaScript
Raw Permalink 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);
// Gaeste des Consiliums sehen nur ihre Taktik - keine Orte teilen oder auswaehlen (📍 versteckt per CSS)
this.guest = !!mapController.guest;
this.pickMode = this.params.get('pick') === '1' && !this.guest;
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.guest || !this.requestedMap() || !isFinite(x) || !isFinite(y)) return;
const zoom = parseInt(this.params.get('zoom'), 10);
// Hoechstens bis zur letzten Zoomstufe dieser Karte (alte Links mit zoom=6 auf Karten bis 5)
this.view.zoom = Math.min(this.view.maxZoom, Math.max(CONFIG.MIN_ZOOM, isFinite(zoom) ? zoom : CONFIG.MIN_ZOOM + 3));
this.view.centerWorldX = x;
this.view.centerWorldY = y;
// Ort am Kartenrand: Ansicht in der Karte halten, der Pin ist dann nicht ganz mittig
this.view.clampView();
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() {
if (this.guest) return;
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 (Dungeon-Karten: /raidplan.php) */
linkPath() {
const map = this.map.mapData.currentMap;
if (!this.pin || !map) return '';
const params = new URLSearchParams({
karte: map.slug,
x: String(this.pin.x),
y: String(this.pin.y),
// Links kennen nur ganze Zoomstufen (Dungeon-Karten zoomen in halben)
zoom: String(Math.max(Math.round(this.view.zoom), CONFIG.MIN_ZOOM + 2)),
});
if (this.label) params.set('ort', this.label);
return this.map.areaInfo(map.area).page + '?' + 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);
}
}