Skip to main content

City of Northampton Housing Partnership, Legal Resources for Tenants and Landlords

Citation
City of Northampton Housing Partnership, Legal Resources for Tenants and Landlords
Source
Official source

Related Parts of This Source

Full Text

1,554 chars
function getAutoCompleteCacheKey(requestUrl, term) {
return requestUrl + "|" + term.toLowerCase();
}

function cacheAutoCompleteResult(cacheKey, data) {
if (!Object.prototype.hasOwnProperty.call(autoCompleteCache, cacheKey)) {
autoCompleteCacheKeys.push(cacheKey);
if (autoCompleteCacheKeys.length > autoCompleteCacheLimit) {
delete autoCompleteCache[autoCompleteCacheKeys.shift()];
}
}
autoCompleteCache[cacheKey] = data;
}

function respondToPendingAutoComplete(data) {
var responses = pendingAutoCompleteResponses;
pendingAutoCompleteResponses = [];
for (var i = 0; i < responses.length; i++) {
responses[i](data);
}
}

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;
}