2011-02-22 14:14:12 +00:00
|
|
|
/*
|
2011-02-22 15:15:11 +00:00
|
|
|
* jQuery UI menubar
|
2011-05-04 06:31:14 +00:00
|
|
|
*
|
|
|
|
* TODO move to jquery.ui.menubar.js
|
2011-02-22 14:14:12 +00:00
|
|
|
*/
|
|
|
|
(function($) {
|
|
|
|
|
2011-05-04 06:31:14 +00:00
|
|
|
// TODO code formatting
|
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-04-15 17:18:58 +00:00
|
|
|
var items = this.items = this.element.children("li")
|
|
|
|
.addClass("ui-menubar-item")
|
|
|
|
.attr("role", "presentation")
|
|
|
|
.children("button, a");
|
2011-03-17 14:10:25 +00:00
|
|
|
items.slice(1).attr("tabIndex", -1);
|
2011-02-24 21:00:32 +00:00
|
|
|
var o = this.options;
|
|
|
|
|
2011-04-15 17:18:58 +00:00
|
|
|
this.element.addClass('ui-menubar ui-widget-header ui-helper-clearfix').attr("role", "menubar");
|
2011-03-17 10:50:21 +00:00
|
|
|
this._focusable(items);
|
2011-03-17 10:51:38 +00:00
|
|
|
this._hoverable(items);
|
2011-05-04 06:31:14 +00:00
|
|
|
// TODO elm is used just once, so the each probably isn't nedded anymore
|
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) {
|
2011-04-15 17:18:58 +00:00
|
|
|
ui.item.parents("ul.ui-menu:last").hide();
|
|
|
|
self._trigger( "select", event, ui );
|
|
|
|
self._close();
|
2011-05-04 06:31:14 +00:00
|
|
|
// TODO what is this targetting? there's probably a better way to access it
|
2011-04-15 17:18:58 +00:00
|
|
|
$(event.target).prev().focus();
|
2011-02-24 15:39:08 +00:00
|
|
|
}
|
2011-04-15 17:18:58 +00:00
|
|
|
}).hide()
|
|
|
|
.attr("aria-hidden", "true")
|
|
|
|
.attr("aria-expanded", "false")
|
|
|
|
.keydown(function(event) {
|
2011-02-22 14:14:12 +00:00
|
|
|
var menu = $(this);
|
|
|
|
if (menu.is(":hidden"))
|
|
|
|
return;
|
|
|
|
switch (event.keyCode) {
|
|
|
|
case $.ui.keyCode.LEFT:
|
2011-03-17 14:10:25 +00:00
|
|
|
self._left(event);
|
2011-02-22 14:14:12 +00:00
|
|
|
event.preventDefault();
|
|
|
|
break;
|
|
|
|
case $.ui.keyCode.RIGHT:
|
2011-03-17 14:10:25 +00:00
|
|
|
self._right(event);
|
2011-02-22 14:14:12 +00:00
|
|
|
event.preventDefault();
|
|
|
|
break;
|
|
|
|
};
|
2011-04-22 18:22:16 +00:00
|
|
|
})
|
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) {
|
2011-03-17 14:26:06 +00:00
|
|
|
// ignore triggered focus event
|
|
|
|
if (event.type == "focus" && !event.originalEvent) {
|
|
|
|
return;
|
|
|
|
}
|
2011-02-28 21:53:06 +00:00
|
|
|
event.preventDefault();
|
2011-05-04 06:31:14 +00:00
|
|
|
// TODO can we simplify or extractthis check? especially the last two expressions
|
|
|
|
// there's a similar active[0] == menu[0] check in _open
|
2011-03-17 14:47:56 +00:00
|
|
|
if (event.type == "click" && menu.is(":visible") && self.active && self.active[0] == menu[0]) {
|
2011-02-28 21:53:06 +00:00
|
|
|
self._close();
|
|
|
|
return;
|
|
|
|
}
|
2011-04-15 17:18:58 +00:00
|
|
|
if ((self.open && event.type == "mouseenter") || event.type == "click") {
|
2011-02-24 21:00:32 +00:00
|
|
|
self._open(event, menu);
|
|
|
|
}
|
|
|
|
})
|
2011-03-17 11:04:22 +00:00
|
|
|
.bind( "keydown", function( event ) {
|
|
|
|
switch ( event.keyCode ) {
|
2011-03-17 14:10:25 +00:00
|
|
|
case $.ui.keyCode.SPACE:
|
2011-03-17 11:04:22 +00:00
|
|
|
case $.ui.keyCode.UP:
|
|
|
|
case $.ui.keyCode.DOWN:
|
|
|
|
self._open( event, $( this ).next() );
|
|
|
|
event.preventDefault();
|
|
|
|
break;
|
2011-03-17 14:10:25 +00:00
|
|
|
case $.ui.keyCode.LEFT:
|
|
|
|
self._prev( event, $( this ) );
|
|
|
|
event.preventDefault();
|
|
|
|
break;
|
|
|
|
case $.ui.keyCode.RIGHT:
|
|
|
|
self._next( event, $( this ) );
|
|
|
|
event.preventDefault();
|
|
|
|
break;
|
2011-03-17 11:04:22 +00:00
|
|
|
}
|
|
|
|
})
|
2011-02-28 21:49:49 +00:00
|
|
|
.addClass("ui-button ui-widget ui-button-text-only ui-menubar-link")
|
2011-04-15 17:18:58 +00:00
|
|
|
.attr("role", "menuitem")
|
|
|
|
.attr("aria-haspopup", "true")
|
2011-02-28 21:49:49 +00:00
|
|
|
.wrapInner("<span class='ui-button-text'></span>");
|
2011-05-04 06:31:14 +00:00
|
|
|
|
|
|
|
// TODO review if these options are a good choice, maybe they can be merged
|
2011-02-28 21:49:49 +00:00
|
|
|
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) {
|
2011-05-04 06:31:14 +00:00
|
|
|
// TODO ui-menubar-link is added above, not needed here?
|
2011-02-28 21:49:49 +00:00
|
|
|
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-03-17 11:00:37 +00:00
|
|
|
self._bind({
|
2011-04-20 18:15:26 +00:00
|
|
|
keydown: function(event) {
|
2011-05-04 06:31:14 +00:00
|
|
|
// TODO merge the two ifs
|
2011-04-22 18:22:16 +00:00
|
|
|
if (event.keyCode == $.ui.keyCode.ESCAPE) {
|
2011-04-22 21:11:53 +00:00
|
|
|
if (self.active && self.active.menu("left", event) !== true) {
|
2011-03-17 14:26:06 +00:00
|
|
|
var active = self.active;
|
|
|
|
self.active.blur();
|
2011-04-15 17:18:58 +00:00
|
|
|
self._close( event );
|
2011-03-17 14:26:06 +00:00
|
|
|
active.prev().focus();
|
2011-03-17 12:58:57 +00:00
|
|
|
}
|
2011-03-17 11:00:37 +00:00
|
|
|
}
|
2011-04-22 18:22:16 +00:00
|
|
|
},
|
|
|
|
focusout : function( event ) {
|
|
|
|
self.closeTimer = setTimeout(function() {
|
|
|
|
self._close( event );
|
|
|
|
}, 100);
|
|
|
|
},
|
2011-05-04 06:31:14 +00:00
|
|
|
// TODO change order, focusin first
|
2011-04-22 18:22:16 +00:00
|
|
|
focusin :function( event ) {
|
|
|
|
clearTimeout(self.closeTimer);
|
2011-03-17 11:00:37 +00:00
|
|
|
}
|
2011-02-22 14:14:12 +00:00
|
|
|
});
|
|
|
|
},
|
2011-04-15 18:31:43 +00:00
|
|
|
|
2011-04-15 17:18:58 +00:00
|
|
|
_destroy : function() {
|
|
|
|
var items = this.element.children("li")
|
|
|
|
.removeClass("ui-menubar-item")
|
|
|
|
.removeAttr("role", "presentation")
|
|
|
|
.children("button, a");
|
|
|
|
|
|
|
|
this.element.removeClass('ui-menubar ui-widget-header ui-helper-clearfix').removeAttr("role", "menubar").unbind(".menubar");;
|
|
|
|
items.unbind("focusin focusout click focus mouseenter keydown");
|
|
|
|
|
2011-04-15 18:31:43 +00:00
|
|
|
items
|
|
|
|
.removeClass("ui-button ui-widget ui-button-text-only ui-menubar-link ui-state-default")
|
2011-04-15 17:18:58 +00:00
|
|
|
.removeAttr("role", "menuitem")
|
|
|
|
.removeAttr("aria-haspopup", "true")
|
|
|
|
.children("span.ui-button-text").each(function(i, e) {
|
|
|
|
var item = $(this);
|
|
|
|
item.parent().html(item.html());
|
2011-04-15 18:31:43 +00:00
|
|
|
})
|
|
|
|
.end()
|
|
|
|
.children(".ui-icon").remove();
|
2011-04-15 17:18:58 +00:00
|
|
|
|
2011-04-15 18:31:43 +00:00
|
|
|
$(document).unbind(".menubar");
|
2011-04-15 17:18:58 +00:00
|
|
|
|
|
|
|
this.element.find(":ui-menu").menu("destroy")
|
|
|
|
.show()
|
|
|
|
.removeAttr("aria-hidden", "true")
|
|
|
|
.removeAttr("aria-expanded", "false")
|
|
|
|
.removeAttr("tabindex")
|
2011-05-04 06:31:14 +00:00
|
|
|
.unbind("keydown", "blur");
|
2011-04-15 17:18:58 +00:00
|
|
|
},
|
2011-02-22 14:14:12 +00:00
|
|
|
|
2011-02-24 21:57:44 +00:00
|
|
|
_close: function() {
|
2011-04-18 21:05:50 +00:00
|
|
|
if (!this.active || !this.active.length)
|
|
|
|
return;
|
2011-04-15 17:18:58 +00:00
|
|
|
this.active.menu("closeAll").hide().attr("aria-hidden", "true").attr("aria-expanded", "false");
|
2011-03-17 14:10:25 +00:00
|
|
|
this.active.prev().removeClass("ui-state-active").removeAttr("tabIndex");
|
|
|
|
this.active = null;
|
2011-04-23 15:31:25 +00:00
|
|
|
this.open = false;
|
2011-02-24 21:57:44 +00:00
|
|
|
},
|
|
|
|
|
2011-02-22 14:14:12 +00:00
|
|
|
_open: function(event, menu) {
|
2011-03-17 14:44:09 +00:00
|
|
|
// on a single-button menubar, ignore reopening the same menu
|
|
|
|
if (this.active && this.active[0] == menu[0]) {
|
|
|
|
return;
|
|
|
|
}
|
2011-03-17 14:37:27 +00:00
|
|
|
// almost the same as _close above, but don't remove tabIndex
|
2011-02-24 21:57:44 +00:00
|
|
|
if (this.active) {
|
2011-04-15 17:18:58 +00:00
|
|
|
this.active.menu("closeAll").hide().attr("aria-hidden", "true").attr("aria-expanded", "false");
|
2011-03-17 14:37:27 +00:00
|
|
|
this.active.prev().removeClass("ui-state-active");
|
2011-02-24 21:57:44 +00:00
|
|
|
}
|
2011-03-17 10:50:21 +00:00
|
|
|
// 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-04-15 17:18:58 +00:00
|
|
|
})
|
|
|
|
.removeAttr("aria-hidden").attr("aria-expanded", "true")
|
|
|
|
.menu("focus", event, menu.children("li").first())
|
2011-05-04 06:31:14 +00:00
|
|
|
// TODO need a comment here why both events are triggered
|
2011-04-22 18:22:16 +00:00
|
|
|
.focus()
|
2011-05-04 06:31:14 +00:00
|
|
|
.focusin();
|
2011-04-23 15:31:25 +00:00
|
|
|
this.open = true;
|
2011-02-22 14:14:12 +00:00
|
|
|
},
|
|
|
|
|
2011-05-04 06:31:14 +00:00
|
|
|
// TODO refactor this and the next three methods
|
2011-03-17 14:10:25 +00:00
|
|
|
_prev: function( event, button ) {
|
|
|
|
button.attr("tabIndex", -1);
|
2011-04-15 17:18:58 +00:00
|
|
|
var prev = button.parent().prevAll("li").children( ".ui-button" ).eq( 0 );
|
2011-03-17 14:10:25 +00:00
|
|
|
if (prev.length) {
|
|
|
|
prev.removeAttr("tabIndex")[0].focus();
|
|
|
|
} else {
|
2011-04-15 17:18:58 +00:00
|
|
|
var lastItem = this.element.children("li:last").children(".ui-button:last");
|
|
|
|
lastItem.removeAttr("tabIndex")[0].focus();
|
2011-03-17 14:10:25 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_next: function( event, button ) {
|
|
|
|
button.attr("tabIndex", -1);
|
2011-04-15 17:18:58 +00:00
|
|
|
var next = button.parent().nextAll("li").children( ".ui-button" ).eq( 0 );
|
2011-03-17 14:10:25 +00:00
|
|
|
if (next.length) {
|
|
|
|
next.removeAttr("tabIndex")[0].focus();
|
|
|
|
} else {
|
2011-04-15 17:18:58 +00:00
|
|
|
var firstItem = this.element.children("li:first").children(".ui-button:first");
|
|
|
|
firstItem.removeAttr("tabIndex")[0].focus();
|
2011-03-17 14:10:25 +00:00
|
|
|
}
|
|
|
|
},
|
2011-05-04 06:31:14 +00:00
|
|
|
|
|
|
|
// TODO rename to parent
|
2011-03-17 14:10:25 +00:00
|
|
|
_left: function(event) {
|
2011-04-15 17:18:58 +00:00
|
|
|
var prev = this.active.parent().prevAll("li:eq(0)").children( ".ui-menu" ).eq( 0 );
|
2011-02-22 14:14:12 +00:00
|
|
|
if (prev.length) {
|
|
|
|
this._open(event, prev);
|
|
|
|
} else {
|
2011-04-15 17:18:58 +00:00
|
|
|
var lastItem = this.element.children("li:last").children(".ui-menu:first");
|
|
|
|
this._open(event, lastItem);
|
2011-02-22 14:14:12 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-05-04 06:31:14 +00:00
|
|
|
// TODO rename to child (or something like that)
|
2011-03-17 14:10:25 +00:00
|
|
|
_right: function(event) {
|
2011-04-15 17:18:58 +00:00
|
|
|
var next = this.active.parent().nextAll("li:eq(0)").children( ".ui-menu" ).eq( 0 );
|
2011-02-22 14:14:12 +00:00
|
|
|
if (next.length) {
|
|
|
|
this._open(event, next);
|
|
|
|
} else {
|
2011-04-15 17:18:58 +00:00
|
|
|
var firstItem = this.element.children("li:first").children(".ui-menu:first");
|
|
|
|
this._open(event, firstItem);
|
2011-02-22 14:14:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}(jQuery));
|