]> source.dussan.org Git - jquery-ui.git/commitdiff
Menu: Refactor focus method, extract _scrollIntroView method
authorJörn Zaefferer <joern.zaefferer@gmail.com>
Mon, 21 May 2012 14:42:14 +0000 (16:42 +0200)
committerJörn Zaefferer <joern.zaefferer@gmail.com>
Mon, 21 May 2012 14:42:14 +0000 (16:42 +0200)
ui/jquery.ui.menu.js

index 8de1eb47a9291eaf936a5a4915bfffea7a33ffb4..d3c32e3a0c206f48616ee51c4ab93f340f874c3a 100644 (file)
@@ -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 );