Menu: Move item matching code into menu

This commit is contained in:
jzaefferer 2011-02-24 10:40:12 +01:00
parent 50958718c2
commit 5d3f91bcbe
3 changed files with 39 additions and 33 deletions

View File

@ -51,39 +51,7 @@
menu.widget().hide(); menu.widget().hide();
break; break;
default: default:
clearTimeout(menu.filterTimer);
var prev = menu.previousFilter || "";
var character = String.fromCharCode(event.keyCode);
var skip = false;
if (character == prev) {
skip = true;
} else {
character = prev + character;
}
var match = menu.widget().children("li").filter(function() {
return new RegExp("^" + character, "i").test($(this).text());
});
var match = skip && match.index(menu.active.next()) != -1 ? match.next() : match;
if (!match.length) {
character = String.fromCharCode(event.keyCode);
match = menu.widget().children("li").filter(function() {
return new RegExp("^" + character, "i").test($(this).text());
});
}
if (match.length) {
menu.focus(event, match);
if (match.length > 1) {
menu.previousFilter = character;
menu.filterTimer = setTimeout(function() {
delete menu.previousFilter;
}, 1000);
} else {
delete menu.previousFilter;
}
} else {
delete menu.previousFilter;
}
} }
}); });
}); });

View File

@ -50,7 +50,9 @@
<li><a href="#">Ada</a></li> <li><a href="#">Ada</a></li>
<li><a href="#">Adamsville</a></li> <li><a href="#">Adamsville</a></li>
<li><a href="#">Addyston</a></li> <li><a href="#">Addyston</a></li>
<li><a href="#">Adelphi</a></li> <li><a href="#">Delphi</a></li>
<li><a href="#">Saarland</a></li>
<li><a href="#">Salzburg</a></li>
</ul> </ul>
<ul id="menu2"> <ul id="menu2">

36
ui/jquery.ui.menu.js vendored
View File

@ -87,6 +87,42 @@ $.widget("ui.menu", {
event.preventDefault(); event.preventDefault();
event.stopImmediatePropagation(); event.stopImmediatePropagation();
break; break;
default:
clearTimeout(self.filterTimer);
var prev = self.previousFilter || "";
var character = String.fromCharCode(event.keyCode);
var skip = false;
if (character == prev) {
skip = true;
} else {
character = prev + character;
}
function escape(value) {
return value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
var match = self.widget().children(".ui-menu-item").filter(function() {
return new RegExp("^" + escape(character), "i").test($(this).text());
});
var match = skip && match.index(self.active.next()) != -1 ? self.active.nextAll(".ui-menu-item") : match;
if (!match.length) {
character = String.fromCharCode(event.keyCode);
match = self.widget().children(".ui-menu-item").filter(function() {
return new RegExp("^" + escape(character), "i").test($(this).text());
});
}
if (match.length) {
self.focus(event, match);
if (match.length > 1) {
self.previousFilter = character;
self.filterTimer = setTimeout(function() {
delete self.previousFilter;
}, 1000);
} else {
delete self.previousFilter;
}
} else {
delete self.previousFilter;
}
} }
}); });
}, },