mirror of
https://github.com/jquery/jquery-ui.git
synced 2024-11-21 11:04:24 +00:00
Menu: Refactor focus method, extract _scrollIntroView method
This commit is contained in:
parent
f89971a2c3
commit
1e586dcd48
35
ui/jquery.ui.menu.js
vendored
35
ui/jquery.ui.menu.js
vendored
@ -311,23 +311,10 @@ $.widget( "ui.menu", {
|
|||||||
},
|
},
|
||||||
|
|
||||||
focus: function( event, item ) {
|
focus: function( event, item ) {
|
||||||
var nested, borderTop, paddingTop, offset, scroll, elementHeight, itemHeight, focused;
|
var nested, focused;
|
||||||
this.blur( event, event && event.type === "focus" );
|
this.blur( event, event && event.type === "focus" );
|
||||||
|
|
||||||
if ( this._hasScroll() ) {
|
this._scrollIntoView( item );
|
||||||
borderTop = parseFloat( $.css( this.activeMenu[0], "borderTopWidth" ) ) || 0;
|
|
||||||
paddingTop = parseFloat( $.css( this.activeMenu[0], "paddingTop" ) ) || 0;
|
|
||||||
offset = item.offset().top - this.activeMenu.offset().top - borderTop - paddingTop;
|
|
||||||
scroll = this.activeMenu.scrollTop();
|
|
||||||
elementHeight = this.activeMenu.height();
|
|
||||||
itemHeight = item.height();
|
|
||||||
|
|
||||||
if ( offset < 0 ) {
|
|
||||||
this.activeMenu.scrollTop( scroll + offset );
|
|
||||||
} else if ( offset + itemHeight > elementHeight ) {
|
|
||||||
this.activeMenu.scrollTop( scroll + offset - elementHeight + itemHeight );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.active = item.first();
|
this.active = item.first();
|
||||||
focused = this.active.children( "a" ).addClass( "ui-state-focus" );
|
focused = this.active.children( "a" ).addClass( "ui-state-focus" );
|
||||||
@ -357,6 +344,24 @@ $.widget( "ui.menu", {
|
|||||||
this._trigger( "focus", event, { item: item } );
|
this._trigger( "focus", event, { item: item } );
|
||||||
},
|
},
|
||||||
|
|
||||||
|
_scrollIntoView: function( item ) {
|
||||||
|
var borderTop, paddingTop, offset, scroll, elementHeight, itemHeight;
|
||||||
|
if ( this._hasScroll() ) {
|
||||||
|
borderTop = parseFloat( $.css( this.activeMenu[0], "borderTopWidth" ) ) || 0;
|
||||||
|
paddingTop = parseFloat( $.css( this.activeMenu[0], "paddingTop" ) ) || 0;
|
||||||
|
offset = item.offset().top - this.activeMenu.offset().top - borderTop - paddingTop;
|
||||||
|
scroll = this.activeMenu.scrollTop();
|
||||||
|
elementHeight = this.activeMenu.height();
|
||||||
|
itemHeight = item.height();
|
||||||
|
|
||||||
|
if ( offset < 0 ) {
|
||||||
|
this.activeMenu.scrollTop( scroll + offset );
|
||||||
|
} else if ( offset + itemHeight > elementHeight ) {
|
||||||
|
this.activeMenu.scrollTop( scroll + offset - elementHeight + itemHeight );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
blur: function( event, fromFocus ) {
|
blur: function( event, fromFocus ) {
|
||||||
if ( !fromFocus ) {
|
if ( !fromFocus ) {
|
||||||
clearTimeout( this.timer );
|
clearTimeout( this.timer );
|
||||||
|
Loading…
Reference in New Issue
Block a user