kneefer / Hide timetable activities (plan.polsl.pl)

// ==UserScript==
// @name         Hide timetable activities (plan.polsl.pl)
// @namespace    http://tampermonkey.net/
// @version      3.1
// @description  Hides selected activities from plan.polsl.pl timetable.
// @author       Szymon Bartnik
// @match        *plan.polsl.pl/*
// @grant        none
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js

// ==/UserScript==
/* jshint -W097 */

'use strict';

//
// Constants & variables
var fadeOutedOpacityLevel = 0.3;
var animationSpeed = "fast";

var activities = [
    { key: "Wykłady",   value: "rgb(123, 247, 141)", isChecked: true },
    { key: "Ćwiczenia", value: "rgb(121, 249, 209)", isChecked: false },
    { key: "Laborki",   value: "rgb(124, 176, 246)", isChecked: false },
    { key: "Projekt",   value: "rgb(239, 131, 221)", isChecked: false },
    { key: "Dodatkowe", value: "rgb(248, 240, 84)", isChecked: false },
];

//
// Functions
function saveNewValuesToLocalStorage(){
    if(typeof(Storage) == "undefined")
        return;
    
    localStorage.setItem("bs.fadeOutedOpacityLevel", fadeOutedOpacityLevel);
    for(var i in activities){
        localStorage.setItem("bs." + activities[i].key, activities[i].isChecked);
    }
}

function restoreValuesFromLocalStorage(){
    if(typeof(Storage) == "undefined")
        return;

    var fadeOutedOpacityLevelData = localStorage.getItem("bs.fadeOutedOpacityLevel");
    if(fadeOutedOpacityLevelData !== null)
        fadeOutedOpacityLevel = fadeOutedOpacityLevelData;
    
    for(var i in activities){
        var visibilityValue = localStorage.getItem("bs." + activities[i].key);
        if(visibilityValue !== null)
        {
            activities[i].isChecked = visibilityValue === "true";
        }
    }
}

function fadeTo(elem, lvl){
    elem.stop();
    elem.fadeTo(animationSpeed, lvl);
}

function generateEventsForActivity(conditions){
    var activitiesToHide = jq('div').filter(function() {
        for(var i in conditions){
            if(jq(this).css('background-color') == conditions[i])
                return true;
        }
        return false;
    });

    var activitiesToShow = jq("[id^=course_]").not(activitiesToHide);

    fadeTo(activitiesToHide, fadeOutedOpacityLevel);

    // Interact lecture items behaviors
    activitiesToHide
      .on("mouseover", function() {
        fadeTo(activitiesToHide, 1.0);
      })
      .on("mouseleave", function() {
        fadeTo(activitiesToHide, fadeOutedOpacityLevel);
      });

    // Interact other activities items behaviors
    activitiesToShow
      .on("mouseover", function() {
        fadeTo(activitiesToHide, 0);
      })
      .on("mouseleave", function() {
        fadeTo(activitiesToHide, fadeOutedOpacityLevel);
      });
}

function regenerateEvents(){
    var allActivities = jq("[id^=course_]");
    allActivities.fadeTo(animationSpeed, 1);
    allActivities.unbind("mouseover");
    allActivities.unbind("mouseleave");
    
    var conditions = [];
    
    for(var i in activities){
        if(activities[i].isChecked)
            conditions.push(activities[i].value);
    }
    
    generateEventsForActivity(conditions);
}

function getOptionsElement(activity){
    var newOuterElement = jq("<li><label style='display:block' /></li>");
    var activityCheckBox = jq("<input class='activityCheckBox' type='checkbox' data-activityvalue='" + activity.value + "' />");

    activityCheckBox.on("change", function(){
        activity.isChecked = activityCheckBox.is(':checked');
        saveNewValuesToLocalStorage();
        regenerateEvents();
    });
    
    activityCheckBox.appendTo(newOuterElement);
    activityCheckBox.attr('checked', activity.isChecked);
    
    jq("<span>" + activity.key + "</span>").appendTo(newOuterElement);
    
    return newOuterElement;
}

function generateOptions(){
    var newOuterNode = jq("<ul style='text-align: left; display: inline-block; list-style-type: none;" + 
                          " -webkit-column-count: 2; -moz-column-count: 2; column-count: 2; margin: 0; padding: 0' />");

    for(var i in activities){
        var newOption = getOptionsElement(activities[i]);
        newOption.appendTo(newOuterNode);
    }
    
    return newOuterNode;
}

function generateUI(){
    var toWrap = jq('#weekBrowser');
    toWrap.css("position", "relative");
    toWrap.css("display", "inline");
    toWrap.css("float", "left");
    toWrap.css("top", "");
    toWrap.css("left", "");
    var newOuterNode = jq("<div style='position: absolute; top: 67px; left: 5px' />");
    newOuterNode.insertAfter(toWrap);
    toWrap.appendTo(newOuterNode);
    var newNode = jq(
        "<div class='wBBox' id='activitiesSelector' style='float: left; width:250px; margin-left: 5px; height: 118px; display: inline;'>" +
            "<div class='titleBar'>Ukrywane zajęcia</div>" +
            "<div class='wBParent' >" + 
                "<div id='activitiesOptions' style='margin: 0 auto; text-align: center' />" +
                "<div style='text-align: center'>" +
                    "Widoczność ukrytych " +
                    "<input type='text' id='opacityLevel' style='width: 25px; margin-top: 5px; text-align: left' />" +
                    " %" +
                "</div>" + 
            "</div>" +
        "</div>");
    newNode.appendTo(newOuterNode);
    
    jq("#opacityLevel").val(fadeOutedOpacityLevel);
    jq("#opacityLevel").on("input propertychange paste", function(){
        var textBoxValue = jq("#opacityLevel").val();
        var floatParsed = parseFloat(textBoxValue);
        if(isNaN(floatParsed))
            return;
        
        fadeOutedOpacityLevel = floatParsed;
        saveNewValuesToLocalStorage();
        regenerateEvents();
    });

    var genOptions = generateOptions();
    genOptions.appendTo(jq('#activitiesOptions'));
}

//
// Entry Point
var jq = jQuery.noConflict(true);

restoreValuesFromLocalStorage();
generateUI();
regenerateEvents();