Skip to content

Commit

Permalink
Support displaying match games (#96)
Browse files Browse the repository at this point in the history
* Use Popover API to display games

* Add title and refactor

* Update exported types

* Allow a Firefox polyfill

* Update exported interfaces

* Fix any previous selections
  • Loading branch information
Drarig29 committed Jul 9, 2023
1 parent daa9043 commit dae5803
Show file tree
Hide file tree
Showing 13 changed files with 379 additions and 93 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = true

[*.yml]
[*.{yml,scss}]
indent_size = 2
2 changes: 1 addition & 1 deletion dist/brackets-viewer.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/brackets-viewer.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stage-form-creator.min.js

Large diffs are not rendered by default.

48 changes: 35 additions & 13 deletions src/dom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Match, ParticipantResult, FinalType, GroupType, Id } from 'brackets-model';
import { Match, ParticipantResult, FinalType, GroupType, Id, MatchGame } from 'brackets-model';
import { Connection, Placement, Ranking, RankingItem } from './types';
import { rankingHeader } from './helpers';
import { isMatchGame, rankingHeader } from './helpers';
import { t } from './lang';

/**
Expand All @@ -14,6 +14,17 @@ export function createTitle(title: string): HTMLElement {
return h1;
}

/**
* Creates the title of a popover.
*
* @param title The title to set.
*/
export function createPopoverTitle(title: string): HTMLElement {
const h4 = document.createElement('h4');
h4.innerText = title;
return h4;
}

/**
* Creates a container which contains a round-robin stage.
*
Expand Down Expand Up @@ -104,38 +115,49 @@ export function createRoundContainer(roundId: Id, title: string): HTMLElement {
/**
* Creates a container which contains a match.
*
* @param matchId ID of the match.
* @param status Status of the match.
* @param match A match or a match game.
*/
export function createMatchContainer(matchId?: Id, status?: number): HTMLElement {
const match = document.createElement('div');
match.classList.add('match');
matchId !== undefined && match.setAttribute('data-match-id', matchId.toString());
status !== undefined && match.setAttribute('data-match-status', status.toString());
return match;
export function createMatchContainer(match?: Match | MatchGame): HTMLElement {
const div = document.createElement('div');
div.classList.add('match');

if (match) {
if (isMatchGame(match))
div.setAttribute('data-match-game-id', match.id.toString());
else
div.setAttribute('data-match-id', match.id.toString());

div.setAttribute('data-match-status', match.status.toString());
}

return div;
}

/**
* Creates a container which contains the label of a match.
*
* @param label The label of the match.
* @param status The status to set as tooltip.
* @param onClick Called when the label is clicked.
*/
export function createMatchLabel(label: string, status: string): HTMLElement {
export function createMatchLabel(label: string | undefined, status: string, onClick?: (event: MouseEvent) => void): HTMLElement {
const span = document.createElement('span');
span.innerText = label;
span.innerText = label || '';
span.title = status;
onClick && span.addEventListener('click', onClick);
return span;
}

/**
* Creates a container which contains the child count label of a match.
*
* @param label The child count label of the match.
* @param onClick Called when the label is clicked.
*/
export function createChildCountLabel(label: string): HTMLElement {
export function createChildCountLabel(label: string, onClick?: (event: MouseEvent) => void): HTMLElement {
const span = document.createElement('span');
span.innerText = label;
onClick && span.addEventListener('click', onClick);
return span;
}

Expand Down
27 changes: 23 additions & 4 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Match, ParticipantResult, GroupType } from 'brackets-model';
import { RankingHeader, Ranking, RankingFormula, RankingItem, RankingMap, Side } from './types';
import { Match, ParticipantResult, GroupType, MatchGame } from 'brackets-model';
import { RankingHeader, Ranking, RankingFormula, RankingItem, RankingMap, Side, MatchWithMetadata } from './types';
import { t } from './lang';

/**
Expand Down Expand Up @@ -73,8 +73,8 @@ export function findRoot(selector?: string): HTMLElement {
* @param matches The list of first round matches.
* @param nextMatches The list of second round matches.
*/
export function completeWithBlankMatches(bracketType: GroupType, matches: Match[], nextMatches?: Match[]): {
matches: (Match | null)[],
export function completeWithBlankMatches(bracketType: GroupType, matches: MatchWithMetadata[], nextMatches?: MatchWithMetadata[]): {
matches: (MatchWithMetadata | null)[],
fromToornament: boolean,
} {
if (!nextMatches)
Expand Down Expand Up @@ -231,3 +231,22 @@ function createRanking(rankingMap: RankingMap): RankingItem[] {

return ranking;
}

/**
* Indicates whether the input is a match.
*
* @param input A match or a match game.
*/
export function isMatch(input: Match | MatchGame): input is Match {
return 'child_count' in input;
}


/**
* Indicates whether the input is a match game.
*
* @param input A match or a match game.
*/
export function isMatchGame(input: Match | MatchGame): input is MatchGame {
return !isMatch(input);
}
4 changes: 3 additions & 1 deletion src/i18n/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"grand-final": "Winner of $t(abbreviations.loser-bracket) Final"
},
"match-label": {
"default": "Match {{matchNumber}}",
"winner-bracket": "$t(abbreviations.winner-bracket)",
"loser-bracket": "$t(abbreviations.loser-bracket)",
"standard-bracket": "$t(abbreviations.match)",
Expand All @@ -18,7 +19,8 @@
"double-elimination-final": "{{matchPrefix}} Final",
"consolation-final": "Consolation Final",
"grand-final-single": "Grand Final",
"grand-final": "$t(abbreviations.grand-final) Round {{roundNumber}}"
"grand-final": "$t(abbreviations.grand-final) Round {{roundNumber}}",
"match-game": "Game {{gameNumber}}"
},
"match-status": {
"locked": "Locked",
Expand Down
Loading

0 comments on commit dae5803

Please sign in to comment.