From c7186b14036420ec66bc7136b7059d02683736a1 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 10 Feb 2016 19:47:45 +0800 Subject: [PATCH] FIX: Close autocomplete is term is blank. --- .../discourse/lib/autocomplete.js.es6 | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/autocomplete.js.es6 b/app/assets/javascripts/discourse/lib/autocomplete.js.es6 index 71dce2d1d68..5da0fb6b02f 100644 --- a/app/assets/javascripts/discourse/lib/autocomplete.js.es6 +++ b/app/assets/javascripts/discourse/lib/autocomplete.js.es6 @@ -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) { if (completeStart === null) return; @@ -304,13 +313,13 @@ export default function(options) { var prevChar = me.val().charAt(caretPosition - 1); if (checkTriggerRule() && (!prevChar || allowedLettersRegex.test(prevChar))) { completeStart = completeEnd = caretPosition; - updateAutoComplete(options.dataSource("")); + updateAutoComplete(dataSource("", options)); } } else if ((completeStart !== null) && (e.charCode !== 0)) { caretPosition = Discourse.Utilities.caretPosition(me[0]); term = me.val().substring(completeStart + (options.key ? 1 : 0), caretPosition); term += String.fromCharCode(e.charCode); - updateAutoComplete(options.dataSource(term)); + updateAutoComplete(dataSource(term, options)); } }); @@ -360,7 +369,7 @@ export default function(options) { completeStart = c; caretPosition = completeEnd = initial; term = me[0].value.substring(c + 1, initial); - updateAutoComplete(options.dataSource(term)); + updateAutoComplete(dataSource(term, options)); return true; } } @@ -444,7 +453,7 @@ export default function(options) { closeAutocomplete(); } - updateAutoComplete(options.dataSource(term)); + updateAutoComplete(dataSource(term, options)); return true; default: completeEnd = caretPosition;