rename consts; element shorthand function

This commit is contained in:
Max Bradbury 2020-07-19 23:12:32 +01:00
parent 7959987ffd
commit 07c5e61280
1 changed files with 36 additions and 32 deletions

View File

@ -1,5 +1,9 @@
import init, {load_default_game_data, add_tiles} from './pkg/bitsy_tiles.js'; import init, {load_default_game_data, add_tiles} from './pkg/bitsy_tiles.js';
function el(id) {
return document.getElementById(id);
}
function pagination(e) { function pagination(e) {
const parent = e.target.parentNode; const parent = e.target.parentNode;
@ -30,21 +34,21 @@ function readFile(input, callback, type = "text") {
async function run() { async function run() {
await init(); await init();
const addImageButton = document.getElementById("add"); const buttonAddImage = el("add");
const buttonGameDataProceed = document.getElementById("game-data-next"); const buttonGameDataProceed = el("game-data-next");
const gameDataInput = document.getElementById("game-data"); const textareaGameDataInput = el("game-data");
const importButton = document.getElementById("import"); const buttonImportGame = el("import");
const load_game_button = document.getElementById("load"); const buttonLoadGame = el("load");
const new_game_button = document.getElementById("new"); const buttonNewGame = el("new");
const output = document.getElementById("output"); const textareaGameDataOutput = el("output");
const preview = document.getElementById("preview"); const preview = el("preview");
const resetButton = document.getElementById("reset"); const resetButton = el("reset");
// hide all pages except start page // hide all pages except start page
for (let page of document.getElementsByClassName('page')) { for (let page of document.getElementsByClassName('page')) {
page.style.display = "none"; page.style.display = "none";
} }
document.getElementById("start").style.display = "block"; el("start").style.display = "block";
for (let pageButton of document.getElementsByClassName("pagination")) { for (let pageButton of document.getElementsByClassName("pagination")) {
pageButton.addEventListener('click', pagination); pageButton.addEventListener('click', pagination);
@ -52,43 +56,43 @@ async function run() {
} }
function new_game() { function new_game() {
gameDataInput.value = load_default_game_data(); textareaGameDataInput.value = load_default_game_data();
// we don't need to look at the default game data, so skip ahead to the image page // we don't need to look at the default game data, so skip ahead to the image page
buttonGameDataProceed.click(); buttonGameDataProceed.click();
} }
function clear_game() { function clear_game() {
gameDataInput.value = ""; textareaGameDataInput.value = "";
} }
new_game_button.addEventListener("click", new_game); buttonNewGame.addEventListener("click", new_game);
new_game_button.addEventListener("touchend", new_game); buttonNewGame.addEventListener("touchend", new_game);
load_game_button.addEventListener("click", clear_game); buttonLoadGame.addEventListener("click", clear_game);
load_game_button.addEventListener("touchend", clear_game); buttonLoadGame.addEventListener("touchend", clear_game);
// handle game data and image // handle game data and image
document.getElementById("game").addEventListener("change", function() { el("game").addEventListener("change", function() {
readFile(this, function (e) { readFile(this, function (e) {
gameDataInput.value = e.target.result; textareaGameDataInput.value = e.target.result;
checkGameData(); checkGameData();
}, "text"); }, "text");
}); });
function checkGameData() { function checkGameData() {
if (gameDataInput.value.length > 0) { if (textareaGameDataInput.value.length > 0) {
buttonGameDataProceed.removeAttribute("disabled"); buttonGameDataProceed.removeAttribute("disabled");
} else { } else {
buttonGameDataProceed.setAttribute("disabled", "disabled"); buttonGameDataProceed.setAttribute("disabled", "disabled");
} }
} }
gameDataInput.addEventListener("change", checkGameData); textareaGameDataInput.addEventListener("change", checkGameData);
gameDataInput.addEventListener("keyup", checkGameData); textareaGameDataInput.addEventListener("keyup", checkGameData);
preview.style.display = "none"; preview.style.display = "none";
document.getElementById('image').addEventListener('change', function () { el('image').addEventListener('change', function () {
readFile(this, function (e) { readFile(this, function (e) {
preview.src = e.target.result; preview.src = e.target.result;
preview.style.display = "initial" preview.style.display = "initial"
@ -96,23 +100,23 @@ async function run() {
}); });
function addTiles() { function addTiles() {
let image = document.getElementById("preview").getAttribute("src"); let image = el("preview").getAttribute("src");
let gameData = document.getElementById("game-data").value; let gameData = el("game-data").value;
let prefix = document.getElementById("prefix").value; let prefix = el("prefix").value;
output.value = add_tiles(gameData, image, prefix); textareaGameDataOutput.value = add_tiles(gameData, image, prefix);
} }
importButton.addEventListener("click", addTiles); buttonImportGame.addEventListener("click", addTiles);
importButton.addEventListener("touchend", addTiles); buttonImportGame.addEventListener("touchend", addTiles);
function addImage() { function addImage() {
gameDataInput.value = output.value; textareaGameDataInput.value = textareaGameDataOutput.value;
output.value = ""; textareaGameDataOutput.value = "";
} }
addImageButton.addEventListener("click", addImage); buttonAddImage.addEventListener("click", addImage);
addImageButton.addEventListener("touchend", addImage); buttonAddImage.addEventListener("touchend", addImage);
function reset() { function reset() {
clear_game(); clear_game();