7
7
import { updateOptionsArr , updateOptionsMap } from "./utils/selectUtils" ;
8
8
import { objectDeepCopy } from "./utils/deepCopy" ;
9
9
import { config , sendAuthenticatedMessage } from "./crypto" ;
10
+ import { ObjectMap } from "../extension/service" ;
10
11
11
12
const editorDefaultText = "<---- Select a service instance to start editing it in here" ;
12
13
const editorCreateText = "<---- Create a new service instance on the left and then you can edit it in here" ;
@@ -23,10 +24,12 @@ document.addEventListener("DOMContentLoaded", () => {
23
24
// Inputs
24
25
const selectInstance = document . getElementById ( "selectInstance" ) as HTMLSelectElement ;
25
26
const selectService = document . getElementById ( "selectService" ) as HTMLSelectElement ;
27
+ const selectPreset = document . getElementById ( "selectPreset" ) as HTMLSelectElement ;
26
28
const inputInstanceName = document . getElementById ( "inputInstanceName" ) as HTMLInputElement ;
27
29
28
30
// Website areas
29
31
const instanceServiceSelector = document . getElementById ( "instanceServiceSelector" ) ;
32
+ const instancePreset = document . getElementById ( "instancePreset" ) ;
30
33
const instanceNameField = document . getElementById ( "instanceNameField" ) ;
31
34
const instanceEditButtons = document . getElementById ( "instanceEditButtons" ) ;
32
35
const instanceCreateButton = document . getElementById ( "instanceCreateButton" ) ;
@@ -62,33 +65,59 @@ export function onInstanceSelectChange(value: string): void {
62
65
showNotice ( undefined ) ;
63
66
switch ( value ) {
64
67
case "new" :
65
- showInMonaco ( "text" , true , editorCreateText ) ;
66
- setCreateInputs ( true , false , true ) ;
68
+ showInMonaco ( true , editorCreateText ) ;
69
+ setCreateInputs ( true , false , true , false ) ;
67
70
inputInstanceName . value = "" ;
68
71
break ;
69
72
case "select" :
70
- showInMonaco ( "text" , true , editorDefaultText ) ;
71
- setCreateInputs ( false , false , true ) ;
73
+ showInMonaco ( true , editorDefaultText ) ;
74
+ setCreateInputs ( false , false , true , false ) ;
72
75
break ;
73
76
default :
74
77
showConfig ( value ) ;
75
78
}
76
79
}
77
80
78
- function showConfig ( value : string ) {
79
- const inst = config . data ?. instances [ value ] ;
81
+ function showConfig ( instName : string ) {
82
+ const inst = config . data ?. instances [ instName ] ;
80
83
const service = config . data ?. services . find ( ( svc ) => svc . serviceType === inst ?. serviceType ) ;
81
84
82
85
if ( ! service ) {
83
- showInMonaco ( "text" , true , editorInvalidServiceText ) ;
86
+ showInMonaco ( true , editorInvalidServiceText ) ;
84
87
} else if ( service . requiresNoConfig ) {
85
- showInMonaco ( "text" , true , editorNotConfigurableText ) ;
88
+ showInMonaco ( true , editorNotConfigurableText ) ;
86
89
} else {
87
- const jsonString = JSON . stringify ( inst ?. config || { } , null , 4 ) ;
88
- showInMonaco ( "json" , false , jsonString , service ?. schema ) ;
90
+ showInMonaco ( false , inst ?. config ?? { } , service ?. schema ) ;
89
91
}
90
92
91
- setCreateInputs ( false , true , ! ( service ?. requiresNoConfig ?? false ) ) ;
93
+ setCreateInputs ( false , true , ! ( service ?. requiresNoConfig ?? false ) , service ?. presets !== undefined ) ;
94
+
95
+ if ( service ?. presets ) {
96
+ renderPresets ( service . presets ) ;
97
+ }
98
+ }
99
+
100
+ // Preset drop-down
101
+ export function selectInstanceConfigPreset ( ) : void {
102
+ const selectedPresetName = selectPreset . options [ selectPreset . selectedIndex ] ?. value ;
103
+ if ( ! selectedPresetName ) {
104
+ return ;
105
+ }
106
+
107
+ const instName = selectInstance . options [ selectInstance . selectedIndex ] ?. value ;
108
+ if ( ! instName ) {
109
+ return ;
110
+ }
111
+
112
+ const instance = config . data ?. instances [ instName ] ;
113
+ if ( ! instance ) {
114
+ return ;
115
+ }
116
+
117
+ const service = config . data ?. services . find ( ( svc ) => svc . serviceType === instance . serviceType ) ;
118
+ const presetValue = service ?. presets ?. [ selectedPresetName ] ?? { } ;
119
+
120
+ showInMonaco ( false , presetValue , service ?. schema ) ;
92
121
}
93
122
94
123
// Save button
@@ -191,6 +220,17 @@ function renderInstances() {
191
220
selectServiceInstance ( previousSelected ) ;
192
221
}
193
222
223
+ function renderPresets ( presets : ObjectMap < unknown > ) {
224
+ updateOptionsMap ( selectPreset , presets ) ;
225
+
226
+ // Add "Select..." element that hints the user that he can use this select box
227
+ // to choose a preset
228
+ const selectHintOption = document . createElement ( "option" ) ;
229
+ selectHintOption . innerText = "Select..." ;
230
+ selectPreset . prepend ( selectHintOption ) ;
231
+ selectPreset . selectedIndex = 0 ; // Select newly added hint
232
+ }
233
+
194
234
// Util functions
195
235
196
236
function selectServiceInstance ( instanceName : string ) {
@@ -208,7 +248,12 @@ function selectServiceInstance(instanceName: string) {
208
248
}
209
249
210
250
// Hides/unhides parts of the website based on the passed parameters
211
- function setCreateInputs ( createMode : boolean , instanceSelected : boolean , showSave : boolean ) {
251
+ function setCreateInputs (
252
+ createMode : boolean ,
253
+ instanceSelected : boolean ,
254
+ showSave : boolean ,
255
+ serviceHasPresets : boolean ,
256
+ ) {
212
257
function setVisible ( node : HTMLElement | null , visible : boolean ) {
213
258
if ( visible && node ?. classList . contains ( "hidden" ) ) {
214
259
node ?. classList . remove ( "hidden" ) ;
@@ -218,6 +263,7 @@ function setCreateInputs(createMode: boolean, instanceSelected: boolean, showSav
218
263
}
219
264
220
265
setVisible ( instanceEditButtons , ! createMode && instanceSelected ) ;
266
+ setVisible ( instancePreset , ! createMode && instanceSelected && serviceHasPresets ) ;
221
267
setVisible ( instanceCreateButton , createMode ) ;
222
268
setVisible ( instanceNameField , createMode ) ;
223
269
setVisible ( instanceServiceSelector , createMode ) ;
@@ -231,12 +277,14 @@ export function showNotice(msg: string | undefined): void {
231
277
}
232
278
233
279
function showInMonaco (
234
- type : "text" | "json" ,
235
280
readOnly : boolean ,
236
- content : string ,
281
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
282
+ content : any ,
237
283
schema ?: Record < string , unknown > ,
238
284
) : void {
239
285
editor ?. updateOptions ( { readOnly } ) ;
286
+ const type = typeof content === "object" ? "json" : "text" ;
287
+ const contentStr = typeof content === "object" ? JSON . stringify ( content , null , 4 ) : content ;
240
288
241
289
// JSON Schema stuff
242
290
// Get rid of old models, as they have to be unique and we may add the same again
@@ -263,5 +311,5 @@ function showInMonaco(
263
311
} ,
264
312
) ;
265
313
266
- editor ?. setModel ( monaco . editor . createModel ( content , type , schema ? modelUri : undefined ) ) ;
314
+ editor ?. setModel ( monaco . editor . createModel ( contentStr , type , schema ? modelUri : undefined ) ) ;
267
315
}
0 commit comments