2010-04-21 19:01:08 +00:00
|
|
|
/*
|
|
|
|
* jQuery UI Menu @VERSION
|
|
|
|
*
|
2011-01-17 14:13:18 +00:00
|
|
|
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
|
2010-07-09 13:31:53 +00:00
|
|
|
* Dual licensed under the MIT or GPL Version 2 licenses.
|
|
|
|
* http://jquery.org/license
|
2010-04-21 19:01:08 +00:00
|
|
|
*
|
|
|
|
* http://docs.jquery.com/UI/Menu
|
|
|
|
*
|
|
|
|
* Depends:
|
|
|
|
* jquery.ui.core.js
|
2010-07-09 13:31:53 +00:00
|
|
|
* jquery.ui.widget.js
|
2010-04-21 19:01:08 +00:00
|
|
|
*/
|
|
|
|
(function($) {
|
2010-11-16 19:35:08 +00:00
|
|
|
|
|
|
|
var idIncrement = 0;
|
2010-04-21 19:01:08 +00:00
|
|
|
|
|
|
|
$.widget("ui.menu", {
|
2011-01-23 22:55:53 +00:00
|
|
|
defaultElement: "<ul>",
|
2010-04-21 19:01:08 +00:00
|
|
|
_create: function() {
|
|
|
|
var self = this;
|
2010-11-16 19:35:08 +00:00
|
|
|
this.menuId = this.element.attr( "id" ) || "ui-menu-" + idIncrement++;
|
2010-04-21 19:01:08 +00:00
|
|
|
this.element
|
2010-10-28 16:12:52 +00:00
|
|
|
.addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
|
2010-04-21 19:01:08 +00:00
|
|
|
.attr({
|
2010-11-16 19:35:08 +00:00
|
|
|
id: this.menuId,
|
|
|
|
role: "listbox"
|
2010-04-21 19:01:08 +00:00
|
|
|
})
|
2010-10-28 16:12:52 +00:00
|
|
|
.bind( "click.menu", function( event ) {
|
2010-10-28 16:11:55 +00:00
|
|
|
if ( self.options.disabled ) {
|
2010-06-18 08:57:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-21 19:01:08 +00:00
|
|
|
if ( !$( event.target ).closest( ".ui-menu-item a" ).length ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// temporary
|
|
|
|
event.preventDefault();
|
|
|
|
self.select( event );
|
2010-10-28 16:11:55 +00:00
|
|
|
})
|
2010-10-28 16:12:52 +00:00
|
|
|
.bind( "mouseover.menu", function( event ) {
|
2010-10-28 16:11:55 +00:00
|
|
|
if ( self.options.disabled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var target = $( event.target ).closest( ".ui-menu-item" );
|
|
|
|
if ( target.length && target.parent()[0] === self.element[0] ) {
|
|
|
|
self.activate( event, target );
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.bind("mouseout.menu", function( event ) {
|
|
|
|
if ( self.options.disabled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var target = $( event.target ).closest( ".ui-menu-item" );
|
|
|
|
if ( target.length && target.parent()[0] === self.element[0] ) {
|
|
|
|
self.deactivate( event );
|
|
|
|
}
|
2010-10-28 16:12:52 +00:00
|
|
|
});
|
2010-04-21 19:01:08 +00:00
|
|
|
this.refresh();
|
2010-04-30 11:04:09 +00:00
|
|
|
|
2010-10-28 16:12:52 +00:00
|
|
|
if ( !this.options.input ) {
|
|
|
|
this.options.input = this.element.attr( "tabIndex", 0 );
|
2010-04-30 11:04:09 +00:00
|
|
|
}
|
2010-10-28 16:12:52 +00:00
|
|
|
this.options.input.bind( "keydown.menu", function( event ) {
|
|
|
|
if ( self.options.disabled ) {
|
2010-06-18 08:57:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-10-28 16:12:52 +00:00
|
|
|
switch ( event.keyCode ) {
|
2010-04-30 11:04:09 +00:00
|
|
|
case $.ui.keyCode.PAGE_UP:
|
|
|
|
self.previousPage();
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
break;
|
|
|
|
case $.ui.keyCode.PAGE_DOWN:
|
|
|
|
self.nextPage();
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
break;
|
|
|
|
case $.ui.keyCode.UP:
|
|
|
|
self.previous();
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
break;
|
|
|
|
case $.ui.keyCode.DOWN:
|
|
|
|
self.next();
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
break;
|
|
|
|
case $.ui.keyCode.ENTER:
|
|
|
|
self.select();
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2010-04-21 19:01:08 +00:00
|
|
|
},
|
|
|
|
|
2011-01-14 22:11:22 +00:00
|
|
|
_destroy: function() {
|
2010-06-18 08:57:20 +00:00
|
|
|
this.element
|
2010-10-28 16:12:52 +00:00
|
|
|
.removeClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
|
|
|
|
.removeAttr( "tabIndex" )
|
|
|
|
.removeAttr( "role" )
|
|
|
|
.removeAttr( "aria-activedescendant" );
|
2010-06-18 08:57:20 +00:00
|
|
|
|
2010-10-28 16:12:52 +00:00
|
|
|
this.element.children( ".ui-menu-item" )
|
|
|
|
.removeClass( "ui-menu-item" )
|
|
|
|
.removeAttr( "role" )
|
|
|
|
.children( "a" )
|
2011-02-14 11:06:04 +00:00
|
|
|
.removeClass( "ui-corner-all ui-state-hover" )
|
2010-10-28 16:12:52 +00:00
|
|
|
.removeAttr( "tabIndex" )
|
|
|
|
.unbind( ".menu" );
|
2010-04-30 08:21:40 +00:00
|
|
|
},
|
|
|
|
|
2010-04-21 19:01:08 +00:00
|
|
|
refresh: function() {
|
|
|
|
// don't refresh list items that are already adapted
|
2010-10-28 16:12:52 +00:00
|
|
|
var items = this.element.children( "li:not(.ui-menu-item):has(a)" )
|
|
|
|
.addClass( "ui-menu-item" )
|
|
|
|
.attr( "role", "menuitem" );
|
2010-04-21 19:01:08 +00:00
|
|
|
|
2010-10-28 16:12:52 +00:00
|
|
|
items.children( "a" )
|
|
|
|
.addClass( "ui-corner-all" )
|
|
|
|
.attr( "tabIndex", -1 );
|
2010-04-21 19:01:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
activate: function( event, item ) {
|
2010-11-16 19:35:08 +00:00
|
|
|
var self = this;
|
2010-04-21 19:01:08 +00:00
|
|
|
this.deactivate();
|
2010-10-28 16:12:52 +00:00
|
|
|
if ( this._hasScroll() ) {
|
2010-10-28 17:15:27 +00:00
|
|
|
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" ),
|
2010-10-28 17:15:27 +00:00
|
|
|
elementHeight = this.element.height(),
|
|
|
|
itemHeight = item.height();
|
|
|
|
if ( offset < 0 ) {
|
2010-10-28 16:12:52 +00:00
|
|
|
this.element.attr( "scrollTop", scroll + offset );
|
2010-10-28 17:15:27 +00:00
|
|
|
} else if ( offset + itemHeight > elementHeight ) {
|
|
|
|
this.element.attr( "scrollTop", scroll + offset - elementHeight + itemHeight );
|
2010-04-21 19:01:08 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-28 16:38:40 +00:00
|
|
|
this.active = item.first()
|
2010-10-28 16:12:52 +00:00
|
|
|
.children( "a" )
|
|
|
|
.addClass( "ui-state-hover" )
|
2010-11-16 19:35:08 +00:00
|
|
|
.attr( "id", function(index, id) {
|
|
|
|
return (self.itemId = id || self.menuId + "-activedescendant");
|
|
|
|
})
|
2010-04-21 19:01:08 +00:00
|
|
|
.end();
|
2010-11-16 19:35:08 +00:00
|
|
|
// need to remove the attribute before adding it for the screenreader to pick up the change
|
|
|
|
// see http://groups.google.com/group/jquery-a11y/msg/929e0c1e8c5efc8f
|
2011-02-11 14:42:44 +00:00
|
|
|
this.element.removeAttr("aria-activedescendant").attr("aria-activedescendant", self.itemId);
|
2010-10-28 16:12:52 +00:00
|
|
|
this._trigger( "focus", event, { item: item } );
|
2010-04-21 19:01:08 +00:00
|
|
|
},
|
|
|
|
|
2010-10-28 16:11:55 +00:00
|
|
|
deactivate: function(event) {
|
2010-11-16 19:35:08 +00:00
|
|
|
if (!this.active) {
|
|
|
|
return;
|
|
|
|
}
|
2010-04-21 19:01:08 +00:00
|
|
|
|
2010-11-16 19:35:08 +00:00
|
|
|
var self = this;
|
|
|
|
this.active.children( "a" ).removeClass( "ui-state-hover" );
|
|
|
|
// remove only generated id
|
|
|
|
$( "#" + self.menuId + "-activedescendant" ).removeAttr( "id" );
|
|
|
|
this.element.removeAttr( "aria-activedescenant" );
|
2010-10-28 16:12:52 +00:00
|
|
|
this._trigger( "blur", event );
|
2010-04-21 19:01:08 +00:00
|
|
|
this.active = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
next: function(event) {
|
2010-10-28 16:38:40 +00:00
|
|
|
this._move( "next", ".ui-menu-item", "first", event );
|
2010-04-21 19:01:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
previous: function(event) {
|
2010-10-28 16:38:40 +00:00
|
|
|
this._move( "prev", ".ui-menu-item", "last", event );
|
2010-04-21 19:01:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
first: function() {
|
2010-10-28 16:12:52 +00:00
|
|
|
return this.active && !this.active.prevAll( ".ui-menu-item" ).length;
|
2010-04-21 19:01:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
last: function() {
|
2010-10-28 16:12:52 +00:00
|
|
|
return this.active && !this.active.nextAll( ".ui-menu-item" ).length;
|
2010-04-21 19:01:08 +00:00
|
|
|
},
|
|
|
|
|
2010-10-28 16:38:40 +00:00
|
|
|
_move: function(direction, edge, filter, event) {
|
2010-10-28 16:12:52 +00:00
|
|
|
if ( !this.active ) {
|
2010-10-28 16:38:40 +00:00
|
|
|
this.activate( event, this.element.children(edge)[filter]() );
|
2010-04-21 19:01:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-10-28 16:12:52 +00:00
|
|
|
var next = this.active[ direction + "All" ]( ".ui-menu-item" ).eq( 0 );
|
|
|
|
if ( next.length ) {
|
|
|
|
this.activate( event, next );
|
2010-04-21 19:01:08 +00:00
|
|
|
} else {
|
2010-10-28 16:38:40 +00:00
|
|
|
this.activate( event, this.element.children(edge)[filter]() );
|
2010-04-21 19:01:08 +00:00
|
|
|
}
|
|
|
|
},
|
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() ) {
|
2010-10-28 16:38:40 +00:00
|
|
|
this.activate( event, this.element.children( ".ui-menu-item" ).first() );
|
2010-04-21 19:01:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var base = this.active.offset().top,
|
|
|
|
height = this.element.height(),
|
2010-10-28 17:15:27 +00:00
|
|
|
result;
|
|
|
|
this.active.nextAll( ".ui-menu-item" ).each( function() {
|
2010-10-28 17:22:57 +00:00
|
|
|
result = $( this );
|
|
|
|
return $( this ).offset().top - base - height < 0;
|
2010-10-28 17:15:27 +00:00
|
|
|
});
|
|
|
|
|
2010-10-28 17:22:57 +00:00
|
|
|
this.activate( event, result );
|
2010-04-21 19:01:08 +00:00
|
|
|
} else {
|
2010-10-28 16:38:40 +00:00
|
|
|
this.activate( event, this.element.children( ".ui-menu-item" )
|
2010-10-28 17:22:57 +00:00
|
|
|
[ !this.active || this.last() ? "first" : "last" ]() );
|
2010-04-21 19:01:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-10-28 16:12:52 +00:00
|
|
|
previousPage: function( event ) {
|
|
|
|
if ( this._hasScroll() ) {
|
|
|
|
if ( !this.active || this.first() ) {
|
2010-10-28 16:38:40 +00:00
|
|
|
this.activate( event, this.element.children( ".ui-menu-item" ).last() );
|
2010-04-21 19:01:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var base = this.active.offset().top,
|
2010-10-28 17:15:27 +00:00
|
|
|
height = this.element.height(),
|
|
|
|
result;
|
|
|
|
this.active.prevAll( ".ui-menu-item" ).each( function() {
|
2010-10-28 17:22:57 +00:00
|
|
|
result = $( this );
|
|
|
|
return $(this).offset().top - base + height > 0;
|
2010-10-28 17:15:27 +00:00
|
|
|
});
|
|
|
|
|
2010-10-28 17:22:57 +00:00
|
|
|
this.activate( event, result );
|
2010-04-21 19:01:08 +00:00
|
|
|
} else {
|
2010-10-28 16:38:40 +00:00
|
|
|
this.activate( event, this.element.children( ".ui-menu-item" )
|
2010-10-28 17:22:57 +00:00
|
|
|
[ !this.active || this.first() ? ":last" : ":first" ]() );
|
2010-04-21 19:01:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-06-19 14:59:27 +00:00
|
|
|
_hasScroll: function() {
|
2010-10-28 16:12:52 +00:00
|
|
|
return this.element.height() < this.element.attr( "scrollHeight" );
|
2010-04-21 19:01:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
select: function( event ) {
|
2010-10-28 16:12:52 +00:00
|
|
|
this._trigger( "select", event, { item: this.active } );
|
2010-04-21 19:01:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-02-03 17:01:26 +00:00
|
|
|
$.ui.menu.version = "@VERSION";
|
|
|
|
|
2010-10-28 16:12:52 +00:00
|
|
|
}( jQuery ));
|