js: Tolerate the absence of various UI elements from the HTML

To make things more consistent, the static buttons now all have their
own variables.
This commit is contained in:
Ben Harris
2022-12-11 12:17:54 +00:00
parent 48ded126a9
commit 5ba1bf5560
2 changed files with 24 additions and 15 deletions

View File

@ -62,9 +62,11 @@ mergeInto(LibraryManager.library, {
* provides neither presets nor configurability. * provides neither presets nor configurability.
*/ */
js_remove_type_dropdown: function() { js_remove_type_dropdown: function() {
if (gametypelist === null) return;
var gametypeitem = gametypelist.closest("li"); var gametypeitem = gametypelist.closest("li");
if (gametypeitem === null) return; if (gametypeitem === null) return;
gametypeitem.parentNode.removeChild(gametypeitem); gametypeitem.parentNode.removeChild(gametypeitem);
gametypelist = null;
}, },
/* /*
@ -74,11 +76,11 @@ mergeInto(LibraryManager.library, {
* time if the game doesn't support an in-game solve function. * time if the game doesn't support an in-game solve function.
*/ */
js_remove_solve_button: function() { js_remove_solve_button: function() {
var solvebutton = document.getElementById("solve"); if (solve_button === null) return;
if (solvebutton === null) return; var solve_item = solve_button.closest("li");
var solveitem = solvebutton.closest("li"); if (solve_item === null) return;
if (solveitem === null) return; solve_item.parentNode.removeChild(solve_item);
solveitem.parentNode.removeChild(solveitem); solve_button = null;
}, },
/* /*

View File

@ -146,9 +146,16 @@ var menuform = document.getElementById("gamemenu");
var permalink_seed = document.getElementById("permalink-seed"); var permalink_seed = document.getElementById("permalink-seed");
var permalink_desc = document.getElementById("permalink-desc"); var permalink_desc = document.getElementById("permalink-desc");
// The undo and redo buttons. Used by js_enable_undo_redo(). // The various buttons. Undo and redo are used by js_enable_undo_redo().
var specific_button = document.getElementById("specific");
var random_button = document.getElementById("random");
var new_button = document.getElementById("new");
var restart_button = document.getElementById("restart");
var undo_button = document.getElementById("undo"); var undo_button = document.getElementById("undo");
var redo_button = document.getElementById("redo"); var redo_button = document.getElementById("redo");
var solve_button = document.getElementById("solve");
var save_button = document.getElementById("save");
var load_button = document.getElementById("load");
// A div element enclosing both the puzzle and its status bar, used // A div element enclosing both the puzzle and its status bar, used
// for positioning the resize handle. // for positioning the resize handle.
@ -359,34 +366,34 @@ function initPuzzle() {
command = Module.cwrap('command', 'void', ['number']); command = Module.cwrap('command', 'void', ['number']);
// Event handlers for buttons and things, which call command(). // Event handlers for buttons and things, which call command().
document.getElementById("specific").onclick = function(event) { if (specific_button) specific_button.onclick = function(event) {
// Ensure we don't accidentally process these events when a // Ensure we don't accidentally process these events when a
// dialog is actually active, e.g. because the button still // dialog is actually active, e.g. because the button still
// has keyboard focus // has keyboard focus
if (dlg_dimmer === null) if (dlg_dimmer === null)
command(0); command(0);
}; };
document.getElementById("random").onclick = function(event) { if (random_button) random_button.onclick = function(event) {
if (dlg_dimmer === null) if (dlg_dimmer === null)
command(1); command(1);
}; };
document.getElementById("new").onclick = function(event) { if (new_button) new_button.onclick = function(event) {
if (dlg_dimmer === null) if (dlg_dimmer === null)
command(5); command(5);
}; };
document.getElementById("restart").onclick = function(event) { if (restart_button) restart_button.onclick = function(event) {
if (dlg_dimmer === null) if (dlg_dimmer === null)
command(6); command(6);
}; };
undo_button.onclick = function(event) { if (undo_button) undo_button.onclick = function(event) {
if (dlg_dimmer === null) if (dlg_dimmer === null)
command(7); command(7);
}; };
redo_button.onclick = function(event) { if (redo_button) redo_button.onclick = function(event) {
if (dlg_dimmer === null) if (dlg_dimmer === null)
command(8); command(8);
}; };
document.getElementById("solve").onclick = function(event) { if (solve_button) solve_button.onclick = function(event) {
if (dlg_dimmer === null) if (dlg_dimmer === null)
command(9); command(9);
}; };
@ -396,7 +403,7 @@ function initPuzzle() {
var free_save_file = Module.cwrap('free_save_file', 'void', ['number']); var free_save_file = Module.cwrap('free_save_file', 'void', ['number']);
var load_game = Module.cwrap('load_game', 'void', ['string', 'number']); var load_game = Module.cwrap('load_game', 'void', ['string', 'number']);
document.getElementById("save").onclick = function(event) { if (save_button) save_button.onclick = function(event) {
if (dlg_dimmer === null) { if (dlg_dimmer === null) {
var savefile_ptr = get_save_file(); var savefile_ptr = get_save_file();
var savefile_text = UTF8ToString(savefile_ptr); var savefile_text = UTF8ToString(savefile_ptr);
@ -418,7 +425,7 @@ function initPuzzle() {
} }
}; };
document.getElementById("load").onclick = function(event) { if (load_button) load_button.onclick = function(event) {
if (dlg_dimmer === null) { if (dlg_dimmer === null) {
var input = document.createElement("input"); var input = document.createElement("input");
input.type = "file"; input.type = "file";