Permalink
Browse files

Menu: Refactor focus method, extract _scrollIntroView method

  • Loading branch information...
1 parent f89971a commit 1e586dcd486f24fda9ed277abc2d94fb8d29aee1 @jzaefferer jzaefferer committed May 21, 2012
Showing with 20 additions and 15 deletions.
  1. +20 −15 ui/jquery.ui.menu.js
View
@@ -311,23 +311,10 @@ $.widget( "ui.menu", {
},
focus: function( event, item ) {
- var nested, borderTop, paddingTop, offset, scroll, elementHeight, itemHeight, focused;
+ var nested, focused;
this.blur( event, event && event.type === "focus" );
- 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 );
- }
- }
+ this._scrollIntoView( item );
this.active = item.first();
focused = this.active.children( "a" ).addClass( "ui-state-focus" );
@@ -357,6 +344,24 @@ $.widget( "ui.menu", {
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 ) {
if ( !fromFocus ) {
clearTimeout( this.timer );

0 comments on commit 1e586dc

Please sign in to comment.