I am using a menu widget that works with jQueryUI and am trying to configure it to hide when the user clicks off the menu. The problem is that the script changes the active layer on the fly (it overlays the main menu panel with sub-menus) and I can't pin down what element to hide.
Here is the abbreviated script (I'd also like to restore the menu to its initial state when hiding, so I've included relevant code for that too):
$(function($) {
$.widget("ui.drilldown", {
_init: function() {
var self = this;
this.active = this.element.find(">ul").attr("tabindex", 0);
// hide submenus and create indicator icons
this.element.find("ul").hide().prev("a").prepend('<html>').end().filter(":first").show();
....
// this code defines the "back" button which returns to previous menu levels
this.back = this.element.children(":last").button()
.click(function() {
self.up();
return false;
}).hide();
},
....
up: function() {
....
// active layer defs
this.active = this.active.parent().parent().show();
this.activeItem = this.active.data("menu").active;
if (!this.active.parent().parent().is(":ui-menu")) {
this.back.hide();
}
},
down: function(event) {
var nested = this.activeItem.find(">ul");
if (nested.length) {
this._open(nested);
nested.menu("activate", event, nested.children(":first"))
}
},
....
hide: function() {
},
widget: function() {
return this.element.find(">ul");
}
});
....
//* My code added to show menu on button click
$(".sitemenu button").button()
.click(function() {
$("#drilldown").show();
});
....
});
});
I adapted the working code from a different config, which is this:
$(document).one("click", function() {
$("#drilldown").hide();
});
But this hides the menu even when I click on the menu. I also tried using focusout:
$("#drilldown").focusout(function() {
$("#drilldown").hide();
});
This hides the menu after I've clicked it once, then click it again. I've tried using other selectors -- .sitemenu, #drilldown, ul (the menu is an unordered list), and various combinations -- but they all do the same thing.
Any ideas? I'll post the entire script if anyone wants.