From 3f266924b40a048c88bd2e26575ffc1785be5f50 Mon Sep 17 00:00:00 2001 From: j2blake Date: Thu, 27 Oct 2011 20:50:47 +0000 Subject: [PATCH] NIHVIVO-2343 Refactoring the URL and Query up another level. --- webapp/web/js/account/accountProxyCommon.js | 76 +++++++++++++ .../js/account/accountProxyProxiesPanel.js | 107 ++++++------------ 2 files changed, 110 insertions(+), 73 deletions(-) diff --git a/webapp/web/js/account/accountProxyCommon.js b/webapp/web/js/account/accountProxyCommon.js index d98e2088e..62c0b12c9 100644 --- a/webapp/web/js/account/accountProxyCommon.js +++ b/webapp/web/js/account/accountProxyCommon.js @@ -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 = ''; + } + +} + diff --git a/webapp/web/js/account/accountProxyProxiesPanel.js b/webapp/web/js/account/accountProxyProxiesPanel.js index f109cd290..6ba9e0e8e 100644 --- a/webapp/web/js/account/accountProxyProxiesPanel.js +++ b/webapp/web/js/account/accountProxyProxiesPanel.js @@ -1,6 +1,21 @@ /* $This file is distributed under the terms of the license in /doc/license.txt$ */ function proxyProxiesPanel(p) { + var query = "PREFIX fn: \n" + + "PREFIX auth: \n" + + "\n" + + "SELECT DISTINCT ?uri ?label ?externalAuthId \n" + + "WHERE { \n" + + " ?uri a auth:UserAccount ; \n" + + " auth:firstName ?firstName ; \n" + + " auth:lastName ?lastName . \n" + + " LET ( ?label := fn:concat(?lastName, ', ', ?firstName) )" + + " OPTIONAL { ?uri auth:externalAuthId ?externalAuthId } \n" + + " FILTER (REGEX(?label, '^%term%', 'i')) \n" + + "} \n" + + "ORDER BY ASC(?lastName) ASC(?firstName) \n" + + "LIMIT 25 \n"; + var self = this; this.disableFormInUnsupportedBrowsers = function() { @@ -45,10 +60,6 @@ function proxyProxiesPanel(p) { } } - this.setupAutoCompleteFields = function() { - this.addAutoCompleteField.autocomplete(new proxyAutocomplete(this)); - } - if (this.disableFormInUnsupportedBrowsers()) { return; } @@ -60,76 +71,26 @@ function proxyProxiesPanel(p) { this.parseProxyTemplate(); this.parseProxyData(); this.displayProxyData(); - this.setupAutoCompleteFields(); -} + + this.getProxyInfos = function() { + return self.proxyData; + } + + this.addProxyInfo = function(uri, label, junk1, junk2) { + self.proxyData.unshift(new proxyInfoElement(self.templateHtml, uri, label, "", "", false)); + self.displayProxyData(); + } + + this.setupAutoCompleteFields = function() { + var parms = { + query: query, + model: "userAccounts", + url: '../ajax/sparqlQuery' + }; + this.addAutoCompleteField.autocomplete(new proxyAutocomplete(parms, this.getProxyInfos, this.addProxyInfo)); + } -function proxyAutocomplete(parent) { - var cache = []; - - var query = "PREFIX fn: \n" - + "PREFIX auth: \n" - + "\n" - + "SELECT DISTINCT ?uri ?label ?externalAuthId \n" - + "WHERE { \n" - + " ?uri a auth:UserAccount ; \n" - + " auth:firstName ?firstName ; \n" - + " auth:lastName ?lastName . \n" - + " LET ( ?label := fn:concat(?lastName, ', ', ?firstName) )" - + " OPTIONAL { ?uri auth:externalAuthId ?externalAuthId } \n" - + " FILTER (REGEX(?label, '^%term%', 'i')) \n" - + "} \n" - + "ORDER BY ASC(?lastName) ASC(?firstName) \n" - + "LIMIT 25 \n"; - - var filterResults = function(parsed, data) { - var filtered = []; - for (var p = 0; p < parsed.length; p++) { - var dupe = false; - for (var d = 0; d < data.length; d++) { - if (data[d].uri == parsed[p].uri) { - dupe = true; - break; - } - } - if (!dupe) { - filtered.push(parsed[p]); - } - } - return filtered; - } - - this.minLength = 3, - - this.source = function(request, response) { - if (request.term in cache) { - var filtered = filterResults(cache[request.term], parent.proxyData); - response(filtered); - return; - } - $.ajax({ - url: '../ajax/sparqlQuery', - dataType: 'json', - data: { - query: query.replace("%term%", request.term), - model: "userAccounts" - }, - complete: function(xhr, status) { - var results = $.parseJSON(xhr.responseText); - var parsed = sparqlUtils.parseSparqlResults(results); - cache[request.term] = parsed; - var filtered = filterResults(parsed, parent.proxyData); - response(filtered); - } - }); - } - - this.select = function(event, ui) { - parent.proxyData.unshift(new proxyInfoElement(parent.templateHtml, ui.item.uri, ui.item.label, "", "", false)); - parent.displayProxyData(); - event.preventDefault(); - event.target.value = ''; - } - + this.setupAutoCompleteFields(); } $(document).ready(function() {