1
1
import './style.scss' ;
2
2
import { Participant , Match , MatchResults , ParticipantResult , StageType } from 'brackets-model' ;
3
- import { splitBy , getRanking } from './helpers' ;
3
+ import { splitBy , getRanking , getOriginAbbreviation } from './helpers' ;
4
4
import * as dom from './dom' ;
5
5
import * as lang from './lang' ;
6
- import { Config , Connection , FinalType , OriginHint , RankingItem , RoundName , ViewerData } from './types' ;
6
+ import { Config , Connection , FinalType , MatchLocation , OriginHint , RankingItem , RoundName , ViewerData } from './types' ;
7
7
8
8
export class BracketsViewer {
9
9
10
10
readonly teamRefsDOM : { [ participantId : number ] : HTMLElement [ ] } = { } ;
11
11
private participants ! : Participant [ ] ;
12
12
private config ! : Config ;
13
+ private skipFirstRound ! : boolean ;
13
14
14
15
/**
15
16
* Renders a stage (round-robin, single or double elimination).
@@ -29,6 +30,8 @@ export class BracketsViewer {
29
30
highlightParticipantOnHover : config && config . highlightParticipantOnHover !== undefined ? config . highlightParticipantOnHover : true ,
30
31
} ;
31
32
33
+ this . skipFirstRound = data . stage . settings . skipFirstRound || false ;
34
+
32
35
this . participants = data . participants ;
33
36
data . participants . forEach ( participant => this . teamRefsDOM [ participant . id ] = [ ] ) ;
34
37
@@ -141,6 +144,7 @@ export class BracketsViewer {
141
144
* @param connectFinal Whether to connect the last match of the bracket to the final.
142
145
*/
143
146
private renderBracket ( root : HTMLElement , matchesByRound : Match [ ] [ ] , roundName : RoundName , inLowerBracket ?: boolean , connectFinal ?: boolean ) : void {
147
+ const matchLocation = inLowerBracket ? 'lower-bracket' : 'upper-bracket' ;
144
148
const bracketContainer = dom . createBracketContainer ( ) ;
145
149
const roundCount = matchesByRound . length ;
146
150
@@ -150,7 +154,7 @@ export class BracketsViewer {
150
154
const roundContainer = dom . createRoundContainer ( roundName ( roundNumber , roundCount ) ) ;
151
155
152
156
for ( const match of matches )
153
- roundContainer . append ( this . createBracketMatch ( roundNumber , roundCount , match , inLowerBracket , connectFinal ) ) ;
157
+ roundContainer . append ( this . createBracketMatch ( roundNumber , roundCount , match , matchLocation , connectFinal ) ) ;
154
158
155
159
bracketContainer . append ( roundContainer ) ;
156
160
roundNumber ++ ;
@@ -233,14 +237,13 @@ export class BracketsViewer {
233
237
* @param roundNumber Number of the round.
234
238
* @param roundCount Count of rounds.
235
239
* @param match Information about the match.
236
- * @param inLowerBracket Whether the match is in lower bracket.
237
240
* @param connectFinal Whether to connect this match to the final if it happens to be the last one of the bracket.
238
241
*/
239
- private createBracketMatch ( roundNumber : number , roundCount : number , match : Match , inLowerBracket ?: boolean , connectFinal ?: boolean ) : HTMLElement {
240
- const connection = dom . getBracketConnection ( roundNumber , roundCount , inLowerBracket , connectFinal ) ;
241
- const matchLabel = lang . getMatchLabel ( match . number , roundNumber , roundCount , inLowerBracket ) ;
242
- const originHint = lang . getOriginHint ( roundNumber , roundCount , inLowerBracket ) ;
243
- return this . createMatch ( match , connection , matchLabel , originHint , inLowerBracket ) ;
242
+ private createBracketMatch ( roundNumber : number , roundCount : number , match : Match , matchLocation ?: MatchLocation , connectFinal ?: boolean ) : HTMLElement {
243
+ const connection = dom . getBracketConnection ( roundNumber , roundCount , matchLocation , connectFinal ) ;
244
+ const matchLabel = lang . getMatchLabel ( match . number , roundNumber , roundCount , matchLocation ) ;
245
+ const originHint = lang . getOriginHint ( roundNumber , roundCount , matchLocation ) ;
246
+ return this . createMatch ( match , connection , matchLabel , originHint , matchLocation , roundNumber ) ;
244
247
}
245
248
246
249
/**
@@ -256,7 +259,7 @@ export class BracketsViewer {
256
259
const connection = dom . getFinalConnection ( type , roundNumber , matches . length ) ;
257
260
const matchLabel = lang . getFinalMatchLabel ( type , roundNumber , roundCount ) ;
258
261
const originHint = lang . getFinalOriginHint ( type , roundNumber ) ;
259
- return this . createMatch ( matches [ roundIndex ] , connection , matchLabel , originHint ) ;
262
+ return this . createMatch ( matches [ roundIndex ] , connection , matchLabel , originHint , 'final-group' ) ;
260
263
}
261
264
262
265
/**
@@ -268,14 +271,14 @@ export class BracketsViewer {
268
271
* @param originHint Origin hint for the match.
269
272
* @param inLowerBracket Whether the match is in lower bracket.
270
273
*/
271
- private createMatch ( results : MatchResults , connection ?: Connection , label ?: string , originHint ?: OriginHint , inLowerBracket ?: boolean ) : HTMLElement {
272
- inLowerBracket = inLowerBracket || false ;
274
+ private createMatch ( results : MatchResults , connection ?: Connection , label ?: string , originHint ?: OriginHint , matchLocation ?: MatchLocation , roundNumber ?: number ) : HTMLElement {
275
+ matchLocation = matchLocation || 'upper-bracket' ;
273
276
274
277
const match = dom . createMatchContainer ( ) ;
275
278
const opponents = dom . createOpponentsContainer ( ) ;
276
279
277
- const team1 = this . createTeam ( results . opponent1 , originHint , inLowerBracket ) ;
278
- const team2 = this . createTeam ( results . opponent2 , originHint , inLowerBracket ) ;
280
+ const team1 = this . createTeam ( results . opponent1 , originHint , matchLocation , roundNumber ) ;
281
+ const team2 = this . createTeam ( results . opponent2 , originHint , matchLocation , roundNumber ) ;
279
282
280
283
if ( label )
281
284
opponents . append ( dom . createMatchLabel ( label , lang . getMatchStatus ( results . status ) ) ) ;
@@ -298,15 +301,15 @@ export class BracketsViewer {
298
301
* @param originHint Origin hint for the match.
299
302
* @param inLowerBracket Whether the match is in lower bracket.
300
303
*/
301
- private createTeam ( participant : ParticipantResult | null , originHint : OriginHint , inLowerBracket : boolean ) : HTMLElement {
304
+ private createTeam ( participant : ParticipantResult | null , originHint : OriginHint , matchLocation : MatchLocation , roundNumber ?: number ) : HTMLElement {
302
305
const participantContainer = dom . createParticipantContainer ( ) ;
303
306
const nameContainer = dom . createNameContainer ( ) ;
304
307
const resultContainer = dom . createResultContainer ( ) ;
305
308
306
309
if ( participant === null )
307
310
nameContainer . innerText = 'BYE' ;
308
311
else
309
- this . renderParticipant ( participantContainer , nameContainer , resultContainer , participant , originHint , inLowerBracket ) ;
312
+ this . renderParticipant ( participantContainer , nameContainer , resultContainer , participant , originHint , matchLocation , roundNumber ) ;
310
313
311
314
participantContainer . append ( nameContainer , resultContainer ) ;
312
315
@@ -316,6 +319,8 @@ export class BracketsViewer {
316
319
return participantContainer ;
317
320
}
318
321
322
+ // TODO: group containers into an object
323
+
319
324
/**
320
325
* Renders a participant.
321
326
*
@@ -326,14 +331,14 @@ export class BracketsViewer {
326
331
* @param originHint Origin hint for the match.
327
332
* @param inLowerBracket Whether the match is in lower bracket.
328
333
*/
329
- private renderParticipant ( participantContainer : HTMLElement , nameContainer : HTMLElement , resultContainer : HTMLElement , participant : ParticipantResult , originHint : OriginHint , inLowerBracket : boolean ) : void {
334
+ private renderParticipant ( participantContainer : HTMLElement , nameContainer : HTMLElement , resultContainer : HTMLElement , participant : ParticipantResult , originHint : OriginHint , matchLocation : MatchLocation , roundNumber ?: number ) : void {
330
335
const found = this . participants . find ( item => item . id === participant . id ) ;
331
336
332
337
if ( found ) {
333
338
nameContainer . innerText = found . name ;
334
- this . renderTeamOrigin ( nameContainer , participant , inLowerBracket ) ;
339
+ this . renderTeamOrigin ( nameContainer , participant , matchLocation , roundNumber ) ;
335
340
} else
336
- this . renderHint ( nameContainer , participant , originHint , inLowerBracket ) ;
341
+ this . renderHint ( nameContainer , participant , originHint , matchLocation ) ;
337
342
338
343
resultContainer . innerText = `${ participant . score || '-' } ` ;
339
344
@@ -349,10 +354,10 @@ export class BracketsViewer {
349
354
* @param originHint Origin hint for the participant.
350
355
* @param inLowerBracket Whether the match is in lower bracket.
351
356
*/
352
- private renderHint ( nameContainer : HTMLElement , participant : ParticipantResult , originHint : OriginHint , inLowerBracket : boolean ) : void {
357
+ private renderHint ( nameContainer : HTMLElement , participant : ParticipantResult , originHint : OriginHint , matchLocation : MatchLocation ) : void {
353
358
if ( originHint === undefined || participant . position === undefined ) return ;
354
359
if ( ! this . config . showSlotsOrigin ) return ;
355
- if ( ! this . config . showLowerBracketSlotsOrigin && inLowerBracket ) return ;
360
+ if ( ! this . config . showLowerBracketSlotsOrigin && matchLocation === 'lower-bracket' ) return ;
356
361
357
362
dom . setupHint ( nameContainer , originHint ( participant . position ) ) ;
358
363
}
@@ -364,13 +369,14 @@ export class BracketsViewer {
364
369
* @param participant The participant result.
365
370
* @param inLowerBracket Whether the match is in lower bracket.
366
371
*/
367
- private renderTeamOrigin ( nameContainer : HTMLElement , participant : ParticipantResult , inLowerBracket : boolean ) : void {
372
+ private renderTeamOrigin ( nameContainer : HTMLElement , participant : ParticipantResult , matchLocation : MatchLocation , roundNumber ?: number ) : void {
368
373
if ( participant . position === undefined ) return ;
369
374
if ( this . config . participantOriginPlacement === 'none' ) return ;
370
375
if ( ! this . config . showSlotsOrigin ) return ;
371
- if ( ! this . config . showLowerBracketSlotsOrigin && inLowerBracket ) return ;
376
+ if ( ! this . config . showLowerBracketSlotsOrigin && matchLocation === 'lower-bracket' ) return ;
372
377
373
- const origin = ( inLowerBracket ? lang . abbreviations . position : lang . abbreviations . seed ) + participant . position ;
378
+ const abbreviation = getOriginAbbreviation ( matchLocation , this . skipFirstRound , roundNumber ) ;
379
+ const origin = abbreviation + participant . position ;
374
380
dom . addTeamOrigin ( nameContainer , origin , this . config . participantOriginPlacement ) ;
375
381
}
376
382
0 commit comments