Skip to content

Commit c55977d

Browse files
committed
Menu: Refactored next/previousPage logic and activate-scrolling, improved much!
1 parent 184ad69 commit c55977d

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

tests/visual/menu/menu.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<script type="text/javascript" src="../../../ui/jquery.ui.core.js"></script>
99
<script type="text/javascript" src="../../../ui/jquery.ui.widget.js"></script>
1010
<script type="text/javascript" src="../../../ui/jquery.ui.menu.js"></script>
11-
<script type="text/javascript" src="http://jqueryui.com/themeroller/themeswitchertool/"></script>
1211
<script type="text/javascript">
1312
$(function() {
1413
$.fn.themeswitcher && $('<div/>').css({

ui/jquery.ui.menu.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,16 @@ $.widget("ui.menu", {
122122
activate: function( event, item ) {
123123
this.deactivate();
124124
if ( this._hasScroll() ) {
125-
var offset = item.offset().top - this.element.offset().top,
125+
var borderTop = parseFloat( $.curCSS( this.element[0], "borderTopWidth", true) ) || 0,
126+
paddingtop = parseFloat( $.curCSS( this.element[0], "paddingTop", true) ) || 0,
127+
offset = item.offset().top - this.element.offset().top - borderTop - paddingtop,
126128
scroll = this.element.attr( "scrollTop" ),
127-
elementHeight = this.element.height();
128-
if (offset < 0) {
129+
elementHeight = this.element.height(),
130+
itemHeight = item.height();
131+
if ( offset < 0 ) {
129132
this.element.attr( "scrollTop", scroll + offset );
130-
} else if (offset > elementHeight) {
131-
this.element.attr( "scrollTop", scroll + offset - elementHeight + item.height() );
133+
} else if ( offset + itemHeight > elementHeight ) {
134+
this.element.attr( "scrollTop", scroll + offset - elementHeight + itemHeight );
132135
}
133136
}
134137
this.active = item.first()
@@ -187,19 +190,16 @@ $.widget("ui.menu", {
187190
}
188191
var base = this.active.offset().top,
189192
height = this.element.height(),
190-
// TODO replace children with nextAll
191-
// TODO replace filter with each, break once close > 0 and use that item as the result
192-
result = this.element.children( ".ui-menu-item" ).filter( function() {
193-
var close = $( this ).offset().top - base - height + $( this ).height();
194-
// TODO replace with check close > 0
195-
return close < 10 && close > -10;
196-
});
197-
198-
// TODO try to catch this earlier when scrollTop indicates the last page anyway
199-
if ( !result.length ) {
200-
result = this.element.children( ".ui-menu-item" ).last();
201-
}
202-
this.activate( event, result );
193+
result;
194+
this.active.nextAll( ".ui-menu-item" ).each( function() {
195+
var close = $( this ).offset().top - base - height;
196+
if (close >= 0) {
197+
result = $( this );
198+
return false;
199+
}
200+
});
201+
202+
this.activate( event, result || this.element.children( ".ui-menu-item" ).last() );
203203
} else {
204204
this.activate( event, this.element.children( ".ui-menu-item" )
205205
// TODO use .first()/.last()
@@ -216,18 +216,17 @@ $.widget("ui.menu", {
216216
}
217217

218218
var base = this.active.offset().top,
219-
height = this.element.height();
220-
result = this.element.children( ".ui-menu-item" ).filter( function() {
221-
var close = $(this).offset().top - base + height - $(this).height();
222-
// TODO improve approximation
223-
return close < 10 && close > -10;
224-
});
225-
226-
// TODO try to catch this earlier when scrollTop indicates the last page anyway
227-
if (!result.length) {
228-
result = this.element.children( ".ui-menu-item" ).first();
229-
}
230-
this.activate( event, result );
219+
height = this.element.height(),
220+
result;
221+
this.active.prevAll( ".ui-menu-item" ).each( function() {
222+
var close = $(this).offset().top - base + height;
223+
if (close <= 0) {
224+
result = $( this );
225+
return false;
226+
}
227+
});
228+
229+
this.activate( event, result || this.element.children( ".ui-menu-item" ).first() );
231230
} else {
232231
this.activate( event, this.element.children( ".ui-menu-item" )
233232
// TODO use .first()/.last()

0 commit comments

Comments
 (0)