Key through search results

v1.18.x
Peter Jaszkowiak 8 years ago
parent f1cfed50a1
commit 8ca98625b9

@ -2,6 +2,15 @@
.dropdown-menu { .dropdown-menu {
max-height: 75vh; max-height: 75vh;
overflow-y: auto; overflow-y: auto;
> li > a {
&.focus {
&:extend(.dropdown-menu>li>a:focus);
}
&:focus {
outline: none;
}
}
} }
.state-start-typing { .state-start-typing {

@ -1,7 +1,7 @@
"use strict"; "use strict";
/* globals socket, app, define, ajaxify, config */ /* globals socket, app, define, ajaxify, config */
define(function () { define(['mousetrap'], function (mousetrap) {
var search = {}; var search = {};
function nsToTitle(namespace) { function nsToTitle(namespace) {
@ -67,8 +67,11 @@ define(function () {
}); });
$('#acp-search').parents('form').on('submit', function (ev) { $('#acp-search').parents('form').on('submit', function (ev) {
var firstResult = menu.find('li:first-child > a').attr('href'); var selected = menu.find('li.result > a.focus').attr('href');
var href = firstResult ? firstResult : config.relative_path + '/search/' + input.val(); if (!selected.length) {
selected = menu.find('li.result > a').first().attr('href');
}
var href = selected ? selected : config.relative_path + '/search/' + input.val();
ajaxify.go(href.replace(/^\//, '')); ajaxify.go(href.replace(/^\//, ''));
@ -81,8 +84,41 @@ define(function () {
return false; return false;
}); });
mousetrap(input[0]).bind(['up', 'down'], function (ev, key) {
var next;
if (key === 'up') {
next = menu.find('li.result > a.focus').removeClass('focus').parent().prev('.result').children();
if (!next.length) {
next = menu.find('li.result > a').last();
}
next.addClass('focus');
if (menu[0].getBoundingClientRect().top > next[0].getBoundingClientRect().top) {
next[0].scrollIntoView(true);
}
} else if (key === 'down') {
next = menu.find('li.result > a.focus').removeClass('focus').parent().next('.result').children();
if (!next.length) {
next = menu.find('li.result > a').first();
}
next.addClass('focus');
if (menu[0].getBoundingClientRect().bottom < next[0].getBoundingClientRect().bottom) {
next[0].scrollIntoView(false);
}
}
ev.preventDefault();
});
var prevValue;
input.on('keyup focus', function () { input.on('keyup focus', function () {
var value = input.val().toLowerCase(); var value = input.val().toLowerCase();
if (value === prevValue) {
return;
}
prevValue = value;
menu.children('.result').remove(); menu.children('.result').remove();
var len = value.length; var len = value.length;
@ -106,7 +142,7 @@ define(function () {
.find('strong') .find('strong')
.html(value); .html(value);
} else { } else {
menu.removeClass('state-no-results'); menu.removeClass('state-no-results state-yes-results');
} }
}); });
} }

Loading…
Cancel
Save