NIHVIVO-646 Organized methods into logical groupings to improve readability.

This commit is contained in:
rjy7 2010-07-04 17:17:15 +00:00
parent 3772389dd7
commit 51b4fb24c2

View file

@ -2,6 +2,8 @@
var addAuthorForm = { var addAuthorForm = {
/* *** Initial page setup *** */
onLoad: function() { onLoad: function() {
this.mixIn(); this.mixIn();
this.initObjects(); this.initObjects();
@ -60,170 +62,8 @@ var addAuthorForm = {
} }
}, },
bindEventListeners: function() {
this.showFormButton.click(function() { /* *** Set up the various page views *** */
addAuthorForm.initFormView();
return false;
});
this.submit.click(function() {
// NB Important JavaScript scope issue: if we call it this way, this = addAuthorForm
// in prepareSubmit. If we do this.submit.click(prepareSubmit); then
// this != addAuthorForm in prepareSubmit.
addAuthorForm.prepareSubmit();
});
this.lastNameField.blur(function() {
addAuthorForm.onLastNameChange();
});
// This is somewhat questionable. If we return to the last name
// field to look again for an existing person, we'd want to hide those
// fields. If we return to it to correct spelling of last name, say,
// we wouldn't.
this.lastNameField.focus(function() {
addAuthorForm.hideFieldsForNewPerson();
});
// When hitting enter in last name field, if not an autocomplete
// selection, show first and middle name fields.
this.lastNameField.keydown(function(event) {
if (event.keyCode === 13) {
addAuthorForm.onLastNameChange();
return false;
}
});
this.removeAuthorshipLinks.click(function() {
addAuthorForm.removeAuthorship(this);
return false;
});
// this.undoLinks.click(function() {
// $.ajax({
// url: $(this).attr('href')
// });
// return false;
// });
},
removeAuthorship: function(link) {
// RY Upgrade this to a modal window
var message = "Are you sure you want to remove this author?";
if (!confirm(message)) {
return false;
}
$.ajax({
url: $(link).attr('href'),
type: 'POST',
data: {
deletion: $(link).parents('.authorship').attr('id')
},
dataType: 'json',
context: link, // context for callback
complete: function(request, status) {
var authorship = $(this).parents('.authorship'),
nextAuthorships = authorship.nextAll(),
rank;
// author = $(this).siblings('span.author'),
// authorLink = author.children('a.authorLink'),
// authorName = authorLink.html();
if (status === 'success') {
if (nextAuthorships.length) {
// Reset the position value of each succeeding authorship
nextAuthorships.each(function() {
//var pos = parseInt($(this).children('.position').attr('id'));
//$(this).children('.position').attr('id', pos-1);
var pos = addAuthorForm.getPosition(this);
addAuthorForm.setPosition(this, pos-1);
});
} else {
// Removed author was last in rank: reset the rank hidden form field
rank = addAuthorForm.getRank(authorship);
$('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() {
$(this).remove();
// Actions that depend on the author having been removed from the DOM:
// If there's just one author remaining, disable drag-drop
if ($('.authorship').length == 1) {
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).siblings('.undo').show();
// author.html(authorName + ' has been removed');
// author.css('width', 'auto');
// author.effect("highlight", {}, 3000);
} else {
alert('Error processing request: author not removed');
}
}
});
},
// Disable DD and associated cues if only one author remains
disableAuthorDD: function() {
var authorship = $('.authorship');
$('#authorships').sortable({ disable: true} );
authorship.css('background', 'none');
authorship.css('padding-left', '0');
authorship.children('.author').attr('title', '');
},
onLastNameChange: function() {
this.showFieldsForNewPerson();
this.firstNameField.focus();
this.fixNames();
},
showFieldsForNewPerson: function() {
this.firstNameWrapper.show();
this.middleNameWrapper.show();
this.toggleLastNameLabel('Name', 'Last name');
},
// User may have typed first name as well as last name into last name field.
// If so, when showing first and middle name fields, move anything after a comma
// or space into the first name field.
fixNames: function() {
var lastNameInput = this.lastNameField.val(),
names = lastNameInput.split(/[, ]+/),
lastName = names[0];
this.lastNameField.val(lastName);
if (names.length > 1) {
//firstName = names[1].replace(/^[, ]+/, '');
this.firstNameField.val(names[1]);
}
},
hideFieldsForNewPerson: function() {
this.hideFields(this.firstNameWrapper);
this.hideFields(this.middleNameWrapper);
this.toggleLastNameLabel('Last name', 'Name');
},
toggleLastNameLabel: function(currentText, newText) {
var lastNameLabelText = this.lastNameLabel.html(),
newLastNameLabelText = lastNameLabelText.replace(currentText, newText);
this.lastNameLabel.html(newLastNameLabelText);
},
// This view shows the list of existing authors and hides the form. // This view shows the list of existing authors and hides the form.
// There is a button to show the form. // There is a button to show the form.
@ -232,7 +72,6 @@ var addAuthorForm = {
this.showFormButtonWrapper.show(); this.showFormButtonWrapper.show();
}, },
// View of form after returning from an invalid submission. On this form, // View of form after returning from an invalid submission. On this form,
// validation errors entail that we were entering a new person, so we show // validation errors entail that we were entering a new person, so we show
// all the fields straightaway. // all the fields straightaway.
@ -282,6 +121,90 @@ var addAuthorForm = {
this.form.show(); this.form.show();
}, },
hideSelectedAuthor: function() {
this.selectedAuthor.hide();
this.selectedAuthorName.html('');
this.personUriField.val('');
},
showFieldsForNewPerson: function() {
this.firstNameWrapper.show();
this.middleNameWrapper.show();
this.toggleLastNameLabel('Name', 'Last name');
},
hideFieldsForNewPerson: function() {
this.hideFields(this.firstNameWrapper);
this.hideFields(this.middleNameWrapper);
this.toggleLastNameLabel('Last name', 'Name');
},
toggleLastNameLabel: function(currentText, newText) {
var lastNameLabelText = this.lastNameLabel.html(),
newLastNameLabelText = lastNameLabelText.replace(currentText, newText);
this.lastNameLabel.html(newLastNameLabelText);
},
/* *** Ajax initializations *** */
/* Autocomplete */
initAutocomplete: function() {
// Make cache a property of this so we can access it after removing
// an author.
this.acCache = {};
this.baseAcUrl = $('.acUrl').attr('id');
this.setAcUrl();
$('#lastName').autocomplete({
minLength: 2,
source: function(request, response) {
if (request.term in addAuthorForm.acCache) {
// console.log("found term in cache");
response(addAuthorForm.acCache[request.term]);
return;
}
// console.log("not getting term from cache");
// If the url query params are too long, we could do a post
// here instead of a get. Add the exclude uris to the data
// rather than to the url.
$.ajax({
url: addAuthorForm.acUrl,
dataType: 'json',
data: request,
complete: function(xhr) {
// Not sure why, but we need an explicit json parse here. jQuery
// should parse the response text and return an json object.
var results = jQuery.parseJSON(xhr.responseText);
addAuthorForm.acCache[request.term] = results;
response(results);
}
});
},
select: function(event, ui) {
addAuthorForm.showSelectedAuthor(ui);
}
});
},
setAcUrl: function() {
var url = this.baseAcUrl,
existingAuthors = $('#authorships .authorLink');
//console.log("in setAcUrl()");
//console.log("number of existing authors: " + existingAuthors.length);
existingAuthors.each(function() {
url += '&excludeUri=' + $(this).attr('id');
});
this.acUrl = url;
},
// 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) {
@ -309,12 +232,7 @@ var addAuthorForm = {
return false; return false;
}, },
hideSelectedAuthor: function() { /* Drag-and-drop */
this.selectedAuthor.hide();
this.selectedAuthorName.html('');
this.personUriField.val('');
},
initAuthorDD: function() { initAuthorDD: function() {
var authorshipList = $('#authorships'), var authorshipList = $('#authorships'),
@ -445,60 +363,56 @@ var addAuthorForm = {
return rankVal; return rankVal;
}, },
initAutocomplete: function() {
// Make cache a property of this so we can access it after removing /* *** Event listeners *** */
// an author.
this.acCache = {};
this.baseAcUrl = $('.acUrl').attr('id');
this.setAcUrl();
$('#lastName').autocomplete({ bindEventListeners: function() {
minLength: 2,
source: function(request, response) {
if (request.term in addAuthorForm.acCache) {
// console.log("found term in cache");
response(addAuthorForm.acCache[request.term]);
return;
}
// console.log("not getting term from cache");
// If the url query params are too long, we could do a post
// here instead of a get. Add the exclude uris to the data
// rather than to the url.
$.ajax({
url: addAuthorForm.acUrl,
dataType: 'json',
data: request,
complete: function(xhr) {
// Not sure why, but we need an explicit json parse here. jQuery
// should parse the response text and return an json object.
var results = jQuery.parseJSON(xhr.responseText);
addAuthorForm.acCache[request.term] = results;
response(results);
}
this.showFormButton.click(function() {
addAuthorForm.initFormView();
return false;
}); });
},
select: function(event, ui) { this.submit.click(function() {
addAuthorForm.showSelectedAuthor(ui); // NB Important JavaScript scope issue: if we call it this way, this = addAuthorForm
// in prepareSubmit. If we do this.submit.click(prepareSubmit); then
// this != addAuthorForm in prepareSubmit.
addAuthorForm.prepareSubmit();
});
this.lastNameField.blur(function() {
addAuthorForm.onLastNameChange();
});
// This is somewhat questionable. If we return to the last name
// field to look again for an existing person, we'd want to hide those
// fields. If we return to it to correct spelling of last name, say,
// we wouldn't.
this.lastNameField.focus(function() {
addAuthorForm.hideFieldsForNewPerson();
});
// When hitting enter in last name field, if not an autocomplete
// selection, show first and middle name fields.
this.lastNameField.keydown(function(event) {
if (event.keyCode === 13) {
addAuthorForm.onLastNameChange();
return false;
} }
}); });
}, this.removeAuthorshipLinks.click(function() {
addAuthorForm.removeAuthorship(this);
setAcUrl: function() { return false;
var url = this.baseAcUrl,
existingAuthors = $('#authorships .authorLink');
//console.log("in setAcUrl()");
//console.log("number of existing authors: " + existingAuthors.length);
existingAuthors.each(function() {
url += '&excludeUri=' + $(this).attr('id');
}); });
this.acUrl = url; // this.undoLinks.click(function() {
// $.ajax({
// url: $(this).attr('href')
// });
// return false;
// });
}, },
prepareSubmit: function() { prepareSubmit: function() {
@ -531,13 +445,110 @@ var addAuthorForm = {
}, },
onLastNameChange: function() {
this.showFieldsForNewPerson();
this.firstNameField.focus();
this.fixNames();
},
// User may have typed first name as well as last name into last name field.
// If so, when showing first and middle name fields, move anything after a comma
// or space into the first name field.
fixNames: function() {
var lastNameInput = this.lastNameField.val(),
names = lastNameInput.split(/[, ]+/),
lastName = names[0];
this.lastNameField.val(lastName);
if (names.length > 1) {
//firstName = names[1].replace(/^[, ]+/, '');
this.firstNameField.val(names[1]);
}
},
removeAuthorship: function(link) {
// RY Upgrade this to a modal window
var message = "Are you sure you want to remove this author?";
if (!confirm(message)) {
return false;
}
$.ajax({
url: $(link).attr('href'),
type: 'POST',
data: {
deletion: $(link).parents('.authorship').attr('id')
},
dataType: 'json',
context: link, // context for callback
complete: function(request, status) {
var authorship = $(this).parents('.authorship'),
nextAuthorships = authorship.nextAll(),
rank;
// author = $(this).siblings('span.author'),
// authorLink = author.children('a.authorLink'),
// authorName = authorLink.html();
if (status === 'success') {
if (nextAuthorships.length) {
// Reset the position value of each succeeding authorship
nextAuthorships.each(function() {
//var pos = parseInt($(this).children('.position').attr('id'));
//$(this).children('.position').attr('id', pos-1);
var pos = addAuthorForm.getPosition(this);
addAuthorForm.setPosition(this, pos-1);
});
} else {
// Removed author was last in rank: reset the rank hidden form field
rank = addAuthorForm.getRank(authorship);
$('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() {
$(this).remove();
// Actions that depend on the author having been removed from the DOM:
// If there's just one author remaining, disable drag-drop
if ($('.authorship').length == 1) {
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).siblings('.undo').show();
// author.html(authorName + ' has been removed');
// author.css('width', 'auto');
// author.effect("highlight", {}, 3000);
} else {
alert('Error processing request: author not removed');
}
}
});
},
// Disable DD and associated cues if only one author remains
disableAuthorDD: function() {
var authorship = $('.authorship');
$('#authorships').sortable({ disable: true} );
authorship.css('background', 'none');
authorship.css('padding-left', '0');
authorship.children('.author').attr('title', '');
},
// RY To be implemented later. // RY To be implemented later.
toggleRemoveLink: function() { toggleRemoveLink: function() {
// when clicking remove: remove the author, and change link text to "undo" // when clicking remove: remove the author, and change link text to "undo"
// when clicking undo: add the author back, and change link text to "remove" // when clicking undo: add the author back, and change link text to "remove"
} }
} };
$(document).ready(function() { $(document).ready(function() {
addAuthorForm.onLoad(); addAuthorForm.onLoad();