NIHVIVO-747 Apply client-side autocomplete filtering to add authors form

This commit is contained in:
rjy7 2010-07-12 16:07:59 +00:00
parent 57a8cdcbe8
commit 266dfadee0
2 changed files with 49 additions and 22 deletions

View file

@ -401,14 +401,14 @@ SPARQL queries for existing values. --%>
<p id="requiredLegend" class="requiredHint">* required fields</p> <p id="requiredLegend" class="requiredHint">* required fields</p>
</form> </form>
<c:url var="baseAcUrl" value="/autocomplete?type=${foaf}Person&tokenize=false&stem=false" /> <c:url var="acUrl" value="/autocomplete?type=${foaf}Person&tokenize=false&stem=false" />
<c:url var="reorderUrl" value="/edit/primitiveRdfEdit" /> <c:url var="reorderUrl" value="/edit/primitiveRdfEdit" />
<script type="text/javascript"> <script type="text/javascript">
var customFormData = { var customFormData = {
rankPred: '${rankPred}', rankPred: '${rankPred}',
rankXsdType: '${intDatatypeUri}', rankXsdType: '${intDatatypeUri}',
baseAcUrl: '${baseAcUrl}', acUrl: '${acUrl}',
reorderUrl: '${reorderUrl}' reorderUrl: '${reorderUrl}'
}; };
</script> </script>

View file

@ -160,8 +160,8 @@ var addAuthorForm = {
// Make cache a property of this so we can access it after removing // Make cache a property of this so we can access it after removing
// an author. // an author.
this.acCache = {}; this.acCache = {};
this.setAcUrl(); this.setAcFilter();
$('#lastName').autocomplete({ $('#lastName').autocomplete({
minLength: 2, minLength: 2,
@ -179,13 +179,16 @@ var addAuthorForm = {
$.ajax({ $.ajax({
url: addAuthorForm.acUrl, url: addAuthorForm.acUrl,
dataType: 'json', dataType: 'json',
data: request, data: {
term: request.term
},
complete: function(xhr, status) { complete: function(xhr, status) {
// Not sure why, but we need an explicit json parse here. jQuery // Not sure why, but we need an explicit json parse here. jQuery
// should parse the response text and return a json object. // should parse the response text and return a json object.
var results = jQuery.parseJSON(xhr.responseText); var results = jQuery.parseJSON(xhr.responseText),
addAuthorForm.acCache[request.term] = results; filteredResults = addAuthorForm.filterAcResults(results);
response(results); addAuthorForm.acCache[request.term] = filteredResults;
response(filteredResults);
} }
}); });
@ -197,19 +200,41 @@ var addAuthorForm = {
}, },
setAcUrl: function() { setAcFilter: function() {
var url = this.baseAcUrl,
existingAuthors = $('#authorships .authorLink'); var existingAuthors = $('#authorships .authorLink');
this.acFilter = [];
//console.log('in setAcUrl()');
//console.log('number of existing authors: ' + existingAuthors.length);
existingAuthors.each(function() { existingAuthors.each(function() {
url += '&excludeUri=' + $(this).attr('id'); var uri = $(this).attr('id');
}); addAuthorForm.acFilter.push(uri);
});
this.acUrl = url;
}, },
removeAuthorFromAcFilter: function(author) {
var index = $.inArray(author, this.acFilter);
if (index > -1) { // this should always be true
this.acFilter.splice(index, 1);
}
},
filterAcResults: function(results) {
var filteredResults = [];
if (!this.acFilter.length) {
return results;
}
$.each(results, function() {
if ($.inArray(this.uri, addAuthorForm.acFilter) == -1) {
console.log("adding " + this.label + " to filtered results");
filteredResults.push(this);
}
else {
console.log("filtering out " + this.label);
}
});
return filteredResults;
},
// Action taken after selecting an author from the autocomplete list // Action taken after selecting an author from the autocomplete list
showSelectedAuthor: function(ui) { showSelectedAuthor: function(ui) {
@ -517,6 +542,7 @@ var addAuthorForm = {
complete: function(request, status) { complete: function(request, status) {
var authorship = $(this).parents('.authorship'), var authorship = $(this).parents('.authorship'),
nextAuthorships = authorship.nextAll(), nextAuthorships = authorship.nextAll(),
author = authorship.find('.authorLink').attr('id'),
rank; rank;
// author = $(this).siblings('span.author'), // author = $(this).siblings('span.author'),
// authorLink = author.children('a.authorLink'), // authorLink = author.children('a.authorLink'),
@ -543,7 +569,11 @@ var addAuthorForm = {
// In future, do this selectively by only clearing terms that match the // In future, do this selectively by only clearing terms that match the
// deleted author's name // deleted author's name
addAuthorForm.acCache = {}; addAuthorForm.acCache = {};
// Remove this author from the acFilter so it can be returned in autocomplete
// results again.
addAuthorForm.removeAuthorFromAcFilter(author);
authorship.fadeOut(400, function() { authorship.fadeOut(400, function() {
$(this).remove(); $(this).remove();
// Actions that depend on the author having been removed from the DOM: // Actions that depend on the author having been removed from the DOM:
@ -551,9 +581,6 @@ var addAuthorForm = {
if ($('.authorship').length == 1) { if ($('.authorship').length == 1) {
addAuthorForm.disableAuthorDD(); addAuthorForm.disableAuthorDD();
} }
// Reset the excluded uris in the autocomplete url so that the
//author just removed is no longer excluded.
addAuthorForm.setAcUrl();
}); });
// $(this).hide(); // $(this).hide();