DEV: allows autocomplete to be debounced (#10166)

Usage:

```
$(".foo").autocomplete({debounced: true});
```
This commit is contained in:
Joffrey JAFFEUX 2020-07-06 09:28:57 +02:00 committed by GitHub
parent b2a2e4f1e5
commit 789a15a897
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -25,6 +25,7 @@ export default Component.extend({
let groupNames = this.groupNames; let groupNames = this.groupNames;
$(this.element.querySelector("input")).autocomplete({ $(this.element.querySelector("input")).autocomplete({
debounced: true,
allowAny: false, allowAny: false,
items: _.isArray(groupNames) items: _.isArray(groupNames)
? groupNames ? groupNames

View File

@ -1,4 +1,5 @@
import { cancel, later } from "@ember/runloop"; import { INPUT_DELAY } from "discourse-common/config/environment";
import { debounce, cancel, later } from "@ember/runloop";
import { iconHTML } from "discourse-common/lib/icon-library"; import { iconHTML } from "discourse-common/lib/icon-library";
import { setCaretPosition, caretPosition } from "discourse/lib/utilities"; import { setCaretPosition, caretPosition } from "discourse/lib/utilities";
import Site from "discourse/models/site"; import Site from "discourse/models/site";
@ -413,6 +414,14 @@ export default function(options) {
} }
$(this).on("keyup.autocomplete", function(e) { $(this).on("keyup.autocomplete", function(e) {
if (options.debounced) {
debounce(this, performAutocomplete, e, INPUT_DELAY);
} else {
performAutocomplete(e);
}
});
function performAutocomplete(e) {
if ([keys.esc, keys.enter].indexOf(e.which) !== -1) return true; if ([keys.esc, keys.enter].indexOf(e.which) !== -1) return true;
let cp = caretPosition(me[0]); let cp = caretPosition(me[0]);
@ -445,7 +454,7 @@ export default function(options) {
let term = me.val().substring(completeStart + (options.key ? 1 : 0), cp); let term = me.val().substring(completeStart + (options.key ? 1 : 0), cp);
updateAutoComplete(dataSource(term, options)); updateAutoComplete(dataSource(term, options));
} }
}); }
$(this).on("keydown.autocomplete", function(e) { $(this).on("keydown.autocomplete", function(e) {
var c, i, initial, prev, prevIsGood, stopFound, term, total, userToComplete; var c, i, initial, prev, prevIsGood, stopFound, term, total, userToComplete;