import { dispatchNotificationEvent, type NotificationEventPayload } from "../../renderer/notificationEventHandler.js"; import { sendToMain } from "../../renderer/sendToMain.js"; import type { ImportWebComponentPayload } from "./ImportWebComponent.js"; import { registerElement } from "../../renderer/registerElement.js"; export type FlightPlanChooserWebComponentState = { flightPlanIndex: number; filepath: string; }; export class FlightPlanChooserWebComponent extends HTMLElement { private isInitialized = false; private elements!: { filePath: HTMLOutputElement; flightplanSelect: HTMLSelectElement; dialog: HTMLDialogElement; }; get state(): FlightPlanChooserWebComponentState { return { flightPlanIndex: this.elements.flightplanSelect.selectedIndex - 1, // -1 because of the default "Select flightplan" option filepath: this.elements.filePath.innerText, }; } set values(values: ImportWebComponentPayload) { this.elements.filePath.innerText = values.filepath; this.elements.flightplanSelect.innerHTML = `
` + values.flightplans .map((flightplan: string, index: number) => ``) .join(""); } private initialize() { this.innerHTML = `\

Select Flight Plan from File

`; this.elements = { filePath: this.querySelector("output") as HTMLOutputElement, flightplanSelect: this.querySelector("select") as HTMLSelectElement, dialog: this.querySelector("dialog") as HTMLDialogElement, }; } open() { this.elements.dialog.showModal(); } connectedCallback() { if (!this.isInitialized) { this.initialize(); this.isInitialized = true; } this.elements.flightplanSelect.addEventListener("change", this.handleChange); } disconnectedCallback() { this.elements.flightplanSelect.removeEventListener("change", this.handleChange); } handleChange = async () => { const response = await sendToMain>( "flightplan:import-flightplan-index", this.state, ); dispatchNotificationEvent(document.body, response.message, response.type); if (response.type === "success") { dispatchNotificationEvent( document.body, "Please remember to set the initial starting position of your aircraft in the simulator.", "info", ); } this.elements.dialog.close(); }; static registerElement() { registerElement("aerofly-flightplan-chooser", FlightPlanChooserWebComponent); } }