jquery-ui/ui/jquery.ui.menu.js

427 lines
12 KiB
JavaScript
Raw Normal View History

/*
* jQuery UI Menu @VERSION
*
2011-01-17 14:13:18 +00:00
* 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/Menu
*
* Depends:
* jquery.ui.core.js
* jquery.ui.widget.js
*/
(function($) {
var idIncrement = 0;
$.widget("ui.menu", {
defaultElement: "<ul>",
delay: 150,
options: {
position: {
my: "left top",
at: "right top"
}
},
_create: function() {
var self = this;
this.activeMenu = this.element;
this.menuId = this.element.attr( "id" ) || "ui-menu-" + idIncrement++;
2011-04-20 15:18:15 +00:00
if (this.element.find(".ui-icon").length) {
this.element.addClass("ui-menu-icons");
}
this.element
2010-10-28 16:12:52 +00:00
.addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
.attr({
id: this.menuId,
role: "menu"
})
2010-10-28 16:12:52 +00:00
.bind( "click.menu", function( event ) {
var item = $( event.target ).closest( ".ui-menu-item:has(a)" );
if ( self.options.disabled ) {
return false;
}
if ( !item.length ) {
return;
}
// temporary
event.preventDefault();
// it's possible to click an item without hovering it (#7085)
if ( !self.active || ( self.active[ 0 ] !== item[ 0 ] ) ) {
self.focus( event, item );
}
self.select( event );
})
2010-10-28 16:12:52 +00:00
.bind( "mouseover.menu", function( event ) {
if ( self.options.disabled ) {
return;
}
var target = $( event.target ).closest( ".ui-menu-item" );
if ( target.length ) {
self.focus( event, target );
}
})
.bind("mouseout.menu", function( event ) {
if ( self.options.disabled ) {
return;
}
var target = $( event.target ).closest( ".ui-menu-item" );
if ( target.length ) {
self.blur( event );
}
2010-10-28 16:12:52 +00:00
});
this.refresh();
this.element.attr( "tabIndex", 0 ).bind( "keydown.menu", function( event ) {
2010-10-28 16:12:52 +00:00
if ( self.options.disabled ) {
return;
}
2010-10-28 16:12:52 +00:00
switch ( event.keyCode ) {
case $.ui.keyCode.PAGE_UP:
self.previousPage( event );
event.preventDefault();
event.stopImmediatePropagation();
break;
case $.ui.keyCode.PAGE_DOWN:
self.nextPage( event );
event.preventDefault();
event.stopImmediatePropagation();
break;
case $.ui.keyCode.UP:
self.previous( event );
event.preventDefault();
event.stopImmediatePropagation();
break;
case $.ui.keyCode.DOWN:
self.next( event );
event.preventDefault();
event.stopImmediatePropagation();
break;
case $.ui.keyCode.LEFT:
if (self.left( event )) {
event.stopImmediatePropagation();
}
event.preventDefault();
break;
case $.ui.keyCode.RIGHT:
if (self.right( event )) {
event.stopImmediatePropagation();
}
event.preventDefault();
break;
case $.ui.keyCode.ENTER:
if (self.active.children("a[aria-haspopup='true']").length) {
if (self.right( event )) {
event.stopImmediatePropagation();
}
}
else {
self.select( event );
event.stopImmediatePropagation();
}
event.preventDefault();
break;
2011-03-17 16:43:11 +00:00
case $.ui.keyCode.ESCAPE:
if ( self.left( event ) ) {
event.stopImmediatePropagation();
}
event.preventDefault();
break;
default:
event.stopPropagation();
clearTimeout(self.filterTimer);
var prev = self.previousFilter || "";
var character = String.fromCharCode(event.keyCode);
var skip = false;
if (character == prev) {
skip = true;
} else {
character = prev + character;
}
function escape(value) {
return value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
var match = self.activeMenu.children(".ui-menu-item").filter(function() {
return new RegExp("^" + escape(character), "i").test($(this).children("a").text());
});
var match = skip && match.index(self.active.next()) != -1 ? self.active.nextAll(".ui-menu-item") : match;
if (!match.length) {
character = String.fromCharCode(event.keyCode);
match = self.activeMenu.children(".ui-menu-item").filter(function() {
return new RegExp("^" + escape(character), "i").test($(this).children("a").text());
});
}
if (match.length) {
self.focus(event, match);
if (match.length > 1) {
self.previousFilter = character;
self.filterTimer = setTimeout(function() {
delete self.previousFilter;
}, 1000);
} else {
delete self.previousFilter;
}
} else {
delete self.previousFilter;
}
}
});
},
_destroy: function() {
//destroy (sub)menus
this.element
.removeAttr( "aria-activedescendant" )
.find("ul")
.andSelf()
2010-10-28 16:12:52 +00:00
.removeClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
.removeAttr( "role" )
.removeAttr("id")
.removeAttr( "aria-labelledby" )
.removeAttr( "aria-expanded" )
.removeAttr( "aria-hidden" )
.show();
//destroy menu items
this.element.find( ".ui-menu-item" )
.unbind( ".menu" )
2010-10-28 16:12:52 +00:00
.removeClass( "ui-menu-item" )
.removeAttr( "role" )
.children( "a" )
.removeClass( "ui-corner-all ui-state-hover" )
2010-10-28 16:12:52 +00:00
.removeAttr( "tabIndex" )
.removeAttr( "role" )
.removeAttr( "aria-haspopup" )
.removeAttr( "id" )
.children(".ui-icon").remove();
2010-04-30 08:21:40 +00:00
},
refresh: function() {
var self = this;
// initialize nested menus
var submenus = this.element.find("ul:not(.ui-menu)")
.addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
.attr("role", "menu")
.hide()
.attr("aria-hidden", "true")
.attr("aria-expanded", "false")
;
// don't refresh list items that are already adapted
var items = submenus.add(this.element).children( "li:not(.ui-menu-item):has(a)" )
2010-10-28 16:12:52 +00:00
.addClass( "ui-menu-item" )
.attr( "role", "presentation" );
2010-10-28 16:12:52 +00:00
items.children( "a" )
.addClass( "ui-corner-all" )
.attr( "tabIndex", -1 )
.attr( "role", "menuitem" )
.attr("id", function(i) {return self.element.attr("id") + "-" + i});
submenus.each(function() {
var menu = $(this);
var item = menu.prev("a")
item.attr("aria-haspopup", "true")
.prepend('<span class="ui-menu-icon ui-icon ui-icon-carat-1-e"></span>');
menu.attr("aria-labelledby", item.attr("id"));
});
},
focus: function( event, item ) {
var self = this;
this.blur();
2010-10-28 16:12:52 +00:00
if ( this._hasScroll() ) {
var borderTop = parseFloat( $.curCSS( this.element[0], "borderTopWidth", true) ) || 0,
paddingtop = parseFloat( $.curCSS( this.element[0], "paddingTop", true) ) || 0,
offset = item.offset().top - this.element.offset().top - borderTop - paddingtop,
2010-10-28 16:12:52 +00:00
scroll = this.element.attr( "scrollTop" ),
elementHeight = this.element.height(),
itemHeight = item.height();
if ( offset < 0 ) {
2010-10-28 16:12:52 +00:00
this.element.attr( "scrollTop", scroll + offset );
} else if ( offset + itemHeight > elementHeight ) {
this.element.attr( "scrollTop", scroll + offset - elementHeight + itemHeight );
}
}
this.active = item.first()
2010-10-28 16:12:52 +00:00
.children( "a" )
.addClass( "ui-state-focus" )
.end();
self.element.attr("aria-activedescendant", self.active.children("a").attr("id"))
// highlight active parent menu item, if any
this.active.parent().closest(".ui-menu-item").children("a:first").addClass("ui-state-active");
self.timer = setTimeout(function() {
self._close();
}, self.delay)
var nested = $(">ul", item);
if (nested.length && /^mouse/.test(event.type)) {
self._startOpening(nested);
}
this.activeMenu = item.parent();
2010-10-28 16:12:52 +00:00
this._trigger( "focus", event, { item: item } );
},
blur: function(event) {
if (!this.active) {
return;
}
clearTimeout(this.timer);
this.active.children( "a" ).removeClass( "ui-state-focus" );
this.active = null;
},
_startOpening: function(submenu) {
clearTimeout(this.timer);
var self = this;
self.timer = setTimeout(function() {
self._close();
self._open(submenu);
}, self.delay);
},
_open: function(submenu) {
2011-04-22 21:14:04 +00:00
clearTimeout(this.timer);
this.element.find(".ui-menu").not(submenu.parents()).hide().attr("aria-hidden", "true");
var position = $.extend({}, {
of: this.active
}, $.type(this.options.position) == "function"
? this.options.position(this.active)
: this.options.position
);
submenu.show().removeAttr("aria-hidden").attr("aria-expanded", "true").position(position);
},
closeAll: function() {
2011-02-24 16:46:51 +00:00
this.element
.find("ul").hide().attr("aria-hidden", "true").attr("aria-expanded", "false").end()
2011-02-24 16:46:51 +00:00
.find("a.ui-state-active").removeClass("ui-state-active");
this.blur();
this.activeMenu = this.element;
},
_close: function() {
this.active.parent()
.find("ul").hide().attr("aria-hidden", "true").attr("aria-expanded", "false").end()
.find("a.ui-state-active").removeClass("ui-state-active");
},
left: function(event) {
var newItem = this.active && this.active.parents("li:not(.ui-menubar-item)").first();
if (newItem && newItem.length) {
this.active.parent().attr("aria-hidden", "true").attr("aria-expanded", "false").hide();
this.focus(event, newItem);
return true;
}
},
right: function(event) {
var self= this;
var newItem = this.active && this.active.children("ul").children("li").first();
if (newItem && newItem.length) {
this._open(newItem.parent());
var current = this.active;
//timeout so Firefox will not hide activedescendant change in expanding submenu from AT
setTimeout(function(){self.focus(event, newItem)}, 20);
return true;
}
},
next: function(event) {
this._move( "next", ".ui-menu-item", "first", event );
},
previous: function(event) {
this._move( "prev", ".ui-menu-item", "last", event );
},
first: function() {
2010-10-28 16:12:52 +00:00
return this.active && !this.active.prevAll( ".ui-menu-item" ).length;
},
last: function() {
2010-10-28 16:12:52 +00:00
return this.active && !this.active.nextAll( ".ui-menu-item" ).length;
},
_move: function(direction, edge, filter, event) {
2010-10-28 16:12:52 +00:00
if ( !this.active ) {
this.focus( event, this.activeMenu.children(edge)[filter]() );
return;
}
2010-10-28 16:12:52 +00:00
var next = this.active[ direction + "All" ]( ".ui-menu-item" ).eq( 0 );
if ( next.length ) {
this.focus( event, next );
} else {
this.focus( event, this.activeMenu.children(edge)[filter]() );
}
},
2010-10-28 17:24:38 +00:00
2010-10-28 16:12:52 +00:00
nextPage: function( event ) {
if ( this._hasScroll() ) {
if ( !this.active || this.last() ) {
this.focus( event, this.activeMenu.children( ".ui-menu-item" ).first() );
return;
}
var base = this.active.offset().top,
height = this.element.height(),
result;
this.active.nextAll( ".ui-menu-item" ).each( function() {
result = $( this );
return $( this ).offset().top - base - height < 0;
});
this.focus( event, result );
} else {
this.focus( event, this.activeMenu.children( ".ui-menu-item" )
[ !this.active || this.last() ? "first" : "last" ]() );
}
},
2010-10-28 16:12:52 +00:00
previousPage: function( event ) {
if ( this._hasScroll() ) {
if ( !this.active || this.first() ) {
this.focus( event, this.activeMenu.children( ".ui-menu-item" ).last() );
return;
}
var base = this.active.offset().top,
height = this.element.height(),
result;
this.active.prevAll( ".ui-menu-item" ).each( function() {
result = $( this );
return $(this).offset().top - base + height > 0;
});
this.focus( event, result );
} else {
this.focus( event, this.activeMenu.children( ".ui-menu-item" )
[ !this.active || this.first() ? ":last" : ":first" ]() );
}
},
_hasScroll: function() {
2010-10-28 16:12:52 +00:00
return this.element.height() < this.element.attr( "scrollHeight" );
},
select: function( event ) {
// save active reference before closeAll triggers blur
var ui = {
item: this.active
};
this.closeAll();
this._trigger( "select", event, ui );
}
});
$.ui.menu.version = "@VERSION";
2010-10-28 16:12:52 +00:00
}( jQuery ));