2023-02-11 02:10:08 +00:00
|
|
|
"use strict";
|
2023-02-11 23:36:11 +00:00
|
|
|
const search_helper = async function(event,from_course_page = true) {
|
2023-02-11 02:10:08 +00:00
|
|
|
event.preventDefault();
|
|
|
|
// "a b cde 12" => "a b cde 12"
|
|
|
|
const input = document.getElementById("search").value.split(" ").join(" ");
|
|
|
|
|
|
|
|
var course_code = false;
|
|
|
|
if(input.length == 8) {
|
|
|
|
// "abcd1345" => ["abcd","1345"]
|
2023-02-11 02:35:21 +00:00
|
|
|
course_code = input.match(/.{1,4}/g)
|
2023-02-11 23:07:49 +00:00
|
|
|
} else if(input.replace(/ |-/g,"").length == 8) {
|
2023-02-11 02:10:08 +00:00
|
|
|
// "abcd - 1345" => ["abcd","1345"]
|
2023-02-11 23:07:49 +00:00
|
|
|
const arr = input.split(/(?:-| )+/);
|
|
|
|
if(arr.length == 2) course_code = arr;
|
2023-02-11 02:10:08 +00:00
|
|
|
}
|
2023-02-11 23:36:11 +00:00
|
|
|
|
2023-02-11 02:10:08 +00:00
|
|
|
// only do this logic if the string might be a course code
|
2023-02-11 23:07:49 +00:00
|
|
|
// avoid having to fetch the courses_list if it definitely isn't one
|
2023-02-11 02:10:08 +00:00
|
|
|
if(course_code) {
|
|
|
|
// ["abcd","1345"] => "ABCD-1345"
|
|
|
|
course_code[0] = course_code[0].toUpperCase();
|
|
|
|
const code_str = course_code.join("-");
|
|
|
|
|
|
|
|
// check if "ABCD-1345" is a real course code
|
2023-02-11 23:36:11 +00:00
|
|
|
const course_exists = await fetch(
|
2023-02-17 02:02:07 +00:00
|
|
|
from_course_page ? "../json/courses_list.json" : "json/courses_list.json"
|
2023-02-11 23:36:11 +00:00
|
|
|
)
|
2023-02-11 02:10:08 +00:00
|
|
|
.then(list => list.json())
|
2023-02-11 23:07:49 +00:00
|
|
|
.then(list => list.includes(code_str));
|
2023-02-11 02:10:08 +00:00
|
|
|
|
|
|
|
// if it is, redirect to it
|
|
|
|
if(course_exists) {
|
2023-02-11 23:36:11 +00:00
|
|
|
if(from_course_page) {
|
|
|
|
location.href = code_str;
|
2023-02-11 02:35:21 +00:00
|
|
|
} else {
|
2023-02-11 23:36:11 +00:00
|
|
|
location.href = "courses/" + code_str;
|
2023-02-11 02:35:21 +00:00
|
|
|
}
|
2023-02-11 02:10:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-11 23:36:11 +00:00
|
|
|
if(from_course_page) {
|
|
|
|
location.href = "../search?search=" + encodeURIComponent(input);
|
|
|
|
} else {
|
|
|
|
location.href = "search?search=" + encodeURIComponent(input);
|
|
|
|
}
|
2023-02-10 20:50:06 +00:00
|
|
|
}
|