From 9d88acff4d715cdc6b44c8955049bb0289fb17f7 Mon Sep 17 00:00:00 2001 From: rstular Date: Sat, 16 May 2020 20:08:05 +0200 Subject: GSEC bug fixes - handling commas properly --- js/about.js | 3 +++ js/absences.js | 42 +++++++++++++++++++++++++++++++++--------- js/app.js | 9 +++++++++ js/gsec.js | 17 ++++++++++++----- js/messaging.js | 2 +- 5 files changed, 58 insertions(+), 15 deletions(-) diff --git a/js/about.js b/js/about.js index 8dd55ab..7056b06 100644 --- a/js/about.js +++ b/js/about.js @@ -1,3 +1,6 @@ +/** + * Redirects user to login page if it's not logged int + */ async function checkLogin() { localforage.getItem("logged_in").then(function (value) { // This code runs once the value has been loaded diff --git a/js/absences.js b/js/absences.js index 9bc4a83..c0d08a7 100644 --- a/js/absences.js +++ b/js/absences.js @@ -1,6 +1,9 @@ // const API_ENDPOINT = "https://gimb.tk/test.php"; // deprecated // const API_ENDPOINT = "http://localhost:5000/test.php"; -var absences; + +/** + * Redirects user to login page if it's not logged int + */ async function checkLogin() { localforage.getItem("logged_in").then(function (value) { // This code runs once the value has been loaded @@ -14,7 +17,10 @@ async function checkLogin() { }); } -// Set loading bar visibility +/** + * Sets visibility of the loading bar + * @param {boolean} state Desired visibility + */ function setLoading(state) { if (state) { $("#loading-bar").removeClass("hidden"); @@ -23,10 +29,14 @@ function setLoading(state) { } } -async function loadAbsences(force_refresh = false) { +/** + * Loads absences from API and displays them + * @param {boolean} forceRefresh If true, cached absences are ignored + */ +async function loadAbsences(forceRefresh = false) { setLoading(true); // Load required data - let promises_to_run = [ + let promisesToRun = [ localforage.getItem("username").then(function (value) { username = value; }), @@ -37,12 +47,12 @@ async function loadAbsences(force_refresh = false) { absences = value; }) ]; - await Promise.all(promises_to_run); + await Promise.all(promisesToRun); // If we don't have a list of absences, query it - if (absences === null || force_refresh) { + if (absences === null || forceRefresh) { try { let gsecInstance = new gsec(); - await gsecInstance.login(username, password); + await gsecInstance.login(username, password); let date = {}; date.from = $("#datepicker-from").val().split("."); date.till = $("#datepicker-to").val().split("."); @@ -91,11 +101,16 @@ async function loadAbsences(force_refresh = false) { } } +/** + * Display absences data - called by loadAbsences + */ function displayData() { absences.forEach(absence => { let li = document.createElement("li"); + + // dateString comes from bundle.js + let date_string = dateString.longFormatted(absence["date"]); - let date_string = dateString.longFormatted(absence["date"]); // javascript sucks - zakaj ob vsej svoji "preprostosti" ne morm met Date za key objecta!?!?'!!11~ let header = document.createElement("div"); header.className = "collapsible-header"; header.innerText = date_string; @@ -112,7 +127,7 @@ function displayData() { let subject_row = document.createElement("tr"); let subject_lesson_icon = document.createElement("td"); let subject_lesson_text = document.createElement("td"); - subject_lesson_text.innerText = S("lesson") + " " + lesson; + subject_lesson_text.innerText = `${S("lesson")} ${lesson}`; let subject_lesson_icon_i = document.createElement("i"); subject_lesson_icon_i.className = "material-icons"; @@ -151,6 +166,9 @@ function displayData() { }); } +/** + * Clear all displayed absences + */ function clearAbsences() { const table = document.getElementById("absences-col"); while (table.firstChild) { @@ -158,11 +176,17 @@ function clearAbsences() { } } +/** + * Force reloading of absences + */ function refreshAbsences() { clearAbsences(); loadAbsences(true); } +/** + * Setup date pickers (from date and to date) + */ function setupPickers() { // Setup pickers var date_object = new Date(); diff --git a/js/app.js b/js/app.js index 20cfae4..1b39086 100644 --- a/js/app.js +++ b/js/app.js @@ -13,6 +13,11 @@ if (navigator.serviceWorker) { }); } +/** + * Displays a user-friendly text to the user and detailed text to developer (console) + * @param {string} usermsg User-friendly message + * @param {string} devmsg Developer-friendly message + */ async function UIAlert(usermsg, devmsg) { if(true) { // če bo kakšen dev switch? M.toast( { html: usermsg } ); @@ -22,6 +27,10 @@ async function UIAlert(usermsg, devmsg) { } } +/** + * Handles GSEC error - notifies the user and prints a console message + * @param {Object} err GSEC error object + */ function gsecErrorHandlerUI(err) { console.log(`gsecErrorHanderUI: handling ${err}`); if(err == GSEC_ERR_NET || err == GSEC_ERR_NET_POSTBACK_GET || err == GSEC_ERR_NET_POSTBACK_POST) { diff --git a/js/gsec.js b/js/gsec.js index 780d542..7f6ac87 100644 --- a/js/gsec.js +++ b/js/gsec.js @@ -363,7 +363,8 @@ class gsec { fetchAbsences(fromDate = null, tillDate = null) { // navedba datumov je deprecated. Internet je dovolj hiter za poslat maksimalno 4160 ur (16 ur/dan, 5 dni/ted, 52 ted/leto) - const FIELDS_REGEX = /^(.+) \((\dP?)<\/span>/; + const SUBJECT_LIST_REGEX = /(.+? \(\dP?<\/span>\))(?:,\s*|$)/g; + const FIELDS_REGEX = /^(.+?) \((\dP?)<\/span>\)/; return new Promise((resolve, reject)=>{ if (!(fromDate instanceof Date) || !(tillDate instanceof Date)) { @@ -390,9 +391,15 @@ class gsec { var subFields = izostanek.getElementsByTagName("td"); var date = subFields[0].innerHTML.trim().split("."); var dateObj = new Date(Date.parse(`${date[2]}-${date[1]}-${date[0]}`)); - var subjects = {}; - for (const subject of subFields[2].innerHTML.split(", ")) { + var subjects = []; + subFields[2].innerHTML.match(SUBJECT_LIST_REGEX).forEach((subject) => { + subjects.push(subject); + }); + + var absencesBySubject = {}; + + for (const subject of subjects) { const matched_info = FIELDS_REGEX.exec(subject); var subjectName = matched_info[1]; @@ -401,9 +408,9 @@ class gsec { // Ce je v "stevilki" P, gre za popoldansko uro -> +7 ur var period = matched_info[3]; period = period.includes("P") ? Number(period.replace("P", "")) + 7 : Number(period); - subjects[period] = {status: status, subject: subjectName}; + absencesBySubject[period] = {status: status, subject: subjectName}; } - absences.push({subjects: subjects, date: dateObj}); + absences.push({subjects: absencesBySubject, date: dateObj}); } resolve(absences); }); diff --git a/js/messaging.js b/js/messaging.js index 3355297..36e302e 100644 --- a/js/messaging.js +++ b/js/messaging.js @@ -1,7 +1,7 @@ const API_ENDPOINT = "https://gimb.tk/test.php"; const DIRECTORY_URL = "/directory.json"; -const ENCRYPTED_MESSAGE_REGEX = /(\S+?)/g; +const ENCRYPTED_MESSAGE_REGEX = /(\S+?)/; // "Global" object for name directory and messages var directory = null; -- cgit v1.2.3