1+ import { ComponentProps , EventBus , DisplayComponent , VNode , FSComponent , Subject , ClockEvents , Subscription } from '@microsoft/msfs-sdk' ;
2+ import { DefaultNavDataBarFieldModelFactory , Fms , NavDataBarFieldModel , NavDataFieldGpsValidity , NavDataFieldType , UnitsUserSettings } from '@microsoft/msfs-garminsdk' ;
3+ import { Sr22tDestAirportInfoFieldRenderer } from './Sr22tDestAirportInfoFieldRenderer' ;
4+
5+ import './Sr22tDestAirportInfoDisplay.css' ;
6+
7+ /** The properties for the {@link Sr22tDestAirportInfoDisplay} component. */
8+ interface Sr22tDestAirportInfoDisplayProps extends ComponentProps {
9+ /** An instance of the event bus. */
10+ bus : EventBus ;
11+
12+ /** The FMS. */
13+ fms : Fms ;
14+
15+ /** The update frequency of the data fields, in hertz. */
16+ updateFreq : number ;
17+ }
18+
19+ /**
20+ * The Sr22tDestAirportInfoComponent.
21+ * Displays Destination Airport Information, including:
22+ * - Destination airport ident
23+ * - Enroute distance
24+ * - Estimated time enroute
25+ * - Bearing to airport
26+ * - Fuel remaining at airport
27+ */
28+ export class Sr22tDestAirportInfoDisplay extends DisplayComponent < Sr22tDestAirportInfoDisplayProps > {
29+ private readonly fieldTypes = [
30+ NavDataFieldType . Destination ,
31+ NavDataFieldType . TimeToDestination ,
32+ NavDataFieldType . FuelOverDestination ,
33+ NavDataFieldType . DistanceToDestination ,
34+ NavDataFieldType . BearingToWaypoint ,
35+ ] ;
36+
37+ private readonly unitsSettingManager = UnitsUserSettings . getManager ( this . props . bus ) ;
38+
39+ private readonly modelFactory = new DefaultNavDataBarFieldModelFactory (
40+ this . props . bus ,
41+ this . props . fms ,
42+ Subject . create ( NavDataFieldGpsValidity . Valid ) ,
43+ ) ;
44+
45+ private readonly fieldRenderer = new Sr22tDestAirportInfoFieldRenderer ( this . unitsSettingManager ) ;
46+
47+ private readonly fieldSlots : VNode [ ] = Array . from (
48+ { length : this . fieldTypes . length } ,
49+ ( ) => < div class = 'nav-data-bar-field-slot' />
50+ ) ;
51+ private readonly models : NavDataBarFieldModel < any > [ ] = [ ] ;
52+
53+ private clockSub ?: Subscription ;
54+
55+ /** @inheritdoc */
56+ public onAfterRender ( ) : void {
57+ for ( const [ index , type ] of this . fieldTypes . entries ( ) ) {
58+ const model = this . modelFactory . create ( type ) ;
59+ model . update ( ) ;
60+ const field = this . fieldRenderer . render ( type , model ) ;
61+
62+ FSComponent . render ( field , this . fieldSlots [ index ] . instance as HTMLDivElement ) ;
63+ this . models [ index ] = model ;
64+ }
65+
66+ this . clockSub = this . props . bus . getSubscriber < ClockEvents > ( ) . on ( 'realTime' ) . whenChangedBy ( 1000 / this . props . updateFreq ) . handle ( this . onUpdated . bind ( this ) ) ;
67+ }
68+
69+ /** Responds to update events. */
70+ private onUpdated ( ) : void {
71+ for ( let i = 0 ; i < this . fieldTypes . length ; i ++ ) {
72+ this . models [ i ] . update ( ) ;
73+ }
74+ }
75+
76+ /** @inheritdoc */
77+ public render ( ) : VNode {
78+ return (
79+ < div class = "sr22t-dest-airport-info-display" >
80+ { this . fieldSlots }
81+ </ div >
82+ ) ;
83+ }
84+
85+ /** @inheritdoc */
86+ public destroy ( ) : void {
87+ this . clockSub ?. destroy ( ) ;
88+
89+ for ( let i = 0 ; i < this . fieldTypes . length ; i ++ ) {
90+ this . models [ i ] ?. destroy ( ) ;
91+ }
92+ }
93+ }
0 commit comments