quatalog-site/js/search_helper.js

43 lines
1.8 KiB
JavaScript

"use strict";
const search_helper = async function(event) {
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"]
course_code = input.match(/.{1,4}/g)
} else if(input.replace(/ |-/g,"").length == 8) {
// "abcd - 1345" => ["abcd","1345"]
const arr = input.split(/(?:-| )+/);
if(arr.length == 2) course_code = arr;
}
// only do this logic if the string might be a course code
// avoid having to fetch the courses_list if it definitely isn't one
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
const course_exists = await fetch("../courses_list.json")
.then(list => list.json())
.then(list => list.includes(code_str));
// if it is, redirect to it
if(course_exists) {
// handle both homepage and courses pages (which are in a directory)
if(window.location.pathname.split("/").slice(-2,-1)[0] != "courses") {
location.href = "courses/"+code_str + ".html";
} else {
location.href = code_str + ".html";
}
return;
}
}
location.href = "../search" + ".html" + "?search=" + encodeURIComponent(input);
}