Skip to content

Instantly share code, notes, and snippets.

Created August 20, 2014 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1ce76e0ee6e70bd6b919 to your computer and use it in GitHub Desktop.
Save anonymous/1ce76e0ee6e70bd6b919 to your computer and use it in GitHub Desktop.
$('body').on('keyup.search', function(e) {
if ($('#search-dropdown').is(':visible')) {
console.log(e.which);
var selectedIndex = Discourse.__container__.lookup("controller:search").get("selectedIndex");
var lastIndex = Discourse.__container__.lookup("controller:search").get("lastIndex");
if (e.which == 9) {
if (document.activeElement) {
selectedElementId = document.activeElement.parentElement.id; //focused element id
if (Ember.View.views.hasOwnProperty(selectedElementId)) { //check if we have an emberview with that id. if yes we assume it is a search result item
var ElementIndex = Ember.View.views[document.activeElement.parentElement.id].content.index; //get the element index
Discourse.__container__.lookup("controller:search").set("selectedIndex", ElementIndex); //and set the item to be selected
} else { //if the element in focus is not a search result. probably a show more, unselect the selected item
var lastIndex = Discourse.__container__.lookup("controller:search").get("selectedIndex");
Discourse.__container__.lookup("controller:search").set("lastIndex", lastIndex);
Discourse.__container__.lookup("controller:search").set("selectedIndex", -1);
}
}
} else if (e.which == 13) { //allow click on "show more" or "(x)" button in the view
if (document.activeElement.classList.contains("filter")) {
$(document.activeElement).click();
}
} else if (e.which == 38) { //arrow up/down restore from unselected mode
if (selectedIndex == -1) {
Discourse.__container__.lookup("controller:search").set("selectedIndex", lastIndex);
}
if (document.activeElement != $("#search-term")[0]) { //make sure search input is selected if the user stopped using tab navigation
$("#search-term").focus(); //assure focus on input
Discourse.__container__.lookup("controller:search").set("selectedIndex", lastIndex); //TODO in some cases lastIndex will no be most up to date and it can we when se it to selected again
}
} else if (e.which == 40) {
if (selectedIndex == -1) {
if (lastIndex == 0) lastIndex = 1;
Discourse.__container__.lookup("controller:search").set("selectedIndex", lastIndex - 1);
}
if (document.activeElement != $("#search-term")[0]) { //make sure search input is selected if the user stopped using tab navigation
$("#search-term").focus(); //assure focus on input
Discourse.__container__.lookup("controller:search").set("selectedIndex", lastIndex);
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment