/**
* 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.mapSelect = document.getElementById('map-select');
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();
// Load first map by default
if (this.mapData.maps.length > 0) {
const firstMap = this.mapData.maps[0];
this.mapSelect.value = firstMap.slug;
await this.loadMap(firstMap.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();
}
/**
* Populate the map selection dropdown
*/
populateMapSelector() {
const maps = this.mapData.maps;
if (maps.length === 0) {
this.mapSelect.innerHTML = '';
return;
}
// Group by zone
let html = '';
const zones = {};
for (const map of maps) {
if (!zones[map.zone]) {
zones[map.zone] = [];
}
zones[map.zone].push(map);
}
for (const [zoneName, zoneMaps] of Object.entries(zones)) {
html += `';
}
this.mapSelect.innerHTML = html;
}
/**
* 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`);
// 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 += `