2011-02-22 14:14:12 +00:00
|
|
|
/*
|
2011-05-07 14:22:04 +00:00
|
|
|
* jQuery UI Menubar @VERSION
|
|
|
|
*
|
|
|
|
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
|
|
|
|
* Dual licensed under the MIT or GPL Version 2 licenses.
|
|
|
|
* http://jquery.org/license
|
|
|
|
*
|
|
|
|
* http://docs.jquery.com/UI/Menubar
|
|
|
|
*
|
|
|
|
* Depends:
|
|
|
|
* jquery.ui.core.js
|
|
|
|
* jquery.ui.widget.js
|
|
|
|
* jquery.ui.position.js
|
|
|
|
* jquery.ui.menu.js
|
2011-02-22 14:14:12 +00:00
|
|
|
*/
|
2011-05-04 09:50:07 +00:00
|
|
|
(function( $ ) {
|
2011-02-22 14:14:12 +00:00
|
|
|
|
2011-05-04 09:50:07 +00:00
|
|
|
// TODO when mixing clicking menus and keyboard navigation, focus handling is broken
|
|
|
|
// there has to be just one item that has tabindex
|
|
|
|
$.widget( "ui.menubar", {
|
2011-05-28 19:39:55 +00:00
|
|
|
version: "@VERSION",
|
2011-05-28 19:42:28 +00:00
|
|
|
options: {
|
2011-07-07 14:29:50 +00:00
|
|
|
autoExpand: false,
|
2011-05-28 19:42:28 +00:00
|
|
|
buttons: false,
|
2011-10-19 18:59:07 +00:00
|
|
|
items: "li",
|
|
|
|
menuElement: "ul",
|
2011-06-09 15:14:21 +00:00
|
|
|
menuIcon: false,
|
|
|
|
position: {
|
|
|
|
my: "left top",
|
|
|
|
at: "left bottom"
|
|
|
|
}
|
2011-05-28 19:42:28 +00:00
|
|
|
},
|
2011-02-22 14:14:12 +00:00
|
|
|
_create: function() {
|
2011-05-04 09:50:07 +00:00
|
|
|
var that = this;
|
2011-10-27 03:54:12 +00:00
|
|
|
this.menuItems = this.element.children( this.options.items );
|
|
|
|
this.items = this.menuItems.children( "button, a" );
|
|
|
|
|
|
|
|
this.menuItems
|
2011-05-04 09:50:07 +00:00
|
|
|
.addClass( "ui-menubar-item" )
|
2011-10-27 03:54:12 +00:00
|
|
|
.attr( "role", "presentation" );
|
2011-05-04 09:50:07 +00:00
|
|
|
// let only the first item receive focus
|
2011-10-27 03:54:12 +00:00
|
|
|
this.items.slice(1).attr( "tabIndex", -1 );
|
2011-05-04 09:50:07 +00:00
|
|
|
|
|
|
|
this.element
|
|
|
|
.addClass( "ui-menubar ui-widget-header ui-helper-clearfix" )
|
|
|
|
.attr( "role", "menubar" );
|
2011-10-27 03:54:12 +00:00
|
|
|
this._focusable( this.items );
|
|
|
|
this._hoverable( this.items );
|
|
|
|
this.items.siblings( this.options.menuElement )
|
2011-05-04 09:50:07 +00:00
|
|
|
.menu({
|
2011-06-09 15:14:21 +00:00
|
|
|
position: {
|
|
|
|
within: this.options.position.within
|
|
|
|
},
|
2011-05-04 09:50:07 +00:00
|
|
|
select: function( event, ui ) {
|
|
|
|
ui.item.parents( "ul.ui-menu:last" ).hide();
|
|
|
|
that._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-07-13 15:03:25 +00:00
|
|
|
that._trigger( "select", event, ui );
|
2011-10-19 18:59:07 +00:00
|
|
|
},
|
|
|
|
items: that.options.menuElement
|
2011-05-04 09:50:07 +00:00
|
|
|
})
|
|
|
|
.hide()
|
2011-07-12 15:36:34 +00:00
|
|
|
.attr({
|
|
|
|
"aria-hidden": "true",
|
|
|
|
"aria-expanded": "false"
|
2011-06-08 21:02:57 +00:00
|
|
|
})
|
2011-05-04 09:50:07 +00:00
|
|
|
.bind( "keydown.menubar", function( event ) {
|
|
|
|
var menu = $( this );
|
|
|
|
if ( menu.is( ":hidden" ) )
|
2011-02-22 14:14:12 +00:00
|
|
|
return;
|
2011-05-04 09:50:07 +00:00
|
|
|
switch ( event.keyCode ) {
|
2011-02-22 14:14:12 +00:00
|
|
|
case $.ui.keyCode.LEFT:
|
2011-10-27 03:54:12 +00:00
|
|
|
that.previous( event );
|
2011-02-22 14:14:12 +00:00
|
|
|
event.preventDefault();
|
|
|
|
break;
|
|
|
|
case $.ui.keyCode.RIGHT:
|
2011-10-27 03:54:12 +00:00
|
|
|
that.next( event );
|
2011-02-22 14:14:12 +00:00
|
|
|
event.preventDefault();
|
|
|
|
break;
|
|
|
|
};
|
2011-05-04 09:50:07 +00:00
|
|
|
});
|
2011-10-27 03:54:12 +00:00
|
|
|
this.items.each(function() {
|
2011-02-22 14:14:12 +00:00
|
|
|
var input = $(this),
|
2011-05-04 09:50:07 +00:00
|
|
|
// TODO menu var is only used on two places, doesn't quite justify the .each
|
2011-10-19 18:59:07 +00:00
|
|
|
menu = input.next( that.options.menuElement );
|
2011-05-28 19:42:28 +00:00
|
|
|
|
2011-05-04 09:50:07 +00:00
|
|
|
input.bind( "click.menubar focus.menubar mouseenter.menubar", function( event ) {
|
2011-03-17 14:26:06 +00:00
|
|
|
// ignore triggered focus event
|
2011-05-04 09:50:07 +00:00
|
|
|
if ( event.type == "focus" && !event.originalEvent ) {
|
2011-03-17 14:26:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-05-28 19:42:28 +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-05-04 09:50:07 +00:00
|
|
|
if ( event.type == "click" && menu.is( ":visible" ) && that.active && that.active[0] == menu[0] ) {
|
|
|
|
that._close();
|
2011-02-28 21:53:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-07-07 14:29:50 +00:00
|
|
|
if ( ( that.open && event.type == "mouseenter" ) || event.type == "click" || that.options.autoExpand ) {
|
|
|
|
if( that.options.autoExpand ) {
|
2011-10-01 14:56:49 +00:00
|
|
|
clearTimeout( that.closeTimer );
|
2011-07-07 14:29:50 +00:00
|
|
|
}
|
2011-07-13 15:03:25 +00:00
|
|
|
|
2011-05-04 09:50:07 +00:00
|
|
|
that._open( event, menu );
|
|
|
|
}
|
2011-05-28 19:42:28 +00:00
|
|
|
})
|
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:
|
2011-05-04 09:50:07 +00:00
|
|
|
that._open( event, $( this ).next() );
|
2011-03-17 11:04:22 +00:00
|
|
|
event.preventDefault();
|
|
|
|
break;
|
2011-03-17 14:10:25 +00:00
|
|
|
case $.ui.keyCode.LEFT:
|
2011-10-27 03:54:12 +00:00
|
|
|
that.previous( event );
|
2011-03-17 14:10:25 +00:00
|
|
|
event.preventDefault();
|
|
|
|
break;
|
|
|
|
case $.ui.keyCode.RIGHT:
|
2011-10-27 03:54:12 +00:00
|
|
|
that.next( event );
|
2011-03-17 14:10:25 +00:00
|
|
|
event.preventDefault();
|
|
|
|
break;
|
2011-03-17 11:04:22 +00:00
|
|
|
}
|
|
|
|
})
|
2011-05-04 09:50:07 +00:00
|
|
|
.addClass( "ui-button ui-widget ui-button-text-only ui-menubar-link" )
|
|
|
|
.attr( "role", "menuitem" )
|
2011-07-12 15:36:34 +00:00
|
|
|
.attr( "aria-haspopup", "true" )
|
2011-05-04 09:50:07 +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-05-04 09:50:07 +00:00
|
|
|
if ( that.options.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-28 21:49:49 +00:00
|
|
|
}
|
2011-05-28 19:42:28 +00:00
|
|
|
|
2011-05-04 09:50:07 +00:00
|
|
|
if ( !that.options.buttons ) {
|
2011-05-04 06:31:14 +00:00
|
|
|
// TODO ui-menubar-link is added above, not needed here?
|
2011-05-04 09:50:07 +00:00
|
|
|
input.addClass( "ui-menubar-link" ).removeClass( "ui-state-default" );
|
2011-05-28 19:42:28 +00:00
|
|
|
};
|
2011-06-09 15:14:21 +00:00
|
|
|
|
2011-02-22 14:14:12 +00:00
|
|
|
});
|
2011-05-04 09:50:07 +00:00
|
|
|
that._bind( {
|
|
|
|
keydown: function( event ) {
|
2011-07-14 15:20:47 +00:00
|
|
|
if ( event.keyCode == $.ui.keyCode.ESCAPE && that.active && that.active.menu( "collapse", event ) !== true ) {
|
2011-05-04 09:50:07 +00:00
|
|
|
var active = that.active;
|
|
|
|
that.active.blur();
|
|
|
|
that._close( event );
|
|
|
|
active.prev().focus();
|
2011-03-17 11:00:37 +00:00
|
|
|
}
|
2011-04-22 18:22:16 +00:00
|
|
|
},
|
2011-05-04 09:50:07 +00:00
|
|
|
focusin: function( event ) {
|
|
|
|
clearTimeout( that.closeTimer );
|
2011-04-22 18:22:16 +00:00
|
|
|
},
|
2011-05-04 09:50:07 +00:00
|
|
|
focusout: function( event ) {
|
|
|
|
that.closeTimer = setTimeout( function() {
|
|
|
|
that._close( event );
|
2011-10-01 14:56:49 +00:00
|
|
|
}, 150);
|
|
|
|
},
|
|
|
|
"mouseleave .ui-menubar-item": function( event ) {
|
|
|
|
if ( that.options.autoExpand ) {
|
|
|
|
that.closeTimer = setTimeout( function() {
|
|
|
|
that._close( event );
|
|
|
|
}, 150);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"mouseenter .ui-menubar-item": function( event ) {
|
|
|
|
clearTimeout( that.closeTimer );
|
2011-03-17 11:00:37 +00:00
|
|
|
}
|
2011-02-22 14:14:12 +00:00
|
|
|
});
|
|
|
|
},
|
2011-05-28 19:42:28 +00:00
|
|
|
|
2011-04-15 17:18:58 +00:00
|
|
|
_destroy : function() {
|
2011-10-27 03:54:12 +00:00
|
|
|
this.menuItems
|
2011-05-04 09:50:07 +00:00
|
|
|
.removeClass( "ui-menubar-item" )
|
2011-10-27 03:54:12 +00:00
|
|
|
.removeAttr( "role" );
|
2011-05-28 19:42:28 +00:00
|
|
|
|
2011-05-04 09:50:07 +00:00
|
|
|
this.element
|
|
|
|
.removeClass( "ui-menubar ui-widget-header ui-helper-clearfix" )
|
2011-06-08 21:02:57 +00:00
|
|
|
.removeAttr( "role" )
|
2011-05-04 09:50:07 +00:00
|
|
|
.unbind( ".menubar" );
|
2011-05-28 19:42:28 +00:00
|
|
|
|
2011-10-27 03:54:12 +00:00
|
|
|
this.items
|
2011-05-04 09:50:07 +00:00
|
|
|
.unbind( ".menubar" )
|
|
|
|
.removeClass( "ui-button ui-widget ui-button-text-only ui-menubar-link ui-state-default" )
|
2011-06-08 21:02:57 +00:00
|
|
|
.removeAttr( "role" )
|
2011-07-12 15:36:34 +00:00
|
|
|
.removeAttr( "aria-haspopup" )
|
2011-05-04 09:50:07 +00:00
|
|
|
// TODO unwrap?
|
|
|
|
.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()
|
2011-05-04 09:50:07 +00:00
|
|
|
.children( ".ui-icon" ).remove();
|
2011-04-15 17:18:58 +00:00
|
|
|
|
2011-05-04 09:50:07 +00:00
|
|
|
this.element.find( ":ui-menu" )
|
|
|
|
.menu( "destroy" )
|
|
|
|
.show()
|
2011-07-12 15:36:34 +00:00
|
|
|
.removeAttr( "aria-hidden" )
|
|
|
|
.removeAttr( "aria-expanded" )
|
2011-05-04 09:50:07 +00:00
|
|
|
.removeAttr( "tabindex" )
|
|
|
|
.unbind( ".menubar" );
|
2011-04-15 17:18:58 +00:00
|
|
|
},
|
2011-05-28 19:42:28 +00:00
|
|
|
|
2011-02-24 21:57:44 +00:00
|
|
|
_close: function() {
|
2011-05-04 09:50:07 +00:00
|
|
|
if ( !this.active || !this.active.length )
|
2011-04-18 21:05:50 +00:00
|
|
|
return;
|
2011-05-04 09:50:07 +00:00
|
|
|
this.active
|
2011-07-14 15:20:47 +00:00
|
|
|
.menu( "collapseAll" )
|
2011-05-04 09:50:07 +00:00
|
|
|
.hide()
|
2011-07-12 15:36:34 +00:00
|
|
|
.attr({
|
|
|
|
"aria-hidden": "true",
|
|
|
|
"aria-expanded": "false"
|
2011-06-08 21:02:57 +00:00
|
|
|
});
|
2011-05-04 09:50:07 +00:00
|
|
|
this.active
|
|
|
|
.prev()
|
|
|
|
.removeClass( "ui-state-active" )
|
|
|
|
.removeAttr( "tabIndex" );
|
2011-03-17 14:10:25 +00:00
|
|
|
this.active = null;
|
2011-04-23 15:31:25 +00:00
|
|
|
this.open = false;
|
2011-02-24 21:57:44 +00:00
|
|
|
},
|
2011-05-28 19:42:28 +00:00
|
|
|
|
2011-05-04 09:50:07 +00:00
|
|
|
_open: function( event, menu ) {
|
2011-03-17 14:44:09 +00:00
|
|
|
// on a single-button menubar, ignore reopening the same menu
|
2011-05-04 09:50:07 +00:00
|
|
|
if ( this.active && this.active[0] == menu[0] ) {
|
2011-03-17 14:44:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-05-04 09:50:07 +00:00
|
|
|
// TODO refactor, almost the same as _close above, but don't remove tabIndex
|
|
|
|
if ( this.active ) {
|
|
|
|
this.active
|
2011-07-14 15:20:47 +00:00
|
|
|
.menu( "collapseAll" )
|
2011-05-04 09:50:07 +00:00
|
|
|
.hide()
|
2011-07-12 15:36:34 +00:00
|
|
|
.attr({
|
|
|
|
"aria-hidden": "true",
|
|
|
|
"aria-expanded": "false"
|
2011-06-08 21:02:57 +00:00
|
|
|
});
|
2011-05-04 09:50:07 +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)
|
2011-05-04 09:50:07 +00:00
|
|
|
var button = menu.prev().addClass( "ui-state-active" ).attr( "tabIndex", -1 );
|
|
|
|
this.active = menu
|
|
|
|
.show()
|
2011-06-09 15:14:21 +00:00
|
|
|
.position( $.extend({
|
2011-05-04 09:50:07 +00:00
|
|
|
of: button
|
2011-06-09 15:14:21 +00:00
|
|
|
}, this.options.position ) )
|
2011-07-12 15:36:34 +00:00
|
|
|
.removeAttr( "aria-hidden" )
|
|
|
|
.attr( "aria-expanded", "true" )
|
2011-10-19 18:59:07 +00:00
|
|
|
.menu("focus", event, menu.children( ".ui-menu-item" ).first() )
|
2011-05-04 09:50:07 +00:00
|
|
|
// TODO need a comment here why both events are triggered
|
|
|
|
.focus()
|
|
|
|
.focusin();
|
2011-04-23 15:31:25 +00:00
|
|
|
this.open = true;
|
2011-02-22 14:14:12 +00:00
|
|
|
},
|
2011-05-28 19:42:28 +00:00
|
|
|
|
2011-10-27 03:54:12 +00:00
|
|
|
next: function( event ) {
|
|
|
|
this._move( "next", "first", event );
|
2011-03-17 14:10:25 +00:00
|
|
|
},
|
2011-05-28 19:42:28 +00:00
|
|
|
|
2011-10-27 03:54:12 +00:00
|
|
|
previous: function( event ) {
|
|
|
|
this._move( "prev", "last", event );
|
2011-03-17 14:10:25 +00:00
|
|
|
},
|
2011-05-04 06:31:14 +00:00
|
|
|
|
2011-10-27 03:54:12 +00:00
|
|
|
_move: function( direction, filter, event ) {
|
|
|
|
var next,
|
|
|
|
wrapItem;
|
|
|
|
if ( this.open ) {
|
|
|
|
next = this.active.closest( ".ui-menubar-item" )[ direction + "All" ]( this.options.items ).first().children( ".ui-menu" ).eq( 0 );
|
|
|
|
wrapItem = this.menuItems[ filter ]().children( ".ui-menu" ).eq( 0 );
|
2011-02-22 14:14:12 +00:00
|
|
|
} else {
|
2011-10-27 03:54:12 +00:00
|
|
|
if ( event ) {
|
|
|
|
next = $( event.target ).closest( ".ui-menubar-item" )[ direction + "All" ]( this.options.items ).children( ".ui-menubar-link" ).eq( 0 );
|
|
|
|
wrapItem = this.menuItems[ filter ]().children( ".ui-menubar-link" ).eq( 0 );
|
|
|
|
} else {
|
|
|
|
next = wrapItem = this.menuItems.children( "a" ).eq( 0 );
|
|
|
|
}
|
2011-02-22 14:14:12 +00:00
|
|
|
}
|
2011-05-28 19:42:28 +00:00
|
|
|
|
2011-05-04 09:50:07 +00:00
|
|
|
if ( next.length ) {
|
2011-10-27 03:54:12 +00:00
|
|
|
if ( this.open ) {
|
|
|
|
this._open( event, next );
|
|
|
|
} else {
|
|
|
|
next.removeAttr( "tabIndex")[0].focus();
|
|
|
|
}
|
2011-02-22 14:14:12 +00:00
|
|
|
} else {
|
2011-10-27 03:54:12 +00:00
|
|
|
if ( this.open ) {
|
|
|
|
this._open( event, wrapItem );
|
|
|
|
} else {
|
|
|
|
wrapItem.removeAttr( "tabIndex")[0].focus();
|
|
|
|
}
|
2011-02-22 14:14:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-05-04 09:50:07 +00:00
|
|
|
}( jQuery ));
|