shithub: puzzles

Download patch

ref: 5ba1bf55600443ca6cdad448aa629ffbce7c4e22
parent: 48ded126a9fd399cf2d9c66ee928f4d1d6d065e4
author: Ben Harris <bjh21@bjh21.me.uk>
date: Sun Dec 11 07:17:54 EST 2022

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.

--- a/emcclib.js
+++ b/emcclib.js
@@ -62,9 +62,11 @@
      * provides neither presets nor configurability.
      */
     js_remove_type_dropdown: function() {
+        if (gametypelist === null) return;
         var gametypeitem = gametypelist.closest("li");
         if (gametypeitem === null) return;
         gametypeitem.parentNode.removeChild(gametypeitem);
+        gametypelist = null;
     },
 
     /*
@@ -74,11 +76,11 @@
      * time if the game doesn't support an in-game solve function.
      */
     js_remove_solve_button: function() {
-        var solvebutton = document.getElementById("solve");
-        if (solvebutton === null) return;
-        var solveitem = solvebutton.closest("li");
-        if (solveitem === null) return;
-        solveitem.parentNode.removeChild(solveitem);
+        if (solve_button === null) return;
+        var solve_item = solve_button.closest("li");
+        if (solve_item === null) return;
+        solve_item.parentNode.removeChild(solve_item);
+        solve_button = null;
     },
 
     /*
--- a/emccpre.js
+++ b/emccpre.js
@@ -146,9 +146,16 @@
 var permalink_seed = document.getElementById("permalink-seed");
 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 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
 // for positioning the resize handle.
@@ -359,7 +366,7 @@
     command = Module.cwrap('command', 'void', ['number']);
 
     // 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
         // dialog is actually active, e.g. because the button still
         // has keyboard focus
@@ -366,27 +373,27 @@
         if (dlg_dimmer === null)
             command(0);
     };
-    document.getElementById("random").onclick = function(event) {
+    if (random_button) random_button.onclick = function(event) {
         if (dlg_dimmer === null)
             command(1);
     };
-    document.getElementById("new").onclick = function(event) {
+    if (new_button) new_button.onclick = function(event) {
         if (dlg_dimmer === null)
             command(5);
     };
-    document.getElementById("restart").onclick = function(event) {
+    if (restart_button) restart_button.onclick = function(event) {
         if (dlg_dimmer === null)
             command(6);
     };
-    undo_button.onclick = function(event) {
+    if (undo_button) undo_button.onclick = function(event) {
         if (dlg_dimmer === null)
             command(7);
     };
-    redo_button.onclick = function(event) {
+    if (redo_button) redo_button.onclick = function(event) {
         if (dlg_dimmer === null)
             command(8);
     };
-    document.getElementById("solve").onclick = function(event) {
+    if (solve_button) solve_button.onclick = function(event) {
         if (dlg_dimmer === null)
             command(9);
     };
@@ -396,7 +403,7 @@
     var free_save_file = Module.cwrap('free_save_file', 'void', ['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) {
             var savefile_ptr = get_save_file();
             var savefile_text = UTF8ToString(savefile_ptr);
@@ -418,7 +425,7 @@
         }
     };
 
-    document.getElementById("load").onclick = function(event) {
+    if (load_button) load_button.onclick = function(event) {
         if (dlg_dimmer === null) {
             var input = document.createElement("input");
             input.type = "file";