Skip to content

Commit

Permalink
Add functionality to skip inaccessible images
Browse files Browse the repository at this point in the history
  • Loading branch information
othieno committed Oct 21, 2015
1 parent 436651c commit 48abdad
Showing 1 changed file with 95 additions and 59 deletions.
154 changes: 95 additions & 59 deletions layout/static/js/geotagx-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
}
else {
pybossa.taskLoaded(onTaskLoaded);
pybossa.presentTask(onTaskPresented);
pybossa.presentTask(onPresentTask);
pybossa.run(shortName_);
geotagx.analytics.onStartProject(shortName_);
}
Expand All @@ -41,79 +41,58 @@
* Handles PyBossa's taskLoaded event.
*/
function onTaskLoaded(task, deferred){
if (!$.isEmptyObject(task))
$("<img/>").load(function(){ deferred.resolve(task); }).attr("src", task.info.image_url);
if (!$.isEmptyObject(task)){
task.photoAccessible = true;

// Cache the image for later use.
$("<img>")
.load(function(){ deferred.resolve(task); })
.error(function(e){
task.photoAccessible = false;
deferred.resolve(task);
})
.attr("src", task.info.image_url);
}
else
deferred.resolve(task);
}
/**
* Handles PyBossa's presentTask event.
*/
function onTaskPresented(task, deferred){
function onPresentTask(task, deferred){
if (!$.isEmptyObject(task)){
// Show/hide respective elements.
$(".show-on-task-loaded").removeClass("show-on-task-loaded").hide().fadeIn(200);
$(".hide-on-task-loaded").hide();

// Update the user progress.
pybossa.userProgress(shortName_).done(function(data){
$("#project-task-count").text(data.done + "/" + data.total);
});

geotagx.questionnaire.setImage(task.info.image_url, task.info.source_uri);
if (task.photoAccessible){
// Show/hide respective elements.
$(".show-on-task-loaded").removeClass("show-on-task-loaded").hide().fadeIn(200);
$(".hide-on-task-loaded").hide();

// Set the submission button's handler. Note that off().on() removes the previous handler
// and sets a new one, every time a new task is loaded. This prevents a chain of events
// being called when a button is pushed once.
$("#submit-analysis").off("click.task").on("click.task", function(){
var $busyIcon = $("#questionnaire-busy-icon");

// Disable the submit button and display the busy icon.
$busyIcon.removeClass("hide").hide().fadeIn(300);
// Update the user progress.
pybossa.userProgress(shortName_).done(function(data){
$("#project-task-count").text(data.done + "/" + data.total);
});

var $submitButton = $(this);
$submitButton.toggleClass("busy btn-success");
$submitButton.prop("disabled", true);
geotagx.questionnaire.setImage(task.info.image_url, task.info.source_uri);

// Append the image URL to the questionnaire results.
var taskRun_ = geotagx.questionnaire.getAnswers();
taskRun_.img = task.info.image_url;
// Set the submission button's handler. Note that off().on() removes the previous handler
// and sets a new one, every time a new task is loaded. This prevents a chain of events
// being called when a button is pushed once.
$("#submit-analysis").off("click.task").on("click.task", function(){
var $submitButton = $("#submit-analysis");
var $busyIcon = $("#questionnaire-busy-icon");

pybossa.saveTask(task.id, taskRun_).done(function(){
$busyIcon.fadeOut(300, function(){ $(this).addClass("hide"); });
// Disable the submit button and display the busy icon.
$busyIcon.removeClass("hide").hide().fadeIn(300);

// Display the status message.
var $message = $("#submit-message-success");
$message.removeClass("hide");
setTimeout(function(){
deferred.resolve();
$message.addClass("hide");
$submitButton.toggleClass("busy btn-success");
$submitButton.prop("disabled", false);
}, 1500);
}).fail(function(response){
$busyIcon.fadeOut(300, function(){ $(this).addClass("hide"); });
$submitButton.toggleClass("busy btn-success");
$submitButton.prop("disabled", false);
$submitButton.prop("disabled", true);

var $message = $("#submit-message-failure");

// If the status code is 403 (FORBIDDEN), then we assume that the
// data was sent but the deferred object has not yet been resolved.
if (response.status === 403){
deferred.resolve();
$message.addClass("hide");
}
else {
$message.removeClass("hide");
$submitButton.one("click", function(){
$message.addClass("hide");
});
}
submitTask(task, deferred);
});
});
geotagx.analytics.onTaskChanged(task.id);
geotagx.questionnaire.start();
geotagx.analytics.onTaskChanged(task.id);
geotagx.questionnaire.start();
}
else
submitTask(task, deferred);
}
else {
// If there're no more tasks, then hide the questionnaire and image,
Expand All @@ -124,6 +103,63 @@
$("#project-task-presenter-header").addClass("hide");
}
}
/**
* Submits the specified task.
*/
function submitTask(task, deferred){
// Append image information to the questionnaire results.
var taskRun = geotagx.questionnaire.getAnswers();
taskRun.img = task.info.image_url;
taskRun.photoAccessible = task.photoAccessible;
taskRun.photoVisible = taskRun.photoAccessible && taskRun.photoVisible;

if (task.photoAccessible === false){
function onResolve(){
deferred.resolve();
}
// If the photo is not accessible, submit the task and load another.
pybossa.saveTask(task.id, taskRun).done(onResolve).fail(onResolve);
}
else if (task.photoAccessible === true){
var $submitButton = $("#submit-analysis");
var $busyIcon = $("#questionnaire-busy-icon");

pybossa.saveTask(task.id, taskRun)
.done(function(){
$busyIcon.fadeOut(300, function(){ $(this).addClass("hide"); });

// Display the status message.
var $message = $("#submit-message-success");
$message.removeClass("hide");
setTimeout(function(){
deferred.resolve();
$message.addClass("hide");
$submitButton.toggleClass("busy btn-success");
$submitButton.prop("disabled", false);
}, 1500);
})
.fail(function(response){
$busyIcon.fadeOut(300, function(){ $(this).addClass("hide"); });
$submitButton.toggleClass("busy btn-success");
$submitButton.prop("disabled", false);

var $message = $("#submit-message-failure");

// If the status code is 403 (FORBIDDEN), then we assume that the
// data was sent but the deferred object has not yet been resolved.
if (response.status === 403){
deferred.resolve();
$message.addClass("hide");
}
else {
$message.removeClass("hide");
$submitButton.one("click", function(){
$message.addClass("hide");
});
}
});
}
}
// Expose the API.
geotagx.project = api_;
})(window.geotagx = window.geotagx || {}, jQuery);

0 comments on commit 48abdad

Please sign in to comment.