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

254 lines
8.4 KiB
JavaScript
Raw Normal View History

2021-04-05 18:23:53 +02:00
jQuery = $;
window.$ = window.jQuery = $;
2021-04-05 15:18:26 +02:00
import "bootstrap";
import "bootstrap/dist/css/bootstrap.min.css";
import "./main.css";
import "./font-awesome.scss";
import "bootstrap-datepicker";
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";
import "jquery-pjax";
import toastr from "toastr";
import "toastr/build/toastr.css";
2021-04-05 18:23:53 +02:00
2021-03-06 16:26:17 +01:00
window.toastr = toastr;
toastr.options = {
2021-04-05 15:18:26 +02:00
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"
};
$.fn.loading = function (action = "start", options) {
const opts = $.extend({}, $.fn.loading.defaults, options);
2020-11-05 00:08:54 +01:00
2021-04-05 15:18:26 +02:00
if (action === "show") {
this.addClass("loading_blur");
$("body").append("<div id='loading_div' class='loading_overlay'><p class=''><b>" + opts.message + "</b></p></div>");
} else if (action === "hide") {
this.removeClass("loading_blur");
this.addClass("loading_no_blur");
setTimeout(() => {
this.removeClass("loading_no_blur");
}, 1000);
$("#loading_div").remove();
2021-03-28 15:37:38 +02:00
}
};
$.fn.loading.defaults = {
message: "Loading..."
};
2021-04-05 15:18:26 +02: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
2021-04-05 15:18:26 +02:00
$(document).pjax("a:not(.pjax_disable)", "#content", { timeout: 100000 });
$(document).on("pjax:start", function () {
if (document.getElementById("topNavBar") !== undefined) {
2021-01-03 00:51:47 +01:00
document.getElementById("topNavBar").className = "topnav";
}
2021-04-05 18:57:52 +02:00
oldData = "null";
2021-02-25 11:18:38 +01:00
fillTable = undefined;
2021-04-05 18:57:52 +02:00
tableEngine = "datatables";
if (window.loadTableInterval !== undefined) {
clearInterval(window.loadTableInterval);
window.loadTableInterval = undefined;
2020-11-05 00:08:54 +01:00
}
2021-04-05 15:18:26 +02:00
});
2020-09-13 00:56:56 +02:00
// Cookie functions from w3schools
2021-04-05 15:18:26 +02:00
function setCookie (cname, cvalue, exdays) {
const d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
2021-04-05 15:18:26 +02:00
const expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
2021-04-05 15:18:26 +02:00
function getCookie (cname) {
const name = cname + "=";
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(";");
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === " ") {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
2021-04-05 15:18:26 +02:00
$(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-04-05 15:18:26 +02:00
if (window.skipServiceWorkerInstallation !== undefined) { // if you want to disable SW for example via GreasyFork userscript
installServiceWorker = false;
}
2021-04-05 15:18:26 +02:00
if (getCookie("disableServiceWorkerInstallation")) {
console.log("Skipping ServiceWorker installation because cookie 'disableServiceWorkerInstallation' exists");
installServiceWorker = false;
}
2021-04-05 15:18:26 +02:00
if ("serviceWorker" in navigator) {
if ("connection" in navigator && navigator.connection.saveData && !getCookie("forceServiceWorkerInstallation")) {
console.log("Skipping ServiceWorker installation because saveData is enabled");
installServiceWorker = false;
}
2021-04-05 15:18:26 +02:00
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) {
2021-04-05 15:18:26 +02:00
console.log("Skipping ServiceWorker installation because memory is low. memory=" + quota);
installServiceWorker = false;
}
2020-10-11 00:33:42 +02:00
});
}
} else {
installServiceWorker = false;
}
}
2021-04-05 15:18:26 +02:00
if (installServiceWorker) {
window.addEventListener("load", () => {
navigator.serviceWorker.register("sw.js").then((registration) => {
console.log("SW registered: ", registration);
2021-04-05 14:52:03 +02:00
}).catch((registrationError) => {
2021-04-05 15:18:26 +02:00
console.log("SW registration failed: ", registrationError);
});
});
}
2021-04-05 15:18:26 +02:00
$(document).ready(function () {
if ($("#frontend_version") !== undefined) {
$("#frontend_version").append(process.env.GIT_VERSION + " aggiornamento " + new Date(process.env.BUNDLE_DATE).toLocaleString());
2021-02-28 22:08:23 +01:00
}
});
2021-04-05 15:18:26 +02:00
const offline = false;
2021-04-05 18:57:52 +02:00
const loadTableInterval = undefined;
var oldData = "null";
var tableEngine = "datatables";
2021-02-24 20:02:29 +01:00
var fillTable = undefined;
2021-04-05 19:13:40 +02:00
async function loadTable ({ tablePage, setInterval = true, interval = 10000, onlineReload = false, useCustomTableEngine = false, callback = false }) {
2021-04-05 15:18:26 +02:00
if (typeof fillTable === "undefined") {
2021-04-05 19:13:40 +02:00
if (useCustomTableEngine !== false) {
tableEngine = useCustomTableEngine;
2021-04-05 15:18:26 +02:00
} else if ("connection" in navigator && navigator.connection.saveData) {
2021-04-05 18:57:52 +02:00
tableEngine = "default";
}
2021-04-05 18:57:52 +02:00
fillTable = await import(`./tableEngine_${tableEngine}.js`)
2021-04-05 15:18:26 +02:00
.then(({ default: _ }) => {
return _;
});
2021-02-24 20:02:29 +01:00
}
2021-04-05 15:18:26 +02:00
if ("getBattery" in navigator) {
navigator.getBattery().then((level, charging) => {
if (!charging && level < 0.2) {
2021-04-05 18:23:53 +02:00
return;
}
2021-04-05 15:18:26 +02:00
});
}
2021-04-05 15:18:26 +02:00
if ("deviceMemory" in navigator && navigator.deviceMemory < 0.2) {
return;
}
2021-04-05 18:57:52 +02:00
const replaceLatLngWithMap = tablePage === "services" || tablePage === "trainings";
2021-04-05 15:18:26 +02:00
$.getJSON({
2021-04-05 18:57:52 +02:00
url: "resources/ajax/ajax_" + tablePage + ".php",
data: { oldData: oldData },
2021-04-05 15:18:26 +02:00
success: function (data, status, xhr) {
2021-04-05 18:57:52 +02:00
oldData = xhr.getResponseHeader("data"); // TODO: refactoring and adding comments
2021-04-05 15:18:26 +02:00
console.log(data);
if (data.length > 0) {
fillTable({ data, replaceLatLngWithMap, callback });
const headers = new Headers();
headers.append("date", Date.now());
caches.open("tables-1").then((cache) => {
2021-04-05 18:57:52 +02:00
cache.put("/table_" + tablePage + ".json", new Response(xhr.responseText, { headers: headers }));
2021-04-05 15:18:26 +02:00
});
}
if (window.offline) { // if xhr request successful, client is online
console.log(onlineReload);
if (onlineReload) {
location.reload(); // for offline page
} else {
$("#offline_alert").hide(400);
window.offline = false;
}
2020-11-06 15:14:59 +01:00
}
}
2021-04-05 15:18:26 +02:00
}).fail(function (data, status) {
2021-04-05 18:23:53 +02:00
if (status === "parsererror") {
2021-04-05 15:18:26 +02:00
if ($("#table_body").children().length === 0) { // this is a server-side authentication error on some cheap hosting providers
2021-04-05 19:13:40 +02:00
loadTable(tablePage, setInterval, interval); // retry
} // else nothing
} else {
2021-04-05 15:18:26 +02:00
caches.open("tables-1").then(cache => {
2021-04-05 18:57:52 +02:00
cache.match("/table_" + tablePage + ".json").then(response => {
2020-10-24 00:17:01 +02:00
response.json().then(data => {
2021-04-05 15:18:26 +02:00
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
});
2021-04-05 15:18:26 +02:00
if (!window.offline) { // if xhr request fails, client is offline
2020-10-25 21:22:32 +01:00
$("#offline_alert").show(400);
window.offline = true;
}
2020-11-05 00:08:54 +01:00
}
});
2021-04-05 19:13:40 +02:00
if (setInterval) {
2021-04-05 15:18:26 +02:00
if ("connection" in navigator && navigator.connection.saveData) {
interval += 5000;
}
2021-04-05 15:18:26 +02:00
console.log("table_load interval " + interval);
2021-04-05 18:57:52 +02:00
window.loadTableInterval = setInterval(function () {
2021-04-05 19:13:40 +02:00
window.loadTable({ tablePage, setInterval: false, interval, onlineReload, useCustomTableEngine, callback: false });
}, interval);
}
}
2021-04-05 15:18:26 +02:00
function chat () {
2020-11-14 23:00:36 +01:00
setCookie("chat", "true", 1);
location.reload();
}
2021-04-05 15:18:26 +02:00
window.addEventListener("securitypolicyviolation", console.error.bind(console));
2020-11-14 23:00:36 +01:00
2021-04-05 15:18:26 +02:00
function menu () {
const topNavBar = document.getElementById("topNavBar");
2020-12-29 00:55:42 +01:00
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
}
}
2021-04-05 18:57:52 +02:00
window.loadTableInterval = loadTableInterval;
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;
2021-04-05 15:18:26 +02:00
window.menu = menu;