Added delete key support for auto complete

This commit is contained in:
Marcin Kurczewski 2015-02-22 18:01:51 +01:00
parent 72e9400e1d
commit 04a16a2a36
2 changed files with 23 additions and 14 deletions

View file

@ -7,6 +7,7 @@ App.Controls.AutoCompleteInput = function($input) {
var tagList = App.DI.get('tagList'); var tagList = App.DI.get('tagList');
var KEY_RETURN = 13; var KEY_RETURN = 13;
var KEY_DELETE = 46;
var KEY_ESCAPE = 27; var KEY_ESCAPE = 27;
var KEY_UP = 38; var KEY_UP = 38;
var KEY_DOWN = 40; var KEY_DOWN = 40;
@ -17,6 +18,7 @@ App.Controls.AutoCompleteInput = function($input) {
maxResults: 15, maxResults: 15,
minLengthToArbitrarySearch: 3, minLengthToArbitrarySearch: 3,
onApply: null, onApply: null,
onDelete: null,
onRender: null, onRender: null,
additionalFilter: null, additionalFilter: null,
}; };
@ -63,27 +65,24 @@ App.Controls.AutoCompleteInput = function($input) {
} }
$input.bind('keydown', function(e) { $input.bind('keydown', function(e) {
var func = null;
if (isShown() && e.which === KEY_ESCAPE) { if (isShown() && e.which === KEY_ESCAPE) {
e.preventDefault(); func = hide;
e.stopPropagation();
e.stopImmediatePropagation();
hide();
} else if (isShown() && e.which === KEY_DOWN) { } else if (isShown() && e.which === KEY_DOWN) {
e.preventDefault(); func = selectNext;
e.stopPropagation();
e.stopImmediatePropagation();
selectNext();
} else if (isShown() && e.which === KEY_UP) { } else if (isShown() && e.which === KEY_UP) {
e.preventDefault(); func = selectPrevious;
e.stopPropagation();
e.stopImmediatePropagation();
selectPrevious();
} else if (isShown() && e.which === KEY_RETURN && activeResult >= 0) { } 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.preventDefault();
e.stopPropagation(); e.stopPropagation();
e.stopImmediatePropagation(); e.stopImmediatePropagation();
applyAutocomplete(); func();
hide();
} else { } else {
window.clearTimeout(showTimeout); window.clearTimeout(showTimeout);
showTimeout = window.setTimeout(showOrHide, 250); 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() { function applyAutocomplete() {
if (options.onApply) { if (options.onApply) {
options.onApply(results[activeResult].tag); options.onApply(results[activeResult].tag);

View file

@ -74,6 +74,10 @@ App.Controls.TagInput = function($underlyingInput) {
function initAutoComplete() { function initAutoComplete() {
var autoComplete = new App.Controls.AutoCompleteInput($input); var autoComplete = new App.Controls.AutoCompleteInput($input);
autoComplete.onDelete = function(text) {
removeTag(text);
$input.val('');
};
autoComplete.onApply = function(text) { autoComplete.onApply = function(text) {
processText(text, SOURCE_AUTOCOMPLETION); processText(text, SOURCE_AUTOCOMPLETION);
$input.val(''); $input.val('');