/**
* Main Javascript file
*
* @author Open Mind
* @date 27/06/2018
* @time 17:42
*/
var fetchHeaders = {
'Accept': 'text/html',
'Content-Type': 'text/html',
'X-Requested-With': 'XMLHttpRequest',
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': '0'
};
function NotificationBuilder(notifications) {
let tplNotify = '';
let htmlNotificationArea = '';
if (notifications.length === 0) {
$('#notification_area_button').addClass('disabled');
$('#sysmsgnum').empty();
} else {
notifications.forEach(function (element) {
tplNotify += `[${element.timestamp}] ${element.caption}${element.count}`;
});
htmlNotificationArea = `
${tplNotify}
Cancella tutto
`;
$('#notification_area_button').removeClass('disabled');
$('#sysmsgnum').html(notifications.length);
}
$('#notification_area').html(htmlNotificationArea);
}
function refreshNotificationArea() {
fetch(`${BASEURL}/api.system.getserverstatus`, {headers: fetchHeaders})
.then(resp => {
if (resp.status === 200)
return resp.json();
else
return Promise.reject("getserverstatus: error");
})
.then(data => {
NotificationBuilder(data.notify);
})
.catch(err => {
console.log(err);
});
}
function NotificationDelete(link, code) {
fetch(`${BASEURL}/api.system.removenotification/${code}`, {method: "POST"})
.then(() => {
if (link !== '#')
window.location.href = `${BASEURL}${link}`;
})
.catch(() => {
alert('NotificationDelete error');
});
}
function SetupConfirmationDialog() {
$('.requestconfirm').on('click', function (ev) {
let confirmText = $(this).data('confirmtext');
if (!confirm(confirmText))
ev.preventDefault();
});
}
function ForceFocus() {
setInterval(function () {
$("[autofocus]").focus();
}, 1000);
}
$(function () {
SetupConfirmationDialog();
refreshNotificationArea();
ForceFocus(); // INPUT AUTOFOCUS
$('#filters #filterIndex').val(currentFilterIdx);
$('body').on('submit', 'form', function () {
$('form input,form select').prop('disabled', false);
setTimeout(function () {
$('form input,form select').prop('disabled', true);
}, 500);
});
setInterval(() => {
refreshNotificationArea();
}, 3000);
});