@@ -74,12 +74,20 @@ export function stageFormCreator(configuration: FormConfiguration, submitCallabl
74
74
createMaskFields ( configuration , 'single_elimination' , parent , submitCallable ) ;
75
75
}
76
76
77
- export function updateFormCreator ( configuration : FormConfiguration , changeCallable : CallbackFunction ) {
77
+ /**
78
+ * Creates a DOM form to update the current stage.
79
+ *
80
+ * @param configuration HTML element IDs to render this form to
81
+ * @param changeCallable Callback function - what should happen onClick on the forms submit button?
82
+ */
83
+ export function updateFormCreator ( configuration : FormConfiguration , changeCallable : CallbackFunction ) : void {
78
84
const urlParams = new URLSearchParams ( window . location . search ) ;
79
85
const id = urlParams . get ( 'id' ) ?? 0 ;
80
86
87
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
81
88
const storedBrackets = JSON . parse ( localStorage . getItem ( 'brackets' ) ?? '' ) ;
82
- const currentBracket = storedBrackets [ id ] as Database
89
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
90
+ const currentBracket = storedBrackets [ id ] as Database ;
83
91
84
92
const parent = document . getElementById ( configuration . parent_id ) ;
85
93
@@ -96,15 +104,15 @@ export function updateFormCreator(configuration: FormConfiguration, changeCallab
96
104
stageSelector . onchange = ( ) : void => {
97
105
removeMaskFields ( parent ) ;
98
106
99
- const stage = ( ( < HTMLInputElement > stageSelector ) . value || 'single_elimination' ) as StageType
107
+ const stage = ( ( < HTMLInputElement > stageSelector ) . value || 'single_elimination' ) as StageType ;
100
108
createMaskFields ( configuration , stage , parent , changeCallable , 'Edit' ) ;
101
109
} ;
102
110
103
111
const teamCountInput = document . getElementById ( configuration . html_team_count_input_id ) ! ;
104
- teamCountInput . oninput = ( ) => {
105
- const stage = ( ( < HTMLInputElement > stageSelector ) . value || 'single_elimination' ) as StageType
106
- applyForm ( configuration , stage , changeCallable )
107
- }
112
+ teamCountInput . oninput = ( ) : void => {
113
+ const stage = ( ( < HTMLInputElement > stageSelector ) . value || 'single_elimination' ) as StageType ;
114
+ applyForm ( configuration , stage , changeCallable ) ;
115
+ } ;
108
116
109
117
createMaskFields ( configuration , currentBracket . stage [ 0 ] . type , parent , changeCallable , 'Edit' ) ;
110
118
}
@@ -117,20 +125,24 @@ export function updateFormCreator(configuration: FormConfiguration, changeCallab
117
125
function removeMaskFields ( parent : HTMLElement ) : void {
118
126
[ ...parent . children ]
119
127
. filter ( c => ! c . classList . contains ( 'base-input' ) )
120
- . forEach ( c => c . remove ( ) )
128
+ . forEach ( c => c . remove ( ) ) ;
121
129
}
122
130
123
131
124
132
/**
125
133
* @param parent HTMLElement
126
134
* @param configuration FormConfiguration
135
+ * @param setDefaultValues Whether to set default values for the form
136
+ * @param showTeamNamesInput Whether to show the input for team names
127
137
*/
128
138
function createBaseMask ( parent : HTMLElement , configuration : FormConfiguration , setDefaultValues = false , showTeamNamesInput = true ) : void {
129
139
const urlParams = new URLSearchParams ( window . location . search ) ;
130
140
const id = urlParams . get ( 'id' ) ?? 0 ;
131
141
142
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
132
143
const storedBrackets = JSON . parse ( localStorage . getItem ( 'brackets' ) ?? '' ) ;
133
- const currentBracket = storedBrackets [ id ] as Database
144
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
145
+ const currentBracket = storedBrackets [ id ] as Database ;
134
146
135
147
// Name
136
148
createInput (
@@ -142,7 +154,7 @@ function createBaseMask(parent: HTMLElement, configuration: FormConfiguration, s
142
154
setDefaultValues ? currentBracket . stage [ 0 ] . name : undefined ,
143
155
undefined ,
144
156
1 ,
145
- 'base-input'
157
+ 'base-input' ,
146
158
) ;
147
159
148
160
// Team names
@@ -153,7 +165,7 @@ function createBaseMask(parent: HTMLElement, configuration: FormConfiguration, s
153
165
t ( 'form-creator.team-label' ) ,
154
166
t ( 'form-creator.team-label-placeholder' ) ,
155
167
undefined ,
156
- 'base-input'
168
+ 'base-input' ,
157
169
) ;
158
170
}
159
171
@@ -167,7 +179,7 @@ function createBaseMask(parent: HTMLElement, configuration: FormConfiguration, s
167
179
setDefaultValues ? currentBracket . participant . length . toString ( ) : '' ,
168
180
'1' ,
169
181
undefined ,
170
- 'base-input'
182
+ 'base-input' ,
171
183
) ;
172
184
173
185
// Stage selector
@@ -177,7 +189,7 @@ function createBaseMask(parent: HTMLElement, configuration: FormConfiguration, s
177
189
t ( 'form-creator.stage-selector-label' ) ,
178
190
stageTypes ,
179
191
setDefaultValues ? currentBracket . stage [ 0 ] . type : undefined ,
180
- 'base-input'
192
+ 'base-input' ,
181
193
) ;
182
194
}
183
195
@@ -188,6 +200,7 @@ function createBaseMask(parent: HTMLElement, configuration: FormConfiguration, s
188
200
* @param stage Which type of stage are we building?
189
201
* @param parent The parent DOM
190
202
* @param submitCallback the callable to call when the data got created
203
+ * @param submitBtnText Text of the submit button
191
204
*/
192
205
function createMaskFields ( config : FormConfiguration , stage : StageType , parent : HTMLElement , submitCallback : CallbackFunction , submitBtnText ?: string ) : void {
193
206
switch ( stage ) {
@@ -236,7 +249,7 @@ function createMaskFields(config: FormConfiguration, stage: StageType, parent: H
236
249
config . html_double_elimination_seed_textarea_id ,
237
250
t ( 'form-creator.seed-order-label' ) ,
238
251
t ( 'form-creator.double-elimination-seed-order-placeholder' ) ,
239
- 'natural'
252
+ 'natural' ,
240
253
) ;
241
254
242
255
break ;
@@ -253,15 +266,22 @@ function createMaskFields(config: FormConfiguration, stage: StageType, parent: H
253
266
submitBtnWrapper . appendChild ( submitBtn ) ;
254
267
255
268
submitBtn . onclick = ( ) : void => {
256
- applyForm ( config , stage , submitCallback )
269
+ applyForm ( config , stage , submitCallback ) ;
257
270
} ;
258
271
259
272
parent . appendChild ( submitBtnWrapper ) ;
260
273
}
261
274
262
- function applyForm ( config : FormConfiguration , stage : StageType , submitCallback : CallbackFunction ) {
275
+ /**
276
+ * Apply a form when click on submit
277
+ *
278
+ * @param config HTML element IDs to render this form to
279
+ * @param stage Which type of stage are we building?
280
+ * @param submitCallback the callable to call when the data got created
281
+ */
282
+ function applyForm ( config : FormConfiguration , stage : StageType , submitCallback : CallbackFunction ) : void {
263
283
let payload : InputStage ;
264
- let teamNamesValue : string
284
+ let teamNamesValue : string ;
265
285
266
286
switch ( stage ) {
267
287
case 'round_robin' :
@@ -287,14 +307,14 @@ function applyForm(config: FormConfiguration, stage: StageType, submitCallback:
287
307
type : stage ,
288
308
} ;
289
309
290
- teamNamesValue = ( < HTMLTextAreaElement > document . getElementById ( config . html_team_names_input_id ) ) ?. value
310
+ teamNamesValue = ( < HTMLTextAreaElement > document . getElementById ( config . html_team_names_input_id ) ) ?. value ;
291
311
292
- if ( teamNamesValue ) {
293
- payload . seeding = teamNamesValue . split ( ',' )
294
- } else {
295
- const teamCount = parseInt ( ( < HTMLTextAreaElement > document . getElementById ( config . html_team_count_input_id ) ) . value )
296
- payload . seeding = Array . from ( { length : teamCount } ) . map ( ( _ , i ) => `Team ${ i + 1 } ` )
297
- roundRobinSettings . size = helpers . getNearestPowerOfTwo ( teamCount )
312
+ if ( teamNamesValue )
313
+ payload . seeding = teamNamesValue . split ( ',' ) ;
314
+ else {
315
+ const teamCount = parseInt ( ( < HTMLTextAreaElement > document . getElementById ( config . html_team_count_input_id ) ) . value ) ;
316
+ payload . seeding = Array . from ( { length : teamCount } ) . map ( ( _ , i ) => `Team ${ i + 1 } ` ) ;
317
+ roundRobinSettings . size = helpers . getNearestPowerOfTwo ( teamCount ) ;
298
318
}
299
319
300
320
break ;
@@ -316,14 +336,14 @@ function applyForm(config: FormConfiguration, stage: StageType, submitCallback:
316
336
type : stage ,
317
337
} ;
318
338
319
- teamNamesValue = ( < HTMLTextAreaElement > document . getElementById ( config . html_team_names_input_id ) ) ?. value
339
+ teamNamesValue = ( < HTMLTextAreaElement > document . getElementById ( config . html_team_names_input_id ) ) ?. value ;
320
340
321
- if ( teamNamesValue ) {
322
- payload . seeding = teamNamesValue . split ( ',' )
323
- } else {
324
- const teamCount = parseInt ( ( < HTMLTextAreaElement > document . getElementById ( config . html_team_count_input_id ) ) . value )
325
- payload . seeding = Array . from ( { length : teamCount } ) . map ( ( _ , i ) => `Team ${ i + 1 } ` )
326
- singleEliminationSettings . size = helpers . getNearestPowerOfTwo ( teamCount )
341
+ if ( teamNamesValue )
342
+ payload . seeding = teamNamesValue . split ( ',' ) ;
343
+ else {
344
+ const teamCount = parseInt ( ( < HTMLTextAreaElement > document . getElementById ( config . html_team_count_input_id ) ) . value ) ;
345
+ payload . seeding = Array . from ( { length : teamCount } ) . map ( ( _ , i ) => `Team ${ i + 1 } ` ) ;
346
+ singleEliminationSettings . size = helpers . getNearestPowerOfTwo ( teamCount ) ;
327
347
}
328
348
329
349
break ;
@@ -348,14 +368,14 @@ function applyForm(config: FormConfiguration, stage: StageType, submitCallback:
348
368
type : stage ,
349
369
} ;
350
370
351
- teamNamesValue = ( < HTMLTextAreaElement > document . getElementById ( config . html_team_names_input_id ) ) ?. value
371
+ teamNamesValue = ( < HTMLTextAreaElement > document . getElementById ( config . html_team_names_input_id ) ) ?. value ;
352
372
353
- if ( teamNamesValue ) {
354
- payload . seeding = teamNamesValue . split ( ',' )
355
- } else {
356
- const teamCount = parseInt ( ( < HTMLTextAreaElement > document . getElementById ( config . html_team_count_input_id ) ) . value )
357
- payload . seeding = Array . from ( { length : teamCount } ) . map ( ( _ , i ) => `Team ${ i + 1 } ` )
358
- doubleEliminationSettings . size = helpers . getNearestPowerOfTwo ( teamCount )
373
+ if ( teamNamesValue )
374
+ payload . seeding = teamNamesValue . split ( ',' ) ;
375
+ else {
376
+ const teamCount = parseInt ( ( < HTMLTextAreaElement > document . getElementById ( config . html_team_count_input_id ) ) . value ) ;
377
+ payload . seeding = Array . from ( { length : teamCount } ) . map ( ( _ , i ) => `Team ${ i + 1 } ` ) ;
378
+ doubleEliminationSettings . size = helpers . getNearestPowerOfTwo ( teamCount ) ;
359
379
}
360
380
361
381
break ;
@@ -433,9 +453,9 @@ function baseValidation(config: FormConfiguration): void {
433
453
if ( ! name || name === '' )
434
454
throw new DOMException ( 'No name provided.' ) ;
435
455
436
- const teamNamesValue = ( < HTMLInputElement > document . getElementById ( config . html_team_names_input_id ) ) ?. value
456
+ const teamNamesValue = ( < HTMLInputElement > document . getElementById ( config . html_team_names_input_id ) ) ?. value ;
437
457
const teams = teamNamesValue ? teamNamesValue . split ( ',' ) : [ ] ;
438
- const teamCount = teams . length || parseInt ( ( < HTMLInputElement > document . getElementById ( config . html_team_count_input_id ) ) . value )
458
+ const teamCount = teams . length || parseInt ( ( < HTMLInputElement > document . getElementById ( config . html_team_count_input_id ) ) . value ) ;
439
459
440
460
if ( teamCount < 2 )
441
461
throw new DOMException ( 'Invalid team amount provided.' ) ;
@@ -451,6 +471,7 @@ function baseValidation(config: FormConfiguration): void {
451
471
* @param labelText the text for the label of the textarea
452
472
* @param textareaPlaceholder the placeholder for the textarea
453
473
* @param textareaDefaultValue the default value for the textarea - if NULL or UNDEFINED this is not set
474
+ * @param className Class name to give to the wrapper
454
475
*/
455
476
function createTextarea ( parent : HTMLElement , textareaId : string , labelText : string , textareaPlaceholder : string , textareaDefaultValue ?: string , className ?: string ) : void {
456
477
const wrapper = document . createElement ( 'div' ) ;
@@ -466,9 +487,8 @@ function createTextarea(parent: HTMLElement, textareaId: string, labelText: stri
466
487
if ( null !== textareaDefaultValue && undefined !== textareaDefaultValue )
467
488
textarea . value = textareaDefaultValue ;
468
489
469
- if ( className ) {
470
- wrapper . classList . add ( className )
471
- }
490
+ if ( className )
491
+ wrapper . classList . add ( className ) ;
472
492
473
493
wrapper . appendChild ( label ) ;
474
494
wrapper . appendChild ( textarea ) ;
@@ -485,6 +505,7 @@ function createTextarea(parent: HTMLElement, textareaId: string, labelText: stri
485
505
* @param inputDefaultValue the default value for the input - if NULL or UNDEFINED this is not set
486
506
* @param inputMinValue the min value for the input - if NULL or UNDEFINED this is not set
487
507
* @param inputMinLengthValue the minLength value for the input - if NULL or UNDEFINED this is not set
508
+ * @param className Class name to give to the wrapper
488
509
*/
489
510
function createInput ( parent : HTMLElement , inputType : string , inputId : string , labelText : string , inputPlaceholder ?: string , inputDefaultValue ?: string , inputMinValue ?: string , inputMinLengthValue ?: number , className ?: string ) : void {
490
511
const wrapper = document . createElement ( 'div' ) ;
@@ -509,9 +530,8 @@ function createInput(parent: HTMLElement, inputType: string, inputId: string, la
509
530
if ( null !== inputMinLengthValue && undefined !== inputMinLengthValue )
510
531
input . minLength = inputMinLengthValue ;
511
532
512
- if ( className ) {
513
- wrapper . classList . add ( className )
514
- }
533
+ if ( className )
534
+ wrapper . classList . add ( className ) ;
515
535
516
536
wrapper . appendChild ( label ) ;
517
537
wrapper . appendChild ( input ) ;
@@ -524,6 +544,8 @@ function createInput(parent: HTMLElement, inputType: string, inputId: string, la
524
544
* @param selectId The ID of the generated select
525
545
* @param labelText The text of the label for the generated select
526
546
* @param options The options to display in select
547
+ * @param selectedOption Which option should be selected by default
548
+ * @param className Class name to give to the wrapper
527
549
*/
528
550
function createSelect ( parent : HTMLElement , selectId : string , labelText : string , options : string [ ] , selectedOption ?: string , className ?: string ) : void {
529
551
const wrapper = document . createElement ( 'div' ) ;
@@ -537,9 +559,8 @@ function createSelect(parent: HTMLElement, selectId: string, labelText: string,
537
559
538
560
createOptions ( select , options , selectedOption ) ;
539
561
540
- if ( className ) {
541
- wrapper . classList . add ( className )
542
- }
562
+ if ( className )
563
+ wrapper . classList . add ( className ) ;
543
564
544
565
wrapper . appendChild ( label ) ;
545
566
wrapper . appendChild ( select ) ;
@@ -550,14 +571,15 @@ function createSelect(parent: HTMLElement, selectId: string, labelText: string,
550
571
/**
551
572
* @param optionSwitch HTMLElement to add the options to
552
573
* @param options string list of possible options
574
+ * @param selectedOption Which option should be selected by default
553
575
*/
554
576
function createOptions ( optionSwitch : HTMLElement , options : string [ ] , selectedOption ?: string ) : void {
555
577
options . forEach ( option => {
556
578
const optionElement = document . createElement ( 'option' ) ;
557
579
optionElement . innerText = option ;
558
- if ( option === selectedOption ) {
559
- optionElement . selected = true
560
- }
580
+ if ( option === selectedOption )
581
+ optionElement . selected = true ;
582
+
561
583
optionSwitch . appendChild ( optionElement ) ;
562
584
} ) ;
563
585
}
0 commit comments