From ad675cbe9738577b236c5a682e4629c562ee4a76 Mon Sep 17 00:00:00 2001 From: rstular Date: Sat, 16 May 2020 23:20:42 +0200 Subject: Linting --- js/absences.js | 281 ++++++------ js/app.js | 39 +- js/changelog.js | 5 +- js/chats.js | 657 +++++++++++++++------------ js/grades.js | 366 +++++++-------- js/gradings.js | 202 ++++----- js/gsec.js | 1235 ++++++++++++++++++++++++++------------------------- js/lang/bundle.js | 906 ++++++++++++++++++------------------- js/lib/he.js | 680 ++++++++++++++-------------- js/login.js | 42 +- js/meals.js | 675 ++++++++++++++-------------- js/messaging.js | 100 ++--- js/settings.js | 22 +- js/setup-storage.js | 68 +-- js/teachers.js | 152 +++---- js/timetable.js | 123 +++-- 16 files changed, 2834 insertions(+), 2719 deletions(-) diff --git a/js/absences.js b/js/absences.js index 9ff6059..a11d042 100644 --- a/js/absences.js +++ b/js/absences.js @@ -34,141 +34,144 @@ function setLoading(state) { * @param {boolean} forceRefresh If true, cached absences are ignored */ async function loadAbsences(forceRefresh = false) { - setLoading(true); - // Load required data - let promisesToRun = [ - localforage.getItem("username").then(function (value) { - username = value; - }), - localforage.getItem("password").then(function (value) { - password = value; - }), - localforage.getItem("absences").then(function (value) { - absences = value; - }) - ]; - await Promise.all(promisesToRun); - // If we don't have a list of absences, query it - if (absences === null || forceRefresh) { - try { - let gsecInstance = new gsec(); - await gsecInstance.login(username, password); - let date = {}; - date.from = $("#datepicker-from").val().split("."); - date.till = $("#datepicker-to").val().split("."); - Object.keys(date).map((key, index) => { - date[key] = new Date(Date.parse(date[key].reverse().join("-"))); - }); - gsecInstance.fetchAbsences().then( (fetchedAbsences) => { - fetchedAbsences.sort((a, b) => { - // Turn your strings into dates, and then subtract them - // to get a value that is either negative, positive, or zero. - return new Date(b.date) - new Date(a.date); - }); - - var fromKey = fetchedAbsences.findIndex((processedElement, processedIndex) => { - if (processedElement.date.getTime() >= date.from.getTime()) { - return true; - } - }); - - var tillKey = fetchedAbsences.findIndex((processedElement, processedIndex) => { - if (processedElement.date.getTime() > date.till.getTime()) { - return true; - } - }); - - // Both were -1, but we increased fromKey and decreased tillKey - // Means no absences in the provided timeframe - if (fromKey === 0 && tillKey === -2) { - fetchedAbsences = []; - } else { - fetchedAbsences = fetchedAbsences.slice(fromKey, tillKey); - } - - absences = fetchedAbsences; - localforage.setItem("absences", fetchedAbsences).then((value) => { - displayData(); - setLoading(false); - }); - setLoading(false); - }).catch( (err) => { - gsecErrorHandlerUI(err); - setLoading(false); - }); - } catch (err) { - gsecErrorHandlerUI(err); - setLoading(false); - } - } else { - displayData(); - setLoading(false); - } + setLoading(true); + // Load required data + let promisesToRun = [ + localforage.getItem("username").then(function (value) { + username = value; + }), + localforage.getItem("password").then(function (value) { + password = value; + }), + localforage.getItem("absences").then(function (value) { + absences = value; + }) + ]; + await Promise.all(promisesToRun); + // If we don't have a list of absences, query it + if (absences === null || forceRefresh) { + try { + let gsecInstance = new gsec(); + await gsecInstance.login(username, password); + let date = {}; + date.from = $("#datepicker-from").val().split("."); + date.till = $("#datepicker-to").val().split("."); + Object.keys(date).map((key) => { + date[key] = new Date(Date.parse(date[key].reverse().join("-"))); + }); + gsecInstance.fetchAbsences().then( (fetchedAbsences) => { + fetchedAbsences.sort((a, b) => { + // Turn your strings into dates, and then subtract them + // to get a value that is either negative, positive, or zero. + return new Date(b.date) - new Date(a.date); + }); + + var fromKey = fetchedAbsences.findIndex((procEl) => { + if (procEl.date.getTime() >= date.from.getTime()) { + return true; + } + }); + + var tillKey = fetchedAbsences.findIndex((procEl) => { + if (procEl.date.getTime() > date.till.getTime()) { + return true; + } + }); + + // Both were -1, but we increased fromKey and decreased tillKey + // Means no absences in the provided timeframe + if (fromKey === 0 && tillKey === -2) { + fetchedAbsences = []; + } else { + fetchedAbsences = fetchedAbsences.slice(fromKey, tillKey); + } + + absences = fetchedAbsences; + localforage.setItem("absences", fetchedAbsences).then(() => { + displayData(); + setLoading(false); + }); + setLoading(false); + }).catch( (err) => { + gsecErrorHandlerUI(err); + setLoading(false); + }); + } catch (err) { + gsecErrorHandlerUI(err); + setLoading(false); + } + } else { + displayData(); + setLoading(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 header = document.createElement("div"); - header.className = "collapsible-header"; - header.innerText = date_string; - - let body = document.createElement("div"); - body.className = "collapsible-body"; - - let body_table = document.createElement("table"); - body_table.className = "highlight"; - - let body_table_tbody = document.createElement("tbody"); - - Object.keys(absence.subjects).forEach(lesson => { - 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}`; - - let subject_lesson_icon_i = document.createElement("i"); - subject_lesson_icon_i.className = "material-icons"; - - switch (absence["subjects"][lesson]["status"]) { - case 0: - subject_lesson_icon_i.innerText = "schedule"; - break; - case 1: - subject_lesson_icon_i.innerText = "check_circle_outline"; - break; - case 2: - subject_lesson_icon_i.innerText = "error_outline"; - break; - case 3: - subject_lesson_icon_i.innerText = "not_interested"; - break; - } - - subject_lesson_icon.appendChild(subject_lesson_icon_i); - - let subject_name = document.createElement("td"); - subject_name.innerText = `${S(gseAbsenceTypes[absence["subjects"][lesson]["status"]])} : ${absence["subjects"][lesson]["subject"]}`; - subject_row.appendChild(subject_lesson_icon); - subject_row.appendChild(subject_lesson_text); - subject_row.appendChild(subject_name); - body_table_tbody.appendChild(subject_row); - }); - - body_table.appendChild(body_table_tbody); - body.appendChild(body_table); - - li.appendChild(header); - li.appendChild(body); - $("#absences-col").append(li); - }); + absences.forEach(absence => { + let li = document.createElement("li"); + + // dateString comes from bundle.js + let dateStringValue = dateString.longFormatted(absence["date"]); + + let header = document.createElement("div"); + header.className = "collapsible-header"; + header.innerText = dateStringValue; + + let body = document.createElement("div"); + body.className = "collapsible-body"; + + let body_table = document.createElement("table"); + body_table.className = "highlight"; + + let body_table_tbody = document.createElement("tbody"); + + Object.keys(absence.subjects).forEach(lesson => { + + let absenceLessonObject = absence["subjects"][lesson]; + + let subjectRow = document.createElement("tr"); + let subjectLessonIcon = document.createElement("td"); + let subjectLessonText = document.createElement("td"); + subjectLessonText.innerText = `${S("lesson")} ${lesson}`; + + let subjectLessonIconInner = document.createElement("i"); + subjectLessonIconInner.className = "material-icons"; + + switch (absenceLessonObject["status"]) { + case 0: + subjectLessonIconInner.innerText = "schedule"; + break; + case 1: + subjectLessonIconInner.innerText = "check_circle_outline"; + break; + case 2: + subjectLessonIconInner.innerText = "error_outline"; + break; + case 3: + subjectLessonIconInner.innerText = "not_interested"; + break; + } + + subjectLessonIcon.appendChild(subjectLessonIconInner); + + let subjectName = document.createElement("td"); + subjectName.innerText = `${S(gseAbsenceTypes[absenceLessonObject["status"]])} : ${absenceLessonObject["subject"]}`; + subjectRow.appendChild(subjectLessonIcon); + subjectRow.appendChild(subjectLessonText); + subjectRow.appendChild(subjectName); + body_table_tbody.appendChild(subjectRow); + }); + + body_table.appendChild(body_table_tbody); + body.appendChild(body_table); + + li.appendChild(header); + li.appendChild(body); + $("#absences-col").append(li); + }); } /** @@ -194,32 +197,32 @@ function refreshAbsences() { */ function setupPickers() { // Setup pickers - var date_object = new Date(); + var dateObject = new Date(); let elems = document.querySelectorAll('#datepicker-to'); let options = { autoClose: true, format: "dd.mm.yyyy", - defaultDate: date_object, + defaultDate: dateObject, setDefaultDate: true, firstDay: 1, onSelect: refreshAbsences - } - - let instances = M.Datepicker.init(elems, options); + } + + M.Datepicker.init(elems, options); - date_object.setDate(date_object.getDate() - 14); + dateObject.setDate(dateObject.getDate() - 14); elems = document.querySelectorAll('#datepicker-from'); options = { autoClose: true, format: "dd.mm.yyyy", - defaultDate: date_object, + defaultDate: dateObject, setDefaultDate: true, firstDay: 1, onSelect: refreshAbsences } - instances = M.Datepicker.init(elems, options); + M.Datepicker.init(elems, options); } document.addEventListener("DOMContentLoaded", () => { @@ -233,8 +236,8 @@ document.addEventListener("DOMContentLoaded", () => { setupPickers(); - let coll_elem = document.querySelectorAll('.collapsible'); - let coll_instance = M.Collapsible.init(coll_elem, {}); + let collectionElem = document.querySelectorAll('.collapsible'); + M.Collapsible.init(collectionElem, {}); // Setup side menu const menus = document.querySelectorAll('.side-menu'); diff --git a/js/app.js b/js/app.js index 1b39086..79ccf06 100644 --- a/js/app.js +++ b/js/app.js @@ -14,17 +14,18 @@ if (navigator.serviceWorker) { } /** - * Displays a user-friendly text to the user and detailed text to developer (console) + * 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 } ); - console.log(`[BežiApp UIAlert] ${usermsg} ${devmsg}`); - } else { - M.toast( { html: `${usermsg} ${devmsg}` } ); - } + if (true) { // če bo kakšen dev switch? + M.toast( { html: usermsg } ); + console.log(`[BežiApp UIAlert] ${usermsg} ${devmsg}`); + } else { + M.toast( { html: `${usermsg} ${devmsg}` } ); + } } /** @@ -32,15 +33,17 @@ async function UIAlert(usermsg, devmsg) { * @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) { - UIAlert( D("gsecErrNet") ); - } else if(err == GSEC_ERR_LOGIN) { - UIAlert( D("gsecErrLogin") ); - localforage.setItem("logged_in", false).then( () => { - window.location.replace("/index.html"); - }); - } else { - UIAlert( D("gsecErrOther") ); - } + console.log(`gsecErrorHanderUI: handling ${err}`); + if(err == GSEC_ERR_NET || err == GSEC_ERR_NET_POSTBACK_GET || + err == GSEC_ERR_NET_POSTBACK_POST) { + + UIAlert( D("gsecErrNet") ); + } else if(err == GSEC_ERR_LOGIN) { + UIAlert( D("gsecErrLogin") ); + localforage.setItem("logged_in", false).then( () => { + window.location.replace("/index.html"); + }); + } else { + UIAlert( D("gsecErrOther") ); + } } diff --git a/js/changelog.js b/js/changelog.js index 9fb9a84..ccc3493 100644 --- a/js/changelog.js +++ b/js/changelog.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 @@ -20,5 +23,5 @@ document.addEventListener("DOMContentLoaded", () => { }); var elems = document.querySelectorAll(".collapsible"); - var instances = M.Collapsible.init(elems, {}); + M.Collapsible.init(elems, {}); }); \ No newline at end of file diff --git a/js/chats.js b/js/chats.js index 673099b..5b4bbfd 100644 --- a/js/chats.js +++ b/js/chats.js @@ -2,15 +2,18 @@ const DIRECTORY_URL = "/directory.json"; const CHATS_BEGIN_TAG = ""; const CHATS_END_TAG = ""; -const CHAT_REGEX = /([\S\s]+?)/g; +const CHAT_REGEX = /([\S\s]+)/g; const CHATS_SUBJECT_PREFIX = "ba-ctlmsg-chat-"; // "Global" object for name directory -var directory = null; -var currentlyChattingWith = null; // msgid -var sogovornik = null; // name -var firstPageOfMessages = null; // so we can test if new messages ever arrive - +let directory = null; +let currentlyChattingWith = null; // msgid +let sogovornik = null; // name +let firstPageOfMessages = null; // so we can test if new messages ever arrive + +/** + * 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 @@ -24,23 +27,46 @@ async function checkLogin() { }); } +/** + * Find the matching key for a provided value in an object + * @param {object} object Object to search + * @param {object} value Value to find the matching key for + * @returns {object} Key + */ function getKeyByValue(object, value) { - return Object.keys(object).find(key => object[key] === value); + return Object.keys(object).find(key => object[key] === value); } // -----------HTML HELPERS----------- +/** + * Encode HTML entities + * @param {string} value Value to encode + * @returns {string} Encoded value + */ 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. + /** + 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 $("