Skip to content

Commit 2cc183f

Browse files
committed
Add a "scramble-cleared" event to <scramble-table>`.
1 parent deb80d5 commit 2cc183f

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/dev/main.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ declare global {
99
const app = document.body.appendChild(new ScrambleTable());
1010
globalThis.app = app;
1111

12+
app.addEventListener(
13+
"scramble-cleared",
14+
(e: CustomEvent<{ displayIndex: number }>) => {
15+
console.log(`Scramble cleared for display index: ${e.detail.displayIndex}`);
16+
},
17+
);
18+
1219
app.displays[0].setScramble({
1320
competitorName: "Amelia Multicube",
1421
competitorCompetitionID: 11,

src/lib/elements/CompetitorScrambleDisplay.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ function nextUnassigned(): string {
2323
}
2424

2525
export class CompetitorScrambleDisplay extends HTMLElement {
26-
constructor(private sharedState: SharedState, private displayIndex: number) {
26+
constructor(
27+
private sharedState: SharedState,
28+
private displayIndex: number,
29+
private onScrambleCleared: () => void,
30+
) {
2731
super();
2832
}
2933

@@ -99,6 +103,7 @@ export class CompetitorScrambleDisplay extends HTMLElement {
99103
this.querySelector<HTMLButtonElement>(".multi .next").disabled = true;
100104
this.querySelector<HTMLButtonElement>(".multi .all").disabled = true;
101105
this.#hideAdditionalActions();
106+
this.onScrambleCleared();
102107
}
103108

104109
#info: AttemptScrambleInfo | undefined;

src/lib/elements/ScrambleTable.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ const DEFAULT_SET_SCRAMBLER_CALLBACK = async (
3737
return prompt(`Please enter the name of scrambler ${displayIndex + 1}:`);
3838
};
3939

40+
/**
41+
*
42+
* Dispatches the following events:
43+
*
44+
* - `"scramble-cleared"`
45+
*/
4046
export class ScrambleTable
4147
extends HTMLElement
4248
implements ScrambleJSONCacheDelegate
@@ -63,6 +69,16 @@ export class ScrambleTable
6369
this.#initializeSettings();
6470
}
6571

72+
#onScrambleCleared(displayIndex: number) {
73+
this.dispatchEvent(
74+
new CustomEvent("scramble-cleared", {
75+
detail: {
76+
displayIndex,
77+
},
78+
}),
79+
);
80+
}
81+
6682
#initializeSettings() {
6783
this.querySelector("header .settings-button").addEventListener(
6884
"click",
@@ -131,7 +147,9 @@ export class ScrambleTable
131147
const idx = this.displays.length;
132148
this.displays.push(
133149
this.querySelector("scramble-table-contents").appendChild(
134-
new CompetitorScrambleDisplay(this.sharedState, idx),
150+
new CompetitorScrambleDisplay(this.sharedState, idx, () => {
151+
this.#onScrambleCleared(idx);
152+
}),
135153
),
136154
);
137155
}

0 commit comments

Comments
 (0)