ico / Releases GANTT chart

// ==UserScript==
// @name         Releases GANTT chart
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  Add GANTT chart to the release overview
// @author       Ico Davids
// @match        https://support.incore.nl/Pocus/RITS/ListOverview.aspx
// @grant        none
// @require      https://unpkg.com/mermaid@8.3.1/dist/mermaid.min.js
// @license      MIT
// @copyright    2019, ico (https://openuserjs.org/users/ico)
// @updateURL    https://openuserjs.org/meta/ico/Releases_GANTT_chart.user.js
// @downloadURL  https://openuserjs.org/install/ico/Releases_GANTT_chart.user.js
// ==/UserScript==

(function() {
    'use strict';

    var isListPlanning = $('#ctl00_MainContent_theDataOverview_theDataOverviewUserviewSelector_theIndependentRepeater_ctl04_cbUserview').is(':checked');
    var isAfdAna = $('#ctl00_MainContent_theDataOverview_theDataOverviewUserviewSelector_theIndependentRepeater_ctl00_cbUserview').is(':checked');
    var isAfdQA = $('#ctl00_MainContent_theDataOverview_theDataOverviewUserviewSelector_theIndependentRepeater_ctl02_cbUserview').is(':checked');
    var isAfdDev = $('#ctl00_MainContent_theDataOverview_theDataOverviewUserviewSelector_theIndependentRepeater_ctl01_cbUserview').is(':checked');

    if (!isListPlanning && !isAfdAna && !isAfdDev && !isAfdQA) {
        return;
    }


    $('.UserViewsTable').parent().css('position', 'relative').append('<a id="gantt-button" href="#graphDiv" style="position: absolute; top: 0px; right: 0" title="Scroll to GANTT chart"><img src="../images/Icons/calendar.png"></a>');

    var listPlanningRows = $("#ctl00_MainContent_theDataOverview_theListGridView_GridViewLists > tbody > tr").not(':first-child').not(':last-child');
    var graphDef = 'gantt\ndateFormat  D-M-YYYY\ntitle Release overview\n';

    listPlanningRows.each(function(row){
        var release = $(this).find('td:nth-child(2)').text().trim();
        var analysis, program, test, deployment, acc, prod = false;

        if (isAfdAna || isListPlanning) {
            analysis = $(this).find('a[id*=sldAnalysis]');
        }

        if (isAfdAna || isAfdDev || isListPlanning) {
            program = $(this).find('a[id*=sldProgram]');
        }

        if (isAfdDev || isAfdQA || isListPlanning) {
            test = $(this).find('a[id*=sldTest]');
        }

        if (isAfdQA || isListPlanning) {
            deployment = $(this).find('a[id*=sldDeploy]');
        }

        if (isListPlanning) {
            acc = $(this).find('a[id*=sldAccept]');
            prod = $(this).find('a[id*=sldProduction]');
        }

        graphDef += '\n\nSection '+release;
        graphDef += generateProcess('Analysis', analysis, program);
        graphDef += generateProcess('Dev', program, test);
        graphDef += generateProcess('Test', test, deployment);
        graphDef += generateProcess('ACC', acc, acc);
        graphDef += generateProcess('PROD', prod, prod);
    });

    console.log(graphDef);

    $('.contentmain').first().append('<div class="mermaid" id="graphDiv" style="min-width:1000px">'+graphDef+'</div>').ready(function(){

        mermaid.initialize({
            startOnLoad:true,
            gantt:{
              titleTopMargin:25,
              barHeight:20,
              barGap:4,
              topPadding:50,
              leftPadding:150,
              gridLineStartPadding:35,
              fontSize:11,
              fontFamily:'"Arial", "sans-serif"',
              numberSectionStyles:2,
              axisFormat:'%d-%m-%Y'
            }
        });

    });

    function getText(el) {
        return el.text().trim();
    }

    function isDone(el) {
        var td = el.parent('td');
        if (td.hasClass('CalEntryCompleted')) {
            return 'done, ';
        }
        return '';
    }

    function isCritical(el1, el2) {
        var td1 = el1.parent('td');
        var td2 = el2.parent('td');
        if (td1.hasClass('CalEntryOverdue') || td2.hasClass('CalEntryOverdue') || td2.hasClass('CalEntryAttention') ) {
            return 'crit, ';
        }
        return '';
    }

    function isActive(el1, el2) {
        var td1 = el1.parent('td');
        var td2 = el2.parent('td');
        if (td1.hasClass('CalEntryCompleted') && !td2.hasClass('CalEntryCompleted')) {
            return 'active, ';
        }
        return '';
    }

    function isProForma(el) {
        var td = el.parent('td');
        if (td.hasClass('CalEntryProForma')) {
            return ' (pro-forma)';
        }
        return '';
    }

    function generateProcess(title, el1, el2) {
        if (!el1 || !el2) {
            return '';
        }
        var date1 = getText(el1);
        var date2 = getText(el2);
        if (date1 == date2) {
            title += ' ' + date2;
            date2 = '1d';
        }
        return '\n' + title + isProForma(el2) + ' : ' + isActive(el1, el2) + isCritical(el1, el2) + isDone(el2) + date1 + ', ' + date2;
    }

})();