From b8d340c3c08a6b45a50d846577543ba97df8d73a Mon Sep 17 00:00:00 2001 From: "Alex Xu (Hello71)" Date: Thu, 4 Oct 2018 18:22:54 -0400 Subject: add features, improve errors --- script.js | 56 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 15 deletions(-) (limited to 'script.js') diff --git a/script.js b/script.js index 0951b5b..6c74539 100644 --- a/script.js +++ b/script.js @@ -197,7 +197,7 @@ const DEPTCRSN_RE = /^([A-Z]{2,4})([0-9]{4})$/; const CRED_RE = /^(?:Cr=([0-9]{1,2}(?:\.[0-9]{0,2})?)|([0-9]{1,2}\.[0-9]{0,2}))$/; const MAYBE_CRED_RE = /^[0-9]{1,2}$/; const MAYBE_DEPT_RE = /^[A-Z]{2,4}$/; -const IGNORE_RE = /^[A-Z]$/; +const IGNORE_RE = /^[A-Za-z]$/; // note: this has to be lowercase, otherwise "ignored search terms" will incorrectly uppercase the input function parse_ycq_s(str) { let ycq_s = JSON.parse(str || "{}"); @@ -258,6 +258,15 @@ function ready() { let state; + let apply_list = function (n, l) { + if (l.length) { + document.getElementById(n).style.display = "block"; + document.getElementById(n + "-list").innerHTML = l.map(s => `
  • ${s}
  • `).join(''); + } else { + document.getElementById(n).style.display = "none"; + } + }; + let apply = function () { state = {subj: "", fac: "", crsn: "", cred: ""}; let multiple = new Set(); @@ -269,6 +278,8 @@ function ready() { state[type] = token; }; + let ignored_tokens = []; + let tryParse = function (token) { let subjcrsn, cred; if (SUBJS.hasOwnProperty(token)) { @@ -287,31 +298,43 @@ function ready() { warn.push(`assuming ${token} is the number of credits`); } else if (MAYBE_DEPT_RE.test(token)) { state["maybeSubj"] = token; - } else if (!IGNORE_RE.test(token)) { + } else if (IGNORE_RE.test(token)) { + ignored_tokens.push(token); + } else { return false; } return true; }; + let unk_tokens = []; input.value.split(TOKEN_DELIM_RE).forEach(function (token) { if (!token || tryParse(token) || tryParse(token.toUpperCase())) return; - warn.push(`don't understand search term: ${token}`); + unk_tokens.push(token); }); + if (unk_tokens.length) { + let dont_understand = unk_tokens.map(t => `"${t}"`).join(', '); + warn.push(`don't understand search terms: ${dont_understand}.`); + } + + if (ignored_tokens.length) { + let ignored_terms = ignored_tokens.map(t => `"${t}"`).join(', '); + warn.push(`ignored search terms: ${ignored_terms}.`); + } if (!state["subj"] && state["maybeSubj"]) { state["subj"] = state["maybeSubj"]; - warn.push("guessing subject is " + state["maybeSubj"]); + warn.push(`guessing subject is "${state["maybeSubj"]}".`); } if (!state["subj"] && state["crsn"] && cached_ycq_s.course_subjs[state["crsn"]]) { state["subj"] = cached_ycq_s.course_subjs[state["crsn"]]; - warn.push(`using saved subject for ${state["crsn"]}: ${state["subj"]}`); + warn.push(`assuming ${state["crsn"]} means ${state["subj"]} ${state["crsn"]}, because you last used ${state["subj"]} for ${state["crsn"]}.`); } if (!state["subj"] && cached_ycq_s.default_subj) { state["subj"] = cached_ycq_s.default_subj; - warn.push(`using saved default subject: ${state["subj"]}`); + warn.push(`using ${state["subj"]}, because that was your most recently searched subject.`); } if (!state["fac"] && state["subj"]) { @@ -321,6 +344,8 @@ function ready() { // 3. most recent compatible faculty // 4. first compatible faculty let compat_facs = SUBJS[state["subj"]]; + + // needs to be a function so it can remove the current selection let other_options = function () { if (compat_facs) { let other_facs = compat_facs @@ -328,7 +353,7 @@ function ready() { .map(f => `${f}`) .join(", "); if (other_facs.length) - return ", other options: " + other_facs; + return "other options: " + other_facs; } return ""; } @@ -336,11 +361,11 @@ function ready() { // 1. most recent faculty for this course if (state["crsn"] && cached_ycq_s.course_facs[state["subj"] + state["crsn"]]) { state["fac"] = cached_ycq_s.course_facs[state["subj"] + state["crsn"]]; - warn.push(`using saved faculty for ${state["subj"]} ${state["crsn"]}: ${state["fac"]}${other_options()}`); + warn.push(`assuming ${state["subj"]} ${state["crsn"]} is ${state["fac"]}/${state["subj"]}, because you last used ${state["fac"]} for ${state["subj"]} ${state["crsn"]}. ${other_options()}`); } else if (cached_ycq_s.subj_facs[state["subj"]]) { // 2. most recent faculty for this subject state["fac"] = cached_ycq_s.subj_facs[state["subj"]]; - warn.push(`using saved faculty for ${state["subj"]}: ${state["fac"]}${other_options()}`); + warn.push(`assuming ${state["subj"]} means ${state["fac"]}/${state["subj"]}, because you last used ${state["fac"]} for ${state["subj"]}. ${other_options()}`); } else if (compat_facs) { // 3. most recent compatible faculty if (cached_ycq_s.default_facs) @@ -349,11 +374,11 @@ function ready() { state["fac"] = cached_ycq_s.default_facs[i]; if (state["fac"]) { - warn.push(`using saved faculty: ${state["fac"]}${other_options()}`); + warn.push(`assuming ${state["subj"]} means ${state["fac"]} ${state["subj"]}, because you recently used ${state["fac"]}. ${other_options()}`); } else { // 4. first compatible faculty state["fac"] = compat_facs[0]; - warn.push(`using default faculty for ${state["subj"]}: ${state["fac"]}${other_options()}`); + warn.push(`assuming ${state["subj"]} means ${state["fac"]} ${state["subj"]}, because ${state["fac"]} is the default for ${state["subj"]}. ${other_options()}`); } } else { error.push("no faculty provided and unknown subject"); @@ -363,10 +388,10 @@ function ready() { if (state["subj"] && state["crsn"] && !state["cred"]) { if (cached_ycq_s.course_creds[state["subj"] + state["crsn"]]) { state["cred"] = cached_ycq_s.course_creds[state["subj"] + state["crsn"]]; - warn.push(`using saved credit number for ${state["subj"]} ${state["crsn"]}: ${state["cred"]}`); + warn.push(`assuming ${state["subj"]} ${state["crsn"]} is ${state["cred"]} credits, because you last used ${state["cred"]} for ${state["subj"]} ${state["crsn"]}.`); } else { state["cred"] = "3.00"; - warn.push("assuming the number of credits is 3.00"); + warn.push("assuming the number of credits is 3.00, because that's the most common number."); } } @@ -390,8 +415,8 @@ function ready() { if (multiple.size) warn.push("multiple terms for " + Array.from(multiple.values()).join(", ") + ", using last occurrence for each"); - document.getElementById("warning").innerHTML = warn.map(w => 'warning: ' + w).join("
    "); - document.getElementById("error").innerHTML = error.map(e => 'error: ' + e).join("
    "); + apply_list("warning", warn); + apply_list("error", error); }; input.addEventListener("input", apply); sessions.addEventListener("change", apply); @@ -465,6 +490,7 @@ function ready() { if (linkContainer.style.display == "block") link.click(); }); + input.addEventListener("blur", save_defaults); input.addEventListener("keydown", function (e) { if (e.key == "Enter") { e.preventDefault(); -- cgit v1.2.3-54-g00ecf