Consistify indenting with other projects
This commit is contained in:
parent
a96ffb0926
commit
81020af4f6
368
index.html
368
index.html
|
@ -23,213 +23,213 @@
|
|||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>Random Fallout 76 record</h1>
|
||||
<p>
|
||||
On this page you can retrieve a random record from the Fallout 76 game files.
|
||||
Simply select the signatures you want to include below, and then press the "Get random record" button.
|
||||
</p>
|
||||
<div class="container">
|
||||
<h1>Random Fallout 76 record</h1>
|
||||
<p>
|
||||
On this page you can retrieve a random record from the Fallout 76 game files.
|
||||
Simply select the signatures you want to include below, and then press the "Get random record" button.
|
||||
</p>
|
||||
|
||||
<h2>Settings</h2>
|
||||
<form>
|
||||
<button id="signatureToggle" class="button button-outline" type="button">Select all signatures</button>
|
||||
<fieldset id="signatures">Loading... please wait.</fieldset>
|
||||
<h2>Settings</h2>
|
||||
<form>
|
||||
<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>
|
||||
<h2>Record</h2>
|
||||
<pre><code id="output"></code></pre>
|
||||
</div>
|
||||
<button id="submit" type="button">Get random record</button>
|
||||
</form>
|
||||
<h2>Record</h2>
|
||||
<pre><code id="output"></code></pre>
|
||||
</div>
|
||||
|
||||
<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>
|
||||
const signatureColCount = 8;
|
||||
<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>
|
||||
const signatureColCount = 8;
|
||||
|
||||
|
||||
const e = id => document.getElementById(id);
|
||||
const q = query => document.querySelector(query);
|
||||
const qa = query => document.querySelectorAll(query);
|
||||
const e = id => document.getElementById(id);
|
||||
const q = query => document.querySelector(query);
|
||||
const qa = query => document.querySelectorAll(query);
|
||||
|
||||
const doAfterLoad = fun => {
|
||||
const oldOnLoad = window.onload || (() => {
|
||||
});
|
||||
const doAfterLoad = fun => {
|
||||
const oldOnLoad = window.onload || (() => {
|
||||
});
|
||||
|
||||
window.onload = (() => {
|
||||
oldOnLoad();
|
||||
fun();
|
||||
});
|
||||
};
|
||||
window.onload = (() => {
|
||||
oldOnLoad();
|
||||
fun();
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns an array of the signatures that are currently selected.
|
||||
*/
|
||||
const getSelectedSignatures = () => {
|
||||
const signatures = [];
|
||||
/**
|
||||
* Returns an array of the signatures that are currently selected.
|
||||
*/
|
||||
const getSelectedSignatures = () => {
|
||||
const signatures = [];
|
||||
|
||||
const selectedCheckboxes = qa("#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 = qa("#signatures input");
|
||||
for (let i = 0; i < checkboxes.length; i++)
|
||||
checkboxes[i].checked = false;
|
||||
|
||||
for (let i = 0; i < signatures.length; i++)
|
||||
q(`#signature-${signatures[i]}`).checked = true;
|
||||
|
||||
updateSignatureToggle();
|
||||
};
|
||||
|
||||
/**
|
||||
* Selects all signatures.
|
||||
*/
|
||||
const setAllSignatures = checked => {
|
||||
const checkboxes = qa("#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 = () => setSelectedSignatures(Cookies.get("selectedSignatures").split(","));
|
||||
|
||||
/**
|
||||
* Saves the currently-selected signatures to a cookie.
|
||||
*/
|
||||
const saveSelectedSignaturesToCookie = () => Cookies.set("selectedSignatures", getSelectedSignatures().join(","));
|
||||
|
||||
/**
|
||||
* Updates the button used to toggle all signatures on or off.
|
||||
*/
|
||||
const updateSignatureToggle = () => {
|
||||
const signatureToggle = q("#signatureToggle");
|
||||
|
||||
if (getSelectedSignatures().length === qa("#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
|
||||
*/
|
||||
const downloadSignatures = callback => {
|
||||
fetch("api.php?action=list-signatures")
|
||||
.then(response => {
|
||||
if (!response.ok)
|
||||
throw new Error("Failed to fetch list of signatures.");
|
||||
|
||||
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 = e("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";
|
||||
const selectedCheckboxes = qa("#signatures input:checked");
|
||||
for (let i = 0; i < selectedCheckboxes.length; i++) {
|
||||
const selectedCheckbox = selectedCheckboxes[i];
|
||||
signatures.push(selectedCheckbox.value);
|
||||
}
|
||||
|
||||
const col = document.createElement("div");
|
||||
col.className = "column";
|
||||
return signatures;
|
||||
};
|
||||
|
||||
const label = document.createElement("label");
|
||||
label.htmlFor = `signature-${signature}`;
|
||||
label.innerHTML = signature;
|
||||
col.appendChild(label);
|
||||
/**
|
||||
* Selects the indicated signatures, and deselects all others.
|
||||
*
|
||||
* @param signatures the array of signatures to select
|
||||
*/
|
||||
const setSelectedSignatures = signatures => {
|
||||
const checkboxes = qa("#signatures input");
|
||||
for (let i = 0; i < checkboxes.length; i++)
|
||||
checkboxes[i].checked = false;
|
||||
|
||||
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);
|
||||
for (let i = 0; i < signatures.length; i++)
|
||||
q(`#signature-${signatures[i]}`).checked = true;
|
||||
|
||||
row.appendChild(col);
|
||||
}
|
||||
};
|
||||
updateSignatureToggle();
|
||||
};
|
||||
|
||||
/**
|
||||
* Downloads a random record from the API.
|
||||
*
|
||||
* @param callback the function to execute with the record
|
||||
*/
|
||||
const downloadRandomRecord = callback => {
|
||||
const selectedSignatures = getSelectedSignatures();
|
||||
/**
|
||||
* Selects all signatures.
|
||||
*/
|
||||
const setAllSignatures = checked => {
|
||||
const checkboxes = qa("#signatures input");
|
||||
for (let i = 0; i < checkboxes.length; i++)
|
||||
checkboxes[i].checked = checked;
|
||||
saveSelectedSignaturesToCookie();
|
||||
|
||||
fetch(`api.php?action=get-random&signatures=${selectedSignatures.join(",")}`)
|
||||
.then(response => {
|
||||
if (!response.ok)
|
||||
throw new Error("Failed to fetch random record.");
|
||||
updateSignatureToggle();
|
||||
};
|
||||
|
||||
return response.text();
|
||||
})
|
||||
.then(record => callback(record));
|
||||
};
|
||||
/**
|
||||
* (De)selects signatures based on the selection stored in a cookie.
|
||||
*/
|
||||
const loadSelectedSignaturesFromCookie = () => setSelectedSignatures(Cookies.get("selectedSignatures").split(","));
|
||||
|
||||
/**
|
||||
* Displays a record on the page.
|
||||
*
|
||||
* @param record the record to display
|
||||
*/
|
||||
const showRecord = (record) => {
|
||||
e("output").innerHTML = record;
|
||||
/**
|
||||
* Saves the currently-selected signatures to a cookie.
|
||||
*/
|
||||
const saveSelectedSignaturesToCookie = () => Cookies.set("selectedSignatures", getSelectedSignatures().join(","));
|
||||
|
||||
const scrollingElement = (document.scrollingElement || document.body);
|
||||
scrollingElement.scrollTop = scrollingElement.scrollHeight;
|
||||
};
|
||||
/**
|
||||
* Updates the button used to toggle all signatures on or off.
|
||||
*/
|
||||
const updateSignatureToggle = () => {
|
||||
const signatureToggle = q("#signatureToggle");
|
||||
|
||||
if (getSelectedSignatures().length === qa("#signatures input").length) {
|
||||
signatureToggle.innerHTML = "Deselect all signatures";
|
||||
signatureToggle.onclick = () => setAllSignatures(false);
|
||||
} else {
|
||||
signatureToggle.innerHTML = "Select all signatures";
|
||||
signatureToggle.onclick = () => setAllSignatures(true);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
doAfterLoad(() => {
|
||||
e("submit").onclick = () => downloadRandomRecord(record => showRecord(record));
|
||||
/**
|
||||
* Downloads an array of signatures from the API.
|
||||
*
|
||||
* @param callback the function to execute with the array of signatures
|
||||
*/
|
||||
const downloadSignatures = callback => {
|
||||
fetch("api.php?action=list-signatures")
|
||||
.then(response => {
|
||||
if (!response.ok)
|
||||
throw new Error("Failed to fetch list of signatures.");
|
||||
|
||||
downloadSignatures(signatures => {
|
||||
createSignatureButtons(signatures);
|
||||
loadSelectedSignaturesFromCookie();
|
||||
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 = e("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";
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
const downloadRandomRecord = callback => {
|
||||
const selectedSignatures = getSelectedSignatures();
|
||||
|
||||
fetch(`api.php?action=get-random&signatures=${selectedSignatures.join(",")}`)
|
||||
.then(response => {
|
||||
if (!response.ok)
|
||||
throw new Error("Failed to fetch random record.");
|
||||
|
||||
return response.text();
|
||||
})
|
||||
.then(record => callback(record));
|
||||
};
|
||||
|
||||
/**
|
||||
* Displays a record on the page.
|
||||
*
|
||||
* @param record the record to display
|
||||
*/
|
||||
const showRecord = (record) => {
|
||||
e("output").innerHTML = record;
|
||||
|
||||
const scrollingElement = (document.scrollingElement || document.body);
|
||||
scrollingElement.scrollTop = scrollingElement.scrollHeight;
|
||||
};
|
||||
|
||||
|
||||
doAfterLoad(() => {
|
||||
e("submit").onclick = () => downloadRandomRecord(record => showRecord(record));
|
||||
|
||||
downloadSignatures(signatures => {
|
||||
createSignatureButtons(signatures);
|
||||
loadSelectedSignaturesFromCookie();
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in New Issue