diff options
author | Alex Xu (Hello71) <alex_y_xu@yahoo.ca> | 2018-09-19 11:12:54 -0400 |
---|---|---|
committer | Alex Xu (Hello71) <alex_y_xu@yahoo.ca> | 2018-09-19 11:12:54 -0400 |
commit | e8d499f682950d02fb143b504ced7cefac23534a (patch) | |
tree | a8258e1c15b4c80bb1f474c38937b6ab5b0f4eea /script.js | |
parent | 87c3ae27d68cdecb967d91a56146dc7874867a8b (diff) | |
download | yorku-course-query-e8d499f682950d02fb143b504ced7cefac23534a.tar.xz yorku-course-query-e8d499f682950d02fb143b504ced7cefac23534a.zip |
Update
Diffstat (limited to 'script.js')
-rw-r--r-- | script.js | 118 |
1 files changed, 73 insertions, 45 deletions
@@ -2,7 +2,7 @@ "use strict"; const FACS = new Set(); const DEPTS = new Map(); -// maintain: CDM Advanced Search +// maintain: CDM Advanced Search, // console.log(Array.prototype.map.call(document.getElementsByName("subjectPopup")[0].children,(e)=>'"'+e.textContent.match(/[A-Z]{2,4}/)[0] + '"').join(", ")) [ ["AP", "ADMB", "ADMS", "ANTH", "ARB", "ASL", "CCY", "CDNS", "CH", "CLST", "CLTR", "COGS", "COMN", "CRIM", "DEMS", "DLLL", "ECON", "EN", "ESL", "FND", "FR", "GCIN", "GEOG", "GER", "GK", "GKM", "GWST", "HEB", "HIST", "HREQ", "HRM", "HUMA", "INDG", "INDV", "IT", "ITEC", "JC", "JP", "KOR", "LA", "LASO", "LING", "LLS", "MIST", "MODR", "PERS", "PHIL", "POLS", "POR", "PPAS", "PRWR", "RU", "SOCI", "SOSC", "SOWK", "SP", "SWAH", "SXST", "TESL", "TYP", "WKLS", "WRIT"], @@ -18,7 +18,7 @@ const DEPTS = new Map(); ].forEach((arr) => { let faculty = arr.shift(); FACS.add(faculty); - arr.forEach((dept) => DEPTS.set(dept, faculty)); + arr.forEach((subj) => DEPTS.set(subj, faculty)); }); const TOKEN_DELIM_RE = /[^A-Za-z0-9.=]/; @@ -32,7 +32,8 @@ const IGNORE_RE = /^[A-Z]$/; let ycq_storage = JSON.parse(localStorage.ycq || "{}"); if (!ycq_storage) ycq_storage = {}; -ycq_storage.courses = ycq_storage.courses || {}; +ycq_storage.course_creds = ycq_storage.course_creds || {}; +ycq_storage.course_subjs = ycq_storage.course_subjs || {}; let now = new Date(); let month = now.getMonth(); @@ -64,9 +65,14 @@ switch (ycq_storage.sess) { fw_sess.selected = true; } -let ENGLISH_TYPES = {"fac": "faculty", "dept": "department/subject", "crsn": "course number", "cred": "credit number", "sess": "session (pick on the right)"}; +let ENGLISH_TYPES = {"fac": "faculty", "subj": "subject/subject", "crsn": "course number", "cred": "credit number", "sess": "session (pick on the right)"}; + +function ready() { + let ywhAd = document.getElementById("ywh-ad"); + if (location.protocol != "http:" && location.protocol != "https:" && location.protocol != "file:") { + ywhAd.style.display = "none"; + } -document.addEventListener("DOMContentLoaded", function () { let sessions = document.querySelector("#sessions"); sessions.appendChild(fw_sess); sessions.appendChild(s_sess); @@ -75,12 +81,10 @@ document.addEventListener("DOMContentLoaded", function () { let link = document.getElementById("link"); let linkContainer = document.getElementById("link-container"); - var crsn, cred; - - var state; + let state; let apply = function () { - state = {dept: "", fac: "", crsn: "", cred: ""}; + state = {subj: "", fac: "", crsn: "", cred: ""}; let multiple = new Set(); let warn = [], error = []; @@ -91,14 +95,14 @@ document.addEventListener("DOMContentLoaded", function () { }; let tryParse = function (token) { - let deptcrsn, cred; + let subjcrsn, cred; if (DEPTS.has(token)) { - got("dept", token); + got("subj", token); } else if (FACS.has(token)) { got("fac", token); - } else if ((deptcrsn = DEPTCRSN_RE.exec(token))) { - got("dept", deptcrsn[1]); - got("crsn", deptcrsn[2]); + } else if ((subjcrsn = DEPTCRSN_RE.exec(token))) { + got("subj", subjcrsn[1]); + got("crsn", subjcrsn[2]); } else if ((cred = CRED_RE.exec(token))) { got("cred", parseFloat(cred[1] || cred[2]).toFixed(2)); } else if (CRSN_RE.test(token)) { @@ -107,7 +111,7 @@ document.addEventListener("DOMContentLoaded", function () { got("cred", parseFloat(token).toFixed(2)); warn.push(`assuming ${token} is the number of credits`); } else if (MAYBE_DEPT_RE.test(token)) { - state.maybeDept = token; + state.maybeSubj = token; } else if (!IGNORE_RE.test(token)) { return false; } @@ -120,21 +124,26 @@ document.addEventListener("DOMContentLoaded", function () { warn.push("don't understand search term: " + token); }); - if (!state.dept && state.maybeDept) { - state.dept = state.maybeDept; - warn.push("guessing department is " + state.maybeDept); + if (!state.subj && state.maybeSubj) { + state.subj = state.maybeSubj; + warn.push("guessing subject is " + state.maybeSubj); + } + + if (!state.subj && ycq_storage.course_subjs[state.crsn]) { + state.subj = ycq_storage.course_subjs[state.crsn]; + warn.push(`using saved subject for ${state.crsn}: ${state.subj}`); } - if (!state.fac && state.dept) { - state.fac = DEPTS.get(state.dept); + if (!state.fac && state.subj) { + state.fac = DEPTS.get(state.subj); if (!state.fac) - error.push("no faculty provided and unknown department"); + error.push("no faculty provided and unknown subject"); } - if (state.crsn && !state.cred) { - if (ycq_storage.courses[state.dept + state.crsn]) { - state.cred = ycq_storage.courses[state.crsn]; - warn.push(`using saved credit number for ${state.dept} ${state.crsn}: ${state.cred}`); + if (state.subj && state.crsn && !state.cred) { + if (ycq_storage.course_creds[state.subj + state.crsn]) { + state.cred = ycq_storage.course_creds[state.crsn]; + warn.push(`using saved credit number for ${state.subj} ${state.crsn}: ${state.cred}`); } else { state.cred = "3.00"; warn.push("assuming the number of credits is 3.00"); @@ -144,19 +153,19 @@ document.addEventListener("DOMContentLoaded", function () { let r_sess = document.getElementById("sessions").value; let sess = r_sess.split(" "); state.sess = sess[0]; - state.year = parseInt(sess[1]); - - if (state.fac && state.dept && state.crsn && state.cred && state.year && state.sess) { - link.href = `https://w2prod.sis.yorku.ca/Apps/WebObjects/cdm.woa/wa/crsq?fa=${state.fac}&sj=${state.dept}&cn=${state.crsn}&cr=${state.cred}&ay=${state.year}&ss=${state.sess}`; - link.textContent = `York University: ${state.fac}/${state.dept} ${state.crsn} ${state.cred} (${sessions.selectedOptions[0].textContent})`; - linkContainer.style = "display: block"; - } else if (state.fac && state.dept && state.sess) { - link.href = `https://w2prod.sis.yorku.ca/Apps/WebObjects/cdm.woa/wa/crsq1?faculty=${state.fac}&subject=${state.dept}&academicyear=${state.year}&studysession=${state.sess}`; - link.textContent = `York University: ${state.fac}/${state.dept} Courses (${sessions.selectedOptions[0]. textContent})`; - linkContainer.style = "display: block"; + state.year = parseInt(sess[1], 10); + + if (state.fac && state.subj && state.crsn && state.cred && state.year && state.sess) { + link.href = `https://w2prod.sis.yorku.ca/Apps/WebObjects/cdm.woa/wa/crsq?fa=${state.fac}&sj=${state.subj}&cn=${state.crsn}&cr=${state.cred}&ay=${state.year}&ss=${state.sess}`; + link.textContent = `York University: ${state.fac}/${state.subj} ${state.crsn} ${state.cred} (${sessions.selectedOptions[0].textContent})`; + linkContainer.style.display = "block"; + } else if (state.fac && state.subj && state.sess) { + link.href = `https://w2prod.sis.yorku.ca/Apps/WebObjects/cdm.woa/wa/crsq1?faculty=${state.fac}&subject=${state.subj}&academicyear=${state.year}&studysession=${state.sess}`; + link.textContent = `York University: ${state.fac}/${state.subj} Courses (${sessions.selectedOptions[0]. textContent})`; + linkContainer.style.display = "block"; } else { - linkContainer.style = "display: none"; - error.push("insufficient search parameters. need at least department/subject. see examples above"); + linkContainer.style.display = "none"; + error.push("insufficient search parameters. need at least subject. see examples above"); } if (multiple.size) @@ -174,18 +183,30 @@ document.addEventListener("DOMContentLoaded", function () { document.getElementById("sample-queries").addEventListener("click", function (e) { if (e.target.tagName == "A") { e.preventDefault(); - input.value = e.target.textContent; + input.select(); + // doesn't work in Firefox, oh well + if (!document.execCommand || !document.execCommand("insertText", false, e.target.textContent)) + input.value = e.target.textContent; apply(); input.focus(); } }); - var apply_crsn_cred = function () { - if (state.crsn && state.cred) { - ycq_storage.courses[state.dept + state.crsn] = state.cred; - localStorage.ycq = JSON.stringify(ycq_storage); + let save_crsn = function () { + let modified = false; + if (state.crsn) { + if (state.subj && state.cred) { + ycq_storage.course_creds[state.subj + state.crsn] = state.cred; + modified = true; + } + if (state.subj) { + ycq_storage.course_subjs[state.crsn] = state.subj; + modified = true; + } } + if (modified) + localStorage.ycq = JSON.stringify(ycq_storage); }; - link.addEventListener("click", apply_crsn_cred); + link.addEventListener("click", save_crsn); document.forms[0].addEventListener("submit", function (e) { e.preventDefault(); if (linkContainer.style.display == "block") @@ -202,7 +223,8 @@ document.addEventListener("DOMContentLoaded", function () { link.click(); link.target = ""; } else if (e.shiftKey) { - apply_crsn_cred(); + // only need save_crsn here because other methods already trigger the link click event + save_crsn(); window.open(link.href, "_blank", "noopener,width=625,resizable,menubar,toolbar,location,personalbar,status,scrollbars"); } else { @@ -215,5 +237,11 @@ document.addEventListener("DOMContentLoaded", function () { } }); apply(); -}); +} + +if (document.readyState === "interactive" || document.readyState === "complete") { + ready(); +} else { + document.addEventListener("DOMContentLoaded", ready); +} }()); |