summaryrefslogtreecommitdiff
path: root/static/script.js
diff options
context:
space:
mode:
authorMarvin Borner2019-09-18 22:24:13 +0200
committerMarvin Borner2019-09-18 22:24:13 +0200
commit26e0a26011af580ec24eb158d450a8ffd4afad52 (patch)
tree4582996ba3d30ead9c3c114ff3b77a861448b835 /static/script.js
Added basic functionality
Diffstat (limited to 'static/script.js')
-rw-r--r--static/script.js131
1 files changed, 131 insertions, 0 deletions
diff --git a/static/script.js b/static/script.js
new file mode 100644
index 0000000..edd399a
--- /dev/null
+++ b/static/script.js
@@ -0,0 +1,131 @@
+const xmlHttp = new XMLHttpRequest();
+xmlHttp.onreadystatechange = function () {
+ if (xmlHttp.readyState === 4 && xmlHttp.status === 200)
+ renderTable(JSON.parse(xmlHttp.responseText));
+};
+xmlHttp.open("GET", "http://127.0.0.1:5000/api", true);
+xmlHttp.send();
+
+function renderTable(data) {
+ const configs = getConfigs();
+ console.log(configs);
+ console.log(data); // TODO: Use DSB data
+ const cells = document.querySelector("#timetable tbody").getElementsByTagName('td');
+ for (let i = 0, cell; cell = cells[i]; i++) {
+ try {
+ const available = [];
+ const subjects = cell.getAttribute("data-subject").split("|");
+ const teachers = cell.getAttribute("data-teacher").split("|");
+ const rooms = cell.getAttribute("data-room").split("|");
+
+ subjects.forEach((elem, index) => {
+ if ((elem.split("@").length > 1 && elem.split("@")[1].includes(getWeekType())) || elem.split("@").length === 1) {
+ const subject = elem.split("@")[0];
+ if (
+ (subject === "f" && !configs.f) || (subject === "f" && teachers[index] !== configs.f) ||
+ (subject === "ph" && !configs.ph) || (subject === "ph" && teachers[index] !== configs.ph) ||
+ (subject === "ch" && !configs.ch) || (subject === "ch" && teachers[index] !== configs.ch) ||
+ (subject === "s" && teachers[index] !== configs.s) ||
+ (subject === "eth" && configs.e !== "eth") ||
+ (subject === "evr" && configs.e !== "evr") ||
+ (subject === "krl" && configs.e !== "krl") ||
+ (cell.getAttribute("data-flag") === "f" && !configs.f) ||
+ (["b", "m+", "bk", "sgt"].includes(subject) && !configs.w)
+ ) {
+ console.log("skipped", subject, teachers[index])
+ } else {
+ available.push({
+ subject: subject.toUpperCase(),
+ teacher: teachers[index].toUpperCase(),
+ room: rooms[index].toUpperCase()
+ })
+ }
+ }
+ });
+
+ cell.innerHTML = `<b>${available[0].subject}</b><br>${available[0].teacher}<br><small>${available[0].room}</small><br>`;
+ } catch (e) {
+ //
+ }
+ }
+
+ document.querySelectorAll("#weekdays col")[(new Date()).getDay()].style.backgroundColor = "rgba(250,255,0,0.42)";
+}
+
+function getConfigs() {
+ // Defaults to personal preferences
+ const url = new URL(window.location.href);
+ const french = url.searchParams.get("f") || false;
+ const sports = url.searchParams.get("s") || "at";
+ const physics = url.searchParams.get("p") || "gm";
+ const chemistry = url.searchParams.get("c") || false;
+ const elective = url.searchParams.get("w") || false;
+ const ethnicity = url.searchParams.get("e") || "eth";
+
+ return {
+ f: french === "false" ? false : french,
+ s: sports,
+ ph: physics === "false" ? false : physics,
+ ch: chemistry === "false" ? false : chemistry,
+ w: elective === "false" ? false : elective,
+ e: ethnicity
+ }
+}
+
+Date.prototype.getWeek = function () {
+ const firstJan = new Date(this.getFullYear(), 0, 1);
+ return Math.ceil((((this - firstJan) / 86400000) + firstJan.getDay() + 1) / 7);
+};
+
+function getWeekType() {
+ const table = {
+ 37: "a",
+ 38: "b",
+ 39: "c",
+ 40: "d",
+ 41: "e",
+ 42: "f",
+ 43: "g",
+ 45: "h",
+ 46: "a",
+ 47: "b",
+ 48: "c",
+ 49: "d",
+ 50: "e",
+ 51: "f",
+ 2: "g",
+ 3: "h",
+ 4: "a",
+ 5: "b",
+ 6: "c",
+ 7: "d",
+ 8: "e",
+ 10: "f",
+ 11: "g",
+ 12: "h",
+ 13: "a",
+ 14: "b",
+ 17: "c",
+ 18: "d",
+ 19: "e",
+ 20: "f",
+ 21: "g",
+ 22: "h",
+ 25: "a",
+ 26: "b",
+ 27: "c",
+ 28: "d",
+ 29: "e",
+ 30: "f",
+ 31: "g"
+ };
+
+ const date = new Date();
+
+ try {
+ return table[date.getWeek()]
+ } catch (e) {
+ alert("Ferien!");
+ return "a"
+ }
+} \ No newline at end of file