|
1 | | -import { ComponentProps, DisplayComponent, FSComponent, VNode } from "@microsoft/msfs-sdk" |
| 1 | +import { |
| 2 | + ComponentProps, |
| 3 | + DisplayComponent, |
| 4 | + FSComponent, |
| 5 | + MappedSubject, |
| 6 | + ObjectSubject, |
| 7 | + Subject, |
| 8 | + VNode, |
| 9 | +} from "@microsoft/msfs-sdk" |
| 10 | +import { NavigraphNavigationDataInterface } from "@navigraph/msfs-navigation-data-interface" |
| 11 | +import { InterfaceNavbarItemV2, InterfaceSwitch } from "../../Utils" |
2 | 12 |
|
3 | | -interface TestPageProps extends ComponentProps {} |
| 13 | +interface TestPageProps extends ComponentProps { |
| 14 | + interface: NavigraphNavigationDataInterface |
| 15 | +} |
| 16 | + |
| 17 | +interface FunctionDescriptor { |
| 18 | + index: number |
| 19 | + arguments: string[] |
| 20 | + name: string |
| 21 | + functionCallback: (input?: string, inputAlt?: string) => unknown |
| 22 | +} |
| 23 | + |
| 24 | +interface InputState { |
| 25 | + active: boolean |
| 26 | + type: InputStateType |
| 27 | +} |
| 28 | + |
| 29 | +enum InputStateType { |
| 30 | + String, |
| 31 | + Bool, |
| 32 | +} |
4 | 33 |
|
5 | 34 | export class TestPage extends DisplayComponent<TestPageProps> { |
| 35 | + private readonly selectedFunction = Subject.create(0) |
| 36 | + private readonly input1State = ObjectSubject.create<InputState>({ |
| 37 | + active: false, |
| 38 | + type: InputStateType.String, |
| 39 | + }) |
| 40 | + private readonly input2State = ObjectSubject.create<InputState>({ |
| 41 | + active: false, |
| 42 | + type: InputStateType.String, |
| 43 | + }) |
| 44 | + |
| 45 | + private strToBool(input?: string): boolean { |
| 46 | + return input == "true" ? true : false |
| 47 | + } |
| 48 | + |
| 49 | + private readonly functionList: FunctionDescriptor[] = [ |
| 50 | + { |
| 51 | + index: 0, |
| 52 | + arguments: ["url: string"], |
| 53 | + name: "DownloadNavigationData", |
| 54 | + functionCallback: input => this.props.interface.download_navigation_data(input ?? ""), |
| 55 | + }, |
| 56 | + { |
| 57 | + index: 1, |
| 58 | + arguments: [], |
| 59 | + name: "GetActivePackage", |
| 60 | + functionCallback: () => this.props.interface.get_active_package(), |
| 61 | + }, |
| 62 | + { |
| 63 | + index: 2, |
| 64 | + arguments: ["sort: bool", "filter: bool"], |
| 65 | + name: "ListAvailablePackages", |
| 66 | + functionCallback: (input, inputAlt) => |
| 67 | + this.props.interface.list_available_packages(this.strToBool(input), this.strToBool(inputAlt)), |
| 68 | + }, |
| 69 | + { |
| 70 | + index: 3, |
| 71 | + arguments: ["uuid: string"], |
| 72 | + name: "SetActivePackage", |
| 73 | + functionCallback: input => this.props.interface.download_navigation_data(input ?? ""), |
| 74 | + }, |
| 75 | + ] |
| 76 | + |
| 77 | + private readonly _generateInputs = this.selectedFunction.map(selectedFunction => { |
| 78 | + const functionObj = this.functionList[selectedFunction] |
| 79 | + |
| 80 | + const functionArgCount = functionObj.arguments.length |
| 81 | + |
| 82 | + switch (functionArgCount) { |
| 83 | + case 1: { |
| 84 | + this.input1State.set("active", true) |
| 85 | + this.input2State.set("active", false) |
| 86 | + break |
| 87 | + } |
| 88 | + case 2: { |
| 89 | + this.input1State.set("active", true) |
| 90 | + this.input2State.set("active", true) |
| 91 | + break |
| 92 | + } |
| 93 | + default: { |
| 94 | + this.input1State.set("active", false) |
| 95 | + this.input2State.set("active", false) |
| 96 | + break |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + functionObj.arguments.forEach((value, index) => { |
| 101 | + const argumentType = value.includes("bool") ? InputStateType.Bool : InputStateType.String |
| 102 | + |
| 103 | + switch (index) { |
| 104 | + case 1: { |
| 105 | + this.input2State.set("type", argumentType) |
| 106 | + break |
| 107 | + } |
| 108 | + default: { |
| 109 | + this.input1State.set("type", argumentType) |
| 110 | + break |
| 111 | + } |
| 112 | + } |
| 113 | + }) |
| 114 | + }) |
| 115 | + |
6 | 116 | render(): VNode { |
7 | 117 | return ( |
8 | 118 | <div class="size-full flex flex-col"> |
9 | 119 | <p class="mb-8 text-4xl">Test</p> |
| 120 | + <div class="flex flex-row"> |
| 121 | + <div class="w-1/3 flex flex-col"> |
| 122 | + <div class="overflow-scroll flex-grow bg-ng-background-500"> |
| 123 | + {this.functionList.map(obj => ( |
| 124 | + <InterfaceNavbarItemV2 |
| 125 | + content={""} |
| 126 | + class="w-full p-2 flex flex-col items-start" |
| 127 | + activeClass="bg-blue-400" |
| 128 | + active={this.selectedFunction.map(index => index === obj.index)} |
| 129 | + setActive={() => this.selectedFunction.set(obj.index)} |
| 130 | + > |
| 131 | + <p class="text-xl">{obj.name}</p> |
| 132 | + <p class="text-lg">({obj.arguments.join(", ")})</p> |
| 133 | + </InterfaceNavbarItemV2> |
| 134 | + ))} |
| 135 | + </div> |
| 136 | + <div class="flex flex-row"> |
| 137 | + <InterfaceSwitch |
| 138 | + active={this.input1State.map(obj => { |
| 139 | + return obj.active ? (obj.type === InputStateType.String ? 0 : 1) : 2 |
| 140 | + })} |
| 141 | + pages={[ |
| 142 | + [0, <p class="text-xl">String</p>], |
| 143 | + [1, <p class="text-xl">Bool</p>], |
| 144 | + [2, <p class="text-xl">None</p>], |
| 145 | + ]} |
| 146 | + /> |
| 147 | + <InterfaceSwitch |
| 148 | + active={this.input2State.map(obj => { |
| 149 | + return obj.active ? (obj.type === InputStateType.String ? 0 : 1) : 2 |
| 150 | + })} |
| 151 | + pages={[ |
| 152 | + [0, <p class="text-xl">String</p>], |
| 153 | + [1, <p class="text-xl">Bool</p>], |
| 154 | + [2, <p class="text-xl">None</p>], |
| 155 | + ]} |
| 156 | + /> |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + <div class="w-2/3 bg-ng-background-700"></div> |
| 160 | + </div> |
10 | 161 | </div> |
11 | 162 | ) |
12 | 163 | } |
|
0 commit comments