Merge pull request #4005 from tgxworld/close_autocomplete_if_term_is_blank

FIX: Close autocomplete is term is blank.
This commit is contained in:
Régis Hanol 2016-02-10 12:57:50 +01:00
commit 510f9c5bed
1 changed files with 13 additions and 4 deletions

View File

@ -242,6 +242,15 @@ export default function(options) {
}); });
}; };
const dataSource = (term, opts) => {
if (term.length !== 0 && term.trim().length === 0) {
closeAutocomplete();
return null;
} else {
return opts.dataSource(term);
}
};
var updateAutoComplete = function(r) { var updateAutoComplete = function(r) {
if (completeStart === null) return; if (completeStart === null) return;
@ -304,13 +313,13 @@ export default function(options) {
var prevChar = me.val().charAt(caretPosition - 1); var prevChar = me.val().charAt(caretPosition - 1);
if (checkTriggerRule() && (!prevChar || allowedLettersRegex.test(prevChar))) { if (checkTriggerRule() && (!prevChar || allowedLettersRegex.test(prevChar))) {
completeStart = completeEnd = caretPosition; completeStart = completeEnd = caretPosition;
updateAutoComplete(options.dataSource("")); updateAutoComplete(dataSource("", options));
} }
} else if ((completeStart !== null) && (e.charCode !== 0)) { } else if ((completeStart !== null) && (e.charCode !== 0)) {
caretPosition = Discourse.Utilities.caretPosition(me[0]); caretPosition = Discourse.Utilities.caretPosition(me[0]);
term = me.val().substring(completeStart + (options.key ? 1 : 0), caretPosition); term = me.val().substring(completeStart + (options.key ? 1 : 0), caretPosition);
term += String.fromCharCode(e.charCode); term += String.fromCharCode(e.charCode);
updateAutoComplete(options.dataSource(term)); updateAutoComplete(dataSource(term, options));
} }
}); });
@ -360,7 +369,7 @@ export default function(options) {
completeStart = c; completeStart = c;
caretPosition = completeEnd = initial; caretPosition = completeEnd = initial;
term = me[0].value.substring(c + 1, initial); term = me[0].value.substring(c + 1, initial);
updateAutoComplete(options.dataSource(term)); updateAutoComplete(dataSource(term, options));
return true; return true;
} }
} }
@ -444,7 +453,7 @@ export default function(options) {
closeAutocomplete(); closeAutocomplete();
} }
updateAutoComplete(options.dataSource(term)); updateAutoComplete(dataSource(term, options));
return true; return true;
default: default:
completeEnd = caretPosition; completeEnd = caretPosition;