Skip to content

Commit

Permalink
Add a "scramble-cleared" event to <scramble-table>`.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgarron committed Mar 28, 2024
1 parent deb80d5 commit 5f06c48
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/dev/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ declare global {
const app = document.body.appendChild(new ScrambleTable());
globalThis.app = app;

app.addEventListener(
"scramble-cleared",
(e: CustomEvent<{ displayIndex: number }>) => {
console.log(`Scramble cleared for display index: ${e.detail.displayIndex}`);
},
);

app.displays[0].setScramble({
competitorName: "Amelia Multicube",
competitorCompetitionID: 11,
Expand Down
7 changes: 6 additions & 1 deletion src/lib/elements/CompetitorScrambleDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ function nextUnassigned(): string {
}

export class CompetitorScrambleDisplay extends HTMLElement {
constructor(private sharedState: SharedState, private displayIndex: number) {
constructor(
private sharedState: SharedState,
private displayIndex: number,
private onScrambleCleared: () => void,
) {
super();
}

Expand Down Expand Up @@ -99,6 +103,7 @@ export class CompetitorScrambleDisplay extends HTMLElement {
this.querySelector<HTMLButtonElement>(".multi .next").disabled = true;
this.querySelector<HTMLButtonElement>(".multi .all").disabled = true;
this.#hideAdditionalActions();
this.onScrambleCleared();
}

#info: AttemptScrambleInfo | undefined;
Expand Down
20 changes: 19 additions & 1 deletion src/lib/elements/ScrambleTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ const DEFAULT_SET_SCRAMBLER_CALLBACK = async (
return prompt(`Please enter the name of scrambler ${displayIndex + 1}:`);
};

/**
*
* Dispatches the following events:
*
* - `"scramble-cleared"`
*/
export class ScrambleTable
extends HTMLElement
implements ScrambleJSONCacheDelegate
Expand All @@ -63,6 +69,16 @@ export class ScrambleTable
this.#initializeSettings();
}

#onScrambleCleared(displayIndex: number) {
this.dispatchEvent(
new CustomEvent("scramble-cleared", {
detail: {
displayIndex,
},
}),
);
}

#initializeSettings() {
this.querySelector("header .settings-button").addEventListener(
"click",
Expand Down Expand Up @@ -131,7 +147,9 @@ export class ScrambleTable
const idx = this.displays.length;
this.displays.push(
this.querySelector("scramble-table-contents").appendChild(
new CompetitorScrambleDisplay(this.sharedState, idx),
new CompetitorScrambleDisplay(this.sharedState, idx, () => {
this.#onScrambleCleared(idx);
}),
),
);
}
Expand Down

0 comments on commit 5f06c48

Please sign in to comment.