All files / src/parts confirmationModal.js

0% Statements 0/46
0% Branches 0/13
0% Functions 0/13
0% Lines 0/45

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                                                                                                                                                                                                                     
/**
 * Neccessary methods for the confirmation modal
 */
import _ from 'lodash';
import LOG from "../components/lslog";
 
const ConfirmationModal = function(e){
    //////PREGENERATED VARIABLES
    //Define the scope
    const _this = this;
    //Set everything to null on default
    const optionsDefault = {
        onclick     : null,
        href        : null,
        message     : null,
        keepopen    : null,    
        postdata   : null,
        gridid      : null,
        "ajax-url"  : null,
    };
 
    //////METHODS
    //Parse available options from specific item.data settings, if not available load relatedTarget settings
    const _parseOptions = (e) => {
        return _.each(optionsDefault, (value, key) => {
            optionsDefault[key] = $(_this).data(key) || $(e.relatedTarget).data(key) || optionsDefault[key];
        });
    },
    //Generate a simple link on the ok button
    _basicLink = () => {
        LOG.log('Binding basicLink in notification panel');
        $(_this).find('.btn-ok').attr('href', options.href);
    },
    //Evaluate a function on ok button click
    _onClickFunction = () => {
        LOG.log('Binding onClick-functions in notification panel');
        const onclick_fn = eval(options.onclick);
        if (typeof onclick_fn == 'function') {
            $(_this).find('.btn-ok').off('click');
 
            $(_this).find('.btn-ok').on('click', function(ev) {
                if(!options.keepopen ) { $('#confirmation-modal').modal('hide'); }
                onclick_fn();
            });
            return
        }
        LOG.error("Confirmation modal: onclick is not a function. Wrap data-onclick content in (function() { ... }).");
        return;
    },
    //Set up an ajax call and regenerate a gridView on ok button click
    _ajaxHandler = () => {
        LOG.log('Binding ajax handler in notification panel');
        $(_this).find('.btn-ok').on('click', function(ev) {
            $.ajax({
                type: "POST",
                url: options['ajax-url'],
                data: options.postdata,
 
                success : function(html, statut)
                {
                    $.fn.yiiGridView.update(options.gridid);                   // Update the surveys list
                    $('#confirmation-modal').modal('hide');
                },
                error :  function(html, statut){
                    $('#confirmation-modal .modal-body-text').append(html.responseText);
                }
 
            });
        });
    },
    _setTarget = () => {
        //Set up normal href
        if (!!options.href) {
            _basicLink();
            return;
        }
        //Set up a complete function
        if (!!options.onclick) {
            _onClickFunction();
            return;
        }
        //Set up an ajax post
        if (!!options['ajax-url']) {
            _ajaxHandler();
            return;
        }
        LOG.error("Confirmation modal: Found neither data-href or data-onclick, nor ajax data.");
    };
 
    //////RUN BINDINGS
    //Current options object
    const options = _parseOptions(e);
    //Set the message if available
    $(this).find('.modal-body-text').html(options.message);
    //Run setTarget to determine loading target
    _setTarget();
};
 
const loadMethods = ()=>{
    LOG.log('ConfirmationModal calling');
    $('#confirmation-modal').on('show.bs.modal', function(e) {
        ConfirmationModal.call(this,e);
    });
};
 
export default loadMethods;