let JS handle the DOM; rearrange code
This commit is contained in:
parent
ffd8a15eda
commit
3cdf52b5e2
92
script.js
92
script.js
|
@ -1,18 +1,6 @@
|
||||||
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';
|
||||||
|
|
||||||
async function run() {
|
function pagination(e) {
|
||||||
await init();
|
|
||||||
|
|
||||||
const output = document.getElementById("output");
|
|
||||||
|
|
||||||
// hide all pages except start page
|
|
||||||
for (let page of document.getElementsByClassName('page')) {
|
|
||||||
page.style.display = "none";
|
|
||||||
}
|
|
||||||
|
|
||||||
document.getElementById("start").style.display = "block";
|
|
||||||
|
|
||||||
function pagination(e) {
|
|
||||||
const parent = e.target.parentNode;
|
const parent = e.target.parentNode;
|
||||||
|
|
||||||
parent.style.display = "none";
|
parent.style.display = "none";
|
||||||
|
@ -24,36 +12,9 @@ async function run() {
|
||||||
} else if (e.target.classList.contains("start")) {
|
} else if (e.target.classList.contains("start")) {
|
||||||
document.getElementById("start").style.display = "block";
|
document.getElementById("start").style.display = "block";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let pageButton of document.getElementsByClassName("pagination")) {
|
function readFile(input, callback, type = "text") {
|
||||||
pageButton.addEventListener('click', pagination);
|
|
||||||
pageButton.addEventListener('touchend', pagination);
|
|
||||||
}
|
|
||||||
|
|
||||||
const gameDataInput = document.getElementById("game-data");
|
|
||||||
const buttonGameDataProceed = document.getElementById("game-data-next");
|
|
||||||
|
|
||||||
function new_game() {
|
|
||||||
load_default_game_data();
|
|
||||||
// we don't need to look at the default game data, so skip ahead to the image page
|
|
||||||
buttonGameDataProceed.click();
|
|
||||||
}
|
|
||||||
|
|
||||||
function clear_game() {
|
|
||||||
gameDataInput.value = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
let new_game_button = document.getElementById("new");
|
|
||||||
new_game_button.addEventListener("click", new_game);
|
|
||||||
new_game_button.addEventListener("touchend", new_game);
|
|
||||||
let load_game_button = document.getElementById("load");
|
|
||||||
load_game_button.addEventListener("click", clear_game);
|
|
||||||
load_game_button.addEventListener("touchend", clear_game);
|
|
||||||
|
|
||||||
// handle game data and image
|
|
||||||
|
|
||||||
function readFile(input, callback, type = "text") {
|
|
||||||
if (input.files && input.files[0]) {
|
if (input.files && input.files[0]) {
|
||||||
let reader = new FileReader();
|
let reader = new FileReader();
|
||||||
reader.onload = callback;
|
reader.onload = callback;
|
||||||
|
@ -64,7 +25,48 @@ async function run() {
|
||||||
reader.readAsDataURL(input.files[0]);
|
reader.readAsDataURL(input.files[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function run() {
|
||||||
|
await init();
|
||||||
|
|
||||||
|
const addImageButton = document.getElementById("add");
|
||||||
|
const buttonGameDataProceed = document.getElementById("game-data-next");
|
||||||
|
const gameDataInput = document.getElementById("game-data");
|
||||||
|
const importButton = document.getElementById("import");
|
||||||
|
const load_game_button = document.getElementById("load");
|
||||||
|
const new_game_button = document.getElementById("new");
|
||||||
|
const output = document.getElementById("output");
|
||||||
|
const preview = document.getElementById("preview");
|
||||||
|
const resetButton = document.getElementById("reset");
|
||||||
|
|
||||||
|
// hide all pages except start page
|
||||||
|
for (let page of document.getElementsByClassName('page')) {
|
||||||
|
page.style.display = "none";
|
||||||
}
|
}
|
||||||
|
document.getElementById("start").style.display = "block";
|
||||||
|
|
||||||
|
for (let pageButton of document.getElementsByClassName("pagination")) {
|
||||||
|
pageButton.addEventListener('click', pagination);
|
||||||
|
pageButton.addEventListener('touchend', pagination);
|
||||||
|
}
|
||||||
|
|
||||||
|
function new_game() {
|
||||||
|
gameDataInput.value = load_default_game_data();
|
||||||
|
// we don't need to look at the default game data, so skip ahead to the image page
|
||||||
|
buttonGameDataProceed.click();
|
||||||
|
}
|
||||||
|
|
||||||
|
function clear_game() {
|
||||||
|
gameDataInput.value = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
new_game_button.addEventListener("click", new_game);
|
||||||
|
new_game_button.addEventListener("touchend", new_game);
|
||||||
|
load_game_button.addEventListener("click", clear_game);
|
||||||
|
load_game_button.addEventListener("touchend", clear_game);
|
||||||
|
|
||||||
|
// handle game data and image
|
||||||
|
|
||||||
document.getElementById("game").addEventListener("change", function() {
|
document.getElementById("game").addEventListener("change", function() {
|
||||||
readFile(this, function (e) {
|
readFile(this, function (e) {
|
||||||
|
@ -84,7 +86,6 @@ async function run() {
|
||||||
gameDataInput.addEventListener("change", checkGameData);
|
gameDataInput.addEventListener("change", checkGameData);
|
||||||
gameDataInput.addEventListener("keyup", checkGameData);
|
gameDataInput.addEventListener("keyup", checkGameData);
|
||||||
|
|
||||||
const preview = document.getElementById("preview");
|
|
||||||
preview.style.display = "none";
|
preview.style.display = "none";
|
||||||
|
|
||||||
document.getElementById('image').addEventListener('change', function () {
|
document.getElementById('image').addEventListener('change', function () {
|
||||||
|
@ -102,7 +103,6 @@ async function run() {
|
||||||
output.value = add_tiles(gameData, image, prefix);
|
output.value = add_tiles(gameData, image, prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
const importButton = document.getElementById("import");
|
|
||||||
importButton.addEventListener("click", addTiles);
|
importButton.addEventListener("click", addTiles);
|
||||||
importButton.addEventListener("touchend", addTiles);
|
importButton.addEventListener("touchend", addTiles);
|
||||||
|
|
||||||
|
@ -111,8 +111,6 @@ async function run() {
|
||||||
output.value = "";
|
output.value = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
const addImageButton = document.getElementById("add");
|
|
||||||
|
|
||||||
addImageButton.addEventListener("click", addImage);
|
addImageButton.addEventListener("click", addImage);
|
||||||
addImageButton.addEventListener("touchend", addImage);
|
addImageButton.addEventListener("touchend", addImage);
|
||||||
|
|
||||||
|
@ -121,8 +119,6 @@ async function run() {
|
||||||
preview.removeAttribute("src");
|
preview.removeAttribute("src");
|
||||||
}
|
}
|
||||||
|
|
||||||
let resetButton = document.getElementById("reset");
|
|
||||||
|
|
||||||
resetButton.addEventListener("click", reset);
|
resetButton.addEventListener("click", reset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,11 +7,8 @@ use wasm_bindgen::prelude::*;
|
||||||
const SD: u32 = 8;
|
const SD: u32 = 8;
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn load_default_game_data() {
|
pub fn load_default_game_data() -> String {
|
||||||
let window = web_sys::window().expect("no global `window` exists");
|
bitsy_parser::mock::game_default().to_string()
|
||||||
let document = window.document().expect("should have a document on window");
|
|
||||||
let game_data_input = document.get_element_by_id("game-data").expect("no game data input");
|
|
||||||
game_data_input.set_inner_html(&bitsy_parser::mock::game_default().to_string());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tile_name(prefix: &str, index: &u32) -> Option<String> {
|
fn tile_name(prefix: &str, index: &u32) -> Option<String> {
|
||||||
|
|
Loading…
Reference in New Issue