openstamanager/assets/src/js/functions/hooks.js

154 lines
4.1 KiB
JavaScript
Raw Normal View History

2019-08-26 18:02:05 +02:00
/**
*
*/
function startHooks() {
$.ajax({
url: globals.rootdir + "/ajax.php",
type: "get",
data: {
op: "hooks",
},
2019-08-27 15:42:13 +02:00
success: function (data) {
2019-08-26 18:02:05 +02:00
hooks = JSON.parse(data);
2019-07-26 17:40:52 +02:00
2019-08-26 18:02:05 +02:00
$("#hooks-header").text(globals.translations.hooksExecuting);
$("#hooks-number").text(hooks.length);
if (hooks.length == 0) {
$("#hooks-loading").hide();
$("#hooks-number").text(0);
$("#hooks-header").text(globals.translations.hookNone);
}
2019-08-27 15:42:13 +02:00
hooks.forEach(function (item, index) {
renderHook(item, {
show: true,
message: globals.translations.hookExecuting.replace('_NAME_', item.name)
});
executeHook(item, true);
2019-08-26 18:02:05 +02:00
});
},
});
}
/**
2019-08-28 09:28:08 +02:00
* Esegue l'hook e lo visualizza.
* Considerare l'utilizzo di localStorage per bloccare l'esecuzione locale multipla dell'hook nel caso di problemi.
2019-08-26 18:02:05 +02:00
*
* @param hook
2019-08-27 15:42:13 +02:00
* @param element_id
2019-08-26 18:02:05 +02:00
*/
2019-08-27 15:42:13 +02:00
function executeHook(hook, init) {
2019-07-26 17:40:52 +02:00
$.ajax({
url: globals.rootdir + "/ajax.php",
type: "get",
data: {
2019-08-27 15:42:13 +02:00
op: "hook",
2019-07-26 17:40:52 +02:00
id: hook.id,
2019-08-27 15:42:13 +02:00
init: init,
2019-07-26 17:40:52 +02:00
},
2019-08-27 15:42:13 +02:00
success: function (data) {
2019-08-26 18:02:05 +02:00
var result = JSON.parse(data);
2019-07-26 17:40:52 +02:00
2019-08-27 15:42:13 +02:00
renderHook(hook, result);
2019-07-26 17:40:52 +02:00
2019-08-27 15:42:13 +02:00
var timeout;
if (result.execute) {
timeout = 1;
2019-07-26 17:40:52 +02:00
} else {
2019-08-27 15:42:13 +02:00
timeout = 30;
2019-07-26 17:40:52 +02:00
}
2019-08-27 15:42:13 +02:00
setTimeout(function () {
executeHook(hook);
}, timeout * 1000);
2019-08-26 18:02:05 +02:00
2019-08-27 15:42:13 +02:00
if (init) {
hookCount("#hooks-counter");
2019-08-26 18:02:05 +02:00
}
2019-07-26 17:40:52 +02:00
// Rimozione eventuale della rotella di caricamento
2019-08-26 18:02:05 +02:00
var counter = $("#hooks-counter").text();
2019-08-27 15:42:13 +02:00
var number = $("#hooks > li").length;
$("#hooks-notified").text(number);
if (counter == $("#hooks-number").text()) {
2019-07-26 17:40:52 +02:00
$("#hooks-loading").hide();
2019-08-27 15:42:13 +02:00
var hookMessage;
if (number > 1) {
2019-07-26 17:40:52 +02:00
hookMessage = globals.translations.hookMultiple.replace('_NUM_', number);
2019-08-27 15:42:13 +02:00
} else if (number == 1) {
2019-07-26 17:40:52 +02:00
hookMessage = globals.translations.hookSingle;
2019-08-27 15:42:13 +02:00
} else {
2019-07-26 17:40:52 +02:00
hookMessage = globals.translations.hookNone;
}
$("#hooks-header").text(hookMessage);
}
},
});
}
2019-08-26 18:02:05 +02:00
/**
* Aggiunta dell'hook al numero totale.
*/
2019-08-27 15:42:13 +02:00
function hookCount(id, value) {
value = value ? value : 1;
var element = $(id);
var number = parseInt(element.text());
2019-08-26 18:02:05 +02:00
number = isNaN(number) ? 0 : number;
2019-08-27 15:42:13 +02:00
number += value;
element.text(number);
2019-08-26 18:02:05 +02:00
return number;
}
/**
2019-08-27 15:42:13 +02:00
* Genera l'HTML per la visualizzazione degli hook.
2019-08-26 18:02:05 +02:00
*
* @param element_id
* @param result
*/
2019-08-27 15:42:13 +02:00
function renderHook(hook, result) {
if (result.length == 0) return;
var element_id = "hook-" + hook.id;
// Inizializzazione
var element = $("#" + element_id);
if (element.length == 0) {
$("#hooks").append('<li class="hook-element" id="' + element_id + '"></li>');
element = $("#" + element_id);
}
// Rimozione
if (!result.show) {
element.remove();
return;
}
// Contenuto
var content = '<a href="' + (result.link ? result.link : "#") + '"><i class="' + result.icon + '"></i><span class="small"> ' + result.message + '</span>';
if (result.progress) {
var current = result.progress.current;
var total = result.progress.total;
2019-08-28 11:52:04 +02:00
var percentage = current / total * 100;
percentage = isNaN(percentage) ? 100 : percentage;
2019-08-27 15:42:13 +02:00
2019-08-27 17:25:52 +02:00
percentage = Math.round(percentage * 100) / 100;
2019-08-27 15:42:13 +02:00
content += '<div class="progress" style="margin-bottom: 0px;"><div class="progress-bar" role="progressbar" aria-valuenow="' + percentage + '" aria-valuemin="0" aria-valuemax="100" style="width:' + percentage + '%">' + percentage + '% (' + current + '/' + total + ')</div></div>';
}
content += '</a>';
element.html(content);
2019-08-26 18:02:05 +02:00
}