Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wmlunits: New pop-up menu implementation
The new code has a smaller markup fingerprint (no more inline event Javascript attributes) and it also ports the outer-click-dismiss functionality over from the wiki. It raises the Javascript engine requirements to IE 9 and later, though, which is inline with the home page code.
- Loading branch information
Showing
2 changed files
with
79 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,80 @@ | ||
var all = new Array(); | ||
|
||
function toggle_menu(event, id, what) { | ||
var e = document.getElementById(id); | ||
var show = what; | ||
|
||
/* Since we have Javascript, disable the CSS menu trigger. */ | ||
if (event.className != "") { | ||
event.className = ""; | ||
e.style.display = 'none'; | ||
return; | ||
} | ||
|
||
if (show == 0 || show == 1) return; | ||
|
||
if (show == 2) { | ||
if (e.style.display == 'block') show = 0; | ||
} | ||
if (show == 0) { | ||
e.style.display = 'none'; | ||
/* | ||
* Popup menu implementation for the Wesnoth units database | ||
*/ | ||
(function() { | ||
var menus = []; | ||
var visibleMenu = false; | ||
|
||
function toggleMenu(event, menu) | ||
{ | ||
hideMenus(menu); | ||
|
||
if (!menu.style.display || menu.style.display === "none") { | ||
menu.style.display = "block"; | ||
visibleMenu = true; | ||
} else { | ||
menu.style.display = "none"; | ||
visibleMenu = false; | ||
} | ||
} | ||
else { | ||
e.style.display = 'block'; | ||
all[id] = 1; | ||
for (mid in all) { | ||
if (mid != id) { | ||
e = document.getElementById(mid); | ||
e.style.display = 'none'; | ||
|
||
function hideMenus(skipMenu) | ||
{ | ||
if (!visibleMenu) { | ||
return; | ||
} | ||
|
||
for (var i = 0; i < menus.length; ++i) { | ||
if (menus[i] !== skipMenu) { | ||
menus[i].style.display = "none"; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function isMenuBox(e) | ||
{ | ||
return e.className.search(/\bpopupmenu\b/) != -1; | ||
} | ||
|
||
function isNavBar(e) | ||
{ | ||
return e.className.search(/\bnavbar\b/) != -1; | ||
} | ||
|
||
window.addEventListener("load", function() { | ||
var navItems = document.getElementsByClassName("popupcontainer"); | ||
|
||
// Set event handlers for individual menu triggers. | ||
for (var i = 0; i < navItems.length; ++i) { | ||
var navItem = navItems[i], | ||
menu = navItem.getElementsByClassName("popupmenu")[0]; | ||
|
||
menus.push(menu); | ||
|
||
var id = menu.id, | ||
a = navItem.getElementsByClassName("popuptrigger")[0]; | ||
|
||
a.addEventListener("click", function(event) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
toggleMenu(event, event.target.nextElementSibling); | ||
}, false); | ||
} | ||
|
||
// Dismiss all visible menus on click outside them. | ||
document.addEventListener("click", function(event) { | ||
if (!visibleMenu) { | ||
return; | ||
} | ||
|
||
var parent = event.target; | ||
while(parent && !isMenuBox(parent) && !isNavBar(parent)) { | ||
parent = parent.parentElement; | ||
} | ||
|
||
if (!parent || !isMenuBox(parent)) { | ||
hideMenus(); | ||
} | ||
}, false); | ||
}, false); | ||
})(); |