Skip to content

Commit 169d1ce

Browse files
committed
Refactor main
1 parent 71fb813 commit 169d1ce

File tree

2 files changed

+351
-254
lines changed

2 files changed

+351
-254
lines changed

src/dom.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import { ParticipantResult } from "brackets-model";
2+
import { rankingHeader } from "./helpers";
3+
4+
export function createResultContainer() {
5+
const resultDOM = document.createElement('div');
6+
resultDOM.classList.add('result');
7+
return resultDOM;
8+
}
9+
10+
export function createNameContainer() {
11+
const nameDOM = document.createElement('div');
12+
nameDOM.classList.add('name');
13+
return nameDOM;
14+
}
15+
16+
export function createTeamContainer() {
17+
const teamDOM = document.createElement('div');
18+
teamDOM.classList.add('team');
19+
return teamDOM;
20+
}
21+
22+
export function createMatchContainer() {
23+
const match = document.createElement('div');
24+
match.classList.add('match');
25+
return match;
26+
}
27+
28+
export function createMatchLabel(label: string) {
29+
const span = document.createElement('span');
30+
span.innerText = label;
31+
return span;
32+
}
33+
34+
export function createTeamsContainer() {
35+
const teams = document.createElement('div');
36+
teams.classList.add('teams');
37+
return teams;
38+
}
39+
40+
export function createBracketContainer() {
41+
const bracket = document.createElement('section');
42+
bracket.classList.add('bracket');
43+
return bracket;
44+
}
45+
46+
export function createCell(text: string): HTMLTableDataCellElement;
47+
export function createCell(data: number): HTMLTableDataCellElement;
48+
49+
export function createCell(data: string | number) {
50+
const td = document.createElement('td');
51+
td.innerText = String(data);
52+
return td;
53+
}
54+
55+
export function createRow() {
56+
return document.createElement('tr');
57+
}
58+
59+
export function createHeaders(ranking: Ranking) {
60+
const headers = document.createElement('tr');
61+
const firstItem = ranking[0];
62+
63+
for (const prop in firstItem) {
64+
const header = rankingHeader(prop as any);
65+
const th = document.createElement('th');
66+
th.innerText = header.value;
67+
th.setAttribute('title', header.tooltip);
68+
headers.append(th);
69+
}
70+
71+
return headers;
72+
}
73+
74+
export function createTable() {
75+
return document.createElement('table');
76+
}
77+
78+
export function createTitle(title: string) {
79+
const h1 = document.createElement('h1');
80+
h1.innerText = title;
81+
return h1;
82+
}
83+
84+
export function createRoundContainer(title: string) {
85+
const h3 = document.createElement('h3');
86+
h3.innerText = title;
87+
88+
const roundDOM = document.createElement('article');
89+
roundDOM.classList.add('round');
90+
roundDOM.append(h3);
91+
return roundDOM;
92+
}
93+
94+
export function createRoundRobinContainer() {
95+
const container = document.createElement('div');
96+
container.classList.add('round-robin');
97+
return container;
98+
}
99+
100+
export function createGroupContainer(title: string) {
101+
const h2 = document.createElement('h2');
102+
h2.innerText = title;
103+
104+
const groupDOM = document.createElement('section');
105+
groupDOM.classList.add('group');
106+
groupDOM.append(h2);
107+
return groupDOM;
108+
}
109+
110+
export function setupHint(nameContainer: HTMLElement, hint: string) {
111+
nameContainer.classList.add('hint');
112+
nameContainer.innerText = hint;
113+
}
114+
115+
export function setupWin(nameContainer: HTMLElement, resultContainer: HTMLElement, team: ParticipantResult) {
116+
if (team.result && team.result === 'win') {
117+
nameContainer.classList.add('win');
118+
resultContainer.classList.add('win');
119+
120+
if (team.score === undefined)
121+
resultContainer.innerText = 'W';
122+
}
123+
}
124+
125+
export function setupLoss(nameContainer: HTMLElement, resultContainer: HTMLElement, team: ParticipantResult) {
126+
if (team.result && team.result === 'loss' || team.forfeit) {
127+
nameContainer.classList.add('loss');
128+
resultContainer.classList.add('loss');
129+
130+
if (team.forfeit)
131+
resultContainer.innerText = 'F'; // Forfeit.
132+
else if (team.score === undefined)
133+
resultContainer.innerText = 'L';
134+
}
135+
}

0 commit comments

Comments
 (0)