NIHVIVO-2343 Refactoring the URL and Query up another level.

This commit is contained in:
j2blake 2011-10-27 20:50:47 +00:00
parent 13a1074602
commit 3f266924b4
2 changed files with 110 additions and 73 deletions

View file

@ -73,3 +73,79 @@ function proxyInfoElement(template, uri, label, classLabel, imageUrl, existing)
return element;
}
}
/*
* ----------------------------------------------------------------------------
* proxyAutoComplete
* ----------------------------------------------------------------------------
* Attach the autocomplete funcionality that we like in proxy panels.
*
* You provide:
* parms -- a map containing the URL of the AJAX controller, the query, and
* the model selector.
* getProxyInfos -- a function that will return an array of proxyInfoElements
* that are already present in the list and so should be filtered out of
* the autocomplete response.
* addProxyInfo -- a function that we can call when an item is selected.
* It will take the uri, label, classLabel, and imageUrl, build a
* proxyInfoElement, and add it to the panel.
* ----------------------------------------------------------------------------
* Before executing the AJAX request, the query from the parms map will be modified,
* replacing "%term%" with the current search term.
* ----------------------------------------------------------------------------
* The functionality includes:
* -- fetching data for the autocomplete list.
* -- cacheing the fetched data
* -- filtering as described above.
* -- calling addProxyInfo() and clearing the field when a value is selected.
* ----------------------------------------------------------------------------
*/
function proxyAutocomplete(parms, getProxyInfos, addProxyInfo) {
var cache = [];
var filterResults = function(parsed) {
var filtered = [];
var existingUris = $.map(getProxyInfos(), function(p) {
return p.uri;
});
$.each(parsed, function(i, p) {
if (-1 == $.inArray(p.uri, existingUris)) {
filtered.push(p);
}
});
return filtered;
}
this.minLength = 3,
this.source = function(request, response) {
if (request.term in cache) {
var filtered = filterResults(cache[request.term]);
response(filtered);
return;
}
$.ajax({
url: parms.url,
dataType: 'json',
data: {
model: parms.model,
query: parms.query.replace("%term%", request.term)
},
complete: function(xhr, status) {
var results = $.parseJSON(xhr.responseText);
var parsed = sparqlUtils.parseSparqlResults(results);
cache[request.term] = parsed;
var filtered = filterResults(parsed);
response(filtered);
}
});
}
this.select = function(event, ui) {
addProxyInfo(ui.item.uri, ui.item.label, ui.item.classLabel, ui.item.imageUrl);
event.preventDefault();
event.target.value = '';
}
}