Skip to content
Snippets Groups Projects
Commit a81547d5 authored by Steven Wittens's avatar Steven Wittens
Browse files

#57255: Cancel autocomplete requests when the user tabs out of the field

parent 34eefb70
Branches
Tags 8.x-1.2 8.x.1.2
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -47,7 +47,7 @@ function jsAC(input, db) { ...@@ -47,7 +47,7 @@ function jsAC(input, db) {
this.db = db; this.db = db;
this.input.onkeydown = function (event) { return ac.onkeydown(this, event); }; this.input.onkeydown = function (event) { return ac.onkeydown(this, event); };
this.input.onkeyup = function (event) { ac.onkeyup(this, event) }; this.input.onkeyup = function (event) { ac.onkeyup(this, event) };
this.input.onblur = function () { ac.hidePopup() }; this.input.onblur = function () { ac.hidePopup(); ac.db.cancel(); };
this.popup = document.createElement('div'); this.popup = document.createElement('div');
this.popup.id = 'autocomplete'; this.popup.id = 'autocomplete';
this.popup.owner = this; this.popup.owner = this;
...@@ -240,7 +240,7 @@ ACDB.prototype.search = function(searchString) { ...@@ -240,7 +240,7 @@ ACDB.prototype.search = function(searchString) {
var db = this; var db = this;
this.timer = setTimeout(function() { this.timer = setTimeout(function() {
addClass(db.owner.input, 'throbbing'); addClass(db.owner.input, 'throbbing');
HTTPGet(db.uri +'/'+ encodeURIComponent(searchString), db.receive, db); db.transport = HTTPGet(db.uri +'/'+ encodeURIComponent(searchString), db.receive, db);
}, this.delay); }, this.delay);
} }
...@@ -263,3 +263,15 @@ ACDB.prototype.receive = function(string, xmlhttp, acdb) { ...@@ -263,3 +263,15 @@ ACDB.prototype.receive = function(string, xmlhttp, acdb) {
acdb.cache[acdb.searchString] = matches; acdb.cache[acdb.searchString] = matches;
acdb.owner.found(matches); acdb.owner.found(matches);
} }
/**
* Cancels the current autocomplete request
*/
ACDB.prototype.cancel = function() {
if (this.owner) removeClass(this.owner.input, 'throbbing');
if (this.timer) clearTimeout(this.timer);
if (this.transport) {
this.transport.onreadystatechange = function() {};
this.transport.abort();
}
}
...@@ -59,7 +59,7 @@ function HTTPGet(uri, callbackFunction, callbackParameter) { ...@@ -59,7 +59,7 @@ function HTTPGet(uri, callbackFunction, callbackParameter) {
} }
} }
} }
return true; return xmlHttp;
} }
else { else {
return xmlHttp.responseText; return xmlHttp.responseText;
...@@ -100,7 +100,7 @@ function HTTPPost(uri, callbackFunction, callbackParameter, object) { ...@@ -100,7 +100,7 @@ function HTTPPost(uri, callbackFunction, callbackParameter, object) {
} }
} }
} }
return true; return xmlHttp;
} }
else { else {
return xmlHttp.responseText; return xmlHttp.responseText;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment