2011-07-07 21:23:51 +00:00
|
|
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
var manageWebpages = {
|
2011-07-07 21:23:51 +00:00
|
|
|
|
|
|
|
/* *** Initial page setup *** */
|
|
|
|
|
|
|
|
onLoad: function() {
|
2011-07-12 16:40:29 +00:00
|
|
|
|
|
|
|
this.mixIn();
|
2011-07-07 21:23:51 +00:00
|
|
|
this.initPage();
|
|
|
|
},
|
|
|
|
|
|
|
|
mixIn: function() {
|
|
|
|
|
|
|
|
// Get the custom form data from the page
|
|
|
|
$.extend(this, customFormData);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Initial page setup. Called only at page load.
|
|
|
|
initPage: function() {
|
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
this.initWebpageData();
|
|
|
|
|
2011-07-07 21:23:51 +00:00
|
|
|
this.bindEventListeners();
|
2011-07-12 16:40:29 +00:00
|
|
|
|
|
|
|
this.initDragAndDrop();
|
2011-07-07 21:23:51 +00:00
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
if ($('.webpage').length) { // make sure we have at least one webpage
|
|
|
|
// Reorder web pages on page load so that previously unranked items get a rank. Otherwise,
|
|
|
|
// when we add a new web page, it will get put ahead of any previously unranked web pages, instead
|
2011-07-07 21:23:51 +00:00
|
|
|
// of at the end of the list. (It is also helpful to normalize the data before we get started.)
|
2011-07-12 16:40:29 +00:00
|
|
|
this.reorder();
|
|
|
|
}
|
2011-07-07 21:23:51 +00:00
|
|
|
},
|
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
// On page load, associate data with each list item. Then we don't
|
|
|
|
// have to keep retrieving data from or modifying the DOM as we manipulate the
|
|
|
|
// items.
|
|
|
|
initWebpageData: function() {
|
|
|
|
$('.webpage').each(function(index) {
|
|
|
|
$(this).data(webpageData[index]);
|
|
|
|
|
|
|
|
// RY We might still need position to put back an element after reordering
|
|
|
|
// failure. Rank might already have been reset? Check.
|
|
|
|
$(this).data('position', index+1);
|
|
|
|
});
|
2011-07-07 21:23:51 +00:00
|
|
|
},
|
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
bindEventListeners: function() {
|
2011-07-07 21:23:51 +00:00
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
$('.remove').click(function() {
|
|
|
|
manageWebpages.removeWebpage(this);
|
2011-07-07 21:23:51 +00:00
|
|
|
return false;
|
|
|
|
});
|
2011-07-12 16:40:29 +00:00
|
|
|
|
2011-07-07 21:23:51 +00:00
|
|
|
},
|
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
/* *** Ajax initializations *** */
|
2011-07-07 21:23:51 +00:00
|
|
|
|
|
|
|
/* Drag-and-drop */
|
2011-07-12 16:40:29 +00:00
|
|
|
initDragAndDrop: function() {
|
2011-07-07 21:23:51 +00:00
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
var webpages = $('#webpageList');
|
2011-07-07 21:23:51 +00:00
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
// No DD if < 2 items
|
|
|
|
if (webpages.children('li') < 2) {
|
2011-07-07 21:23:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
$('.webpageName').each(function() {
|
|
|
|
$(this).attr('title', 'Drag and drop to reorder web pages');
|
2011-07-07 21:23:51 +00:00
|
|
|
});
|
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
webpages.sortable({
|
2011-07-07 21:23:51 +00:00
|
|
|
cursor: 'move',
|
|
|
|
update: function(event, ui) {
|
2011-07-12 16:40:29 +00:00
|
|
|
manageWebpages.reorder(event, ui);
|
2011-07-07 21:23:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
// Reorder webpages. Called on page load and after drag-and-drop and remove.
|
2011-07-07 21:23:51 +00:00
|
|
|
// Event and ui parameters are defined only in the case of drag-and-drop.
|
2011-07-12 16:40:29 +00:00
|
|
|
reorder: function(event, ui) {
|
|
|
|
var webpages = $('li.webpage').map(function(index, el) {
|
|
|
|
return $(this).data('webpageUri');
|
2011-07-07 21:23:51 +00:00
|
|
|
}).get();
|
|
|
|
|
|
|
|
$.ajax({
|
2011-07-12 16:40:29 +00:00
|
|
|
url: manageWebpages.reorderUrl,
|
2011-07-07 21:23:51 +00:00
|
|
|
data: {
|
2011-07-12 16:40:29 +00:00
|
|
|
predicate: manageWebpages.rankPredicate,
|
|
|
|
individuals: webpages
|
2011-07-07 21:23:51 +00:00
|
|
|
},
|
|
|
|
traditional: true, // serialize the array of individuals for the server
|
|
|
|
dataType: 'json',
|
|
|
|
type: 'POST',
|
|
|
|
success: function(data, status, request) {
|
|
|
|
var pos;
|
2011-07-12 16:40:29 +00:00
|
|
|
$('.webpage').each(function(index){
|
2011-07-07 21:23:51 +00:00
|
|
|
pos = index + 1;
|
|
|
|
// Set the new position for this element. The only function of this value
|
|
|
|
// is so we can reset an element to its original position in case reordering fails.
|
2011-07-12 16:40:29 +00:00
|
|
|
manageWebpages.setPosition(this, pos);
|
|
|
|
});
|
2011-07-07 21:23:51 +00:00
|
|
|
},
|
|
|
|
error: function(request, status, error) {
|
2011-07-12 16:40:29 +00:00
|
|
|
// ui is undefined on page load and after a webpage removal.
|
2011-07-07 21:23:51 +00:00
|
|
|
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. ??
|
2011-07-12 16:40:29 +00:00
|
|
|
var pos = manageWebpages.getPosition(ui.item),
|
2011-07-07 21:23:51 +00:00
|
|
|
nextpos = pos + 1,
|
2011-07-12 16:40:29 +00:00
|
|
|
webpages = $('#webpageList'),
|
|
|
|
next = manageWebpages.findWebpage('position', nextpos);
|
2011-07-07 21:23:51 +00:00
|
|
|
|
|
|
|
if (next.length) {
|
|
|
|
ui.item.insertBefore(next);
|
|
|
|
}
|
|
|
|
else {
|
2011-07-12 16:40:29 +00:00
|
|
|
ui.item.appendTo(webpages);
|
2011-07-07 21:23:51 +00:00
|
|
|
}
|
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
alert('Reordering of web pages failed.');
|
2011-07-07 21:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
getPosition: function(webpage) {
|
|
|
|
return $(webpage).data('position');
|
2011-07-07 21:23:51 +00:00
|
|
|
},
|
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
setPosition: function(webpage, pos) {
|
|
|
|
$(webpage).data('position', pos);
|
2011-07-07 21:23:51 +00:00
|
|
|
},
|
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
findWebpage: function(key, value) {
|
|
|
|
var matchingWebpage = $(); // if we don't find one, return an empty jQuery set
|
2011-07-07 21:23:51 +00:00
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
$('.webpage').each(function() {
|
|
|
|
var webpage = $(this);
|
|
|
|
if ( webpage.data(key) === value ) {
|
|
|
|
matchingWebpage = webpage;
|
2011-07-07 21:23:51 +00:00
|
|
|
return false; // stop the loop
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
return matchingWebpage;
|
2011-07-07 21:23:51 +00:00
|
|
|
},
|
2011-07-12 16:40:29 +00:00
|
|
|
|
|
|
|
removeWebpage: function(link) {
|
2011-07-07 21:23:51 +00:00
|
|
|
// RY Upgrade this to a modal window
|
2011-07-12 19:01:16 +00:00
|
|
|
var removeLast = false,
|
|
|
|
message = 'Are you sure you want to remove this web page?';
|
|
|
|
|
2011-07-07 21:23:51 +00:00
|
|
|
if (!confirm(message)) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-12 19:01:16 +00:00
|
|
|
|
|
|
|
if ($(link)[0] === $('.remove:last')[0]) {
|
|
|
|
removeLast = true;
|
|
|
|
}
|
|
|
|
|
2011-07-07 21:23:51 +00:00
|
|
|
$.ajax({
|
|
|
|
url: $(link).attr('href'),
|
|
|
|
type: 'POST',
|
|
|
|
data: {
|
2011-07-12 16:40:29 +00:00
|
|
|
deletion: $(link).parents('.webpage').data('webpageUri')
|
2011-07-07 21:23:51 +00:00
|
|
|
},
|
|
|
|
dataType: 'json',
|
|
|
|
context: link, // context for callback
|
|
|
|
complete: function(request, status) {
|
2011-07-12 16:40:29 +00:00
|
|
|
var webpage;
|
2011-07-07 21:23:51 +00:00
|
|
|
|
|
|
|
if (status === 'success') {
|
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
webpage = $(this).parents('.webpage');
|
2011-07-07 21:23:51 +00:00
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
webpage.fadeOut(400, function() {
|
|
|
|
var numWebpages;
|
2011-07-07 21:23:51 +00:00
|
|
|
|
|
|
|
// Remove from the DOM
|
|
|
|
$(this).remove();
|
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
// Actions that depend on the webpage having been removed from the DOM:
|
|
|
|
numWebpages = $('.webpage').length; // retrieve the new length after removing webpage from the DOM
|
2011-07-12 19:01:16 +00:00
|
|
|
|
|
|
|
// If removed item not last, reorder to remove any gaps
|
|
|
|
if (numWebpages > 0 && ! removeLast) {
|
2011-07-12 16:40:29 +00:00
|
|
|
manageWebpages.reorder();
|
2011-07-07 21:23:51 +00:00
|
|
|
}
|
2011-07-12 19:01:16 +00:00
|
|
|
|
|
|
|
// If fewer than two webpages remaining, disable drag-drop
|
|
|
|
if (numWebpages < 2) {
|
|
|
|
manageWebpages.disableDD();
|
|
|
|
}
|
2011-07-07 21:23:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
2011-07-12 16:40:29 +00:00
|
|
|
alert('Error processing request: web page not removed');
|
2011-07-07 21:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
// Disable DD and associated cues if only one item remains
|
|
|
|
disableDD: function() {
|
|
|
|
var webpages = $('#webpageList');
|
2011-07-07 21:23:51 +00:00
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
$('#webpageList').sortable({ disable: true } )
|
|
|
|
/* Use class dd rather than jQuery UI's class ui-sortable, so that we can remove
|
|
|
|
* the class if there's fewer than one webpage. We don't want to remove the ui-sortable
|
|
|
|
* class, in case we want to re-enable DD without a page reload (e.g., if implementing
|
|
|
|
* adding a webpage via Ajax request).
|
|
|
|
*/
|
|
|
|
.removeClass('dd');
|
2011-07-07 21:23:51 +00:00
|
|
|
|
2011-07-12 16:40:29 +00:00
|
|
|
$('.webpageName').removeAttr('title');
|
|
|
|
}
|
2011-07-07 21:23:51 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
$(document).ready(function() {
|
2011-07-12 16:40:29 +00:00
|
|
|
manageWebpages.onLoad();
|
2011-07-07 21:23:51 +00:00
|
|
|
});
|