random-fo76/index.html

275 lines
9.0 KiB
HTML
Raw Normal View History

2019-06-08 14:52:40 +02:00
<!DOCTYPE html>
<html lang="en">
<head>
2019-06-15 14:35:17 +02:00
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
2019-06-08 14:52:40 +02:00
<meta name="author" content="Felix W. Dekker" />
<meta name="application-name" content="Random Fallout 76 record" />
<meta name="description" content="Returns a random Fallout 76 record." />
<meta name="theme-color" content="#0033cc" />
<title>Random Fallout 76 record</title>
2019-06-10 14:32:55 +02:00
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic"
crossorigin="anonymous" />
2020-03-14 12:58:33 +01:00
<link rel="stylesheet" href="https://static.fwdekker.com/css/milligram-bundle.min.css" crossorigin="anonymous" />
2019-06-08 14:52:40 +02:00
</head>
<body>
2020-03-14 12:58:33 +01:00
<main class="wrapper">
<!-- Header -->
<header class="header">
<div class="container">
<h1>Random Fallout 76 record</h1>
<noscript>
<span style="color: red; font-weight: bold;">
This website does not function if JavaScript is disabled.
Please check the <a href="https://www.enable-javascript.com/">
instructions on how to enable JavaScript in your web browser</a>.
</span>
</noscript>
2020-03-17 19:09:53 +01:00
<blockquote>
<p><em>
2020-03-14 12:58:33 +01:00
On this page you can retrieve a random record from the <i>Fallout 76</i> game files.
Simply select the signatures you want to include below, and then press the "Get random record"
button.
2020-03-17 19:09:53 +01:00
</em></p>
</blockquote>
2020-03-14 12:58:33 +01:00
</div>
</header>
<!-- Input -->
<section class="container">
<h2>Settings</h2>
2020-03-17 19:09:53 +01:00
<form id="signatureForm">
2020-03-14 12:58:33 +01:00
<button id="signatureToggle" class="button button-outline" type="button">Select all signatures</button>
<fieldset id="signatures">Loading... please wait.</fieldset>
<button id="submit" type="button">Get random record</button>
</form>
</section>
<!-- Input -->
<section class="container">
<h2>Record</h2>
<pre><code id="output"></code></pre>
</section>
<!-- Footer -->
<footer class="footer">
2019-06-10 14:32:55 +02:00
<section class="container">
2020-03-14 12:58:33 +01:00
Made by <a href="https://fwdekker.com/">Felix W. Dekker</a>.
Licensed under the
<a href="https://git.fwdekker.com/FWDekker/random-fo76/src/branch/master/LICENSE">MIT License</a>.
Source code available on <a href="https://git.fwdekker.com/FWDekker/random-fo76/">git</a>.
2019-06-10 14:32:55 +02:00
2020-03-17 19:09:53 +01:00
<div style="float: right;">v1.0.7</div>
2019-06-10 14:32:55 +02:00
</section>
2020-03-14 12:58:33 +01:00
</footer>
</main>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.2.0/js.cookie.min.js"
integrity="sha256-9Nt2r+tJnSd2A2CRUvnjgsD+ES1ExvjbjBNqidm9doI=" crossorigin="anonymous"></script>
<script src="https://static.fwdekker.com/js/common.js" crossorigin="anonymous"></script>
<script>
const signatureColCount = 8;
/**
* Returns an array of the signatures that are currently selected.
*/
const getSelectedSignatures = () => {
const signatures = [];
const selectedCheckboxes = $a("#signatures input:checked");
for (let i = 0; i < selectedCheckboxes.length; i++) {
const selectedCheckbox = selectedCheckboxes[i];
signatures.push(selectedCheckbox.value);
}
return signatures;
};
/**
* Selects the indicated signatures, and deselects all others.
*
* @param signatures the array of signatures to select
*/
const setSelectedSignatures = signatures => {
const checkboxes = $a("#signatures input");
for (let i = 0; i < checkboxes.length; i++)
checkboxes[i].checked = false;
for (let i = 0; i < signatures.length; i++)
$(`#signature-${signatures[i]}`).checked = true;
updateSignatureToggle();
};
/**
* Selects all signatures.
*/
const setAllSignatures = checked => {
const checkboxes = $a("#signatures input");
for (let i = 0; i < checkboxes.length; i++)
checkboxes[i].checked = checked;
saveSelectedSignaturesToCookie();
updateSignatureToggle();
};
/**
* (De)selects signatures based on the selection stored in a cookie.
*/
const loadSelectedSignaturesFromCookie = () => {
const cookie = Cookies.get("selectedSignatures");
let signatures;
if (cookie === undefined)
signatures = [];
else
signatures = cookie.split(",");
setSelectedSignatures(signatures);
};
/**
* Saves the currently-selected signatures to a cookie.
*/
const saveSelectedSignaturesToCookie =
() => Cookies.set("selectedSignatures", getSelectedSignatures().join(","), {expires: 5 * 365});
/**
* Updates the button used to toggle all signatures on or off.
*/
const updateSignatureToggle = () => {
const signatureToggle = $("#signatureToggle");
if (getSelectedSignatures().length === $a("#signatures input").length) {
signatureToggle.innerHTML = "Deselect all signatures";
signatureToggle.onclick = () => setAllSignatures(false);
} else {
signatureToggle.innerHTML = "Select all signatures";
signatureToggle.onclick = () => setAllSignatures(true);
}
};
/**
* Downloads an array of signatures from the API.
*
* @param callback the function to execute with the array of signatures
2020-03-17 19:09:53 +01:00
* @param handle the function to execute if signatures could not be downloaded
2020-03-14 12:58:33 +01:00
*/
2020-03-17 19:09:53 +01:00
const downloadSignatures = (callback, handle) => {
2020-03-14 12:58:33 +01:00
fetch("api.php?action=list-signatures")
.then(response => {
2020-03-17 19:09:53 +01:00
if (!response.ok) {
if (handle) handle(response);
console.error(response);
2020-03-14 12:58:33 +01:00
throw new Error("Failed to fetch list of signatures.");
2020-03-17 19:09:53 +01:00
}
2020-03-14 12:58:33 +01:00
return response.json();
})
.then(signatures => callback(signatures));
};
/**
* Creates buttons for the signatures and adds them to the form.
*
* @param signatures an array of signatures to create buttons for
*/
const createSignatureButtons = signatures => {
const form = $("#signatures");
form.innerHTML = "";
let row;
for (let i = 0; i < signatures.length; i++) {
const signature = signatures[i];
if (i % signatureColCount === 0) {
if (row !== undefined)
form.appendChild(row);
row = document.createElement("div");
row.className = "row";
2019-06-08 14:52:40 +02:00
}
2020-03-14 12:58:33 +01:00
const col = document.createElement("div");
col.className = "column";
const label = document.createElement("label");
label.htmlFor = `signature-${signature}`;
label.innerHTML = signature;
col.appendChild(label);
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.id = `signature-${signature}`;
checkbox.name = `signature-${signature}`;
checkbox.value = signature;
checkbox.onclick = () => {
updateSignatureToggle();
saveSelectedSignaturesToCookie();
};
col.appendChild(checkbox);
row.appendChild(col);
}
};
/**
* Downloads a random record from the API.
*
* @param callback the function to execute with the record
2020-03-17 19:09:53 +01:00
* @param handle the function to execute if signatures could not be downloaded
2020-03-14 12:58:33 +01:00
*/
2020-03-17 19:09:53 +01:00
const downloadRandomRecord = (callback, handle) => {
2020-03-14 12:58:33 +01:00
const selectedSignatures = getSelectedSignatures();
fetch(`api.php?action=get-random&signatures=${selectedSignatures.join(",")}`)
.then(response => {
2020-03-17 19:09:53 +01:00
if (!response.ok) {
if (handle) handle(response);
console.error(response);
2020-03-14 12:58:33 +01:00
throw new Error("Failed to fetch random record.");
2020-03-17 19:09:53 +01:00
}
2020-03-14 12:58:33 +01:00
return response.text();
})
.then(record => callback(record));
};
/**
* Displays a record on the page.
*
* @param record the record to display
*/
const showRecord = (record) => {
$("#output").innerHTML = record;
const scrollingElement = (document.scrollingElement || document.body);
scrollingElement.scrollTop = scrollingElement.scrollHeight;
};
doAfterLoad(() => {
$("#submit").onclick = () => downloadRandomRecord(record => showRecord(record));
2020-03-17 19:09:53 +01:00
downloadSignatures(
signatures => {
createSignatureButtons(signatures);
loadSelectedSignaturesFromCookie();
},
errorResponse => {
const form = $("#signatureForm");
form.style.color = "red";
form.innerHTML = "Error: Failed to download signatures."
});
2020-03-14 12:58:33 +01:00
});
</script>
2019-06-08 14:52:40 +02:00
</body>
</html>