Javascript puzzles: switch to a CSS-based drop-down system.

The previous control buttons and dropdowns based on form elements were
always a bit ugly: partly in a purely visual sense, and partly because
of the nasty bodge I had to do with splitting the usual 'Custom' game
type menu item into two (to get round the fact that if an element of a
<select> is already selected, browsers won't send an event when it's
re-selected). Also, I'm about to want to introduce hierarchical
submenus in the Type menu, and <select> doesn't support that at all.

So here's a replacement system which does everything by CSS
properties, including the popping-up of menus when the mouse moves
over their parent menu item. (Thanks to the Internet in general for
showing me how that trick is done.)
This commit is contained in:
Simon Tatham
2017-04-26 14:39:45 +01:00
parent ce6e3df99b
commit bc2c1f69fd
4 changed files with 179 additions and 86 deletions

View File

@ -45,7 +45,7 @@ mergeInto(LibraryManager.library, {
* provides neither presets nor configurability.
*/
js_remove_type_dropdown: function() {
document.getElementById("gametype").style.display = "none";
gametypelist.style.display = "none";
},
/*
@ -67,34 +67,35 @@ mergeInto(LibraryManager.library, {
* index back to the C code when a selection is made.)
*
* The special 'Custom' preset is requested by passing NULL to
* this function, rather than the string "Custom", since in that
* case we need to do something special - see below.
* this function.
*/
js_add_preset: function(ptr) {
var name = (ptr == 0 ? "Customise..." : Pointer_stringify(ptr));
var value = gametypeoptions.length;
var option = document.createElement("option");
option.value = value;
option.appendChild(document.createTextNode(name));
gametypeselector.appendChild(option);
gametypeoptions.push(option);
var name = (ptr == 0 ? "Custom" : Pointer_stringify(ptr));
var value = gametypeitems.length;
var item = document.createElement("li");
if (ptr == 0) {
// The option we've just created is the one for inventing
// a new custom setup.
gametypenewcustom = option;
option.value = -1;
gametypecustom = item;
value = -1;
}
// Now create another element called 'Custom', which will
// be auto-selected by us to indicate the custom settings
// you've previously selected. However, we don't add it to
// the game type selector; it will only appear when the
// user actually has custom settings selected.
option = document.createElement("option");
option.value = -2;
option.appendChild(document.createTextNode("Custom"));
gametypethiscustom = option;
item.setAttribute("data-index", value);
var tick = document.createElement("span");
tick.appendChild(document.createTextNode("\u2713"));
tick.style.color = "transparent";
tick.style.paddingRight = "0.5em";
item.appendChild(tick);
item.appendChild(document.createTextNode(name));
gametypelist.appendChild(item);
gametypeitems.push(item);
item.onclick = function(event) {
if (dlg_dimmer === null) {
gametypeselectedindex = value;
command(2);
}
}
},
@ -105,12 +106,7 @@ mergeInto(LibraryManager.library, {
* dropdown.
*/
js_get_selected_preset: function() {
for (var i in gametypeoptions) {
if (gametypeoptions[i].selected) {
return gametypeoptions[i].value;
}
}
return 0;
return gametypeselectedindex;
},
/*
@ -121,33 +117,15 @@ mergeInto(LibraryManager.library, {
* which turn out to exactly match a preset).
*/
js_select_preset: function(n) {
if (gametypethiscustom !== null) {
// Fiddle with the Custom/Customise options. If we're
// about to select the Custom option, then it should be in
// the menu, and the other one should read "Re-customise";
// if we're about to select another one, then the static
// Custom option should disappear and the other one should
// read "Customise".
if (gametypethiscustom.parentNode == gametypeselector)
gametypeselector.removeChild(gametypethiscustom);
if (gametypenewcustom.parentNode == gametypeselector)
gametypeselector.removeChild(gametypenewcustom);
if (n < 0) {
gametypeselector.appendChild(gametypethiscustom);
gametypenewcustom.lastChild.data = "Re-customise...";
gametypeselectedindex = n;
for (var i in gametypeitems) {
var item = gametypeitems[i];
var tick = item.firstChild;
if (item.getAttribute("data-index") == n) {
tick.style.color = "inherit";
} else {
gametypenewcustom.lastChild.data = "Customise...";
tick.style.color = "transparent";
}
gametypeselector.appendChild(gametypenewcustom);
gametypenewcustom.selected = false;
}
if (n < 0) {
gametypethiscustom.selected = true;
} else {
gametypeoptions[n].selected = true;
}
},
@ -192,8 +170,8 @@ mergeInto(LibraryManager.library, {
* after a move.
*/
js_enable_undo_redo: function(undo, redo) {
undo_button.disabled = (undo == 0);
redo_button.disabled = (redo == 0);
disable_menu_item(undo_button, (undo == 0));
disable_menu_item(redo_button, (redo == 0));
},
/*