From 04a16a2a36d2cafd9948512c5b87c1705567b449 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 22 Feb 2015 18:01:51 +0100 Subject: [PATCH] Added delete key support for auto complete --- public_html/js/Controls/AutoCompleteInput.js | 33 +++++++++++--------- public_html/js/Controls/TagInput.js | 4 +++ 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/public_html/js/Controls/AutoCompleteInput.js b/public_html/js/Controls/AutoCompleteInput.js index 27bc20ad..fdae3e9a 100644 --- a/public_html/js/Controls/AutoCompleteInput.js +++ b/public_html/js/Controls/AutoCompleteInput.js @@ -7,6 +7,7 @@ App.Controls.AutoCompleteInput = function($input) { var tagList = App.DI.get('tagList'); var KEY_RETURN = 13; + var KEY_DELETE = 46; var KEY_ESCAPE = 27; var KEY_UP = 38; var KEY_DOWN = 40; @@ -17,6 +18,7 @@ App.Controls.AutoCompleteInput = function($input) { maxResults: 15, minLengthToArbitrarySearch: 3, onApply: null, + onDelete: null, onRender: null, additionalFilter: null, }; @@ -63,27 +65,24 @@ App.Controls.AutoCompleteInput = function($input) { } $input.bind('keydown', function(e) { + var func = null; if (isShown() && e.which === KEY_ESCAPE) { - e.preventDefault(); - e.stopPropagation(); - e.stopImmediatePropagation(); - hide(); + func = hide; } else if (isShown() && e.which === KEY_DOWN) { - e.preventDefault(); - e.stopPropagation(); - e.stopImmediatePropagation(); - selectNext(); + func = selectNext; } else if (isShown() && e.which === KEY_UP) { - e.preventDefault(); - e.stopPropagation(); - e.stopImmediatePropagation(); - selectPrevious(); + func = selectPrevious; } else if (isShown() && e.which === KEY_RETURN && activeResult >= 0) { + func = function() { applyAutocomplete(); hide(); }; + } else if (isShown() && e.which === KEY_DELETE && activeResult >= 0) { + func = function() { applyDelete(); hide(); }; + } + + if (func !== null) { e.preventDefault(); e.stopPropagation(); e.stopImmediatePropagation(); - applyAutocomplete(); - hide(); + func(); } else { window.clearTimeout(showTimeout); showTimeout = window.setTimeout(showOrHide, 250); @@ -182,6 +181,12 @@ App.Controls.AutoCompleteInput = function($input) { } } + function applyDelete() { + if (options.onDelete) { + options.onDelete(results[activeResult].tag); + } + } + function applyAutocomplete() { if (options.onApply) { options.onApply(results[activeResult].tag); diff --git a/public_html/js/Controls/TagInput.js b/public_html/js/Controls/TagInput.js index 34843588..8a7cb1be 100644 --- a/public_html/js/Controls/TagInput.js +++ b/public_html/js/Controls/TagInput.js @@ -74,6 +74,10 @@ App.Controls.TagInput = function($underlyingInput) { function initAutoComplete() { var autoComplete = new App.Controls.AutoCompleteInput($input); + autoComplete.onDelete = function(text) { + removeTag(text); + $input.val(''); + }; autoComplete.onApply = function(text) { processText(text, SOURCE_AUTOCOMPLETION); $input.val('');