Skip to content

Commit d654be9

Browse files
committed
Menubar: Add support for non-menu items, adding a 'About' button to each menubar in the demo
1 parent 03a981e commit d654be9

File tree

2 files changed

+54
-48
lines changed

2 files changed

+54
-48
lines changed

demos/menubar/default.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555

5656
<div class="demo">
5757
<ul id="bar1" class="menubar">
58+
<li><a href="#About">About</a></li>
5859
<li>
5960
<a href="#File">File</a>
6061
<ul>
@@ -101,6 +102,7 @@
101102
</ul>
102103

103104
<ul id="bar2" class="menubar-icons">
105+
<li><a href="#About">About</a></li>
104106
<li>
105107
<a href="#File">File</a>
106108
<ul>
@@ -147,6 +149,7 @@
147149
</ul>
148150

149151
<div id="bar3" class="menubar">
152+
<div class="menubarItem"><a href="#About">About</a></div>
150153
<div class="menubarItem">
151154
<a href="#File">File</a>
152155
<div class="menuElement">

ui/jquery.ui.menubar.js

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -86,60 +86,63 @@ $.widget( "ui.menubar", {
8686
// TODO menu var is only used on two places, doesn't quite justify the .each
8787
menu = input.next( that.options.menuElement );
8888

89-
input.bind( "click.menubar focus.menubar mouseenter.menubar", function( event ) {
90-
// ignore triggered focus event
91-
if ( event.type === "focus" && !event.originalEvent ) {
92-
return;
93-
}
94-
event.preventDefault();
95-
// TODO can we simplify or extractthis check? especially the last two expressions
96-
// there's a similar active[0] == menu[0] check in _open
97-
if ( event.type === "click" && menu.is( ":visible" ) && that.active && that.active[0] === menu[0] ) {
98-
that._close();
99-
return;
100-
}
101-
if ( ( that.open && event.type === "mouseenter" ) || event.type === "click" || that.options.autoExpand ) {
102-
if( that.options.autoExpand ) {
103-
clearTimeout( that.closeTimer );
89+
// might be a non-menu button
90+
if ( menu.length ) {
91+
input.bind( "click.menubar focus.menubar mouseenter.menubar", function( event ) {
92+
// ignore triggered focus event
93+
if ( event.type === "focus" && !event.originalEvent ) {
94+
return;
10495
}
105-
106-
that._open( event, menu );
107-
}
108-
})
109-
.bind( "keydown", function( event ) {
110-
switch ( event.keyCode ) {
111-
case $.ui.keyCode.SPACE:
112-
case $.ui.keyCode.UP:
113-
case $.ui.keyCode.DOWN:
114-
that._open( event, $( this ).next() );
11596
event.preventDefault();
116-
break;
117-
case $.ui.keyCode.LEFT:
118-
that.previous( event );
119-
event.preventDefault();
120-
break;
121-
case $.ui.keyCode.RIGHT:
122-
that.next( event );
123-
event.preventDefault();
124-
break;
125-
}
126-
})
127-
.addClass( "ui-button ui-widget ui-button-text-only ui-menubar-link" )
128-
.attr( "role", "menuitem" )
129-
.attr( "aria-haspopup", "true" )
130-
.wrapInner( "<span class='ui-button-text'></span>" );
97+
// TODO can we simplify or extractthis check? especially the last two expressions
98+
// there's a similar active[0] == menu[0] check in _open
99+
if ( event.type === "click" && menu.is( ":visible" ) && that.active && that.active[0] === menu[0] ) {
100+
that._close();
101+
return;
102+
}
103+
if ( ( that.open && event.type === "mouseenter" ) || event.type === "click" || that.options.autoExpand ) {
104+
if( that.options.autoExpand ) {
105+
clearTimeout( that.closeTimer );
106+
}
131107

132-
// TODO review if these options are a good choice, maybe they can be merged
133-
if ( that.options.menuIcon ) {
134-
input.addClass( "ui-state-default" ).append( "<span class='ui-button-icon-secondary ui-icon ui-icon-triangle-1-s'></span>" );
135-
input.removeClass( "ui-button-text-only" ).addClass( "ui-button-text-icon-secondary" );
136-
}
108+
that._open( event, menu );
109+
}
110+
})
111+
.bind( "keydown", function( event ) {
112+
switch ( event.keyCode ) {
113+
case $.ui.keyCode.SPACE:
114+
case $.ui.keyCode.UP:
115+
case $.ui.keyCode.DOWN:
116+
that._open( event, $( this ).next() );
117+
event.preventDefault();
118+
break;
119+
case $.ui.keyCode.LEFT:
120+
that.previous( event );
121+
event.preventDefault();
122+
break;
123+
case $.ui.keyCode.RIGHT:
124+
that.next( event );
125+
event.preventDefault();
126+
break;
127+
}
128+
})
129+
.attr( "aria-haspopup", "true" );
137130

138-
if ( !that.options.buttons ) {
139-
// TODO ui-menubar-link is added above, not needed here?
140-
input.addClass( "ui-menubar-link" ).removeClass( "ui-state-default" );
131+
// TODO review if these options (menuIcon and buttons) are a good choice, maybe they can be merged
132+
if ( that.options.menuIcon ) {
133+
input.addClass( "ui-state-default" ).append( "<span class='ui-button-icon-secondary ui-icon ui-icon-triangle-1-s'></span>" );
134+
input.removeClass( "ui-button-text-only" ).addClass( "ui-button-text-icon-secondary" );
135+
}
141136
}
142137

138+
input
139+
.addClass( "ui-button ui-widget ui-button-text-only ui-menubar-link" )
140+
.attr( "role", "menuitem" )
141+
.wrapInner( "<span class='ui-button-text'></span>" );
142+
143+
if ( that.options.buttons ) {
144+
input.removeClass( "ui-menubar-link" ).addClass( "ui-state-default" );
145+
}
143146
});
144147
that._on( {
145148
keydown: function( event ) {

0 commit comments

Comments
 (0)