From 28d9bc36ecd71c2b1f9935d8ed03705e33220d95 Mon Sep 17 00:00:00 2001 From: Cleric-K <9365881+Cleric-K@users.noreply.github.com> Date: Thu, 1 Aug 2019 13:11:26 +0300 Subject: [PATCH] CliAutocomplete fix mouse activation #1481 The mouse cursor selects an item only if the mouse is moved --- src/css/tabs-dark/cli-dark.css | 1 - src/css/tabs/cli.css | 1 - src/js/CliAutoComplete.js | 35 ++++++++++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/css/tabs-dark/cli-dark.css b/src/css/tabs-dark/cli-dark.css index affeebe6..b3491d39 100644 --- a/src/css/tabs-dark/cli-dark.css +++ b/src/css/tabs-dark/cli-dark.css @@ -24,7 +24,6 @@ .cli-textcomplete-dropdown a { color: white; } -.cli-textcomplete-dropdown li:hover, .cli-textcomplete-dropdown .active { background-color: var(--quietHeader); } diff --git a/src/css/tabs/cli.css b/src/css/tabs/cli.css index f6e4e538..7d625baf 100644 --- a/src/css/tabs/cli.css +++ b/src/css/tabs/cli.css @@ -121,7 +121,6 @@ padding: 2px 5px; } -.cli-textcomplete-dropdown li:hover, .cli-textcomplete-dropdown .active { background-color: rgb(255, 187, 0); } diff --git a/src/js/CliAutoComplete.js b/src/js/CliAutoComplete.js index 18055543..84f1d97f 100644 --- a/src/js/CliAutoComplete.js +++ b/src/js/CliAutoComplete.js @@ -196,6 +196,8 @@ CliAutoComplete._initTextcomplete = function() { var $textarea = this.$textarea; var cache = self.cache; + var savedMouseoverItemHandler = null; + // helper functions var highlighter = function(anywhere) { return function(value, term) { @@ -261,7 +263,36 @@ CliAutoComplete._initTextcomplete = function() { } } } - ); + ) + .on('textComplete:show', function(e) { + /** + * The purpose of this code is to disable initially the `mouseover` menu item handler. + * Normally, when the menu pops up, if the mouse cursor is in the same area, + * the `mouseover` event triggers immediately and activates the item under + * the cursor. This might be undesirable when using the keyboard. + * + * Here we save the original `mouseover` handler and remove it on popup show. + * Then add `mousemove` handler. If the mouse moves we consider that mouse interaction + * is desired so we reenable the `mouseover` handler + */ + if (!savedMouseoverItemHandler) { + // save the original 'mouseover' handeler + savedMouseoverItemHandler = $._data($('.textcomplete-dropdown')[0], 'events').mouseover[0].handler; + } + + $('.textcomplete-dropdown') + .off('mouseover') // initially disable it + .off('mousemove') // avoid `mousemove` accumulation if previous show did not trigger `mousemove` + .on('mousemove', '.textcomplete-item', function(e) { + // the mouse has moved so reenable `mouseover` + $(this).parent() + .off('mousemove') + .on('mouseover', '.textcomplete-item', savedMouseoverItemHandler); + + // trigger the mouseover handler to select the item under the cursor + savedMouseoverItemHandler(e); + }); + }); // textcomplete autocomplete strategies @@ -290,7 +321,7 @@ CliAutoComplete._initTextcomplete = function() { search: function(term, callback) { sendOnEnter = true; searcher(term, function(arr) { - if (arr.length > 1) { + if (term.length > 0 && arr.length > 1) { // prepend the uncompleted term in the popup arr = [term].concat(arr); }