allerta-vvf/server/resources/src/main.js

248 lines
8.3 KiB
JavaScript
Raw Normal View History

jQuery = $;
2020-09-13 00:56:56 +02:00
window.$ = window.jQuery = $;
import 'bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css';
2020-11-14 23:00:36 +01:00
import './main.css';
import './font-awesome.scss';
import 'bootstrap-datepicker';
2020-10-22 23:56:09 +02:00
import '../node_modules/bootstrap-toggle/css/bootstrap-toggle.css';
import '../node_modules/bootstrap-toggle/js/bootstrap-toggle.js';
import '../node_modules/bootstrap-datepicker/dist/css/bootstrap-datepicker3.css';
import 'time-input-polyfill/auto';
2020-11-05 00:08:54 +01:00
import 'jquery-pjax';
2021-03-06 16:26:17 +01:00
import toastr from 'toastr'
import 'toastr/build/toastr.css';
window.toastr = toastr;
toastr.options = {
"closeButton": false,
"debug": false,
"newestOnTop": false,
"progressBar": true,
"positionClass": "toast-bottom-right",
"preventDuplicates": false,
"onclick": null,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "5000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
}
2020-11-05 00:08:54 +01:00
2021-03-28 15:37:38 +02:00
$.fn.loading = function(action="start", options) {
var opts = $.extend( {}, $.fn.loading.defaults, options );
2021-04-05 14:52:03 +02:00
if(action === "show") {
2021-03-28 15:37:38 +02:00
this.addClass("loading_blur");
$( "body" ).append( "<div id='loading_div' class='loading_overlay'><p class=''><b>"+opts.message+"</b></p></div>" );
2021-04-05 14:52:03 +02:00
} else if(action === "hide") {
2021-03-28 15:37:38 +02:00
this.removeClass("loading_blur");
this.addClass("loading_no_blur");
setTimeout(() => {
this.removeClass("loading_no_blur");
}, 1000);
$( "#loading_div" ).remove();
}
};
$.fn.loading.defaults = {
message: "Loading..."
};
2020-11-27 23:53:14 +01:00
console.log("Commit: "+process.env.GIT_VERSION);
console.log("Date: "+process.env.GIT_AUTHOR_DATE);
console.log("Bundle mode: "+process.env.BUNDLE_MODE);
console.log("Bundle date: "+new Date(process.env.BUNDLE_DATE).toISOString());
2020-11-27 23:53:14 +01:00
2020-11-13 22:38:52 +01:00
$(document).pjax('a:not(.pjax_disable)', '#content', {timeout: 100000});
2020-11-05 00:08:54 +01:00
$(document).on('pjax:start', function() {
2021-01-03 00:51:47 +01:00
if(document.getElementById("topNavBar") !== undefined){
document.getElementById("topNavBar").className = "topnav";
}
old_data = "null";
2021-02-25 11:18:38 +01:00
fillTable = undefined;
table_engine = "datatables";
2020-11-05 00:08:54 +01:00
if(window.loadTable_interval !== undefined){
clearInterval(window.loadTable_interval);
window.loadTable_interval = undefined;
}
})
2020-09-13 00:56:56 +02:00
// Cookie functions from w3schools
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
$( document ).ajaxError(function(event, xhr, settings, error) {
console.error("Error requesting content: "+error+" - status code "+xhr.status);
console.log(event);
console.log(xhr);
console.log(settings);
});
if (getCookie("authenticated")) {
2021-03-31 22:45:35 +02:00
var installServiceWorker = false;
2021-02-20 21:05:03 +01:00
if(window.skipServiceWorkerInstallation !== undefined){ //if you want to disable SW for example via GreasyFork userscript
installServiceWorker = false;
}
if(getCookie("disableServiceWorkerInstallation")){
console.log("Skipping ServiceWorker installation because cookie 'disableServiceWorkerInstallation' exists");
installServiceWorker = false;
}
if ('serviceWorker' in navigator) {
if ('connection' in navigator && navigator.connection.saveData && !getCookie("forceServiceWorkerInstallation")) {
console.log("Skipping ServiceWorker installation because saveData is enabled");
installServiceWorker = false;
}
if ('storage' in navigator && 'estimate' in navigator.storage && !getCookie("forceServiceWorkerInstallation")){
2021-04-05 14:52:03 +02:00
navigator.storage.estimate().then((quota) => {
const requiredMemory = 3 * 1e+6;
if (quota < requiredMemory) {
console.log("Skipping ServiceWorker installation because memory is low. memory="+quota);
installServiceWorker = false;
}
2020-10-11 00:33:42 +02:00
});
}
} else {
installServiceWorker = false;
}
}
if(installServiceWorker){
window.addEventListener('load', () => {
2021-04-05 14:52:03 +02:00
navigator.serviceWorker.register('sw.js').then((registration) => {
console.log('SW registered: ', registration);
2021-04-05 14:52:03 +02:00
}).catch((registrationError) => {
console.log('SW registration failed: ', registrationError);
});
});
}
2021-02-28 22:08:23 +01:00
$( document ).ready(function() {
if($("#frontend_version") !== undefined){
2021-03-02 00:02:38 +01:00
$("#frontend_version").append(process.env.GIT_VERSION+" aggiornamento "+new Date(process.env.BUNDLE_DATE).toLocaleString());
2021-02-28 22:08:23 +01:00
}
});
2020-10-25 21:22:32 +01:00
var offline = false;
2020-11-05 00:08:54 +01:00
var loadTable_interval = undefined;
2020-12-17 00:06:08 +01:00
var old_data = "null";
2021-02-24 23:17:01 +01:00
var table_engine = "datatables";
2021-02-24 20:02:29 +01:00
var fillTable = undefined;
async function loadTable({table_page, set_interval=true, interval=10000, onlineReload=false, use_custom_table_engine=false, callback=false}){
2021-02-24 20:02:29 +01:00
if (typeof fillTable === "undefined"){
if(use_custom_table_engine !== false){
table_engine = use_custom_table_engine;
2021-02-28 20:43:38 +01:00
} else if ('connection' in navigator && navigator.connection.saveData) {
table_engine = "default";
}
2021-02-28 17:41:45 +01:00
fillTable = await import(`./table_engine_${table_engine}.js`)
2021-02-24 20:02:29 +01:00
.then(({default: _}) => {
return _;
});
}
if ('getBattery' in navigator) {
navigator.getBattery().then((level, charging) => {
if (!charging && level < 0.2) {
return;
}
})
}
if ('deviceMemory' in navigator && navigator.deviceMemory < 0.2) {
return;
}
2020-11-13 15:22:26 +01:00
let replaceLatLngWithMap = table_page == "services" || table_page == "trainings";
2020-12-17 00:06:08 +01:00
$.getJSON({ url: "resources/ajax/ajax_"+table_page+".php", data: { "old_data": old_data }, success: function( data, status, xhr ) {
old_data = xhr.getResponseHeader('data'); //TODO: refactoring and adding comments
console.log(data);
if(data.length > 0){
fillTable({data, replaceLatLngWithMap, callback});
2020-12-17 00:06:08 +01:00
var headers = new Headers();
headers.append('date', Date.now());
caches.open('tables-1').then((cache) => {
cache.put('/table_'+table_page+'.json', new Response(xhr.responseText, {headers: headers}))
});
}
if(window.offline){ // if xhr request successful, client is online
2020-11-06 15:14:59 +01:00
console.log(onlineReload);
if(onlineReload){
location.reload(); //for offline page
} else {
$("#offline_alert").hide(400);
window.offline = false;
}
}
2020-12-17 00:06:08 +01:00
}}).fail(function(data, status) {
if(status == "parsererror"){
2021-04-05 14:52:03 +02:00
if($("#table_body").children().length === 0) { //this is a server-side authentication error on some cheap hosting providers
2020-11-13 15:22:26 +01:00
loadTable(table_page, set_interval, interval); //retry
} // else nothing
} else {
2020-10-24 00:17:01 +02:00
caches.open('tables-1').then(cache => {
cache.match("/table_"+table_page+".json").then(response => {
response.json().then(data => {
fillTable({data, replaceLatLngWithMap, callback});
2020-10-24 00:17:01 +02:00
console.log("Table loaded from cache");
2020-10-25 21:22:32 +01:00
$("#offline_update").text(new Date(parseInt(response.headers.get("date"))).toLocaleString());
});
});
2020-10-24 00:17:01 +02:00
});
2020-10-25 21:22:32 +01:00
if(!window.offline){ // if xhr request fails, client is offline
$("#offline_alert").show(400);
window.offline = true;
}
2020-11-05 00:08:54 +01:00
}
});
if(set_interval){
if ('connection' in navigator && navigator.connection.saveData) {
interval += 5000;
}
console.log("table_load interval "+interval);
window.loadTable_interval = setInterval(function() {
window.loadTable({table_page, set_interval: false, interval, onlineReload, use_custom_table_engine, callback: false});
}, interval);
}
}
2020-11-14 23:00:36 +01:00
function chat() {
setCookie("chat", "true", 1);
location.reload();
}
window.addEventListener('securitypolicyviolation',console.error.bind(console));
function menu() {
2020-12-29 00:55:42 +01:00
var topNavBar = document.getElementById("topNavBar");
if (topNavBar.className === "topnav") {
topNavBar.className += " responsive";
2020-11-14 23:00:36 +01:00
} else {
2020-12-29 00:55:42 +01:00
topNavBar.className = "topnav";
2020-11-14 23:00:36 +01:00
}
}
2020-11-05 00:08:54 +01:00
window.loadTable_interval = loadTable_interval;
2020-11-13 15:22:26 +01:00
window.loadTable = loadTable;
window.setCookie = setCookie;
2020-11-14 23:00:36 +01:00
window.getCookie = getCookie;
window.chat = chat;
window.menu = menu;