From fefeee7ccf1b6fb6a133c8b9c5eb07b1760c2d94 Mon Sep 17 00:00:00 2001 From: rstular Date: Mon, 17 Feb 2020 15:15:55 +0100 Subject: Reformatting --- js/app.js | 12 +- js/initialize.js | 36 +++-- js/lib/xss.js | 1 - js/login.js | 19 +-- js/messaging.js | 462 +++++++++++++++++++++++++++++++------------------------ js/timetable.js | 2 +- 6 files changed, 295 insertions(+), 237 deletions(-) (limited to 'js') diff --git a/js/app.js b/js/app.js index eebb62d..98da025 100644 --- a/js/app.js +++ b/js/app.js @@ -5,12 +5,12 @@ if ("serviceWorker" in navigator) { } // Listen to messages from service workers. -if(navigator.serviceWorker) { -navigator.serviceWorker.addEventListener('message', (event) => { - if (event.data.msg === "install") { - window.location.replace("/index.html"); - } -}); +if (navigator.serviceWorker) { + navigator.serviceWorker.addEventListener('message', (event) => { + if (event.data.msg === "install") { + window.location.replace("/index.html"); + } + }); } if (location.protocol != 'https:') { diff --git a/js/initialize.js b/js/initialize.js index 2023921..ca29e2f 100644 --- a/js/initialize.js +++ b/js/initialize.js @@ -1,15 +1,13 @@ function getUrlParameter(sParam) { - var sPageURL = window.location.search.substring(1), - sURLVariables = sPageURL.split('&'), - sParameterName, - i; - for (i = 0; i < sURLVariables.length; i++) { - sParameterName = sURLVariables[i].split('='); - if (sParameterName[0] === sParam) { - return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]); - } + const url_params = new URLSearchParams(window.location.search); + const found_param = url_params.get(sParam); + if (found_param === null) { + return "" + } else { + return found_param } -}; +} + function setupStorage() { promises_to_run = [ localforage.setItem("logged_in", false), @@ -21,7 +19,8 @@ function setupStorage() { localforage.setItem("gradings", []), localforage.setItem("grades", []), localforage.setItem("absences", {}), - localforage.setItem("messages", {}) + localforage.setItem("messages", {}), + localforage.setItem("directory", {}) ]; Promise.all(promises_to_run) @@ -36,15 +35,20 @@ localforage.getItem("logged_in") // This code runs once the value has been loaded // from the offline store. if (value === null) { + // Setup the storage if it doesn't exist setupStorage(); } else if (value === false) { + // If storage exists, but user isn't logged in, redirect to login window.location.replace("/login.html"); } else { - if(getUrlParameter("m")) { - window.location.replace("pages/messaging.html?"+getUrlParameter("m")); - } else { - window.location.replace("/pages/timetable.html"); - } + // User is logged in, execute appropriate action + + if (getUrlParameter("m") !== "") { + window.location.replace("/pages/messaging.html?m=" + getUrlParameter("m")); + } else { + window.location.replace("/pages/timetable.html"); + } + } } ).catch( diff --git a/js/lib/xss.js b/js/lib/xss.js index bddbdd8..e0a6ba0 100644 --- a/js/lib/xss.js +++ b/js/lib/xss.js @@ -1611,4 +1611,3 @@ module.exports = { }; },{}]},{},[2]); - diff --git a/js/login.js b/js/login.js index 6a68d98..aba0fa3 100644 --- a/js/login.js +++ b/js/login.js @@ -7,17 +7,18 @@ document.addEventListener("DOMContentLoaded", () => { function setupEventListeners() { // Setup login button listener - $("#login-button").click(function () { + $("#login-button").click(() => { login(); }); - window.addEventListener("keyup", function(event) { - // Number 13 is the "Enter" key on the keyboard - if (event.keyCode === 13) { - // Cancel the default action, if needed - event.preventDefault(); - login(); - } - }); + + window.addEventListener("keyup", (event) => { + // Number 13 is the "Enter" key on the keyboard + if (event.keyCode === 13) { + // Cancel the default action, if needed + event.preventDefault(); + login(); + } + }); } // Handle login button click diff --git a/js/messaging.js b/js/messaging.js index 42bbd0b..4004147 100644 --- a/js/messaging.js +++ b/js/messaging.js @@ -1,3 +1,10 @@ +const API_ENDPOINT = "https://gimb.tk/test.php"; +const DIRECTORY_URL = "/directory.json"; +// const API_ENDPOINT = "http://localhost:5000/test.php"; + +// "Global" object for name directory +var directory = null; + async function checkLogin() { localforage.getItem("logged_in").then(function (value) { // This code runs once the value has been loaded @@ -11,31 +18,72 @@ async function checkLogin() { }); } -function htmlEncode(value){ - // Create a in-memory element, set its inner text (which is automatically encoded) - // Then grab the encoded contents back out. The element never exists on the DOM. - return $('