From 48abdadb03663cf9659309c5d1a39f3c9fb69853 Mon Sep 17 00:00:00 2001 From: Jeremy Othieno Date: Wed, 21 Oct 2015 15:01:03 +0200 Subject: [PATCH] Add functionality to skip inaccessible images Fixes geotagx/geotagx-theme#121. --- layout/static/js/geotagx-project.js | 154 +++++++++++++++++----------- 1 file changed, 95 insertions(+), 59 deletions(-) diff --git a/layout/static/js/geotagx-project.js b/layout/static/js/geotagx-project.js index 623568d..79a1247 100644 --- a/layout/static/js/geotagx-project.js +++ b/layout/static/js/geotagx-project.js @@ -26,7 +26,7 @@ } else { pybossa.taskLoaded(onTaskLoaded); - pybossa.presentTask(onTaskPresented); + pybossa.presentTask(onPresentTask); pybossa.run(shortName_); geotagx.analytics.onStartProject(shortName_); } @@ -41,79 +41,58 @@ * Handles PyBossa's taskLoaded event. */ function onTaskLoaded(task, deferred){ - if (!$.isEmptyObject(task)) - $("").load(function(){ deferred.resolve(task); }).attr("src", task.info.image_url); + if (!$.isEmptyObject(task)){ + task.photoAccessible = true; + + // Cache the image for later use. + $("") + .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, @@ -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);