2016-08-13 21:51:01 +02:00
|
|
|
/*
|
|
|
|
* status.js
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-12-23 07:02:45 +01:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-08-13 21:51:01 +02:00
|
|
|
*/
|
|
|
|
|
2017-01-02 10:34:01 +01:00
|
|
|
/** global: jobImportUrl, langImportSingleError, langImportMultiError, jobStartUrl, langImportTimeOutError, langImportFinished, langImportFatalError */
|
|
|
|
|
2016-08-13 21:51:01 +02:00
|
|
|
var startedImport = false;
|
2016-08-13 23:28:01 +02:00
|
|
|
var startInterval = 2000;
|
2016-08-13 21:51:01 +02:00
|
|
|
var interval = 500;
|
2016-08-14 09:10:47 +02:00
|
|
|
var timeoutLimit = 5000;
|
|
|
|
var currentLimit = 0;
|
|
|
|
var stepCount = 0;
|
2016-08-13 21:51:01 +02:00
|
|
|
$(function () {
|
|
|
|
"use strict";
|
|
|
|
|
2016-08-14 11:31:09 +02:00
|
|
|
$('#import-status-intro').hide();
|
|
|
|
$('#import-status-more-info').hide();
|
|
|
|
|
2016-08-13 21:51:01 +02:00
|
|
|
// check status, every 500 ms.
|
2016-08-13 23:28:01 +02:00
|
|
|
setTimeout(checkImportStatus, startInterval);
|
2016-08-13 21:51:01 +02:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function checkImportStatus() {
|
|
|
|
"use strict";
|
2016-09-16 13:29:56 +02:00
|
|
|
$.getJSON(jobImportUrl).done(reportOnJobImport).fail(failedJobImport);
|
2016-08-13 21:51:01 +02:00
|
|
|
}
|
|
|
|
|
2017-01-02 10:34:01 +01:00
|
|
|
function importComplete() {
|
2016-08-13 21:51:01 +02:00
|
|
|
"use strict";
|
2016-08-13 23:28:01 +02:00
|
|
|
var bar = $('#import-status-bar');
|
|
|
|
bar.removeClass('active');
|
|
|
|
}
|
2016-08-13 21:51:01 +02:00
|
|
|
|
2016-08-13 23:28:01 +02:00
|
|
|
function updateBar(data) {
|
|
|
|
"use strict";
|
2016-08-13 21:51:01 +02:00
|
|
|
var bar = $('#import-status-bar');
|
|
|
|
if (data.showPercentage) {
|
|
|
|
bar.addClass('progress-bar-success').removeClass('progress-bar-info');
|
|
|
|
bar.attr('aria-valuenow', data.percentage);
|
|
|
|
bar.css('width', data.percentage + '%');
|
|
|
|
$('#import-status-bar').text(data.stepsDone + '/' + data.steps);
|
|
|
|
|
|
|
|
if (data.percentage >= 100) {
|
2016-08-13 23:28:01 +02:00
|
|
|
importComplete(data);
|
2016-08-13 21:51:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-08-13 23:28:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// dont show percentage:
|
|
|
|
bar.removeClass('progress-bar-success').addClass('progress-bar-info');
|
|
|
|
bar.attr('aria-valuenow', 100);
|
|
|
|
bar.css('width', '100%');
|
|
|
|
}
|
|
|
|
|
|
|
|
function reportErrors(data) {
|
|
|
|
"use strict";
|
|
|
|
if (data.errors.length == 1) {
|
2016-08-14 08:34:59 +02:00
|
|
|
$('#import-status-error-intro').text(langImportSingleError);
|
|
|
|
//'An error has occured during the import. The import can continue, however.'
|
2016-08-13 23:28:01 +02:00
|
|
|
}
|
|
|
|
if (data.errors.length > 1) {
|
2016-08-14 08:34:59 +02:00
|
|
|
// 'Errors have occured during the import. The import can continue, however.'
|
|
|
|
$('#import-status-error-intro').text(langImportMultiError);
|
2016-08-13 23:28:01 +02:00
|
|
|
}
|
2016-08-13 21:51:01 +02:00
|
|
|
|
2016-08-14 08:34:59 +02:00
|
|
|
// fill the list with error texts
|
2016-08-13 23:28:01 +02:00
|
|
|
$('#import-status-error-list').empty();
|
|
|
|
for (var i = 0; i < data.errors.length; i++) {
|
2016-08-14 09:49:04 +02:00
|
|
|
var item = $('<li>').html(data.errors[i]);
|
2016-08-13 23:28:01 +02:00
|
|
|
$('#import-status-error-list').append(item);
|
2016-08-13 21:51:01 +02:00
|
|
|
}
|
2016-08-13 23:28:01 +02:00
|
|
|
}
|
2016-08-13 21:51:01 +02:00
|
|
|
|
2016-08-13 23:28:01 +02:00
|
|
|
function reportStatus(data) {
|
|
|
|
"use strict";
|
2016-08-13 21:51:01 +02:00
|
|
|
$('#import-status-txt').removeClass('text-danger').text(data.statusText);
|
2016-08-13 23:28:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function kickStartJob() {
|
|
|
|
"use strict";
|
2017-01-02 08:30:20 +01:00
|
|
|
$.post(jobStartUrl);
|
2016-08-13 23:28:01 +02:00
|
|
|
startedTheImport();
|
|
|
|
startedImport = true;
|
|
|
|
}
|
|
|
|
|
2016-08-14 09:10:47 +02:00
|
|
|
function updateTimeout(data) {
|
|
|
|
"use strict";
|
|
|
|
if (data.stepsDone != stepCount) {
|
|
|
|
stepCount = data.stepsDone;
|
|
|
|
currentLimit = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentLimit = currentLimit + interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
function timeoutError() {
|
|
|
|
"use strict";
|
|
|
|
// set status
|
|
|
|
$('#import-status-txt').addClass('text-danger').text(langImportTimeOutError);
|
|
|
|
|
|
|
|
// remove progress bar.
|
|
|
|
$('#import-status-holder').hide();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-14 10:11:49 +02:00
|
|
|
function importJobFinished(data) {
|
2016-08-13 23:28:01 +02:00
|
|
|
"use strict";
|
2016-08-14 10:11:49 +02:00
|
|
|
return data.finished;
|
|
|
|
}
|
2016-08-13 23:28:01 +02:00
|
|
|
|
2016-08-14 11:31:09 +02:00
|
|
|
function finishedJob(data) {
|
2016-08-14 10:11:49 +02:00
|
|
|
"use strict";
|
|
|
|
// "There was an error during the import routine. Please check the log files. The error seems to be: '"
|
|
|
|
$('#import-status-txt').removeClass('text-danger').addClass('text-success').text(langImportFinished);
|
|
|
|
|
|
|
|
// remove progress bar.
|
|
|
|
$('#import-status-holder').hide();
|
2016-08-14 11:31:09 +02:00
|
|
|
|
|
|
|
// show info:
|
|
|
|
$('#import-status-intro').show();
|
|
|
|
$('#import-status-more-info').html(data.finishedText).show();
|
|
|
|
|
2016-08-14 10:11:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function reportOnJobImport(data) {
|
|
|
|
"use strict";
|
2016-08-13 23:28:01 +02:00
|
|
|
updateBar(data);
|
|
|
|
reportErrors(data);
|
|
|
|
reportStatus(data);
|
2016-08-14 09:10:47 +02:00
|
|
|
updateTimeout(data);
|
|
|
|
|
2016-08-14 10:11:49 +02:00
|
|
|
if (importJobFinished(data)) {
|
2016-08-14 11:31:09 +02:00
|
|
|
finishedJob(data);
|
2016-08-14 10:11:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-14 09:10:47 +02:00
|
|
|
// same number of steps as last time?
|
|
|
|
if (currentLimit > timeoutLimit) {
|
|
|
|
timeoutError();
|
|
|
|
return;
|
|
|
|
}
|
2016-08-13 21:51:01 +02:00
|
|
|
|
|
|
|
// if the job has not actually started, do so now:
|
|
|
|
if (!data.started && !startedImport) {
|
2016-08-13 23:28:01 +02:00
|
|
|
kickStartJob();
|
|
|
|
return;
|
2016-08-13 21:51:01 +02:00
|
|
|
}
|
2016-08-13 23:28:01 +02:00
|
|
|
|
|
|
|
// trigger another check.
|
|
|
|
setTimeout(checkImportStatus, interval);
|
|
|
|
|
2016-08-13 21:51:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function startedTheImport() {
|
|
|
|
"use strict";
|
2016-08-13 23:28:01 +02:00
|
|
|
setTimeout(checkImportStatus, interval);
|
2016-08-13 21:51:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function failedJobImport(jqxhr, textStatus, error) {
|
|
|
|
"use strict";
|
|
|
|
// set status
|
2016-08-14 08:34:59 +02:00
|
|
|
// "There was an error during the import routine. Please check the log files. The error seems to be: '"
|
2016-08-14 09:10:47 +02:00
|
|
|
$('#import-status-txt').addClass('text-danger').text(langImportFatalError + ' ' + textStatus + ' ' + error);
|
2016-08-13 21:51:01 +02:00
|
|
|
|
|
|
|
// remove progress bar.
|
|
|
|
$('#import-status-holder').hide();
|
|
|
|
}
|