From 007b8264450b694be57d2b1f0c8d301acbe826b8 Mon Sep 17 00:00:00 2001 From: Frans de Jonge Date: Fri, 3 Feb 2017 15:32:01 +0100 Subject: [PATCH] Improved keyboard usability * Enter immediately loads the first result * Down arrow immediately looks up results and focuses the first entry of the result list * Escape clears input * Up and down arrows focus links in the result list * Right arrow loads the currently focused result and focuses on article * Escape clears and focuses input * Enter focuses input (without clearing) * Left arrow focuses the current article's entry in the result list if left scrolling is no longer an option * Escape clears and focuses input --- src/script.js | 167 +++++++++++++++++++++++++++++++++++++++++++++++--- src/style.css | 2 +- 2 files changed, 161 insertions(+), 8 deletions(-) diff --git a/src/script.js b/src/script.js index 5310340..9323c37 100644 --- a/src/script.js +++ b/src/script.js @@ -1,6 +1,8 @@ $( function() { + var dontClearContent = false; + var previousLookup = ''; var scheduledLookupID; var $word = $('#word'); @@ -13,6 +15,7 @@ $( var defaultStyle = 'Default'; $content.on('load', function(){ + $content.contents().on('keydown', onContentKeyDown); try { var contentLocation = $content.contents().attr('location'); if (contentLocation.href === 'about:blank') { @@ -85,6 +88,7 @@ $( }); var doLookup = function(dontClearContent) { + var deferred = $.Deferred(); var word = $word.val(); console.log(word); $lookupResult.empty(); @@ -93,12 +97,14 @@ $( } if (!word) { - return; + return deferred.reject(); } + previousLookup = word; $.getJSON('/find/?key='+encodeURIComponent(word), function(data) { - if (!data || data.length == 0) { + if (!data || data.length === 0) { var $div = $('
').attr('align', 'center').text('Nothing found'); $lookupResult.append($div); + deferred.reject(); return; } var $ul = $('