Skip to content

Commit 1e586dc

Browse files
committed
Menu: Refactor focus method, extract _scrollIntroView method
1 parent f89971a commit 1e586dc

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

ui/jquery.ui.menu.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -311,23 +311,10 @@ $.widget( "ui.menu", {
311311
},
312312

313313
focus: function( event, item ) {
314-
var nested, borderTop, paddingTop, offset, scroll, elementHeight, itemHeight, focused;
314+
var nested, focused;
315315
this.blur( event, event && event.type === "focus" );
316316

317-
if ( this._hasScroll() ) {
318-
borderTop = parseFloat( $.css( this.activeMenu[0], "borderTopWidth" ) ) || 0;
319-
paddingTop = parseFloat( $.css( this.activeMenu[0], "paddingTop" ) ) || 0;
320-
offset = item.offset().top - this.activeMenu.offset().top - borderTop - paddingTop;
321-
scroll = this.activeMenu.scrollTop();
322-
elementHeight = this.activeMenu.height();
323-
itemHeight = item.height();
324-
325-
if ( offset < 0 ) {
326-
this.activeMenu.scrollTop( scroll + offset );
327-
} else if ( offset + itemHeight > elementHeight ) {
328-
this.activeMenu.scrollTop( scroll + offset - elementHeight + itemHeight );
329-
}
330-
}
317+
this._scrollIntoView( item );
331318

332319
this.active = item.first();
333320
focused = this.active.children( "a" ).addClass( "ui-state-focus" );
@@ -357,6 +344,24 @@ $.widget( "ui.menu", {
357344
this._trigger( "focus", event, { item: item } );
358345
},
359346

347+
_scrollIntoView: function( item ) {
348+
var borderTop, paddingTop, offset, scroll, elementHeight, itemHeight;
349+
if ( this._hasScroll() ) {
350+
borderTop = parseFloat( $.css( this.activeMenu[0], "borderTopWidth" ) ) || 0;
351+
paddingTop = parseFloat( $.css( this.activeMenu[0], "paddingTop" ) ) || 0;
352+
offset = item.offset().top - this.activeMenu.offset().top - borderTop - paddingTop;
353+
scroll = this.activeMenu.scrollTop();
354+
elementHeight = this.activeMenu.height();
355+
itemHeight = item.height();
356+
357+
if ( offset < 0 ) {
358+
this.activeMenu.scrollTop( scroll + offset );
359+
} else if ( offset + itemHeight > elementHeight ) {
360+
this.activeMenu.scrollTop( scroll + offset - elementHeight + itemHeight );
361+
}
362+
}
363+
},
364+
360365
blur: function( event, fromFocus ) {
361366
if ( !fromFocus ) {
362367
clearTimeout( this.timer );

0 commit comments

Comments
 (0)