NIHVIVO-765 Call reorder when loading add authors form, to prevent problems when adding a new author and the last existing author is unranked.

This commit is contained in:
rjy7 2010-07-08 21:57:05 +00:00
parent 9944d56635
commit bd5acb1ef0
2 changed files with 121 additions and 94 deletions

View file

@ -362,6 +362,8 @@ SPARQL queries for existing values. --%>
// A new author will be ranked last when added. // A new author will be ranked last when added.
// This wouldn't handle gaps in the ranking: vreq.setAttribute("rank", authorships.size()+1); // This wouldn't handle gaps in the ranking: vreq.setAttribute("rank", authorships.size()+1);
// This value is now inserted by JavaScript, but leave it here as a safety net in case page
// load reordering returns an error.
request.setAttribute("newRank", maxRank + 1); request.setAttribute("newRank", maxRank + 1);
request.setAttribute("rankPred", rankPredicateUri); request.setAttribute("rankPred", rankPredicateUri);
%> %>

View file

@ -70,6 +70,10 @@ var addAuthorForm = {
initAuthorListOnlyView: function() { initAuthorListOnlyView: function() {
this.hideForm(); this.hideForm();
this.showFormButtonWrapper.show(); this.showFormButtonWrapper.show();
// We reorder authors on page load so that previously unranked authors get a rank. Otherwise,
// when we add a new author, it will get put ahead of any previously unranked authors, instead
// of at the end of the list. (It is also helpful to normalize the data before we get started.)
this.reorderAuthors();
}, },
// View of form after returning from an invalid submission. On this form, // View of form after returning from an invalid submission. On this form,
@ -248,91 +252,113 @@ var addAuthorForm = {
authorshipList.sortable({ authorshipList.sortable({
stop: function(event, ui) { stop: function(event, ui) {
var predicateUri = '<' + $('.rankPred').attr('id') + '>', addAuthorForm.reorderAuthors(event, ui);
rankXsdType = $('.rankXsdType').attr('id'), }
additions = '', });
retractions = '', },
authorships = [];
$('li.authorship').each(function(index) { // Reorder authors. Called on page load and after author drag-and-drop.
var uri = $(this).attr('id'), // Event and ui parameters are defined only in the case of drag-and-drop.
subjectUri = '<' + uri + '>', reorderAuthors: function(event, ui) {
oldRankVal = addAuthorForm.getRankVal(this), var predicateUri = '<' + $('.rankPred').attr('id') + '>',
newRank = index + 1, rankXsdType = $('.rankXsdType').attr('id'),
newRankForN3, additions = '',
oldRank, retractions = '',
oldRankType, authorships = [];
oldRankForN3,
rankVals;
if (oldRankVal) { $('li.authorship').each(function(index) {
// e.g., 1_http://www.w3.org/2001/XMLSchema#int var uri = $(this).attr('id'),
// We handle typeless values formatted as either "1" or "1_". subjectUri = '<' + uri + '>',
rankVals = oldRankVal.split('_'); oldRankVal = addAuthorForm.getRankStrVal(this),
oldRank = rankVals[0]; newRank = index + 1,
oldRankType = rankVals.length > 1 ? rankVals[1] : ''; newRankForN3,
oldRankForN3 = addAuthorForm.makeRankDataPropVal(oldRank, oldRankType); oldRank,
retractions += subjectUri + ' ' + predicateUri + ' ' + oldRankForN3 + ' .'; oldRankType,
} oldRankForN3,
rankVals;
newRankForN3 = addAuthorForm.makeRankDataPropVal(newRank, rankXsdType); if (oldRankVal) {
additions += subjectUri + ' ' + predicateUri + ' ' + newRankForN3 + ' .'; // e.g., 1_http://www.w3.org/2001/XMLSchema#int
// We handle typeless values formatted as either "1" or "1_".
rankVals = oldRankVal.split('_');
oldRank = rankVals[0];
oldRankType = rankVals.length > 1 ? rankVals[1] : '';
oldRankForN3 = addAuthorForm.makeRankDataPropVal(oldRank, oldRankType);
retractions += subjectUri + ' ' + predicateUri + ' ' + oldRankForN3 + ' .';
}
// This data will be used to modify the page after successful completion newRankForN3 = addAuthorForm.makeRankDataPropVal(newRank, rankXsdType);
// of the Ajax request. additions += subjectUri + ' ' + predicateUri + ' ' + newRankForN3 + ' .';
authorship = {
uri: uri,
rankVal: newRank + '_' + rankXsdType
};
authorships.push(authorship);
// This data will be used to modify the page after successful completion
// of the Ajax request.
authorship = {
uri: uri,
rankVal: newRank + '_' + rankXsdType
};
authorships.push(authorship);
});
// console.log(authorships)
// console.log('additions: ' + additions);
// console.log('retractions: ' + retractions);
$.ajax({
url: $('.reorderUrl').attr('id'),
data: {
additions: additions,
retractions: retractions
},
authorships: authorships,
processData: 'false',
dataType: 'json',
type: 'POST',
success: function(data, status, request) {
var maxRank;
$.each(authorships, function(index, obj) {
// find the element with this uri as id
var el = $('li[id=' + obj.uri + ']'),
// because all ranks have been reordered without gaps,
// we can get the position from the rank
pos = obj.rankVal.split('_')[0];
// set the new rank and position for this element
addAuthorForm.setRankStrVal(el, obj.rankVal);
addAuthorForm.setPosition(el, pos);
}); });
// console.log(authorships) // On page load, we're calling reorder to assign a rank to any
// console.log('additions: ' + additions); // unranked authorships. We thus need to set the rank form field
// console.log('retractions: ' + retractions); // to the new highest rank + 1.
if (!ui) {
maxRank = addAuthorForm.getRankIntVal($('.authorship:last'));
$('#rank').val(maxRank + 1);
}
},
error: function(request, status, error) {
// This is performed only after drag-and-drop.
if (ui) {
// Put the moved item back to its original position.
// Seems we need to do this by hand. Can't see any way to do it with jQuery UI. ??
var pos = addAuthorForm.getPosition(ui.item), //ui.item.children('.position').attr('id'),
nextpos = pos + 1, authorships = $('#authorships'),
next = authorships.find('.position[id=' + nextpos + ']').parent();
$.ajax({ if (next.length) {
url: $('.reorderUrl').attr('id'), ui.item.insertBefore(next);
data: {
additions: additions,
retractions: retractions
},
authorships: authorships,
processData: 'false',
dataType: 'json',
type: 'POST',
success: function(data, status, request) {
$.each(authorships, function(index, obj) {
// find the element with this uri as id
var el = $('li[id=' + obj.uri + ']'),
// because all ranks have been reordered without gaps,
// we can get the position from the rank
pos = obj.rankVal.split('_')[0];
// set the new rank and position for this element
addAuthorForm.setRankVal(el, obj.rankVal);
addAuthorForm.setPosition(el, pos);
});
},
error: function(request, status, error) {
// Put the moved item back to its original position.
// Seems we need to do this by hand. Can't see any way to do it with jQuery UI. ??
var pos = addAuthorForm.getPosition(ui.item),
//ui.item.children('.position').attr('id'),
nextpos = pos + 1,
authorships = $('#authorships'),
next = authorships.find('.position[id=' + nextpos + ']').parent();
if (next.length) {
ui.item.insertBefore(next);
} else {
ui.item.appendTo(authorships);
}
alert('Reordering of authors failed.');
} }
}); else {
} // end stop callback ui.item.appendTo(authorships);
}
alert('Reordering of authors failed.');
} // What should we do if the reordering fails?
else {
}
}
}); });
}, },
@ -345,16 +371,16 @@ var addAuthorForm = {
}, },
// Get the authorship rank value, which includes xsd type // Get the authorship rank value, which includes xsd type
getRankVal: function(authorship) { getRankStrVal: function(authorship) {
return $(authorship).children('.rank').attr('id'); return $(authorship).children('.rank').attr('id');
}, },
// Get the integer value from the authorship rank value // Get the integer rank value from the authorship rank string
getRank: function(authorship) { getRankIntVal: function(authorship) {
return this.getRankVal(authorship).split('_')[0]; return parseInt(this.getRankStrVal(authorship).split('_')[0]);
}, },
setRankVal: function(authorship, rank) { setRankStrVal: function(authorship, rank) {
$(authorship).children('.rank').attr('id', rank); $(authorship).children('.rank').attr('id', rank);
}, },
@ -495,18 +521,17 @@ var addAuthorForm = {
// authorName = authorLink.html(); // authorName = authorLink.html();
if (status === 'success') { if (status === 'success') {
// Both these cases can be replaced by calling a reorder. And we could
// do away with the position span altogether in that case.
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'));
//$(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.getRankIntVal(authorship);
$('input#rank').val(rank); $('input#rank').val(rank);
} }