File size: 5,062 Bytes
b8952af
 
 
 
 
 
 
 
878012f
 
 
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { MapView } from './components/Map.js';
import { Timeline } from './components/Timeline.js';
import { EventCard } from './components/EventCard.js';
import { Countdown } from './components/Countdown.js';
import { HOME_BASE } from './config.js';
import airshowData from './data/airshows.json';

const FLIGHT_ANIMATION_DURATION = 2000; // ms
const MOBILE_BREAKPOINT = 768;

const isMobile = () => window.innerWidth <= MOBILE_BREAKPOINT;

class App {
  constructor() {
    this.map = null;
    this.timeline = null;
    this.eventCard = null;
    this.countdown = null;
    this.isPlaying = false;

    this.init();
  }

  init() {
    // Initialize event card
    this.eventCard = new EventCard(document.getElementById('event-card'));
    this.eventCard.setOnClose(() => this.handleCardClose());

    // Initialize countdown
    this.countdown = new Countdown(document.getElementById('countdown'));
    this.initCountdown();

    // Initialize map
    this.map = new MapView('map', {
      onEventSelect: (event) => this.handleMapEventClick(event)
    });

    // Initialize timeline
    this.timeline = new Timeline(document.getElementById('timeline'));
    this.timeline.setOnEventSelect((event, index) => this.handleTimelineSelect(event, index));
    this.timeline.setOnPlayStateChange((playing) => this.handlePlayStateChange(playing));
    this.timeline.setEvents(airshowData.events);

    // Set events on map after it loads
    this.map.getMap().on('load', () => {
      this.map.setEvents(airshowData.events);
    });

    // Keyboard navigation
    document.addEventListener('keydown', (e) => this.handleKeydown(e));
  }

  initCountdown() {
    // Find next upcoming event
    const now = new Date();
    const sortedEvents = [...airshowData.events].sort((a, b) => new Date(a.date) - new Date(b.date));
    const nextEvent = sortedEvents.find(e => new Date(e.date) >= now);

    if (nextEvent) {
      this.countdown.setNextEvent(nextEvent);
    }
  }

  handleMapEventClick(event) {
    // Clicking map marker pauses playback and selects event
    if (this.isPlaying) {
      this.timeline.pause();
      this.isPlaying = false;
    }
    this.timeline.selectEvent(event.id);
  }

  handleTimelineSelect(event, index) {
    // Update map marker selection
    this.map.selectEvent(event.id);

    // Show event card
    this.eventCard.show(event);

    // Get the "from" location (previous event or home base)
    const prevEvent = index > 0 ? this.timeline.sortedEvents[index - 1] : null;
    const fromCoords = prevEvent ? prevEvent.location.coordinates : HOME_BASE.coordinates;
    const toCoords = event.location.coordinates;

    if (this.isPlaying) {
      // Animate the flight path being traced (no zoom/pan during playback)
      this.map.animateFlightPath(fromCoords, toCoords, FLIGHT_ANIMATION_DURATION, () => {
        // When animation completes, advance to next event
        if (this.isPlaying) {
          this.advanceToNextEvent();
        }
      });
    } else {
      // Show static full path to next event
      this.map.flyTo(toCoords, 8);
      const nextEvent = this.timeline.getNextEvent();
      if (nextEvent) {
        this.map.showFlightPath(toCoords, nextEvent.location.coordinates);
      } else {
        this.map.hideFlightPath();
      }
    }
  }

  handlePlayStateChange(playing) {
    this.isPlaying = playing;

    if (playing) {
      // On mobile, zoom out to show all of France during playback
      if (isMobile()) {
        this.map.zoomToFrance();
      }

      // Start playback
      if (this.timeline.selectedIndex < 0) {
        // No event selected, start from first
        this.timeline.selectEventByIndex(0);
      } else {
        // Re-trigger current selection to start animation
        const currentEvent = this.timeline.getCurrentEvent();
        if (currentEvent) {
          this.handleTimelineSelect(currentEvent, this.timeline.selectedIndex);
        }
      }
    } else {
      // Paused - show static path to next event
      const currentEvent = this.timeline.getCurrentEvent();
      const nextEvent = this.timeline.getNextEvent();
      if (currentEvent && nextEvent) {
        this.map.showFlightPath(currentEvent.location.coordinates, nextEvent.location.coordinates);
      }
    }
  }

  advanceToNextEvent() {
    const hasNext = this.timeline.nextEvent();
    if (!hasNext) {
      // Reached end, stop playing
      this.timeline.pause();
      this.isPlaying = false;
      this.map.hideFlightPath();
    }
  }

  handleCardClose() {
    // Pause if playing
    if (this.isPlaying) {
      this.timeline.pause();
      this.isPlaying = false;
    }
    this.timeline.clearSelection();
    this.map.selectEvent(null);
    this.map.resetView();
  }

  handleKeydown(e) {
    if (e.key === 'Escape') {
      this.handleCardClose();
    } else if (e.key === ' ' || e.code === 'Space') {
      e.preventDefault();
      this.timeline.togglePlay();
    } else if (e.key === 'ArrowRight' && !this.isPlaying) {
      this.timeline.nextEvent();
    }
  }
}

// Start app
new App();