Skip to content

Commit 184ad69

Browse files
committed
Menu: Refactored menu to use .first()/.last() instead of :first/:last whereever possible
1 parent 1bd57c7 commit 184ad69

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

ui/jquery.ui.menu.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ $.widget("ui.menu", {
131131
this.element.attr( "scrollTop", scroll + offset - elementHeight + item.height() );
132132
}
133133
}
134-
this.active = item.eq( 0 )
134+
this.active = item.first()
135135
.children( "a" )
136136
.addClass( "ui-state-hover" )
137137
.attr( "id", "ui-active-menuitem" )
@@ -150,11 +150,11 @@ $.widget("ui.menu", {
150150
},
151151

152152
next: function(event) {
153-
this._move( "next", ".ui-menu-item:first", event );
153+
this._move( "next", ".ui-menu-item", "first", event );
154154
},
155155

156156
previous: function(event) {
157-
this._move( "prev", ".ui-menu-item:last", event );
157+
this._move( "prev", ".ui-menu-item", "last", event );
158158
},
159159

160160
first: function() {
@@ -165,69 +165,73 @@ $.widget("ui.menu", {
165165
return this.active && !this.active.nextAll( ".ui-menu-item" ).length;
166166
},
167167

168-
_move: function(direction, edge, event) {
168+
_move: function(direction, edge, filter, event) {
169169
if ( !this.active ) {
170-
this.activate( event, this.element.children(edge) );
170+
this.activate( event, this.element.children(edge)[filter]() );
171171
return;
172172
}
173173
var next = this.active[ direction + "All" ]( ".ui-menu-item" ).eq( 0 );
174174
if ( next.length ) {
175175
this.activate( event, next );
176176
} else {
177-
this.activate( event, this.element.children(edge) );
177+
this.activate( event, this.element.children(edge)[filter]() );
178178
}
179179
},
180180

181181
// TODO merge with previousPage
182182
nextPage: function( event ) {
183183
if ( this._hasScroll() ) {
184-
// TODO merge with no-scroll-else
185184
if ( !this.active || this.last() ) {
186-
this.activate( event, this.element.children(":first") );
185+
this.activate( event, this.element.children( ".ui-menu-item" ).first() );
187186
return;
188187
}
189188
var base = this.active.offset().top,
190189
height = this.element.height(),
191-
result = this.element.children( "li" ).filter( function() {
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() {
192193
var close = $( this ).offset().top - base - height + $( this ).height();
193-
// TODO improve approximation
194+
// TODO replace with check close > 0
194195
return close < 10 && close > -10;
195196
});
196197

197198
// TODO try to catch this earlier when scrollTop indicates the last page anyway
198199
if ( !result.length ) {
199-
result = this.element.children( ":last" );
200+
result = this.element.children( ".ui-menu-item" ).last();
200201
}
201202
this.activate( event, result );
202203
} else {
203-
this.activate( event, this.element.children( !this.active || this.last() ? ":first" : ":last" ) );
204+
this.activate( event, this.element.children( ".ui-menu-item" )
205+
// TODO use .first()/.last()
206+
.filter( !this.active || this.last() ? ":first" : ":last" ) );
204207
}
205208
},
206209

207210
// TODO merge with nextPage
208211
previousPage: function( event ) {
209212
if ( this._hasScroll() ) {
210-
// TODO merge with no-scroll-else
211213
if ( !this.active || this.first() ) {
212-
this.activate( event, this.element.children(":last") );
214+
this.activate( event, this.element.children( ".ui-menu-item" ).last() );
213215
return;
214216
}
215217

216218
var base = this.active.offset().top,
217219
height = this.element.height();
218-
result = this.element.children( "li" ).filter( function() {
220+
result = this.element.children( ".ui-menu-item" ).filter( function() {
219221
var close = $(this).offset().top - base + height - $(this).height();
220222
// TODO improve approximation
221223
return close < 10 && close > -10;
222224
});
223225

224226
// TODO try to catch this earlier when scrollTop indicates the last page anyway
225227
if (!result.length) {
226-
result = this.element.children( ":first" );
228+
result = this.element.children( ".ui-menu-item" ).first();
227229
}
228230
this.activate( event, result );
229231
} else {
230-
this.activate( event, this.element.children( !this.active || this.first() ? ":last" : ":first" ) );
232+
this.activate( event, this.element.children( ".ui-menu-item" )
233+
// TODO use .first()/.last()
234+
.filter( !this.active || this.first() ? ":last" : ":first" ) );
231235
}
232236
},
233237

0 commit comments

Comments
 (0)