-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.menubar.js
116 lines (105 loc) · 3.08 KB
/
jquery.menubar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* this is the custom menu widget extension to remap the key presses for the top level items only */
var menubarMenu = $.widget("ui.menubarMenu", $.ui.menu, {
_keydown: function (event) {
var focusedElement = this.element.find(".ui-state-focus").get(0);
if (typeof focusedElement !== "undefined") {
if (this.element.get(0) === $(focusedElement).parent().get(0)) {
switch (event.keyCode) {
case $.ui.keyCode.UP:
event.keyCode = $.ui.keyCode.LEFT;
break;
case $.ui.keyCode.DOWN:
event.keyCode = $.ui.keyCode.RIGHT;
break;
case $.ui.keyCode.LEFT:
event.keyCode = $.ui.keyCode.UP;
break;
case $.ui.keyCode.RIGHT:
event.keyCode = $.ui.keyCode.DOWN;
break;
}
}
}
return this._super(event);
},
_isDivider: function (item) {
if ($(item).text() === "-") {
return true;
}
return false;
}
});
/* this is the new menubar widget */
var menubar = $.widget("ui.menubar", {
version: "1.11.4",
defaultElement: "<ul>",
delay: 300,
options: {
icons: {
submenuDown: "ui-icon-carat-1-s",
submenuRight: "ui-icon-carat-1-e"
},
items: "> *",
menus: "ul",
downPosition: {
my: "left top",
at: "left bottom"
},
rightPosition: {
my: "left top",
at: "right top"
},
role: "menubar",
submenuRole: "menu",
// callbacks
blur: null,
focus: null,
select: null
},
_create: function () {
var menubar = this;
menubar.element.menubarMenu({
icons: {
submenu: menubar.options.submenuRight
},
items: menubar.options.items,
menus: menubar.options.menus,
position: menubar.options.downPosition,
role: menubar.options.submenuRole,
blur: menubar.options.blur,
/* this bit of magic positions the submenus properly */
focus: function (event, item) {
if (menubar.element.get(0) === $(item).get(0).item.parent().get(0)) {
$(this).menubarMenu("option", "position", menubar.options.downPosition);
} else {
$(this).menubarMenu("option", "position", menubar.options.rightPosition);
}
menubar._trigger("focus", event, item);
},
/* this bit of magic makes anchor elements work when the menuitem is selected */
select: function (event, ui) {
if (ui.item.menubarItemSelected) {
event.stopPropagation();
} else {
ui.item.menubarItemSelected = true;
var a = ui.item.children("a").get(0);
if (typeof a !== "undefined") {
a.click();
}
menubar._trigger("select", event, ui);
ui.item.menubarItemSelected = false;
}
}
});
/* add the ui-menubar class */
menubar.element.addClass("ui-menubar");
/* fix the role of the top level menu */
menubar.element.attr("role", menubar.options.role);
/* change the icons of the top level menu items */
menubar.element.children(".ui-menu-item").children("." + menubar.options.icons.submenuRight).each(function () {
$(this).removeClass(menubar.options.icons.submenuRight).addClass(menubar.options.icons.submenuDown);
});
/* don't let anchors inside the menubar get keyboard focus - the focus should stay on the li */
menubar.element.find("a").prop("tabindex", -1);
}
});