# All files / src/parts globalMethods.js

0% Statements 0/66
0% Branches 0/28
0% Functions 0/19
0% Lines 0/63

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136

     /**
     * Define global setters for LimeSurvey
     * Also bootstrapping methods and window bound methods are set here
     */
    import LOG from '../components/lslog';

    const globalWindowMethods = {
        renderBootstrapSwitch : () => {
            try{
                if(!$('[data-is-bootstrap-switch]').parent().hasClass('bootstrap-switch-container')) {
                    $('[data-is-bootstrap-switch]').bootstrapSwitch({
                        onInit: () => LOG.log("BootstrapSwitch Initialized")
                    });
                }
            } catch(e) { LOG.error(e); }
        },
        unrenderBootstrapSwitch : () => {
            try{
                $('[data-is-bootstrap-switch]').bootstrapSwitch('destroy');
            } catch(e) { LOG.error(e); }
        },
        validatefilename: (form, strmessage) => {
            if (form.the_file.value == "") {
                $('#pleaseselectfile-popup').modal();
                form.the_file.focus();
                return false ;
            }
            return true ;
        },
        doToolTip: () => {
            try {
                $(".btntooltip").tooltip("destroy");
            } catch (e) {}
            try {
                $('[data-tooltip="true"]').tooltip("destroy");
            } catch (e) {}
            try {
                $('[data-tooltip="true"]').tooltip("destroy");
            } catch (e) {}

            $(".btntooltip").tooltip();
            $('[data-tooltip="true"]').tooltip();
            $('[data-bs-toggle="tooltip"]').tooltip();

        },
        // finds any duplicate array elements using the fewest possible comparison
        arrHasDupes:  ( arrayToCheck ) => {
            return (_.uniq(arrayToCheck).length !== arrayToCheck.length);
        },
        arrHasDupesWhich: ( arrayToCheck ) => {
            return (_.difference(_.uniq(arrayToCheck), arrayToCheck)).length > 0;
        },
        getkey :  (e) => {
            return (window.event) ? window.event.keyCode :(e ? e.which : null);
        },
        goodchars : (e, goods) => {
            const key = getkey(e);
            if (key == null) return true;

            // get character
            const keychar = (String.fromCharCode(key)).toLowerCase();

            goods = goods.toLowerCase();

            return (goods.indexOf(keychar) != -1) || ( key==null || key==0 || key==8 || key==9  || key==27 );

        },
        tableCellAdapters: () => {
            $('table.activecell').on("click", [
                'tbody td input:checkbox',
                'tbody td input:radio',
                'tbody td label',
                'tbody th input:checkbox',
                'tbody th input:radio',
                'tbody th label'
            ].join(', '), function(e) {
                e.stopPropagation();
            });
            $('table.activecell').on("click", 'tbody td, tbody th', function() {
                if($(this).find("input:radio,input:checkbox").length==1)
                {
                  $(this).find("input:radio").click();
                  $(this).find("input:radio").triggerHandler("click");
                  $(this).find("input:checkbox").click();
                  $(this).find("input:checkbox").triggerHandler("click");
                }
            });
        },
        sendPost: (url,content, contentObject) => {
            contentObject = contentObject || {};
            const $form = $("<form method='POST'>").attr("action", url);
            if(typeof content == 'string' && content != ''){
                try {
                    contentObject = _.merge(contentObject, JSON.parse(content));
                } catch(e) { console.error('JSON parse on sendPost failed!') }
            }

            _.each(contentObject, (value,key) => {
                $("<input type='hidden'>").attr("name", key).attr("value", value).appendTo($form);
            });

            $("<input type='hidden'>").attr("name", LS.data.csrfTokenName).attr("value", LS.data.csrfToken).appendTo($form);
            $form.appendTo("body");
            $form.submit();
        },
        addHiddenElement: (form, name, value) => {
            $('<input type="hidden"/>').attr('name', name).attr('value', value).appendTo($(form));
        },
        fixAccordionPosition : () => {
            $('#accordion').on('shown.bs.collapse',".panel-collapse.collapse", function (e) {
                if(e.target != this) return;
                $('#accordion').find('.panel-collapse.collapse').not('#'+$(this).attr('id')).collapse('hide');
            });
        }
    };
    const globalStartUpMethods = {
        bootstrapping : ()=>{
            $('button,input[type=submit],input[type=button],input[type=reset],.button').button();
            $('button,input[type=submit],input[type=button],input[type=reset],.button').addClass("limebutton");

            $(".progressbar").each(function(){
                var pValue = parseInt($(this).attr('name'));
                $(this).progressbar({value: pValue});

                if (pValue > 85){ $("div",$(this)).css({ 'background': 'Red' }); }
                $("div",this).html(pValue + "%");
            });

            globalWindowMethods.tableCellAdapters();
        }
    };

    export {globalStartUpMethods, globalWindowMethods};