Code coverage report for src/components/confirmdeletemodal.js

# All files / src/components confirmdeletemodal.js

0% Statements 0/121
0% Branches 0/66
0% Functions 0/22
0% Lines 0/121

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
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197

     const ConfirmDeleteModal = function (options) {
        const $item = $(this);

        options.fnOnShown = options.fnOnShown || function () {};
        options.fnOnHide = options.fnOnHide || function () {};
        options.removeOnClose = options.removeOnClose || function () {};
        options.fnOnHidden = options.fnOnHidden || function () {};
        options.fnOnLoaded = options.fnOnLoaded || function () {};

        const postUrl = options.postUrl || $item.attr('href'),
            confirmText = options.confirmText || $item.data('text') || '',
            confirmTitle = options.confirmTitle || $item.attr('title') || '',
            postObject = options.postObject || $item.data('post'),
            showTextArea = options.showTextArea || $item.data('show-text-area') || '',
            useAjax = options.useAjax || $item.data('use-ajax') || '',
            keepopen = options.keepopen || $item.data('keepopen') || '',
            gridReload = options.gridReload || $item.data('grid-reload') || '',
            gridid = options.gridid || $item.data('grid-id') || '',
            buttonNo = options.buttonNo || $item.data('button-no') || '<i class="fa fa-times"></i>',
            buttonYes = options.buttonYes || $item.data('button-yes') || '<i class="fa fa-check"></i>',
            parentElement = options.parentElement || $item.data('parent-element') || 'body';

        const closeIconHTML = '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>',
            closeButtonHTML = '<button type="button" class="btn btn-outline-secondary" data-dismiss="modal">' + buttonNo + '</button>',
            confirmButtonHTML = '<button type="button" class="btn btn-primary selector--button-confirm">' + buttonYes + '</button>';

        //Define all the blocks and combine them by jquery methods
        const outerBlock = $('<div class="modal fade" tabindex="-1" role="dialog"></div>'),
            innerBlock = $('<div class="modal-dialog" role="document"></div>'),
            contentBlock = $('<div class="modal-content"></div>'),
            headerBlock = $('<div class="modal-header"></div>'),
            headlineBlock = $('<h4 class="modal-title"></h4>'),
            bodyBlock = $('<div class="modal-body"></div>'),
            footerBlock = $('<div class="modal-footer"></div>'),
            closeIcon = $(closeIconHTML),
            closeButton = $(closeButtonHTML),
            confirmButton = $(confirmButtonHTML);

        let modalObject = null;

        const combineModal = () => {
                const thisContent = contentBlock.clone();

                thisContent.append(bodyBlock.clone());

                if (confirmTitle !== '') {
                    const thisHeader = headerBlock.clone();
                    headlineBlock.text(confirmTitle);
                    thisHeader.append(closeIcon.clone());
                    thisHeader.append(headlineBlock);
                    thisContent.prepend(thisHeader);
                }

                const thisFooter = footerBlock.clone();

                thisFooter.append(closeButton.clone());
                thisFooter.append(confirmButton.clone());
                thisContent.append(thisFooter);

                modalObject = outerBlock.clone();
                modalObject.append(innerBlock.clone().append(thisContent));
            },
            addForm = function () {
                const formObject = $('<form name="' + Math.round(Math.random() * 1000) + '_' + confirmTitle.replace(/[^a-bA-B0-9]/g, '') + '" method="post" action="' + postUrl + '"></form>');
                for (let key in postObject) {
                    let type = 'hidden',
                        value = postObject[key],
                        htmlClass = '';

                    if (typeof postObject[key] == 'object') {
                        type = postObject[key].type;
                        value = postObject[key].value;
                        htmlClass = postObject[key].class
                    }

                    formObject.append('<input name="' + key + '" value="' + value + '" type="' + type + '" ' + (htmlClass ? 'class="' + htmlClass + '"' : '') + ' />');
                }

                formObject.append('<input name="' + LS.data.csrfTokenName + '" value="' + LS.data.csrfToken + '" type="hidden" />');
                modalObject.find('.modal-body').append(formObject)
                modalObject.find('.modal-body').append('<p>' + confirmText + '</p>');

                if (showTextArea !== '') {
                    modalObject.find('form').append('<textarea id="modalTextArea" name="modalTextArea" ></textarea>');
                }

            },
            runAjaxRequest = function () {
                return LS.AjaxHelper.ajax({
                    url: postUrl,
                    type: 'POST',
                    data: modalObject.find('form').serialize(),

                    // html contains the buttons
                    success: function (html, statut) {

                        if (keepopen != 'true') {
                            modalObject.modal('hide'); // $modal.modal('hide');
                        } else {
                            modalObject.find('.modal-body').empty().html(html); // Inject the returned HTML in the modal body
                        }

                        // Reload grid
                        if (gridReload) {
                            $('#' + gridid).yiiGridView('update'); // Update the surveys list
                            setTimeout(function () {
                                $(document).trigger("actions-updated");
                            }, 500); // Raise an event if some widgets inside the modals need some refresh (eg: position widget in question list)
                        }

                        if (html.ajaxHelper) {
                            LS.AjaxHelper.onSuccess(html);
                            return;
                        }

                        if (onSuccess) {
                            var func = new Function(onSuccess);
                            func(html);
                            return;
                        }

                    },
                    error: function (html, statut) {
                        modalObject.find('.modal-body').empty().html(html.responseText);
                        console.ls.log(html);
                    }
                });
            },
            bindEvents = function () {
                modalObject.on('show.bs.modal', function () {
                    addForm();
                    try {
                        options.fnOnShow
                    } catch (e) {}
                });
                modalObject.on('shown.bs.modal', function () {
                    var self = this;
                    modalObject.find('.selector--button-confirm').on('click', function (e) {
                        e.preventDefault();

                        if (!useAjax) {
                            modalObject.find('form').trigger('submit');
                            modalObject.modal('close');
                        } else {
                            // Ajax request
                            runAjaxRequest();
                        }
                    });
                    options.fnOnShown.call(this);
                });
                modalObject.on('hide.bs.modal', options.fnOnHide);
                modalObject.on('hidden.bs.modal', function () {
                    if (options.removeOnClose === true) {
                        modalObject.find('.modal-body').html(" ");
                    }
                    try {
                        options.fnOnHidden
                    } catch (e) {}
                });
                modalObject.on('loaded.ls.remotemodal', options.fnOnLoaded);
            },
            bindToElement = function () {
                $item.on('click.confirmmodal', function () {
                    modalObject.modal('toggle');
                });
            },
            runPrepare = function () {

                if ($item.data('confirm-modal-appended') == 'yes') {
                    return;
                }
                combineModal();
                modalObject.appendTo($(parentElement));
                bindToElement.call(this);
                bindEvents.call(this);

                $item.data('confirm-modal-appended', 'yes');
            };

        runPrepare();
    };

    jQuery.fn.extend({
        confirmModal: ConfirmDeleteModal
    });

    export default function confirmDeletemodal() {
        $(document).off('click.confirmModalSelector', 'a.selector--ConfirmModal');
        $(document).on('click.confirmModalSelector', 'a.selector--ConfirmModal', function (e) {
            e.preventDefault();
            $(this).confirmModal({});
            $(this).trigger('click.confirmmodal');
        });
    };