forked from tools/doomsday
Create classes to reduce duplication
This commit is contained in:
parent
99b8044b10
commit
b475b9769e
360
index.html
360
index.html
|
@ -22,6 +22,10 @@
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.error-message {
|
||||||
|
color: red;
|
||||||
|
}
|
||||||
|
|
||||||
input[data-entered=true]:valid {
|
input[data-entered=true]:valid {
|
||||||
background-color: green;
|
background-color: green;
|
||||||
}
|
}
|
||||||
|
@ -52,26 +56,26 @@
|
||||||
<form>
|
<form>
|
||||||
<label for="century" id="century-label">Century</label>
|
<label for="century" id="century-label">Century</label>
|
||||||
<input type="text" id="century" autocomplete="off" autofocus /> <!--list="weekdays"-->
|
<input type="text" id="century" autocomplete="off" autofocus /> <!--list="weekdays"-->
|
||||||
<span class="invisible" id="century-error">Error message</span>
|
<span class="invisible error-message" id="century-error">Error message</span>
|
||||||
|
|
||||||
<label for="year" id="year-label">Year</label>
|
<label for="year" id="year-label">Year</label>
|
||||||
<input type="text" id="year" autocomplete="off" />
|
<input type="text" id="year" autocomplete="off" />
|
||||||
<span class="invisible" id="year-error">Error message</span>
|
<span class="invisible error-message" id="year-error">Error message</span>
|
||||||
|
|
||||||
<label for="day" id="day-label">Day</label>
|
<label for="day" id="day-label">Day</label>
|
||||||
<input type="text" id="day" autocomplete="off" />
|
<input type="text" id="day" autocomplete="off" />
|
||||||
<span class="invisible" id="day-error">Error message</span>
|
<span class="invisible error-message" id="day-error">Error message</span>
|
||||||
|
|
||||||
|
|
||||||
<!-- <datalist id="weekdays">-->
|
<!-- <datalist id="weekdays">-->
|
||||||
<!-- <option value="Monday">-->
|
<!-- <option value="Monday">-->
|
||||||
<!-- <option value="Tuesday">-->
|
<!-- <option value="Tuesday">-->
|
||||||
<!-- <option value="Wednesday">-->
|
<!-- <option value="Wednesday">-->
|
||||||
<!-- <option value="Thursday">-->
|
<!-- <option value="Thursday">-->
|
||||||
<!-- <option value="Friday">-->
|
<!-- <option value="Friday">-->
|
||||||
<!-- <option value="Saturday">-->
|
<!-- <option value="Saturday">-->
|
||||||
<!-- <option value="Sunday">-->
|
<!-- <option value="Sunday">-->
|
||||||
<!-- </datalist>-->
|
<!-- </datalist>-->
|
||||||
</form>
|
</form>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
@ -91,121 +95,257 @@
|
||||||
<!-- Scripts -->
|
<!-- Scripts -->
|
||||||
<script src="https://static.fwdekker.com/js/common.js" crossorigin="anonymous"></script>
|
<script src="https://static.fwdekker.com/js/common.js" crossorigin="anonymous"></script>
|
||||||
<script>
|
<script>
|
||||||
// rand in range [min, max] (both inclusive)
|
/**
|
||||||
const random = function (min, max) {
|
* Returns a number between `min` (inclusive) and `max` (inclusive).
|
||||||
|
*
|
||||||
|
* @param min the lower bound of permissible values
|
||||||
|
* @param max the upper bound of permissible values
|
||||||
|
*/
|
||||||
|
function generateRandom(min, max) {
|
||||||
return Math.floor((Math.random() * (max - min + 1)) + min);
|
return Math.floor((Math.random() * (max - min + 1)) + min);
|
||||||
};
|
}
|
||||||
|
|
||||||
// generate date in range [0001-01-01, 9999-12-31]
|
|
||||||
const generateDate = function () {
|
|
||||||
const minDate = new Date("0001-01-01").getTime() / 86400000;
|
|
||||||
const maxDate = new Date("9999-12-31").getTime() / 86400000;
|
|
||||||
return new Date(random(minDate, maxDate) * 86400000);
|
|
||||||
};
|
|
||||||
|
|
||||||
const dayNumberToWeekDay = function (dayNumber) {
|
/**
|
||||||
switch (dayNumber) {
|
* An element that can be validated.
|
||||||
case 0:
|
*
|
||||||
return "Sunday";
|
* In particular, the century, year, and day inputs of the Doomsday test.
|
||||||
case 1:
|
*/
|
||||||
return "Monday";
|
class ValidatableElement {
|
||||||
case 2:
|
/**
|
||||||
return "Tuesday";
|
* Constructs a new validatable element and registers event listeners.
|
||||||
case 3:
|
*
|
||||||
return "Wednesday";
|
* @param element the element that is validatable
|
||||||
case 4:
|
* @param titleLabel the label with the `labelFor` property for the element given as the first parameter
|
||||||
return "Thursday";
|
* @param errorLabel the label to display errors in
|
||||||
case 5:
|
*/
|
||||||
return "Friday";
|
constructor(element, titleLabel, errorLabel) {
|
||||||
case 6:
|
this.element = element;
|
||||||
return "Saturday";
|
this.titleLabel = titleLabel;
|
||||||
|
this.errorLabel = errorLabel;
|
||||||
|
|
||||||
|
this.element.addEventListener("keydown", event => {
|
||||||
|
if (event.key !== "Enter")
|
||||||
|
return;
|
||||||
|
|
||||||
|
this.element.dataset["entered"] = "true";
|
||||||
|
|
||||||
|
const errorMessage = this.validate(this.element.value);
|
||||||
|
this.element.setCustomValidity(errorMessage || "");
|
||||||
|
this.errorLabel.innerText = errorMessage || "";
|
||||||
|
if (errorMessage === null) {
|
||||||
|
this.errorLabel.classList.add("invisible");
|
||||||
|
this.onValidInput();
|
||||||
|
} else {
|
||||||
|
this.errorLabel.classList.remove("invisible");
|
||||||
|
this.element.select();
|
||||||
|
this.onInvalidInput();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
const centuryToAnchor = function (century) { // century is e.g. 1900 or 19, doesn't really matter since it's mod
|
|
||||||
return dayNumberToWeekDay((5 * (century % 4)) % 7 + 2);
|
|
||||||
};
|
|
||||||
|
|
||||||
const yearToAnchor = function (year) {
|
|
||||||
return dayNumberToWeekDay((new Date("" + year + "-04-04")).getDay());
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
const showError = function (input, errorLabel) {
|
/**
|
||||||
|
* Returns `undefined` if the element is valid, or an error message explaining why it is invalid otherwise.
|
||||||
|
*
|
||||||
|
* This method **must** be implemented by subclasses.
|
||||||
|
*
|
||||||
|
* @param value the value of the element to validate
|
||||||
|
*/
|
||||||
|
validate(value) {
|
||||||
|
throw new Error("Implement this method.");
|
||||||
|
}
|
||||||
|
|
||||||
};
|
/**
|
||||||
|
* Runs when the user submits a valid input.
|
||||||
|
*
|
||||||
|
* This method **must** be implemented by subclasses.
|
||||||
|
*/
|
||||||
|
onValidInput() {
|
||||||
|
throw new Error("Implement this method.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs when a user submits an invalid input.
|
||||||
|
*
|
||||||
|
* This method **must** be implemented by subclasses.
|
||||||
|
*/
|
||||||
|
onInvalidInput() {
|
||||||
|
throw new Error("Implement this method.");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets the element to its initial state, removing error messages and user input.
|
||||||
|
*/
|
||||||
|
reset() {
|
||||||
|
this.element.dataset["entered"] = "false";
|
||||||
|
this.element.value = "";
|
||||||
|
this.element.setCustomValidity("");
|
||||||
|
|
||||||
|
this.errorLabel.classList.add("invisible");
|
||||||
|
this.errorLabel.innerText = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Focuses the input element.
|
||||||
|
*/
|
||||||
|
focus() {
|
||||||
|
this.element.select();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the title above the input.
|
||||||
|
*
|
||||||
|
* @param title the title to set
|
||||||
|
*/
|
||||||
|
setTitle(title) {
|
||||||
|
this.titleLabel.innerText = title;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A wrapper around the good ol' `Date` class that provides a bunch of useful Doomsday-specific methods.
|
||||||
|
*/
|
||||||
|
class DoomsdayDate {
|
||||||
|
/**
|
||||||
|
* Wraps a `DoomsdayDate` around the given `Date`.
|
||||||
|
*
|
||||||
|
* @param date the `Date` to be wrapped
|
||||||
|
*/
|
||||||
|
constructor(date) {
|
||||||
|
this.date = date;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the day of the week of the anchor of this `DoomsdayDate`'s century as a string.
|
||||||
|
*/
|
||||||
|
getCenturyAnchorString() {
|
||||||
|
const yearNumber = this.date.getFullYear();
|
||||||
|
const centuryNumber = Math.floor(yearNumber / 100);
|
||||||
|
const centuryAnchorNumber = (5 * (centuryNumber % 4)) % 7 + 2;
|
||||||
|
return DoomsdayDate.dayNumberToString(centuryAnchorNumber);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the day of the week of the anchor day of this `DoomsdayDate`'s year as a string.
|
||||||
|
*/
|
||||||
|
getYearAnchorString() {
|
||||||
|
const anchorDate = new Date(this.date);
|
||||||
|
anchorDate.setMonth(3); // April
|
||||||
|
anchorDate.setDate(4); // 4th
|
||||||
|
return DoomsdayDate.dayNumberToString(anchorDate.getDay());
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the day of the week of this `DoomsdayDate` as a string.
|
||||||
|
*/
|
||||||
|
getWeekdayString() {
|
||||||
|
return DoomsdayDate.dayNumberToString(this.date.getDay());
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the name of the day given its 0-based index, where 0 is `Sunday`.
|
||||||
|
*
|
||||||
|
* @param dayNumber the number of the day, as returned by `Date`'s `#getDay` function.
|
||||||
|
*/
|
||||||
|
static dayNumberToString(dayNumber) {
|
||||||
|
switch (dayNumber % 7) {
|
||||||
|
case 0:
|
||||||
|
return "Sunday";
|
||||||
|
case 1:
|
||||||
|
return "Monday";
|
||||||
|
case 2:
|
||||||
|
return "Tuesday";
|
||||||
|
case 3:
|
||||||
|
return "Wednesday";
|
||||||
|
case 4:
|
||||||
|
return "Thursday";
|
||||||
|
case 5:
|
||||||
|
return "Friday";
|
||||||
|
case 6:
|
||||||
|
return "Saturday";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a random date in the range `0001-01-01` (inclusive) to `9999-12-31` (inclusive), wrapped inside a
|
||||||
|
* `DoomsdayDate` object.
|
||||||
|
*/
|
||||||
|
static random() {
|
||||||
|
// TODO Give custom dates to this method
|
||||||
|
const minDate = new Date("0001-01-01").getTime() / 86400000;
|
||||||
|
const maxDate = new Date("9999-12-31").getTime() / 86400000;
|
||||||
|
return new DoomsdayDate(new Date(generateRandom(minDate, maxDate) * 86400000));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
doAfterLoad(() => {
|
doAfterLoad(() => {
|
||||||
const centuryInput = $("#century");
|
|
||||||
const centuryLabel = $("#century-label");
|
|
||||||
const yearInput = $("#year");
|
|
||||||
const yearLabel = $("#year-label");
|
|
||||||
const dayInput = $("#day");
|
|
||||||
const dayLabel = $("#day-label");
|
|
||||||
|
|
||||||
let quizDate;
|
let quizDate;
|
||||||
|
|
||||||
|
const centuryClass = new class extends ValidatableElement {
|
||||||
|
validate(value) {
|
||||||
|
return value === quizDate.getCenturyAnchorString() ? null : "";
|
||||||
|
}
|
||||||
|
|
||||||
const reloadQuiz = function () {
|
onValidInput() {
|
||||||
centuryInput.dataset["entered"] = "false";
|
console.log("valid");
|
||||||
centuryInput.value = "";
|
yearClass.focus();
|
||||||
yearInput.dataset["entered"] = "false";
|
}
|
||||||
yearInput.value = "";
|
|
||||||
dayInput.dataset["entered"] = "false";
|
|
||||||
dayInput.value = "";
|
|
||||||
|
|
||||||
quizDate = generateDate();
|
onInvalidInput() {
|
||||||
centuryLabel.innerText = Math.floor(quizDate.getFullYear() / 100) * 100;
|
// Do nothing
|
||||||
yearLabel.innerText = quizDate.getFullYear();
|
}
|
||||||
dayLabel.innerText = quizDate.toISOString().substr(0, 10);
|
}($("#century"), $("#century-label"), $("#century-error"));
|
||||||
|
const yearClass = new class extends ValidatableElement {
|
||||||
|
validate(value) {
|
||||||
|
return value === quizDate.getYearAnchorString() ? null : "";
|
||||||
|
}
|
||||||
|
|
||||||
centuryInput.select();
|
onValidInput() {
|
||||||
|
dayClass.focus();
|
||||||
|
}
|
||||||
|
|
||||||
|
onInvalidInput() {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
}($("#year"), $("#year-label"), $("#year-error"));
|
||||||
|
const dayClass = new class extends ValidatableElement {
|
||||||
|
validate(value) {
|
||||||
|
return value === quizDate.getWeekdayString() ? null : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
onValidInput() {
|
||||||
|
reloadQuiz();
|
||||||
|
}
|
||||||
|
|
||||||
|
onInvalidInput() {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
}($("#day"), $("#day-label"), $("#day-error"));
|
||||||
|
|
||||||
|
|
||||||
|
function reloadQuiz() {
|
||||||
|
quizDate = DoomsdayDate.random();
|
||||||
|
centuryClass.reset();
|
||||||
|
yearClass.reset();
|
||||||
|
dayClass.reset();
|
||||||
|
|
||||||
|
centuryClass.setTitle("" + Math.floor(quizDate.date.getFullYear() / 100) * 100);
|
||||||
|
yearClass.setTitle("" + quizDate.date.getFullYear());
|
||||||
|
dayClass.setTitle("" + quizDate.date.toISOString().substr(0, 10));
|
||||||
|
|
||||||
|
centuryClass.focus();
|
||||||
|
|
||||||
|
// console.log(centuryToAnchor(quizDate));
|
||||||
|
// console.log(yearToAnchor(quizDate));
|
||||||
|
// console.log(dateToWeekDay(quizDate));
|
||||||
|
}
|
||||||
|
|
||||||
console.log(centuryToAnchor(Math.floor(quizDate.getFullYear() / 100)));
|
|
||||||
console.log(yearToAnchor(quizDate.getFullYear()));
|
|
||||||
console.log(dayNumberToWeekDay(quizDate.getDay()));
|
|
||||||
};
|
|
||||||
reloadQuiz();
|
reloadQuiz();
|
||||||
|
|
||||||
|
|
||||||
centuryInput.addEventListener("keydown", event => {
|
|
||||||
if (event.key === "Enter") {
|
|
||||||
centuryInput.dataset["entered"] = "true";
|
|
||||||
if (centuryInput.value === centuryToAnchor(Math.floor(quizDate.getFullYear() / 100))) {
|
|
||||||
centuryInput.setCustomValidity("");
|
|
||||||
yearInput.select();
|
|
||||||
} else {
|
|
||||||
centuryInput.setCustomValidity("Wrong day");
|
|
||||||
centuryInput.select();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
yearInput.addEventListener("keydown", event => {
|
|
||||||
if (event.key === "Enter") {
|
|
||||||
yearInput.dataset["entered"] = "true";
|
|
||||||
if (yearInput.value === yearToAnchor(quizDate.getFullYear())) {
|
|
||||||
yearInput.setCustomValidity("");
|
|
||||||
dayInput.select();
|
|
||||||
} else {
|
|
||||||
yearInput.setCustomValidity("Wrong day");
|
|
||||||
yearInput.select();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
dayInput.addEventListener("keydown", event => {
|
|
||||||
if (event.key === "Enter") {
|
|
||||||
dayInput.dataset["entered"] = "true";
|
|
||||||
if (dayInput.value === dayNumberToWeekDay(quizDate.getDay())) {
|
|
||||||
reloadQuiz();
|
|
||||||
} else {
|
|
||||||
dayInput.setCustomValidity("Wrong day");
|
|
||||||
dayInput.select();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
Loading…
Reference in New Issue