/**
* MapController - Main controller for the map viewer application
*/
class MapController {
constructor() {
this.mapData = new MapData();
this.tileService = new TileService();
this.markerService = new MarkerService();
// DOM elements
this.canvas = document.getElementById('map-canvas');
this.mapPicker = document.getElementById('map-picker');
this.mapPickerList = document.getElementById('map-picker-list');
this.mapPickerBtn = document.getElementById('map-picker-btn');
this.mapCurrentName = document.getElementById('map-current-name');
this.categoriesList = document.getElementById('categories-list');
this.zoomLevelEl = document.getElementById('zoom-level');
this.loadingOverlay = document.getElementById('loading-overlay');
this.tooltip = document.getElementById('tooltip');
// Initialize view
this.view = new MapView(this.canvas);
this.view.resize();
window.addEventListener('resize', () => {
this.view.resize();
this.render();
});
}
/**
* Initialize the application
*/
async init() {
this.showLoading(true);
try {
// Discover available maps
await this.discoverMaps();
// Zuletzt gewaehlte Karte, sonst die Standardkarte, sonst die erste
if (this.mapData.maps.length > 0) {
let saved = null;
try {
saved = localStorage.getItem('lumina_map');
} catch (e) {
saved = null;
}
const has = slug => this.mapData.maps.some(m => m.slug === slug);
const slug = has(saved) ? saved : (has(CONFIG.DEFAULT_MAP) ? CONFIG.DEFAULT_MAP : this.mapData.maps[0].slug);
await this.loadMap(slug);
}
} catch (e) {
console.error('Failed to initialize:', e);
} finally {
this.showLoading(false);
}
// Setup event listeners
this.setupEventListeners();
}
/**
* Discover available maps from local maps.json file
*/
async discoverMaps() {
try {
const response = await fetch(CONFIG.MAPS_LIST);
if (!response.ok) throw new Error('maps.json not found');
const data = await response.json();
const maps = data.maps || [];
for (const mapEntry of maps) {
this.mapData.addMap({
id: mapEntry.id,
slug: mapEntry.slug,
name: mapEntry.name,
zone: mapEntry.zone
});
}
} catch (e) {
console.warn('Failed to load maps list:', e);
}
// Populate map selector dropdown
this.populateMapSelector();
}
/**
* Kartenauswahl aufbauen: pro Gebiet eine Reihe Karten mit Vorschaubild.
*/
populateMapSelector() {
const maps = this.mapData.maps;
this.mapPickerList.textContent = '';
if (maps.length === 0) {
this.mapCurrentName.textContent = 'Keine Karten';
return;
}
// Nach den Gebieten aus CONFIG.MAP_ZONES ordnen (maps.json kennt keine Gebiete).
const zones = {};
for (const zone of CONFIG.MAP_ZONES) {
const zoneMaps = zone.maps.map(slug => maps.find(m => m.slug === slug)).filter(Boolean);
if (zoneMaps.length) zones[zone.name] = zoneMaps;
}
const assigned = new Set(CONFIG.MAP_ZONES.flatMap(zone => zone.maps));
const rest = maps.filter(m => !assigned.has(m.slug));
if (rest.length) zones['Weitere'] = rest;
for (const [zoneName, zoneMaps] of Object.entries(zones)) {
const section = document.createElement('section');
section.className = 'map-picker-zone';
const heading = document.createElement('h3');
heading.textContent = zoneName;
const grid = document.createElement('div');
grid.className = 'map-picker-grid';
for (const map of zoneMaps) {
const card = document.createElement('button');
card.type = 'button';
card.className = 'map-card';
card.dataset.slug = map.slug;
const img = document.createElement('img');
img.src = `${CONFIG.PREVIEWS_DIR}/${map.slug}.webp`;
img.alt = '';
// Nicht lazy: die zehn kleinen Vorschauen (~300 KB) laden gleich mit, damit sie
// beim Oeffnen der Auswahl sofort da sind.
img.decoding = 'async';
img.onerror = () => img.remove();
const name = document.createElement('span');
name.className = 'map-card-name';
name.textContent = map.name;
card.append(img, name);
card.addEventListener('click', () => {
this.closeMapPicker();
if (map.slug !== this.mapData.currentMap?.slug) {
this.loadMap(map.slug);
}
});
grid.appendChild(card);
}
section.append(heading, grid);
this.mapPickerList.appendChild(section);
}
}
openMapPicker() {
this.mapPickerList.querySelectorAll('.map-card').forEach(card => {
card.classList.toggle('active', card.dataset.slug === this.mapData.currentMap?.slug);
});
this.mapPicker.classList.remove('hidden');
const active = this.mapPickerList.querySelector('.map-card.active') || this.mapPickerList.querySelector('.map-card');
if (active) active.focus();
}
closeMapPicker() {
this.mapPicker.classList.add('hidden');
this.mapPickerBtn.focus();
}
/**
* Load a specific map by slug
*/
async loadMap(slug) {
this.showLoading(true);
try {
// Set current map in model
const mapInfo = this.mapData.setCurrentMap(slug);
if (!mapInfo) throw new Error(`Map ${slug} not found`);
this.mapCurrentName.textContent = mapInfo.name;
try {
localStorage.setItem('lumina_map', slug);
} catch (e) {
// ohne Speicher eben jedes Mal die Standardkarte
}
// Load tile manifest
await this.tileService.loadManifest(slug);
// Load markers
try {
const markerData = await this.markerService.loadMarkers(slug);
this.mapData.loadMarkers(markerData);
} catch (e) {
console.warn(`No markers found for ${slug}`);
}
// Reset view and render
this.view.resetView();
this.buildCategoryUI();
this.render();
if (this.onMapChanged) this.onMapChanged(slug);
} catch (e) {
console.error('Failed to load map:', e);
} finally {
this.showLoading(false);
}
}
/**
* Build the category filter UI
*/
buildCategoryUI() {
const categories = this.mapData.categories;
if (Object.keys(categories).length === 0) {
this.categoriesList.innerHTML = '
No categories available
';
return;
}
// Group by group_name
const groups = {};
Object.entries(categories).forEach(([key, cat]) => {
if (!groups[cat.group]) {
groups[cat.group] = [];
}
groups[cat.group].push({ key, ...cat });
});
let html = '';
for (const [groupName, cats] of Object.entries(groups)) {
html += `