dieuph / aa-simulator

// ==UserScript==
// @name                aa-simulator
// @namespace           https://github.com/dieuph
// @description         Interface Simulator for Acrevis/Alpha
// @author              dieu
// @license             MIT; https://opensource.org/licenses/MIT
// @homepageURL         https://github.com/dieuph
// @updateURL           https://openuserjs.org/meta/dieuph/aa-simulator.meta.js
// @copyright           2018, dieu (https://github.com/dieuph)
// @include             /^https?://.*\.axonivy\.io/.*$/
// @include             /^http?://.*localhost:\d{4}/ivy.*$/
// @include             /^http?://.*192.168.80.27:\d{4}/ivy.*$/
// @include             /^http?://.*10.124.1.75:\d{4}/ivy.*$/
// @include             /^http?://.*10.124.1.149:\d{4}/ivy.*$/
// @include             /^http?://.*10.124.1.231:\d{4}/ivy.*$/
// @resource            tingle https://cdnjs.cloudflare.com/ajax/libs/tingle/0.13.2/tingle.min.css
// @require             https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js
// @require             https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js
// @require             https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js
// @require             https://cdnjs.cloudflare.com/ajax/libs/tingle/0.13.2/tingle.min.js
// @grant               GM_getValue
// @grant               GM_setValue
// @grant               GM_deleteValue
// @grant               GM_xmlhttpRequest
// @grant               GM_listValues
// @grant               GM_registerMenuCommand
// @grant               GM_log
// @grant               GM_addStyle
// @grant               GM_getResourceText
// @version             0.1
// ==/UserScript==

/*
*/

var $ = unsafeWindow.$;
var jQuery = unsafeWindow.jQuery;

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

    //*************************************************************************
    // SETTINGS
    //*************************************************************************

    GM_addStyle(GM_getResourceText("tingle"));
    GM_addStyle(`
        .flyover-btn {
            float: left;
            margin: 0px 0px 10px 10px;
            bottom: 0vh;
            width: 100px;
            height: 35px;
            font-weight: 600 !important;
            font-family: \"Open Sans\", sans-serif;
            text-shadow: rgba(0, 0, 0, 0.4) 1px 1px 1px;
            font-size: 15px !important;
            text-align: center !important;
            box-shadow: rgba(0, 0, 0, 0.2) 0px 3px 0px 0px;
            cursor: pointer !important;
            display: block !important;
        }
        .flyover-wrapper {
            padding: 0px;
            font-weight: 500;
            -webkit-box-shadow: -1px 0px 11px -1px rgba(0,0,0,0.75);
            -moz-box-shadow: -1px 0px 11px -1px rgba(0,0,0,0.75);
            box-shadow: -1px 0px 11px -1px rgba(0,0,0,0.75);
        }
        .flyover-close {
            margin: 2px 0px 0px 115px;
            display: inline-block;
            font-size: 20px;
            cursor: pointer
        }
        .flyover {
            right: -650px;
            bottom: 0px;
            overflow: hidden;
            position: fixed;
            width: 150px;
            opacity: 1;
            z-index: 1050;
            -webkit-transition: all .6s ease;
            -moz-transition: all .6s ease;
            -o-transition: all .6s ease;
            -ms-transition: all .6s ease;
            transition: all .6s ease;
        }
        .flyover.in {
            left: 0px; bottom: 0px;
        }
        .flyover.out {
            left: -110px;
            bottom: 0px;
        }
        .text-left {
            text-align: left !important;
        }
    `);


    //*************************************************************************
    // CONSTANTS
    //*************************************************************************

    var COOKIE_KEY = 'aa-simulator-flyover-panel';

    var BUTTONS = [{
        id: 'elca',
        text: 'Elca',
        event: onElcaButtonClick
    }, {
        id: 'finnova',
        text: 'Finnova',
        event: onFinnovaButtonClick
    }];

    //*************************************************************************
    //  APPLICATION INITIALIZE
    //*************************************************************************

    (function() {
        createApplication();
    })();

    function createApplication() {
        createPanel();
        createButtons();
    }

    function createPanel() {
        _.isEmpty(Cookies.get(COOKIE_KEY)) && Cookies.set(COOKIE_KEY, 'open', { expires: 90 });
        let display = Cookies.get(COOKIE_KEY) == "open" ? "in" : "out";
        let icon = (display == 'in') ? '<<' : '>>';
        $('footer').after('<div id="flyover-wrapper" class="flyover-wrapper flyover ' + display + '"><a><span class="flyover-close">' + icon +'</span></a><div class="flyover-box"></div></div>');
        $('span.flyover-close').html(icon);
        $('.flyover-close').click(function() {
            let toggle = Cookies.get(COOKIE_KEY) == "open";
            let icon = toggle ? '>>' : '<<';
            let status = toggle ? 'close' : 'open';

            Cookies.set(COOKIE_KEY, status, { expires: 90 });
            $('span.flyover-close').html(icon);
            $('#flyover-wrapper').css({left: Cookies.get(COOKIE_KEY) == "open" ? 0 : -110});
        });
    }

    function createButtons() {
        _.map(BUTTONS, (value, key, collection) => { createButton(value.id, value.text, value. event); });
    }

    function createButton(id, text, event) {
        let template = '<button class="flyover-btn tingle-btn tingle-btn--primary" id="{buttonId}"><span class="btn-text">{buttonText}</span></button>';
        let html = template.replace('{buttonId}', id).replace('{buttonText}', text);
        $('.flyover-box').append(html);
        $("#" + id).click(event);
    }

    //*************************************************************************
    //  BUTTON EVENT
    //*************************************************************************

    function onElcaButtonClick(e) {
        e.preventDefault();
        console.log('Elca clicked')

        var modal = new tingle.modal({
            footer: true,
            stickyFooter: false,
            closeMethods: ['overlay', 'button', 'escape'],
            cssClass: ['custom-class-1', 'custom-class-2'],
            onOpen: function() {
                console.log('modal open');
            },
            onClose: function() {
                console.log('modal closed');
            },
            beforeClose: function() {
                return true;
            }
        });

        modal.setContent(`
            <h2>ELCA Feedback Simulator</h2>
            <form class="form-horizontal" style="margin-top:30px">
                <div class="form-group">
                    <label for="dossierId" class="col-sm-2 control-label text-left">Dossier Id</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" id="dossierId" placeholder="COB000010" />
                    </div>
                </div>
                <div class="form-group row">
                    <label for="documentId" class="col-sm-2 control-label text-left">Document Id</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" id="documentId" placeholder="COBD000000015" />
                    </div>
                </div>
                <div class="form-group row">
                    <label for="documentType" class="col-sm-2 control-label text-left">Document Type</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" id="documentType" placeholder="BV049" />
                    </div>
                </div>
                <div class="form-group row">
                    <label for="documentPages" class="col-sm-2 control-label text-left">Document Pages</label>
                    <div class="col-sm-2">
                        <select class="form-control" id="documentPages">
                            <option>1</option>
                            <option>2</option>
                        </select>
                    </div>
                </div>
            </form>
        `);

        modal.addFooterBtn('Send', 'tingle-btn tingle-btn--primary', () => { modal.close(); });
        modal.addFooterBtn('Close', 'tingle-btn tingle-btn--danger', () => { modal.close(); });
        modal.open();
    }

    function onFinnovaButtonClick(e) {
        e.preventDefault();
        console.log('Finnova clicked')
    }

    //*************************************************************************
    // PRIVATE METHOD
    //*************************************************************************

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