File size: 3,630 Bytes
b8952af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70f6fef
b8952af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
878012f
 
 
 
 
 
 
 
b8952af
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import mapboxgl from 'mapbox-gl';
import { MAPBOX_TOKEN, MAP_CONFIG, HOME_BASE, COLORS } from '../config.js';
import { FlightPathLayer } from '../webgl/FlightPathLayer.js';

export class MapView {
  constructor(container, options = {}) {
    this.container = container;
    this.markers = new window.Map();
    this.homeMarker = null;
    this.onEventSelect = options.onEventSelect || (() => {});

    this.flightPathLayer = null;

    this.init();
  }

  init() {
    mapboxgl.accessToken = MAPBOX_TOKEN;

    this.map = new mapboxgl.Map({
      container: this.container,
      ...MAP_CONFIG
    });

    this.map.on('load', () => {
      this.addHomeBase();
      this.flightPathLayer = new FlightPathLayer(this.map);
      this.map.resize();
    });

    this.map.addControl(new mapboxgl.NavigationControl(), 'top-left');
  }

  addHomeBase() {
    const el = document.createElement('div');
    el.className = 'home-marker';
    el.innerHTML = `
      <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
        <circle cx="12" cy="12" r="10" fill="${COLORS.blue}" stroke="${COLORS.white}" stroke-width="2"/>
        <path d="M12 6L16 14H8L12 6Z" fill="${COLORS.white}"/>
      </svg>
    `;

    this.homeMarker = new mapboxgl.Marker({ element: el, anchor: 'center' })
      .setLngLat(HOME_BASE.coordinates)
      .addTo(this.map);
  }

  setEvents(events) {
    this.events = events;

    // Clear existing markers
    this.markers.forEach(marker => marker.remove());
    this.markers.clear();

    events.forEach(event => {
      const el = this.createMarkerElement(event);

      const marker = new mapboxgl.Marker({ element: el, anchor: 'center' })
        .setLngLat(event.location.coordinates)
        .addTo(this.map);

      el.addEventListener('click', () => {
        this.onEventSelect(event);
      });

      this.markers.set(event.id, marker);
    });
  }

  createMarkerElement(event) {
    const el = document.createElement('div');
    el.className = `event-marker ${event.type}`;

    const color = event.type === 'international' ? COLORS.blue : COLORS.red;

    el.innerHTML = `
      <svg width="20" height="20" viewBox="0 0 20 20" fill="none">
        <circle cx="10" cy="10" r="8" fill="${color}" opacity="0.2"/>
        <circle cx="10" cy="10" r="5" fill="${color}"/>
      </svg>
    `;

    return el;
  }

  flyTo(coordinates, zoom = 9) {
    this.map.flyTo({
      center: coordinates,
      zoom,
      duration: 1500,
      essential: true
    });
  }

  selectEvent(eventId) {
    // Reset all markers
    this.markers.forEach((marker, id) => {
      marker.getElement().classList.remove('selected');
    });

    // Highlight selected marker
    const marker = this.markers.get(eventId);
    if (marker) {
      marker.getElement().classList.add('selected');
    }
  }

  // Show full static path between two points
  showFlightPath(from, to) {
    if (this.flightPathLayer) {
      this.flightPathLayer.showPath(from, to);
    }
  }

  // Animate path being traced, call onComplete when done
  animateFlightPath(from, to, duration, onComplete) {
    if (this.flightPathLayer) {
      this.flightPathLayer.animatePath(from, to, duration, onComplete);
    }
  }

  hideFlightPath() {
    if (this.flightPathLayer) {
      this.flightPathLayer.hide();
    }
  }

  resetView() {
    this.map.flyTo({
      center: MAP_CONFIG.center,
      zoom: MAP_CONFIG.zoom,
      duration: 1000
    });

    this.hideFlightPath();
  }

  zoomToFrance() {
    this.map.flyTo({
      center: MAP_CONFIG.center,
      zoom: 4.3,
      duration: 800
    });
  }

  getMap() {
    return this.map;
  }
}