nav.html
4.18 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Keyboard Navigation in Menu - jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="../../themes/icon.css">
<link rel="stylesheet" type="text/css" href="../demo.css">
<script type="text/javascript" src="../../jquery.min.js"></script>
<script type="text/javascript" src="../../jquery.easyui.min.js"></script>
</head>
<body>
<h2>Keyboard Navigation in Menu</h2>
<p>Press Alt+W to focus the menu. Once the menu get focus, you will be able to navigate menu using keyboard keys.</p>
<div style="margin:20px 0;"></div>
<div class="easyui-panel" title="Menu" style="width:150px;">
<div id="mm" data-options="inline:true" style="width:100%">
<div onclick="javascript:alert('new')">New</div>
<div>
<span>Open</span>
<div style="width:150px;">
<div><b>Word</b></div>
<div>Excel</div>
<div>PowerPoint</div>
<div>
<span>M1</span>
<div style="width:120px;">
<div>sub1</div>
<div>sub2</div>
<div>
<span>Sub</span>
<div style="width:80px;">
<div>sub21</div>
<div>sub22</div>
<div>sub23</div>
</div>
</div>
<div>sub3</div>
</div>
</div>
<div>
<span>Window Demos</span>
<div style="width:120px;">
<div>Window</div>
<div>Dialog</div>
<div>EasyUI</div>
</div>
</div>
</div>
</div>
<div data-options="iconCls:'icon-save'">Save</div>
<div data-options="iconCls:'icon-print',disabled:true">Print</div>
<div class="menu-sep"></div>
<div>Exit</div>
</div>
</div>
<script type="text/javascript">
(function($){
function getParentMenu(rootMenu, menu){
return findParent(rootMenu);
function findParent(pmenu){
var p = undefined;
$(pmenu).find('.menu-item').each(function(){
if (!p && this.submenu){
if ($(this.submenu)[0] == $(menu)[0]){
p = pmenu;
} else {
p = findParent(this.submenu);
}
}
});
return p;
}
}
function getParentItem(pmenu, menu){
var item = undefined;
$(pmenu).find('.menu-item').each(function(){
if ($(this.submenu)[0] == $(menu)[0]){
item = $(this);
return false;
}
});
return item;
}
$.extend($.fn.menu.methods, {
enableNav: function(jq, rootMenu){
var firstItemSelector = '.menu-item:not(.menu-item-disabled):first';
var lastItemSelector = '.menu-item:not(.menu-item-disabled):last';
return jq.each(function(){
var menu = $(this);
rootMenu = $(rootMenu).length ? $(rootMenu) : menu;
menu.attr('tabindex','0').css('outline','none').unbind('.menunav').bind('keydown.menunav', function(e){
var item = $(this).find('.menu-active');
switch(e.keyCode){
case 13: // enter
item.trigger('click');
break;
case 27: // esc
rootMenu.find('.menu-active').trigger('mouseleave');
break;
case 38: // up
var prev = item.length ? item.prevAll(firstItemSelector) : menu.find(lastItemSelector);
prev.trigger('mouseenter');
return false;
case 40: // down
var next = item.length ? item.nextAll(firstItemSelector) : menu.find(firstItemSelector);
next.trigger('mouseenter');
return false;
case 37: // left
var pmenu = getParentMenu(rootMenu, menu);
if (pmenu){
item.trigger('mouseleave');
var pitem = getParentItem(pmenu, menu);
if (pitem){
pitem.trigger('mouseenter');
}
pmenu.focus();
}
return false;
case 39: // right
if (item.length && item[0].submenu){
$(item[0].submenu).menu('enableNav', rootMenu).find(firstItemSelector).trigger('mouseenter');
$(item[0].submenu).focus();
}
return false;
}
});
});
}
});
})(jQuery);
$(function(){
$('#mm').menu().menu('enableNav');
$(document).keydown(function(e){
if (e.altKey && e.keyCode == 87){
$('#mm').focus();
}
})
})
</script>
</body>
</html>