Code coverage report for src/components/bootstrap-remote-modals.js

# All files / src/components bootstrap-remote-modals.js

0% Statements 0/76
0% Branches 0/52
0% Functions 0/13
0% Lines 0/74

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

     /**
     * Simple way to have remotely loaded modals withoud the need to have a perfect markup and replace everything.
     */

    var BootstrapRemoteModal = function(presetOptions, templateOptions){
        presetOptions = presetOptions || {};
        templateOptions = templateOptions || {};

        "use strict";
        var options = {
            parentElement       : presetOptions.parentElement      || 'body',
            header              : presetOptions.header             || true,
            footer              : presetOptions.footer             || true,
            saveButton          : presetOptions.saveButton         || false,
            closeIcon           : presetOptions.closeIcon          || true,
            modalTitle          : presetOptions.modalTitle         || '',
            remoteLink          : presetOptions.remoteLink         || "",
            fnOnShow            : presetOptions.fnOnShow           || null,
            fnOnShown           : presetOptions.fnOnShown          || null,
            fnOnHide            : presetOptions.fnOnHide           || null,
            fnOnHidden          : presetOptions.fnOnHidden         || null,
            fnOnLoaded          : presetOptions.fnOnLoaded         || null,
            removeOnClose       : presetOptions.removeOnClose      || false,
            parseScriptsOnLoad  : presetOptions.parseScriptsOnLoad || false,
            blocking            : presetOptions.blocking           || false
        }

        var templateStrings = {
            closeIcon   : templateOptions.closeIcon || '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>',
            closeButton : templateOptions.closeButton || '<button type="button" class="btn btn-outline-secondary" data-dismiss="modal">Close</button>',
            saveButton  : templateOptions.saveButton || '<button type="button" class="btn btn-primary">Save changes</button>'
        };

        //Define all the blocks and combine them by jquery methods
        var 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       = $(templateOptions.closeIcon),
            closeButton     = $(templateOptions.closeButton),
            saveButton      = $(templateOptions.saveButton);

        var modalObject = null;

        var
        convertKebabCase = function(string){
            return string.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
        },
        parseOptions = function(){
            var self=this;
            $.each(options, function(key, option){
                options[key] = self.data(convertKebabCase(key)) || options[key];
            });
        },
        bindEvents = function(){
            modalObject.on('show.bs.modal', function(){
                loadRemote();
                try{ options.fnOnShow } catch (e) {}
            });
            modalObject.on('shown.bs.modal', options.fnOnShown);
            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);
        },
        loadRemote = function(){
            var modal_body = modalObject.find('.modal-body')
            $.ajax({
                url : options.remoteLink,
                method: 'GET',
                success: function(resolve){
                    modal_body.html(resolve);
                    modalObject.trigger('loaded.ls.remotemodal');
                }
            });
        },
        combineModal = function(){
            var thisContent = contentBlock.clone();

            thisContent.append(bodyBlock.clone());

            if(options.header === true){
                var thisHeader = headerBlock.clone();
                headlineBlock.text(options.modalTitle);
                thisHeader.append(closeIcon.clone());
                thisHeader.append(headlineBlock);
                thisContent.prepend(thisHeader);
            }
            if(options.footer === true){
                var thisFooter = footerBlock.clone();
                thisFooter.append(closeButton.clone());

                if(options.saveButton === true)
                    thisFooter.append(saveButton.clone());

                thisContent.append(thisFooter);
            }
            modalObject = outerBlock.clone();
            modalObject.append(innerBlock.clone().append(thisContent));
        },
        bindToElement = function(){
            this.on('click.remotemodal', function(){
                modalObject.modal('toggle');
            });
        },
        runPrepare = function(){
            if(this.data('remote-modal-appended') == 'yes') {
                return;
            }
            parseOptions.call(this);

            combineModal();
            modalObject.appendTo($(options.parentElement));
            bindToElement.call(this);
            bindEvents.call(this);

            this.data('remote-modal-appended', 'yes');
        };

        parseOptions.call(this);

        combineModal();
        modalObject.appendTo($(options.parentElement));

        bindToElement.call(this);
        bindEvents.call(this);
    };

    jQuery.fn.extend({
        remoteModal : BootstrapRemoteModal
    });