function cancelPendingAutoComplete() {
clearTimeout(autoCompleteDebounceTimer);
autoCompleteDebounceKey = null;
if (currentAutoCompleteRequest != null) {
currentAutoCompleteRequest.abort();
currentAutoCompleteRequest = null;
}
currentAutoCompleteRequestKey = null;
respondToPendingAutoComplete([]);
}
function createWidgetAutoCompleteSource(options) {
return function (request, response) {
var term = normalizeAutoCompleteTerm(request.term);
if (!options.shouldSearch(term)) {
response([]);
return;
}
var requestUrl = options.url();
var cacheKey = getAutoCompleteCacheKey(requestUrl, term);
if (Object.prototype.hasOwnProperty.call(autoCompleteCache, cacheKey)) {
response(autoCompleteCache[cacheKey]);
if (options.success) {
options.success(autoCompleteCache[cacheKey]);
}
return;
}
if (currentAutoCompleteRequestKey === cacheKey || autoCompleteDebounceKey === cacheKey) {
pendingAutoCompleteResponses.push(response);
return;
}
cancelPendingAutoComplete();
autoCompleteDebounceKey = cacheKey;
pendingAutoCompleteResponses.push(response);
autoCompleteDebounceTimer = setTimeout(function () {
autoCompleteDebounceKey = null;
currentAutoCompleteRequestKey = cacheKey;
currentAutoCompleteRequest = $.ajax({
url: requestUrl,
dataType: "json",
timeout: options.timeout,
data: {
term: term,
},
success: function (data) {
if (currentAutoCompleteRequestKey === cacheKey) {
cacheAutoCompleteResult(cacheKey, data);
respondToPendingAutoComplete(data);
if (options.success) {
options.success(data);
}
}
},
error: function (xmlhttprequest, textstatus, message) {
if (textstatus === "abort") {
return;
}
if (options.error) {
options.error(xmlhttprequest, textstatus, message);
}