var JOBSPOLLINTERVALL = 1000; var USERS = { "testbenutzer": { "e-mail": "t.benutzer@uni-bielefeld.de", "firstName": "Test", "id": "testbenutzer", "lastName": "Benutzer", "notifications": true, "password": "passwort", "phone": "+49 521 106-XXXXX" } }; // The job list of the current user var jobs; // The current user var user; /** * Login a user and redirect the visitor to the portal page. * @param {string} password - The password to use for login. * @param {string} user - The user to use for login. */ function login(password, user) { // Check if the user exists if (USERS[user]) { // Check if the password is correct if (USERS[user]["password"] === password) { // Save the user data to the local storage localStorage.setItem("user", JSON.stringify(USERS[user])); // Redirect the visitor to the portal page window.location = "/vre/portal.html"; return; } } // The function only ends here, when the user doesn't exists or when the // password was wrong, in both cases it should throw an exception throw "User doesn't exist or password was wrong!"; } /** * Logout the current user and redirect the visitor to the login page. */ function logout() { // delete all data from the local storage localStorage.clear(); // redirect to the login page window.location = "/vre/"; } /** * The main function, which is executed on each page after it's completly * loaded. */ function main() { M.Dropdown.init( document.getElementById("main-nav-account"), {"constrainWidth": false, "coverTrigger": false} ); // Indicates whether the current page is the login page var isLoginPage; // Indicates whether the current page is a service page (like ocr or nlp) var isServicePage; isLoginPage = window.location.pathname === "/vre/" || window.location.pathname === "/vre/index.html"; isServicePage = window.location.pathname === "/vre/nlp.html" || window.location.pathname === "/vre/ocr.html"; // Check if the visitor is logged in, by checking if the local storage // contains a user if (localStorage.getItem("user")) { // Redirect to the portal page if the current page is the login page if (isLoginPage) window.location = "portal.html"; // Load user from the local storage user = JSON.parse(localStorage.getItem("user")); // If a function named setUserCallback exists, call it if (typeof(setUserCallback) === "function") setUserCallback(); } else { // Redirect to the login page if the current page isn't the login page if (!isLoginPage) window.location = "index.html"; return; } // Check if the local storage contains a copy of the users job list if (localStorage.getItem("jobs")) { // Load the job list from the local storage jobs = JSON.parse(localStorage.getItem("jobs")); // If a function named setJobsCallback exists, call it if (typeof(setJobsCallback) === "function") setJobsCallback(); } else { // Load the job list from the vre server getJobs({"user": user["id"]}, function(newJobs) { jobs = newJobs; // If a function named setJobsCallback exists, call it if (typeof(setJobsCallback) === "function") setJobsCallback(); }); } // Set a continous poll for the job list setInterval(getJobs, JOBSPOLLINTERVALL, {"user": user["id"]}, updateJobs); } /** * Updates the job list in the local storage and the contents of the global * job list variable. It also informs the visitor about job status changes. * @param {Object[]} newJobs - The new job list. */ function updateJobs(newJobs) { var i; // Indicates whether the new job list differs from the old one var hasJobsChanged; // The new job list as JSON string var newJobsAsJSONString; newJobsAsJSONString = JSON.stringify(newJobs); hasJobsChanged = localStorage.getItem("jobs") != newJobsAsJSONString; if (hasJobsChanged) { // Iterate over the old job list for (i = 0; i < jobs.length; i++) { // Check whether the status of the currently iterated job changed if (jobs[i]["status"] != newJobs[i]["status"]) { // Inform the visitor with a toast message about the new status switch (newJobs[i]["status"]) { case "failed": M.toast({html: "Auftrag '" + jobs[i]["name"] + "' is fehlgeschlagen."}); break; case "finished": M.toast({html: "Auftrag '" + jobs[i]["name"] + "' wurde abgeschlossen."}); break; case "running": M.toast({html: "Auftrag '" + jobs[i]["name"] + "' wird bearbeitet."}); break; default: } } } // Save the new job list to the local storage localStorage.setItem("jobs", newJobsAsJSONString); // Replace the global job list variable with the new job list jobs = newJobs; // If a function named setJobsCallback exists, call it if (typeof(setJobsCallback) === "function") setJobsCallback(); } } window.onload = main;