Skip to content

Commit 02aad7b

Browse files
committed
Widget delegation: Update menu to use _bind with delegation. Clean up test.
1 parent 5a45f48 commit 02aad7b

File tree

2 files changed

+34
-48
lines changed

2 files changed

+34
-48
lines changed

tests/unit/widget/widget_core.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,6 @@ test( "_bind() with delegate", function() {
670670
expect( 8 );
671671
$.widget( "ui.testWidget", {
672672
_create: function() {
673-
var that = this;
674673
this.element = {
675674
bind: function( event, handler ) {
676675
equal( event, "click.testWidget" );
@@ -682,7 +681,7 @@ test( "_bind() with delegate", function() {
682681
ok( $.isFunction(handler) );
683682
},
684683
trigger: $.noop
685-
}
684+
};
686685
this._bind({
687686
"click": "handler",
688687
"click a": "handler",
@@ -698,7 +697,7 @@ test( "_bind() with delegate", function() {
698697
}
699698
});
700699
$.ui.testWidget();
701-
})
700+
});
702701

703702
test( "._hoverable()", function() {
704703
$.widget( "ui.testWidget", {

ui/jquery.ui.menu.js

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -37,53 +37,40 @@ $.widget( "ui.menu", {
3737
.attr({
3838
id: this.menuId,
3939
role: "menu"
40-
})
41-
.bind( "click.menu", function( event ) {
42-
var item = $( event.target ).closest( ".ui-menu-item:has(a)" );
43-
if ( self.options.disabled ) {
44-
return false;
45-
}
46-
if ( !item.length ) {
47-
return;
48-
}
40+
});
41+
this.element.bind("click.menu", function( event ) {
42+
if ( self.options.disabled ) {
43+
event.preventDefault();
44+
}
45+
});
46+
this._bind({
47+
"click .ui-menu-item:has(a)": function( event ) {
48+
event.stopImmediatePropagation();
49+
var target = $( event.currentTarget );
4950
// it's possible to click an item without hovering it (#7085)
50-
if ( !self.active || ( self.active[ 0 ] !== item[ 0 ] ) ) {
51-
self.focus( event, item );
52-
}
53-
self.select( event );
54-
})
55-
.bind( "mouseover.menu", function( event ) {
56-
if ( self.options.disabled ) {
57-
return;
58-
}
59-
var target = $( event.target ).closest( ".ui-menu-item" );
60-
if ( target.length ) {
61-
//Remove ui-state-active class from siblings of the newly focused menu item to avoid a jump caused by adjacent elements both having a class with a border
62-
target.siblings().children( ".ui-state-active" ).removeClass( "ui-state-active" );
63-
self.focus( event, target );
51+
if ( !this.active || ( this.active[ 0 ] !== target[ 0 ] ) ) {
52+
this.focus( event, target );
6453
}
65-
})
66-
.bind( "mouseout.menu", function( event ) {
67-
if ( self.options.disabled ) {
68-
return;
69-
}
70-
var target = $( event.target ).closest( ".ui-menu-item" );
71-
if ( target.length ) {
72-
self.blur( event );
73-
}
74-
})
75-
.bind( "focus.menu", function( event ) {
76-
if ( self.options.disabled ) {
77-
return;
78-
}
79-
self.focus( event, $( event.target ).children( ".ui-menu-item:first" ) );
80-
})
81-
.bind( "blur.menu", function( event ) {
82-
if ( self.options.disabled ) {
83-
return;
84-
}
85-
self.collapseAll( event );
86-
});
54+
this.select( event );
55+
},
56+
"mouseover .ui-menu-item": function( event ) {
57+
event.stopImmediatePropagation();
58+
var target = $( event.currentTarget );
59+
// Remove ui-state-active class from siblings of the newly focused menu item to avoid a jump caused by adjacent elements both having a class with a border
60+
target.siblings().children( ".ui-state-active" ).removeClass( "ui-state-active" );
61+
this.focus( event, target );
62+
},
63+
"mouseout .ui-menu-item": function( event ) {
64+
this.blur( event );
65+
},
66+
"focus": function( event ) {
67+
this.focus( event, $( event.target ).children( ".ui-menu-item:first" ) );
68+
},
69+
"blur": function( event ) {
70+
this.collapseAll( event );
71+
}
72+
});
73+
8774
this.refresh();
8875

8976
this.element.attr( "tabIndex", 0 ).bind( "keydown.menu", function( event ) {

0 commit comments

Comments
 (0)