-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathform.ts
588 lines (472 loc) · 22.6 KB
/
form.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
import { GrandFinalType, InputStage, RoundRobinMode, SeedOrdering, StageSettings, StageType } from 'brackets-model';
import { t } from './lang';
import { Database, helpers } from 'brackets-manager';
const stageTypes: StageType[] = ['single_elimination', 'double_elimination', 'round_robin'];
const roundRobinMode: RoundRobinMode[] = ['simple', 'double'];
const roundRobinSeeds: SeedOrdering[] = ['groups.effort_balanced', 'groups.seed_optimized', 'groups.bracket_optimized'];
const eliminationOrderings: SeedOrdering[] = ['natural', 'reverse', 'half_shift', 'reverse_half_shift', 'pair_flip', 'inner_outer'];
const grandFinalTypes: GrandFinalType[] = ['none', 'simple', 'double'];
export type CallbackFunction = (config: InputStage) => void;
export type FormConfiguration = {
parent_id: string,
html_name_id: string,
html_stage_type_selector_id: string
html_team_names_input_id: string
html_team_count_input_id: string
html_group_id: string
html_seed_order_id: string
html_round_robin_mode_id: string
html_consolation_final_checkbox_id: string
html_skip_first_round_checkbox_id: string
html_grand_final_type_id: string
html_double_elimination_seed_textarea_id: string
group_default_size: number
};
/**
* Creates a DOM form to create different stages for the brackets-manager
*
* @param configuration HTML element IDs to render this form to
* @param submitCallable Callback function - what should happen onClick on the forms submit button?
*/
export function stageFormCreator(configuration: FormConfiguration, submitCallable: CallbackFunction): void {
const parent = document.getElementById(configuration.parent_id);
if (null === parent)
throw new DOMException('parent with ID: ' + configuration.parent_id + ' was not found!');
createBaseMask(parent, configuration);
const stageSelector = document.getElementById(configuration.html_stage_type_selector_id);
if (null === stageSelector)
throw new DOMException('somehow we could not create a select!');
stageSelector.onchange = (): void => {
removeMaskFields(parent);
let stage: StageType;
switch ((<HTMLInputElement>stageSelector).value) {
case 'round_robin':
stage = 'round_robin';
break;
case 'double_elimination':
stage = 'double_elimination';
break;
case 'single_elimination':
stage = 'single_elimination';
break;
default:
throw new DOMException('stage ' + (<HTMLInputElement>stageSelector).value + ' seems to be not implemented yet.');
}
createMaskFields(configuration, stage, parent, submitCallable);
};
// We're creating a round_robin here by default cause its selected on first load.
createMaskFields(configuration, 'single_elimination', parent, submitCallable);
}
/**
* Creates a DOM form to update the current stage.
*
* @param configuration HTML element IDs to render this form to
* @param changeCallable Callback function - what should happen onClick on the forms submit button?
*/
export function updateFormCreator(configuration: FormConfiguration, changeCallable: CallbackFunction): void {
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id') ?? 0;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const storedBrackets = JSON.parse(localStorage.getItem('brackets') ?? '{}');
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const currentBracket = storedBrackets[id] as Database;
const parent = document.getElementById(configuration.parent_id);
if (null === parent)
throw new DOMException('parent with ID: ' + configuration.parent_id + ' was not found!');
createBaseMask(parent, configuration, true, false);
const stageSelector = document.getElementById(configuration.html_stage_type_selector_id);
if (null === stageSelector)
throw new DOMException('somehow we could not create a select!');
stageSelector.onchange = (): void => {
removeMaskFields(parent);
const stage = ((<HTMLInputElement>stageSelector).value || 'single_elimination') as StageType;
createMaskFields(configuration, stage, parent, changeCallable, 'Edit');
};
const teamCountInput = document.getElementById(configuration.html_team_count_input_id)!;
teamCountInput.oninput = (): void => {
const stage = ((<HTMLInputElement>stageSelector).value || 'single_elimination') as StageType;
applyForm(configuration, stage, changeCallable);
};
createMaskFields(configuration, currentBracket.stage[0].type, parent, changeCallable, 'Edit');
}
/**
* Removes all fields of the mask which are not the name and the stage selector.
*
* @param parent The HTML parent to hold the elements.
*/
function removeMaskFields(parent: HTMLElement): void {
[...parent.children]
.filter(c => !c.classList.contains('base-input'))
.forEach(c => c.remove());
}
/**
* @param parent HTMLElement
* @param configuration FormConfiguration
* @param setDefaultValues Whether to set default values for the form
* @param showTeamNamesInput Whether to show the input for team names
*/
function createBaseMask(parent: HTMLElement, configuration: FormConfiguration, setDefaultValues = false, showTeamNamesInput = true): void {
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id') ?? 0;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const storedBrackets = JSON.parse(localStorage.getItem('brackets') ?? '{}');
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const currentBracket = storedBrackets[id] as Database;
// Name
createInput(
parent,
'text',
configuration.html_name_id,
t('form-creator.stage-name-label'),
t('form-creator.stage-name-placeholder'),
setDefaultValues ? currentBracket.stage[0].name : undefined,
undefined,
1,
'base-input',
);
// Team names
if (showTeamNamesInput) {
createTextarea(
parent,
configuration.html_team_names_input_id,
t('form-creator.team-label'),
t('form-creator.team-label-placeholder'),
undefined,
'base-input',
);
}
// or Team count
createInput(
parent,
'number',
configuration.html_team_count_input_id,
showTeamNamesInput ? t('form-creator.team-count') : 'Team count',
t('form-creator.team-count-placeholder'),
setDefaultValues ? currentBracket.participant.length.toString() : '',
'1',
undefined,
'base-input',
);
// Stage selector
createSelect(
parent,
configuration.html_stage_type_selector_id,
t('form-creator.stage-selector-label'),
stageTypes,
setDefaultValues ? currentBracket.stage[0].type : undefined,
'base-input',
);
}
/**
* removing all fields of the mask which are not the name and the stage selector
*
* @param config HTML element IDs to render this form to
* @param stage Which type of stage are we building?
* @param parent The parent DOM
* @param submitCallback the callable to call when the data got created
* @param submitBtnText Text of the submit button
*/
function createMaskFields(config: FormConfiguration, stage: StageType, parent: HTMLElement, submitCallback: CallbackFunction, submitBtnText?: string): void {
switch (stage) {
case 'round_robin':
// Teams amount
createInput(
parent,
'number',
config.html_group_id,
t('form-creator.group-label'),
t('form-creator.group-placeholder'),
config.group_default_size.toString(),
'1',
);
// Seed ordering
createSelect(parent, config.html_seed_order_id, t('form-creator.seed-order-label'), roundRobinSeeds);
// Round robin mode
createSelect(parent, config.html_round_robin_mode_id, t('form-creator.round-robin-mode-label'), roundRobinMode);
break;
case 'single_elimination':
// Seed ordering
createSelect(parent, config.html_seed_order_id, t('form-creator.seed-order-label'), eliminationOrderings);
// Consolation Final
createInput(parent, 'checkbox', config.html_consolation_final_checkbox_id, t('form-creator.consolation-final-label'));
break;
case 'double_elimination':
// Consolation Final
createInput(parent, 'checkbox', config.html_consolation_final_checkbox_id, t('form-creator.consolation-final-label'));
// Skip first round
createInput(parent, 'checkbox', config.html_skip_first_round_checkbox_id, t('form-creator.skip-first-round-label'));
// Grand final types
createSelect(parent, config.html_grand_final_type_id, t('form-creator.grand-final-type-label'), grandFinalTypes);
// Seed orders
createTextarea(
parent,
config.html_double_elimination_seed_textarea_id,
t('form-creator.seed-order-label'),
t('form-creator.double-elimination-seed-order-placeholder'),
'natural',
);
break;
default:
throw new DOMException(`stage ${stage as string} seems to be not implemented yet.`);
}
const submitBtnWrapper = document.createElement('div');
const submitBtn = document.createElement('button');
submitBtn.innerText = submitBtnText || t('form-creator.submit');
submitBtn.type = 'submit';
submitBtnWrapper.appendChild(submitBtn);
submitBtn.onclick = (): void => {
applyForm(config, stage, submitCallback);
};
parent.appendChild(submitBtnWrapper);
}
/**
* Apply a form when click on submit
*
* @param config HTML element IDs to render this form to
* @param stage Which type of stage are we building?
* @param submitCallback the callable to call when the data got created
*/
function applyForm(config: FormConfiguration, stage: StageType, submitCallback: CallbackFunction): void {
let payload: InputStage;
let teamNamesValue: string;
switch (stage) {
case 'round_robin':
try {
validateRoundRobin(config);
} catch (e) {
alert((<DOMException>e).message);
return;
}
const roundRobinSettings: StageSettings = {
seedOrdering: [
(<SeedOrdering>(<HTMLInputElement>document.getElementById(config.html_seed_order_id)).value ?? 'groups.effort_balanced'),
],
roundRobinMode: (<RoundRobinMode>(<HTMLSelectElement>document.getElementById(config.html_round_robin_mode_id)).value ?? 'simple'),
groupCount: parseInt((<HTMLInputElement>document.getElementById(config.html_group_id)).value ?? '0'),
};
payload = {
name: (<HTMLInputElement>document.getElementById(config.html_name_id)).value ?? '',
settings: roundRobinSettings,
tournamentId: 0,
type: stage,
};
teamNamesValue = (<HTMLTextAreaElement>document.getElementById(config.html_team_names_input_id))?.value;
if (teamNamesValue)
payload.seeding = teamNamesValue.split(',');
else {
const teamCount = parseInt((<HTMLTextAreaElement>document.getElementById(config.html_team_count_input_id)).value);
payload.seeding = Array.from({ length: teamCount }).map((_, i) => `Team ${i + 1}`);
roundRobinSettings.size = helpers.getNearestPowerOfTwo(teamCount);
}
break;
case 'single_elimination':
validateSingleElimination(config);
const singleEliminationSettings: StageSettings = {
seedOrdering: [
(<SeedOrdering>(<HTMLInputElement>document.getElementById(config.html_seed_order_id)).value ?? 'natural'),
],
consolationFinal: (<HTMLInputElement>document.getElementById(config.html_consolation_final_checkbox_id)).checked,
};
payload = {
name: (<HTMLInputElement>document.getElementById(config.html_name_id)).value ?? '',
settings: singleEliminationSettings,
tournamentId: 0,
type: stage,
};
teamNamesValue = (<HTMLTextAreaElement>document.getElementById(config.html_team_names_input_id))?.value;
if (teamNamesValue)
payload.seeding = teamNamesValue.split(',');
else {
const teamCount = parseInt((<HTMLTextAreaElement>document.getElementById(config.html_team_count_input_id)).value);
payload.seeding = Array.from({ length: teamCount }).map((_, i) => `Team ${i + 1}`);
singleEliminationSettings.size = helpers.getNearestPowerOfTwo(teamCount);
}
break;
case 'double_elimination':
validateDoubleElimination(config);
const rawSeedOrder = (<HTMLTextAreaElement>document.getElementById(config.html_double_elimination_seed_textarea_id)).value.split(',');
const seedOrder = rawSeedOrder.map(order => order.trim() as SeedOrdering);
const doubleEliminationSettings: StageSettings = {
seedOrdering: seedOrder,
consolationFinal: (<HTMLInputElement>document.getElementById(config.html_consolation_final_checkbox_id)).checked,
skipFirstRound: (<HTMLInputElement>document.getElementById(config.html_skip_first_round_checkbox_id)).checked,
grandFinal: <GrandFinalType>(<HTMLSelectElement>document.getElementById(config.html_grand_final_type_id)).value,
};
payload = {
name: (<HTMLInputElement>document.getElementById(config.html_name_id)).value ?? '',
settings: doubleEliminationSettings,
tournamentId: 0,
type: stage,
};
teamNamesValue = (<HTMLTextAreaElement>document.getElementById(config.html_team_names_input_id))?.value;
if (teamNamesValue)
payload.seeding = teamNamesValue.split(',');
else {
const teamCount = parseInt((<HTMLTextAreaElement>document.getElementById(config.html_team_count_input_id)).value);
payload.seeding = Array.from({ length: teamCount }).map((_, i) => `Team ${i + 1}`);
doubleEliminationSettings.size = helpers.getNearestPowerOfTwo(teamCount);
}
break;
default:
throw new DOMException(`stage ${stage as string} seems to be not implemented yet.`);
}
submitCallback(payload);
}
/**
* validates everything for the double_elimination stage
*
* @param config FormConfig
*/
function validateDoubleElimination(config: FormConfiguration): void {
baseValidation(config);
const grandFinalType = (<HTMLSelectElement>document.getElementById(config.html_grand_final_type_id)).value as GrandFinalType;
if (!grandFinalType || !grandFinalTypes.includes(grandFinalType))
throw new DOMException('grand_final_type must be one of: ' + grandFinalTypes.toString());
const orderings = (<HTMLTextAreaElement>document.getElementById(config.html_double_elimination_seed_textarea_id)).value.split(',');
orderings.forEach(value => {
const ordering = value.trim() as SeedOrdering;
if (!eliminationOrderings.includes(ordering))
throw new DOMException('elimination seed_ordering wrong found: ' + ordering + 'must be one of: ' + eliminationOrderings.toString());
});
}
/**
* validates everything for a single_elimination stage
*
* @param config FormConfiguration
*/
function validateSingleElimination(config: FormConfiguration): void {
baseValidation(config);
const ordering = (<HTMLSelectElement>document.getElementById(config.html_seed_order_id)).value as SeedOrdering;
if (!ordering || !eliminationOrderings.includes(ordering))
throw new DOMException('seed_ordering must be one of: ' + eliminationOrderings.toString());
}
/**
* validates everything for a round_robin stage
*
* @param config FormConfiguration
*/
function validateRoundRobin(config: FormConfiguration): void {
baseValidation(config);
const groupAmount = parseInt((<HTMLInputElement>document.getElementById(config.html_group_id)).value);
if (groupAmount <= 0)
throw new DOMException('group_amount must be equal or bigger than 1');
const ordering = (<HTMLSelectElement>document.getElementById(config.html_seed_order_id)).value as SeedOrdering;
if (!roundRobinSeeds.includes(ordering))
throw new DOMException('seed_ordering must be one of ' + roundRobinSeeds.toString());
const roundRobinMode = (<HTMLSelectElement>document.getElementById(config.html_round_robin_mode_id)).value;
if (!roundRobinMode.includes(roundRobinMode))
throw new DOMException('round_robin_mode must be one of ' + roundRobinMode.toString());
}
/**
* validates the field every specification has (name, teams and stage)
*
* @param config FormConfiguration
*/
function baseValidation(config: FormConfiguration): void {
const name = (<HTMLInputElement>document.getElementById(config.html_name_id)).value;
if (!name || name === '')
throw new DOMException('No name provided.');
const teamNamesValue = (<HTMLInputElement>document.getElementById(config.html_team_names_input_id))?.value;
const teams = teamNamesValue ? teamNamesValue.split(',') : [];
const teamCount = teams.length || parseInt((<HTMLInputElement>document.getElementById(config.html_team_count_input_id)).value);
if (teamCount < 2)
throw new DOMException('Invalid team amount provided.');
const stageType = (<HTMLInputElement>document.getElementById(config.html_stage_type_selector_id)).value as StageType;
if (!stageType && stageTypes.includes(stageType))
throw new DOMException('Invalid stage.');
}
/**
* @param parent the parent DOM element to render to
* @param textareaId the id of the textarea
* @param labelText the text for the label of the textarea
* @param textareaPlaceholder the placeholder for the textarea
* @param textareaDefaultValue the default value for the textarea - if NULL or UNDEFINED this is not set
* @param className Class name to give to the wrapper
*/
function createTextarea(parent: HTMLElement, textareaId: string, labelText: string, textareaPlaceholder: string, textareaDefaultValue?: string, className?: string): void {
const wrapper = document.createElement('div');
const label = document.createElement('label');
label.setAttribute('for', textareaId);
label.innerText = labelText;
const textarea = document.createElement('textarea');
textarea.placeholder = textareaPlaceholder;
textarea.id = textareaId;
if (null !== textareaDefaultValue && undefined !== textareaDefaultValue)
textarea.value = textareaDefaultValue;
if (className)
wrapper.classList.add(className);
wrapper.appendChild(label);
wrapper.appendChild(textarea);
parent.appendChild(wrapper);
}
/**
* @param parent parent to render the DOM to
* @param inputType input type
* @param inputId the id for the input
* @param labelText the label text for the input
* @param inputPlaceholder the placeholder for the input - if NULL or UNDEFINED this is not set
* @param inputDefaultValue the default value for the input - if NULL or UNDEFINED this is not set
* @param inputMinValue the min value for the input - if NULL or UNDEFINED this is not set
* @param inputMinLengthValue the minLength value for the input - if NULL or UNDEFINED this is not set
* @param className Class name to give to the wrapper
*/
function createInput(parent: HTMLElement, inputType: string, inputId: string, labelText: string, inputPlaceholder?: string, inputDefaultValue?: string, inputMinValue?: string, inputMinLengthValue?: number, className?: string): void {
const wrapper = document.createElement('div');
const label = document.createElement('label');
label.setAttribute('for', inputId);
label.innerText = labelText;
const input = document.createElement('input');
input.type = inputType;
input.id = inputId;
if (null !== inputPlaceholder && undefined !== inputPlaceholder)
input.placeholder = inputPlaceholder;
if (null !== inputDefaultValue && undefined !== inputDefaultValue)
input.value = inputDefaultValue;
if (null !== inputMinValue && undefined !== inputMinValue)
input.min = inputMinValue;
if (null !== inputMinLengthValue && undefined !== inputMinLengthValue)
input.minLength = inputMinLengthValue;
if (className)
wrapper.classList.add(className);
wrapper.appendChild(label);
wrapper.appendChild(input);
parent.appendChild(wrapper);
}
/**
* @param parent The wrapper to render the created DOM Elements to
* @param selectId The ID of the generated select
* @param labelText The text of the label for the generated select
* @param options The options to display in select
* @param selectedOption Which option should be selected by default
* @param className Class name to give to the wrapper
*/
function createSelect(parent: HTMLElement, selectId: string, labelText: string, options: string[], selectedOption?: string, className?: string): void {
const wrapper = document.createElement('div');
const label = document.createElement('label');
label.setAttribute('for', selectId);
label.innerText = labelText;
const select = document.createElement('select');
select.id = selectId;
createOptions(select, options, selectedOption);
if (className)
wrapper.classList.add(className);
wrapper.appendChild(label);
wrapper.appendChild(select);
parent.appendChild(wrapper);
}
/**
* @param optionSwitch HTMLElement to add the options to
* @param options string list of possible options
* @param selectedOption Which option should be selected by default
*/
function createOptions(optionSwitch: HTMLElement, options: string[], selectedOption?: string): void {
options.forEach(option => {
const optionElement = document.createElement('option');
optionElement.innerText = option;
if (option === selectedOption)
optionElement.selected = true;
optionSwitch.appendChild(optionElement);
});
}
window.stageFormCreator = stageFormCreator;
window.updateFormCreator = updateFormCreator;