Commit a299af9e by Alessandro

Minor refactoring

TNL-925
parent 8efadaec
...@@ -16,7 +16,6 @@ define( ...@@ -16,7 +16,6 @@ define(
'ERROR' : 4 'ERROR' : 4
} }
var error = null;
var current = { stage: 0, state: STATE.READY }; var current = { stage: 0, state: STATE.READY };
var file = { name: null, url: null }; var file = { name: null, url: null };
var timeout = { id: null, delay: 1000 }; var timeout = { id: null, delay: 1000 };
...@@ -29,6 +28,15 @@ define( ...@@ -29,6 +28,15 @@ define(
/********** Private functions *****************************************/ /********** Private functions *****************************************/
/** /**
* Destroys any event listener Import might have needed
* during the process the import
*
*/
var destroyEventListeners = function () {
window.onbeforeunload = null;
}
/**
* Makes Import feedback status list visible * Makes Import feedback status list visible
* *
*/ */
...@@ -37,17 +45,32 @@ define( ...@@ -37,17 +45,32 @@ define(
}; };
/** /**
* Initializes the event listeners
*
*/
var initEventListeners = function () {
window.onbeforeunload = function () {
if (current.stage <= 1 ) {
return gettext('Your import is in progress; navigating away will abort it.');
}
}
}
/**
* Sets the Import on the "success" status * Sets the Import on the "success" status
* (callbacks: complete)
* *
* If it wasn't already, marks the stored import as "completed",
* and updates its date timestamp
*/ */
var success = function () { var success = function () {
window.onbeforeunload = null;
current.state = STATE.SUCCESS; current.state = STATE.SUCCESS;
if (CourseImport.storedImport().completed !== true) { if (CourseImport.storedImport().completed !== true) {
storeImport(true); storeImport(true);
} }
destroyEventListeners();
updateFeedbackList(); updateFeedbackList();
if (typeof CourseImport.callbacks.complete === 'function') { if (typeof CourseImport.callbacks.complete === 'function') {
...@@ -58,8 +81,12 @@ define( ...@@ -58,8 +81,12 @@ define(
/** /**
* Updates the Import feedback status list * Updates the Import feedback status list
* *
* @param {string} [currStageMsg=''] The message to show on the
* current stage (for now only in case of error)
*/ */
var updateFeedbackList = function () { var updateFeedbackList = function (currStageMsg) {
var $checkmark = $dom.successStage.find('.icon');
currStageMsg = currStageMsg || '';
function completeStage(stage) { function completeStage(stage) {
$(stage) $(stage)
...@@ -75,8 +102,6 @@ define( ...@@ -75,8 +102,6 @@ define(
.find('p.copy').show(); .find('p.copy').show();
} }
var $checkmark = $dom.successStage.find('.icon');
switch (current.state) { switch (current.state) {
case STATE.READY: case STATE.READY:
_.map($dom.stages, resetStage); _.map($dom.stages, resetStage);
...@@ -110,7 +135,7 @@ define( ...@@ -110,7 +135,7 @@ define(
var $prev = $dom.stages.slice(0, current.stage + 1); var $prev = $dom.stages.slice(0, current.stage + 1);
var $curr = $dom.stages.eq(current.stage); var $curr = $dom.stages.eq(current.stage);
var $next = $dom.stages.slice(current.stage + 1); var $next = $dom.stages.slice(current.stage + 1);
var message = error || gettext("There was an error with the upload"); var error = currStageMsg || gettext("There was an error with the upload");
_.map($prev, completeStage); _.map($prev, completeStage);
_.map($next, resetStage); _.map($next, resetStage);
...@@ -121,7 +146,7 @@ define( ...@@ -121,7 +146,7 @@ define(
.addClass('has-error') .addClass('has-error')
.find('p.copy') .find('p.copy')
.hide() .hide()
.after("<p class='copy error'>" + message + "</p>"); .after("<p class='copy error'>" + error + "</p>");
} }
break; break;
...@@ -148,7 +173,7 @@ define( ...@@ -148,7 +173,7 @@ define(
})); }));
} }
/********** Public functions *************************************************/ /********** Public functions ******************************************/
var CourseImport = { var CourseImport = {
...@@ -161,6 +186,7 @@ define( ...@@ -161,6 +186,7 @@ define(
/** /**
* Sets the Import in the "error" status. * Sets the Import in the "error" status.
* (callbacks: complete)
* *
* Immediately stops any further polling from the server. * Immediately stops any further polling from the server.
* Displays the error message at the list element that corresponds * Displays the error message at the list element that corresponds
...@@ -170,14 +196,12 @@ define( ...@@ -170,14 +196,12 @@ define(
* @param {int} [stage=current.stage] Stage of import process at which error occurred. * @param {int} [stage=current.stage] Stage of import process at which error occurred.
*/ */
error: function (msg, stage) { error: function (msg, stage) {
window.onbeforeunload = null
current.stage = Math.abs(stage || current.stage); // Could be negative current.stage = Math.abs(stage || current.stage); // Could be negative
current.state = STATE.ERROR; current.state = STATE.ERROR;
error = msg;
destroyEventListeners();
clearTimeout(timeout.id); clearTimeout(timeout.id);
updateFeedbackList(); updateFeedbackList(msg);
if (typeof this.callbacks.complete === 'function') { if (typeof this.callbacks.complete === 'function') {
this.callbacks.complete(); this.callbacks.complete();
...@@ -258,17 +282,12 @@ define( ...@@ -258,17 +282,12 @@ define(
* about the import status * about the import status
*/ */
start: function (fileName, fileUrl) { start: function (fileName, fileUrl) {
window.onbeforeunload = function () { current.state = STATE.IN_PROGRESS;
if (current.stage <= 1 ) {
return gettext('Your import is in progress; navigating away will abort it.');
}
}
file.name = fileName; file.name = fileName;
file.url = fileUrl; file.url = fileUrl;
current.state = STATE.IN_PROGRESS; initEventListeners();
storeImport(); storeImport();
displayFeedbackList(); displayFeedbackList();
updateFeedbackList(); updateFeedbackList();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment