braunbearded / PrettyAUAS

// ==UserScript==
// @name         PrettyAUAS
// @namespace    https://openuserjs.org/users/braunbearded
// @version      0.3
// @description  https://bitbucket.org/braunbearded/prettyauas/src/master/README.md
// @copyright    2018, braunbearded (https://openuserjs.org/users/braunbearded)
// @license      MIT
// @author       Stefan Matz
// @require      https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.2/babel.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.16.0/polyfill.js
// @match        https://auas.cs.uni-duesseldorf.de/*
// ==/UserScript==

// ==OpenUserJS==
// @author braunbearded
// ==/OpenUserJS==

/* jshint ignore:start */
var inline_src = (<><![CDATA[
/* jshint ignore:end */
/* jshint esnext: false */
/* jshint esversion: 6 */

let courses = document.getElementsByTagName("fieldset");
let overview = document.getElementsByClassName("icon-plus-circled").length > 0;
let mailInput = document.getElementById("LoginEmail");

//Loged in?
if (courses.length > 0 && overview) {
    addExpand(courses);
} else if (mailInput){
    mailInput.focus();
    console.log("Bitte anmelden");
}

function addExpand(courses) {
    for (let i = 0; i < courses.length; i++) {
        let course = courses[i];
        let children = course.children;

        //is course hidden from the start?
        let courseName = course.children[0].textContent;
        let hidden = localStorage.getItem(courseName);
        if (hidden === null || hidden === "false") hidden = false;
        else hidden = true;


        //add/show collabs button
        let legend = course.children[0];
        let expand = document.createElement("Button");
        let expandText = document.createTextNode("hide/collapse");
        expand.appendChild(expandText);
        legend.appendChild(expand);
        if (!hidden) expand.style.display = "none";

        //restore last session
        if (hidden === true) {
            for (let child of Array.from(children)) {
                if (child != legend) {
                    child.style.display = "none";
                }
            }
        }

        course.addEventListener("mouseenter", event => {
            expand.style.display = "initial";
        });

        course.addEventListener("mouseleave", event => {
            if (expand && !hidden) expand.style.display = "none";
        });

        expand.addEventListener("click", event => {
            if (hidden) {
                for (let child of Array.from(children)) {
                    if (child != legend && child != expand) {
                        if (child.tagName === "TABLE") {
                            child.style.display = "table";
                        } else {
                            child.style.display = "block";
                        }
                    }
                }
                hidden = false;
                localStorage.setItem(courseName, false);
            } else {
                for (let child of Array.from(children)) {
                    if (child != legend) {
                        child.style.display = "none";
                    }
                }
                hidden = true;
                localStorage.setItem(courseName, true);
            }
        });
        sorting(course, i);
    }
}

function sorting(course, courseNumber) {
    let headerElements = course.getElementsByClassName("tablesorter-header");
    let tables = course.getElementsByTagName("table");
    let savedSorting = JSON.parse(localStorage.getItem(courseNumber));
    for (let j = 0; j < headerElements.length; j++) {
        let numHeaderButton;
        let numTable = 0

        if (j < 4) {    // first table
            numHeaderButton = j;
        } else {        //second table
            numHeaderButton = j - 4;
            numTable = 1;
        }

        headerElements[j].addEventListener("click", event => {
            let saved = JSON.parse(localStorage.getItem(courseNumber));
            if (saved[numTable][0] === numHeaderButton) {
                saved[numTable][1] = toggleCircle(saved[numTable][1]);
                localStorage.setItem(courseNumber, JSON.stringify(saved));
            } else {
                saved[numTable][0] = numHeaderButton;
                saved[numTable][1] = 0;
                localStorage.setItem(courseNumber, JSON.stringify(saved));
            }
        });
    }

    //load saved sorting
    //defaults: -1 = initial, 0 = asc, 1 = desc
    if (savedSorting === null) {
        localStorage.setItem(courseNumber, "[[0,-1],[0,-1]]");     //default
    } else {
        if (savedSorting[0][1] !== -1) {
            let headerElement = savedSorting[0][0];
            let headerEleSorting = savedSorting[0][1];
            $(tables).eq(0).trigger("sorton", [[[headerElement, headerEleSorting]]]);
        }
        if (savedSorting[1][1] !== -1) {
            let headerElement = savedSorting[1][0];
            let headerEleSorting = savedSorting[1][1];
            $(tables).eq(1).trigger("sorton", [[[headerElement, headerEleSorting]]]);
        }
    }
}

function toggleCircle(state) {
    switch (state) {
        case -1:
            return 0;
        case 0:
            return 1;
        case 1:
            return -1;
    }
}

/* jshint ignore:start */
]]></>).toString();
var c = Babel.transform(inline_src, { presets: [ "es2015", "es2016" ] });
eval(c.code);
/* jshint ignore:end */