brobada / Extra CPQ Tweaks

// ==UserScript==
// @name         Extra CPQ Tweaks
// @version      0.22
// @description  Extra CPQ Tweaks
// @author       Obada Kadri
// @match        *://*.bigmachines.com/*
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

document.addEventListener("keyup", function(e) {
    if (e.keyCode == 13 && e.ctrlKey) { // Ctrl + Enter
        document.getElementById("update").click();
    }
});

// Environment colors
var envName = location.href.split( '/' )[2].split('.')[0];
var envColors = {
    'cpqau': 'goldenrod',
    'cpqas': '#333333',
    'cpqab': '#2647a0',
    'cpqad': '#006666',
    'cpqap': 'maroon'
};

var envColorStyle = envColors[envName] ?
`html {
    height: 100%;
    background-color: ${envColors[envName]};
    padding: 0 25px;
}
html:after {
    content: "${envName}";
    position: absolute;
    top: 50%;
    left: 50%;
    z-index: -1;
    font-size: 80px;
    transform: translate(-50%,-50%);
}
body {
    min-height: 100%;
    margin: 0 auto;
    background-color: white;
    padding: 0 5px;
    box-shadow: 0 0 10px 10px rgba(0,0,0,0.3);
}` : '';

// Auto refresh

// Only for deployment center
dCenters = ['schedule_deployment.jsp', 'schedule_events.jsp'];
if (dCenters.indexOf(document.URL.split('/').pop().split('?').shift()) >= 0) {
    var refreshBtn = document.querySelector("[name='refresh']");
    var timer = 10; // in seconds
    var autoRefresh = true;
    var note, btn, refreshInterval, timeout;

    var toggleAutoRefresh = function() {
        autoRefresh = !autoRefresh;

        if (autoRefresh) {
            startAutoRefresh();
        } else {
            btn.innerHTML = "Auto Refresh Off";
            timer = 30;
            clearInterval(refreshInterval);
            clearTimeout(timeout);
        }
    };

    var startAutoRefresh = function() {
        btn.innerHTML = "Auto refreshing in " + timer + "...";
        refreshInterval = setInterval(function () {
            var text;
            if (--timer > 0) {
                text = "Auto refreshing in " + timer + "...";
            } else {
                text = "Auto refreshing now...";
            }
            btn.innerHTML = text;
        }, 1000);

        timeout = setTimeout(function () {
            refreshBtn.click();
        }, 1000 * timer);
    };


    if (refreshBtn) {
        note = document.createTextNode("Auto refreshing in " + timer + "...");
        btn = document.createElement('button');
        btn.className += " auto-refresh";
        btn.addEventListener("click", toggleAutoRefresh);

        btn.appendChild(note);

        document.body.appendChild(btn);

        if (autoRefresh) {
            startAutoRefresh();
        }
    }

    // Delete a history entry in deployment center if more than 5 entries
    KEEP_ENTRIES = 5;
    var historyEntries = document.querySelectorAll("tr[class^='bgcolor-list-']");
    Array.from(historyEntries).forEach(function(el, i) {
    	if (i < historyEntries.length - KEEP_ENTRIES) {
    	    el.querySelector('input[type="checkbox"]').click();
    	}
    });
    if (historyEntries.length > KEEP_ENTRIES) {
        document.getElementById('delete').click();
    }
}

// Redirect login to SSO
/*if (document.URL.split('/').pop().split('?').shift() == "display_company_profile.jsp" && document.getElementById('login-form')) {
    setTimeout(function(){
        window.location.replace("/sso/saml_request.jsp");
    }, 5000);
}*/

// Prevent Timeout (auto clicks on 'Continue Working' button)
setInterval(function() {
	var timeOutPopup = document.getElementById('session-timeout-popup');
	var timeOutBtn = document.getElementById('reset-session');
	if (timeOutPopup && timeOutPopup.style.display != "none") {
		timeOutBtn.click();
	}
}, 30000);

// Force display menu on home
var maxCheckTimes = 10; // Number of times to check if the menu is hidden
var delayTime = 1000; // Delay in between in ms
var checkTimes = 0;
if (jQuery('#hpwrap')) {
    var homeInterval = setInterval(function() {
        checkTimes++;
        var menuTable = jQuery('#hpwrap').closest('table');
        if (!menuTable || checkTimes > maxCheckTimes) {
            clearInterval(homeInterval);
        } else {
            menuTable.css('display','table');
        }
    }, delayTime);
}

// Remove create quote and save quote in AP
prodUrls = [
    "https://cpqap.bigmachines.com/commerce/buyside/commerce_manager.jsp",
    "https://cpqap.bigmachines.com/commerce/buyside/document.jsp"
];
if (prodUrls.indexOf(window.window.location.href.split("?")[0]) >= 0) {
    var newQuote = document.querySelector("[name='new_quote']");
    newQuote.parentElement.removeChild(newQuote);
    var save = document.querySelector("[name='save']");
    save.parentElement.removeChild(save);
}

// Styling Stuff
var bm_css_src = `
.overlay {
	display: none;
}
#loading-dialog {
    -webkit-box-shadow: 0 0 10px 2000px rgba(0,0,0,.5);
    box-shadow: 0 0 10px 2000px rgba(0,0,0,.5);
}
button.auto-refresh {
    position: fixed;
    bottom: 0;
    left: 5em;
    color: #fff;
    background-color: #d9534f;
    border-color: #d43f3a;
    display: inline-block;
    padding: 3px 6px;
    margin-bottom: 0;
    font-size: 11px;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    -ms-touch-action: manipulation;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    background-image: none;
    border: 1px solid transparent;
    border-radius: 5px 5px 0 0;
}

/*
form[name="bmForm"], form[name="listings"] {
	margin-bottom: 5em;
}

form[name="bmForm"] + div, form[name="bmForm"] > table:last-child, form[name="listings"] > table:last-child {
	position: fixed;
	bottom: 0;
	right: 0;
	background-color: #fff;
	padding: 10px;
}
*/

/* Remove 2px height of hidden arrays
.column {
    min-height: 0;
}
.array-wrapper {
    margin: 0;
} */
`;
GM_addStyle(bm_css_src);