Skip to content

Commit d63e4e3

Browse files
committed
Add config about participants position
1 parent 5afb451 commit d63e4e3

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

demo/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
matches: data.match,
2020
matchGames: data.match_game,
2121
participants: data.participant,
22+
}, {
23+
participantPositionPlacement: 'before',
24+
showParticipantPosition: true,
25+
showLowerBracketParticipantPosition: true,
2226
});
2327
})();
2428
</script>

src/main.ts

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,51 @@ import { Participant, Match, MatchResults, ParticipantResult, ViewerData } from
33
import { splitBy, getRanking, rankingHeader } from "./helpers";
44

55
type ConnectionType = 'square' | 'straight' | false;
6+
type Placement = 'before' | 'after';
67

78
interface Connection {
89
connectPrevious?: ConnectionType,
910
connectNext?: ConnectionType,
1011
}
1112

13+
interface Config {
14+
/**
15+
* Where the position of a participant is placed.
16+
* - If `before`, the position is prepended before the team name. "#1 Team"
17+
* - If `after`, the position is appended after the team name, in parentheses. "Team (#1)"
18+
*/
19+
participantPositionPlacement: Placement,
20+
21+
/**
22+
* Whether to show the position of a participant wherever possible.
23+
*/
24+
showParticipantPosition: boolean,
25+
26+
/**
27+
* Whether to show the position of a participant in the lower bracket of an elimination stage.
28+
*/
29+
showLowerBracketParticipantPosition: boolean,
30+
}
31+
1232
class BracketsViewer {
1333

1434
readonly teamRefsDOM: { [key: number]: HTMLElement[] } = {};
1535
private participants!: Participant[];
36+
private config!: Config;
1637

17-
public render(rootSelector: string, data: ViewerData) {
38+
public render(rootSelector: string, data: ViewerData, config?: Config) {
1839
const root = $(rootSelector);
1940

2041
if (root.length === 0) {
2142
throw Error('Root not found. You must have a root element with id "root"')
2243
}
2344

45+
this.config = {
46+
participantPositionPlacement: config && config.participantPositionPlacement || 'before',
47+
showParticipantPosition: config && config.showParticipantPosition || true,
48+
showLowerBracketParticipantPosition: config && config.showLowerBracketParticipantPosition || false,
49+
};
50+
2451
switch (data.stage.type) {
2552
case 'round_robin':
2653
this.renderRoundRobin(root, data);
@@ -162,7 +189,7 @@ class BracketsViewer {
162189
};
163190
}
164191

165-
roundDOM.append(this.renderMatch(match, connection, `M ${roundNumber}.${match.number}`));
192+
roundDOM.append(this.renderMatch(match, connection, `M ${roundNumber}.${match.number}`, lowerBracket));
166193
}
167194

168195
bracket.append(roundDOM);
@@ -189,9 +216,9 @@ class BracketsViewer {
189216
}
190217
}
191218

192-
private renderMatch(results: MatchResults, connection?: Connection, label?: string) {
193-
const team1 = this.renderTeam(results.opponent1);
194-
const team2 = this.renderTeam(results.opponent2);
219+
private renderMatch(results: MatchResults, connection?: Connection, label?: string, lowerBracket?: boolean) {
220+
const team1 = this.renderTeam(results.opponent1, lowerBracket || false);
221+
const team2 = this.renderTeam(results.opponent2, lowerBracket || false);
195222

196223
const teams = $('<div class="teams">');
197224
if (label) teams.append($('<span>').text(label));
@@ -215,7 +242,7 @@ class BracketsViewer {
215242
return match;
216243
}
217244

218-
private renderTeam(team: ParticipantResult | null) {
245+
private renderTeam(team: ParticipantResult | null, lowerBracket: boolean) {
219246
const teamDOM = $(`<div class="team">`);
220247
const nameDOM = $('<div class="name">');
221248
const resultDOM = $('<div class="result">');
@@ -228,8 +255,7 @@ class BracketsViewer {
228255
nameDOM.text(participant === undefined ? 'TBD' : participant.name);
229256
resultDOM.text(team.score === undefined ? '-' : team.score);
230257

231-
if (team.position !== undefined)
232-
nameDOM.append($('<span>').text(` (#${team.position})`));
258+
this.renderTeamPosition(nameDOM, team, lowerBracket);
233259

234260
if (team.result && team.result === 'win') {
235261
nameDOM.addClass('win');
@@ -263,6 +289,24 @@ class BracketsViewer {
263289

264290
return teamDOM;
265291
}
292+
293+
private renderTeamPosition(name: JQuery, team: ParticipantResult, lowerBracket: boolean) {
294+
if (team.position === undefined) return;
295+
if (!this.config.showParticipantPosition) return;
296+
if (!this.config.showLowerBracketParticipantPosition && lowerBracket) return;
297+
298+
// 'P' for position (where the participant comes from) and '#' for actual seeding.
299+
const text = lowerBracket ? `P${team.position}` : `#${team.position}`;
300+
301+
this.addPosition(name, text, this.config.participantPositionPlacement);
302+
}
303+
304+
private addPosition(name: JQuery, text: string, placement: Placement,) {
305+
if (placement === 'before')
306+
name.prepend($('<span>').text(`${text} `));
307+
else
308+
name.append($('<span>').text(` (${text})`));
309+
}
266310
}
267311

268312
(window as any).bracketsViewer = new BracketsViewer();

0 commit comments

Comments
 (0)