iswhoa/public/scripts/controllers/picker.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

import { Controller } from "https://unpkg.com/@hotwired/stimulus/dist/stimulus.js";
2025-06-20 12:00:23 +00:00
import { gameState } from "./gamestate.js";
export default class extends Controller {
2025-06-20 12:00:23 +00:00
static targets = ["radio", "answerDetails", "submit"];
static values = {
2025-06-20 12:00:23 +00:00
qid: String,
answer: String
};
connect() {
2025-06-20 12:00:23 +00:00
let hasAnswer = gameState.getQuestionChoice(this.qidValue);
if (hasAnswer) {
this._revealAnswer();
2025-06-22 12:06:49 +00:00
let e = this.radioTargets.find((e) => e.value == hasAnswer.cId);
2025-06-20 12:00:23 +00:00
if (e) {
e.checked = true;
}
}
}
checkRadio(ev) {
this.submitTarget.disabled = false;
}
submitAnswer(ev) {
ev.preventDefault();
2025-06-20 12:00:23 +00:00
this.submitTarget.disabled = false;
let e = this.radioTargets.find((e) => e.checked);
if (!e) {
alert("Please select an item");
return;
}
2025-06-22 12:06:49 +00:00
gameState.setQuestionChoice(this.qidValue, e.value, e.value === this.answerValue);
2025-06-20 12:00:23 +00:00
this.element.classList.add("reveal");
window.setTimeout(() => { this._revealAnswer(); });
}
_revealAnswer() {
this.element.classList.add("reveal");
2025-06-20 12:00:23 +00:00
this.radioTargets.forEach(e => {
e.disabled = true;
if (e.value === this.answerValue) {
e.classList.add("answer");
} else {
e.classList.add("wrong");
}
});
2025-06-19 12:14:46 +00:00
this.answerDetailsTarget.classList.remove("hidden");
}
};