2011-02-22 14:14:12 +00:00
|
|
|
/*
|
2011-02-22 15:15:11 +00:00
|
|
|
* jQuery UI menubar
|
2011-02-22 14:14:12 +00:00
|
|
|
*
|
2011-02-22 15:15:11 +00:00
|
|
|
* backported from Michael Lang's fork: http://www.nexul.com/prototypes/toolbar/demo.html
|
2011-02-22 14:14:12 +00:00
|
|
|
*/
|
|
|
|
(function($) {
|
|
|
|
|
2011-02-24 15:39:08 +00:00
|
|
|
// TODO take non-menubar buttons into account
|
2011-02-22 14:14:12 +00:00
|
|
|
$.widget("ui.menubar", {
|
2011-02-24 21:00:32 +00:00
|
|
|
options: {
|
|
|
|
buttons: false,
|
|
|
|
menuIcon: false
|
|
|
|
},
|
2011-02-22 14:14:12 +00:00
|
|
|
_create: function() {
|
|
|
|
var self = this;
|
2011-02-24 21:57:44 +00:00
|
|
|
var items = this.items = this.element.children("button, a");
|
2011-02-24 21:00:32 +00:00
|
|
|
var o = this.options;
|
|
|
|
|
2011-02-24 16:06:12 +00:00
|
|
|
this.element.addClass('ui-menubar ui-widget-header ui-helper-clearfix');
|
2011-03-17 10:50:21 +00:00
|
|
|
this._focusable(items);
|
2011-02-22 14:14:12 +00:00
|
|
|
items.next("ul").each(function(i, elm) {
|
2011-02-24 15:39:08 +00:00
|
|
|
$(elm).menu({
|
|
|
|
select: function(event, ui) {
|
|
|
|
ui.item.parents("ul:last").hide()
|
|
|
|
self.options.select.apply(this, arguments);
|
|
|
|
}
|
|
|
|
}).hide().keydown(function(event) {
|
2011-02-22 14:14:12 +00:00
|
|
|
var menu = $(this);
|
|
|
|
if (menu.is(":hidden"))
|
|
|
|
return;
|
|
|
|
event.stopPropagation();
|
|
|
|
switch (event.keyCode) {
|
|
|
|
case $.ui.keyCode.LEFT:
|
|
|
|
self.left(event);
|
|
|
|
event.preventDefault();
|
|
|
|
break;
|
|
|
|
case $.ui.keyCode.RIGHT:
|
|
|
|
self.right(event);
|
|
|
|
event.preventDefault();
|
|
|
|
break;
|
|
|
|
};
|
2011-03-17 10:50:21 +00:00
|
|
|
}).blur(function() {
|
|
|
|
$(this).hide().prev().removeClass("ui-state-active").removeAttr("tabIndex");
|
|
|
|
self.timer = setTimeout(function() {
|
|
|
|
self.open = false;
|
|
|
|
}, 13);
|
2011-02-22 14:14:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
items.each(function() {
|
|
|
|
var input = $(this),
|
2011-02-24 21:00:32 +00:00
|
|
|
menu = input.next("ul");
|
|
|
|
|
2011-02-28 21:53:06 +00:00
|
|
|
input.bind("click focus mouseenter", function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
if (menu.is(":visible") && self.active && self.active[0] == menu[0]) {
|
|
|
|
self._close();
|
|
|
|
return;
|
|
|
|
}
|
2011-03-17 10:50:21 +00:00
|
|
|
if (menu.length && (self.open || event.type == "click")) {
|
2011-02-24 21:00:32 +00:00
|
|
|
self._open(event, menu);
|
|
|
|
}
|
|
|
|
})
|
2011-02-28 21:49:49 +00:00
|
|
|
.addClass("ui-button ui-widget ui-button-text-only ui-menubar-link")
|
|
|
|
.wrapInner("<span class='ui-button-text'></span>");
|
|
|
|
self._hoverable(input)
|
|
|
|
|
|
|
|
if (o.menuIcon) {
|
|
|
|
input.addClass("ui-state-default").append("<span class='ui-button-icon-secondary ui-icon ui-icon-triangle-1-s'></span>");
|
|
|
|
input.removeClass("ui-button-text-only").addClass("ui-button-text-icon-secondary");
|
|
|
|
}
|
2011-02-24 21:00:32 +00:00
|
|
|
|
2011-02-28 21:49:49 +00:00
|
|
|
if (!o.buttons) {
|
|
|
|
input.addClass('ui-menubar-link').removeClass('ui-state-default');
|
|
|
|
};
|
2011-02-24 21:00:32 +00:00
|
|
|
|
2011-02-22 14:14:12 +00:00
|
|
|
});
|
2011-02-24 16:16:15 +00:00
|
|
|
$(document).click(function(event) {
|
2011-02-24 21:57:44 +00:00
|
|
|
!$(event.target).closest(".ui-menubar").length && self._close();
|
2011-02-22 14:14:12 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2011-02-24 21:57:44 +00:00
|
|
|
_close: function() {
|
|
|
|
this.items.next("ul").hide();
|
2011-03-17 10:50:21 +00:00
|
|
|
this.items.removeClass("ui-state-active").removeAttr("tabIndex");
|
|
|
|
this.open = false;
|
2011-02-24 21:57:44 +00:00
|
|
|
},
|
|
|
|
|
2011-02-22 14:14:12 +00:00
|
|
|
_open: function(event, menu) {
|
2011-02-24 21:57:44 +00:00
|
|
|
if (this.active) {
|
|
|
|
this.active.menu("closeAll").hide();
|
2011-03-17 10:50:21 +00:00
|
|
|
this.active.prev().removeClass("ui-state-active").removeAttr("tabIndex");
|
2011-02-24 21:57:44 +00:00
|
|
|
}
|
2011-03-17 10:50:21 +00:00
|
|
|
clearTimeout(this.timer);
|
|
|
|
this.open = true;
|
|
|
|
// set tabIndex -1 to have the button skipped on shift-tab when menu is open (it gets focus)
|
|
|
|
var button = menu.prev().addClass("ui-state-active").attr("tabIndex", -1);
|
2011-02-24 15:39:08 +00:00
|
|
|
this.active = menu.show().position({
|
2011-02-22 14:14:12 +00:00
|
|
|
my: "left top",
|
|
|
|
at: "left bottom",
|
2011-02-24 21:57:44 +00:00
|
|
|
of: button
|
2011-02-22 14:14:12 +00:00
|
|
|
}).focus();
|
|
|
|
},
|
|
|
|
|
|
|
|
left: function(event) {
|
2011-02-24 15:39:08 +00:00
|
|
|
var prev = this.active.prevAll( ".ui-menu" ).eq( 0 );
|
2011-02-22 14:14:12 +00:00
|
|
|
if (prev.length) {
|
|
|
|
this._open(event, prev);
|
|
|
|
} else {
|
2011-02-24 15:39:08 +00:00
|
|
|
this._open(event, this.element.children(".ui-menu:last"));
|
2011-02-22 14:14:12 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
right: function(event) {
|
2011-02-24 15:39:08 +00:00
|
|
|
var next = this.active.nextAll( ".ui-menu" ).eq( 0 );
|
2011-02-22 14:14:12 +00:00
|
|
|
if (next.length) {
|
|
|
|
this._open(event, next);
|
|
|
|
} else {
|
2011-02-24 15:39:08 +00:00
|
|
|
this._open(event, this.element.children(".ui-menu:first"));
|
2011-02-22 14:14:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}(jQuery));
|