NIHVIVO-646 Use autocomplete cache

This commit is contained in:
rjy7 2010-07-04 13:58:27 +00:00
parent dccd622b5b
commit 96d9b0508e

View file

@ -51,7 +51,7 @@ var addAuthorForm = {
this.initAutocomplete(); this.initAutocomplete();
this.initAuthorReordering(); this.initAuthorDD();
if (this.findValidationErrors()) { if (this.findValidationErrors()) {
this.initFormAfterInvalidSubmission(); this.initFormAfterInvalidSubmission();
@ -96,65 +96,70 @@ var addAuthorForm = {
}); });
this.removeAuthorshipLinks.click(function() { this.removeAuthorshipLinks.click(function() {
// RY Upgrade this to a modal window // RY Upgrade this to a modal window
var message = "Are you sure you want to remove this author?"; var message = "Are you sure you want to remove this author?";
if (!confirm(message)) { if (!confirm(message)) {
return false; return false;
} }
$.ajax({ $.ajax({
url: $(this).attr('href'), url: $(this).attr('href'),
type: 'POST', type: 'POST',
data: { data: {
deletion: $(this).parents('.authorship').attr('id') deletion: $(this).parents('.authorship').attr('id')
}, },
dataType: 'json', dataType: 'json',
context: $(this), // context for callback context: $(this), // context for callback
complete: function(request, status) { complete: function(request, status) {
var authorship = $(this).parents('.authorship'), var authorship = $(this).parents('.authorship'),
nextAuthorships = authorship.nextAll(), nextAuthorships = authorship.nextAll(),
rank; rank;
// author = $(this).siblings('span.author'), // author = $(this).siblings('span.author'),
// authorLink = author.children('a.authorLink'), // authorLink = author.children('a.authorLink'),
// authorName = authorLink.html(); // authorName = authorLink.html();
if (status === 'success') { if (status === 'success') {
if (nextAuthorships.length) { if (nextAuthorships.length) {
// Reset the position value of each succeeding authorship // Reset the position value of each succeeding authorship
nextAuthorships.each(function() { nextAuthorships.each(function() {
//var pos = parseInt($(this).children('.position').attr('id')); //var pos = parseInt($(this).children('.position').attr('id'));
//$(this).children('.position').attr('id', pos-1); //$(this).children('.position').attr('id', pos-1);
var pos = addAuthorForm.getPosition(this); var pos = addAuthorForm.getPosition(this);
addAuthorForm.setPosition(this, pos-1); addAuthorForm.setPosition(this, pos-1);
}); });
} else { } else {
// Removed author was last in rank: reset the rank hidden form field // Removed author was last in rank: reset the rank hidden form field
rank = addAuthorForm.getRank(authorship); rank = addAuthorForm.getRank(authorship);
$('input#rank').val(rank); $('input#rank').val(rank);
} }
// In future, do this selectively by only clearing terms that match the
// deleted author's name
addAuthorForm.acCache = {};
authorship.fadeOut(400, function() { authorship.fadeOut(400, function() {
$(this).remove(); $(this).remove();
// If there's just one author remaining, disable drag-drop // Actions that depend on the author having been removed from the DOM:
if ($('.authorship').length == 1) { // If there's just one author remaining, disable drag-drop
addAuthorForm.disableAuthorDD(); if ($('.authorship').length == 1) {
} addAuthorForm.disableAuthorDD();
// Reset the excluded uris in the autocomplete url so that the }
// author just removed is no longer excluded. // Reset the excluded uris in the autocomplete url so that the
$('#lastName').autocomplete('option', 'source', addAuthorForm.getAcUrl()); //author just removed is no longer excluded.
}); addAuthorForm.setAcUrl();
});
// $(this).hide(); // $(this).hide();
// $(this).siblings('.undo').show(); // $(this).siblings('.undo').show();
// author.html(authorName + ' has been removed'); // author.html(authorName + ' has been removed');
// author.css('width', 'auto'); // author.css('width', 'auto');
// author.effect("highlight", {}, 3000); // author.effect("highlight", {}, 3000);
} else { } else {
alert('Error processing request: author not removed'); alert('Error processing request: author not removed');
} }
} }
}); });
return false; return false;
}); });
// this.undoLinks.click(function() { // this.undoLinks.click(function() {
@ -305,7 +310,7 @@ var addAuthorForm = {
this.personUriField.val(''); this.personUriField.val('');
}, },
initAuthorReordering: function() { initAuthorDD: function() {
var authorshipList = $('#authorships'), var authorshipList = $('#authorships'),
authorships = authorshipList.children(); authorships = authorshipList.children();
@ -437,37 +442,39 @@ var addAuthorForm = {
initAutocomplete: function() { initAutocomplete: function() {
//var cache = {}; // Make cache a property of this so we can access it after removing
// RY change to this.cache = {} // an author.
// then we'll have access to it when removing an author this.acCache = {};
this.baseAcUrl = $('.acUrl').attr('id');
this.setAcUrl();
$('#lastName').autocomplete({ $('#lastName').autocomplete({
minLength: 2, minLength: 2,
source: addAuthorForm.getAcUrl(), source: function(request, response) {
// RY For now, not using cache because there are complex interactions between filtering and caching. if (request.term in addAuthorForm.acCache) {
// We want to filter out existingAuthors from autocomplete results, and this seems easiest to do console.log("found term in cache");
// server-side. But if an author gets removed, we need to put them back in the results. If results response(addAuthorForm.acCache[request.term]);
// are cached, the cache needs to be cleared on a remove. return;
// source: function(request, response) { }
// if (request.term in cache) { console.log("not getting term from cache");
// //console.log("found term in cache");
// response(cache[request.term]); // If the url query params are too long, we could do a post
// return; // here instead of a get. Add the exclude uris to the data
// } // rather than to the url.
// $.ajax({
// $.ajax({ url: addAuthorForm.acUrl,
// url: url, dataType: 'json',
// dataType: 'json', data: request,
// data: request, complete: function(xhr) {
// complete: function(data) { // Not sure why, but we need an explicit json parse here. jQuery
// cache[request.term] = data; // should parse the response text and return an json object.
// console.log(data); var results = jQuery.parseJSON(xhr.responseText);
// //console.log("not getting term from cache"); addAuthorForm.acCache[request.term] = results;
// response(data); response(results);
// } }
//
// }); });
// }, },
select: function(event, ui) { select: function(event, ui) {
addAuthorForm.showSelectedAuthor(ui); addAuthorForm.showSelectedAuthor(ui);
} }
@ -475,15 +482,18 @@ var addAuthorForm = {
}, },
getAcUrl: function() { setAcUrl: function() {
var url = $('.acUrl').attr('id'), var url = this.baseAcUrl,
existingAuthors = $('#authorships .authorLink'); existingAuthors = $('#authorships .authorLink');
//console.log("in setAcUrl()");
//console.log("number of existing authors: " + existingAuthors.length);
existingAuthors.each(function() { existingAuthors.each(function() {
url += '&excludeUri=' + $(this).attr('id'); url += '&excludeUri=' + $(this).attr('id');
}); });
return url; this.acUrl = url;
}, },
prepareSubmit: function() { prepareSubmit: function() {