Skip to content

Commit b544f18

Browse files
committed
Add 'data-*' attributes to DOM
1 parent 02bd4d7 commit b544f18

File tree

4 files changed

+65
-44
lines changed

4 files changed

+65
-44
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@
4848
"webpack-cli": "^3.3.11"
4949
},
5050
"dependencies": {
51-
"brackets-model": "^1.3.1"
51+
"brackets-model": "^1.3.4"
5252
}
5353
}

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dom.ts

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,49 +25,59 @@ export function createRoundRobinContainer(): HTMLElement {
2525

2626
/**
2727
* Creates a container which contains a single bracket of a single or double elimination.
28+
*
29+
* @param id ID of the group.
2830
*/
29-
export function createBracketContainer(): HTMLElement {
31+
export function createBracketContainer(id: number): HTMLElement {
3032
const bracket = document.createElement('section');
3133
bracket.classList.add('bracket');
34+
bracket.setAttribute('data-group-id', id.toString());
3235
return bracket;
3336
}
3437

3538
/**
3639
* Creates a container which contains a group.
3740
*
41+
* @param id ID of the group.
3842
* @param title Title of the group.
3943
*/
40-
export function createGroupContainer(title: string): HTMLElement {
44+
export function createGroupContainer(id: number, title: string): HTMLElement {
4145
const h2 = document.createElement('h2');
4246
h2.innerText = title;
4347

44-
const groupDOM = document.createElement('section');
45-
groupDOM.classList.add('group');
46-
groupDOM.append(h2);
47-
return groupDOM;
48+
const group = document.createElement('section');
49+
group.classList.add('group');
50+
group.setAttribute('data-group-id', id.toString());
51+
group.append(h2);
52+
return group;
4853
}
4954

5055
/**
5156
* Creates a container which contains a round.
5257
*
58+
* @param id ID of the round.
5359
* @param title Title of the round.
5460
*/
55-
export function createRoundContainer(title: string): HTMLElement {
61+
export function createRoundContainer(id: number, title: string): HTMLElement {
5662
const h3 = document.createElement('h3');
5763
h3.innerText = title;
5864

59-
const roundDOM = document.createElement('article');
60-
roundDOM.classList.add('round');
61-
roundDOM.append(h3);
62-
return roundDOM;
65+
const round = document.createElement('article');
66+
round.classList.add('round');
67+
round.setAttribute('data-round-id', id.toString());
68+
round.append(h3);
69+
return round;
6370
}
6471

6572
/**
6673
* Creates a container which contains a match.
74+
*
75+
* @param id ID of the match.
6776
*/
68-
export function createMatchContainer(): HTMLElement {
77+
export function createMatchContainer(id: number): HTMLElement {
6978
const match = document.createElement('div');
7079
match.classList.add('match');
80+
match.setAttribute('data-match-id', id.toString());
7181
return match;
7282
}
7383

@@ -95,29 +105,35 @@ export function createOpponentsContainer(): HTMLElement {
95105

96106
/**
97107
* Creates a container which contains a participant.
108+
*
109+
* @param id ID of the participant.
98110
*/
99-
export function createParticipantContainer(): HTMLElement {
100-
const teamDOM = document.createElement('div');
101-
teamDOM.classList.add('participant');
102-
return teamDOM;
111+
export function createParticipantContainer(id: number | null): HTMLElement {
112+
const team = document.createElement('div');
113+
team.classList.add('participant');
114+
115+
if (id !== null)
116+
team.setAttribute('data-participant-id', id.toString());
117+
118+
return team;
103119
}
104120

105121
/**
106122
* Creates a container which contains the name of a participant.
107123
*/
108124
export function createNameContainer(): HTMLElement {
109-
const nameDOM = document.createElement('div');
110-
nameDOM.classList.add('name');
111-
return nameDOM;
125+
const name = document.createElement('div');
126+
name.classList.add('name');
127+
return name;
112128
}
113129

114130
/**
115131
* Creates a container which contains the result of a match for a participant.
116132
*/
117133
export function createResultContainer(): HTMLElement {
118-
const resultDOM = document.createElement('div');
119-
resultDOM.classList.add('result');
120-
return resultDOM;
134+
const result = document.createElement('div');
135+
result.classList.add('result');
136+
return result;
121137
}
122138

123139
/**

src/main.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,15 @@ export class BracketsViewer {
7272
let groupNumber = 1;
7373

7474
for (const groupMatches of matchesByGroup) {
75-
const groupContainer = dom.createGroupContainer(lang.getGroupName(groupNumber++));
75+
const groupId = groupMatches[0].group_id;
76+
const groupContainer = dom.createGroupContainer(groupId, lang.getGroupName(groupNumber++));
7677
const matchesByRound = splitBy(groupMatches, 'round_id');
7778

7879
let roundNumber = 1;
7980

8081
for (const roundMatches of matchesByRound) {
81-
const roundContainer = dom.createRoundContainer(lang.getRoundName(roundNumber++, 0));
82+
const roundId = roundMatches[0].round_id;
83+
const roundContainer = dom.createRoundContainer(roundId, lang.getRoundName(roundNumber++, 0));
8284

8385
for (const match of roundMatches)
8486
roundContainer.append(this.createMatch(match));
@@ -153,13 +155,15 @@ export class BracketsViewer {
153155
* @param connectFinal Whether to connect the last match of the bracket to the final.
154156
*/
155157
private renderBracket(root: HTMLElement, matchesByRound: Match[][], roundName: RoundName, bracketType: BracketType, connectFinal?: boolean): void {
156-
const bracketContainer = dom.createBracketContainer();
158+
const groupId = matchesByRound[0][0].group_id;
157159
const roundCount = matchesByRound.length;
160+
const bracketContainer = dom.createBracketContainer(groupId);
158161

159162
let roundNumber = 1;
160163

161164
for (const matches of matchesByRound) {
162-
const roundContainer = dom.createRoundContainer(roundName(roundNumber, roundCount));
165+
const roundId = matches[0].round_id;
166+
const roundContainer = dom.createRoundContainer(roundId, roundName(roundNumber, roundCount));
163167

164168
for (const match of matches)
165169
roundContainer.append(this.createBracketMatch(roundNumber, roundCount, match, bracketType, connectFinal));
@@ -187,8 +191,9 @@ export class BracketsViewer {
187191

188192
const roundCount = matches.length;
189193

190-
for (let roundNumber = 1; roundNumber <= finalMatches.length; roundNumber++) {
191-
const roundContainer = dom.createRoundContainer(lang.getFinalMatchLabel(finalType, roundNumber, roundCount));
194+
for (let roundIndex = 0; roundIndex < finalMatches.length; roundIndex++) {
195+
const roundNumber = roundIndex + 1;
196+
const roundContainer = dom.createRoundContainer(finalMatches[roundIndex].round_id, lang.getFinalMatchLabel(finalType, roundNumber, roundCount));
192197
roundContainer.append(this.createFinalMatch(finalType, finalMatches, roundNumber, roundCount));
193198
upperBracket.append(roundContainer);
194199
}
@@ -274,32 +279,32 @@ export class BracketsViewer {
274279
/**
275280
* Creates a match based on its results.
276281
*
277-
* @param results Results of the match.
282+
* @param match Results of the match.
278283
* @param matchLocation Location of the match.
279284
* @param connection Connection of this match with the others.
280285
* @param label Label of the match.
281286
* @param originHint Origin hint for the match.
282287
* @param roundNumber Number of the round.
283288
*/
284-
private createMatch(results: MatchResults, matchLocation?: BracketType, connection?: Connection, label?: string, originHint?: OriginHint, roundNumber?: number): HTMLElement {
285-
const match = dom.createMatchContainer();
289+
private createMatch(match: MatchResults, matchLocation?: BracketType, connection?: Connection, label?: string, originHint?: OriginHint, roundNumber?: number): HTMLElement {
290+
const matchContainer = dom.createMatchContainer(match.id);
286291
const opponents = dom.createOpponentsContainer();
287292

288-
const team1 = this.createTeam(results.opponent1, originHint, matchLocation, roundNumber);
289-
const team2 = this.createTeam(results.opponent2, originHint, matchLocation, roundNumber);
293+
const team1 = this.createTeam(match.opponent1, originHint, matchLocation, roundNumber);
294+
const team2 = this.createTeam(match.opponent2, originHint, matchLocation, roundNumber);
290295

291296
if (label)
292-
opponents.append(dom.createMatchLabel(label, lang.getMatchStatus(results.status)));
297+
opponents.append(dom.createMatchLabel(label, lang.getMatchStatus(match.status)));
293298

294299
opponents.append(team1, team2);
295-
match.append(opponents);
300+
matchContainer.append(opponents);
296301

297302
if (!connection)
298-
return match;
303+
return matchContainer;
299304

300-
dom.setupConnection(opponents, match, connection);
305+
dom.setupConnection(opponents, matchContainer, connection);
301306

302-
return match;
307+
return matchContainer;
303308
}
304309

305310
/**
@@ -312,7 +317,7 @@ export class BracketsViewer {
312317
*/
313318
private createTeam(participant: ParticipantResult | null, originHint: OriginHint, matchLocation?: BracketType, roundNumber?: number): HTMLElement {
314319
const containers: ParticipantContainers = {
315-
participant: dom.createParticipantContainer(),
320+
participant: dom.createParticipantContainer(participant && participant.id),
316321
name: dom.createNameContainer(),
317322
result: dom.createResultContainer(),
318323
};

0 commit comments

Comments
 (0)