All files / web/bundles/pimui/js pim-dialogform.js

3.8% Statements 3/79
0% Branches 0/32
6.25% Functions 1/16
3.8% Lines 3/79

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  100x                                         100x       100x                                                                                                                                                                                                                                                                                                              
/* global console */
define(
    [
        'jquery',
        'underscore',
        'oro/mediator',
        'pim/router',
        'oro/loading-mask',
        'pim/initselect2',
        'jquery-ui',
        'bootstrap'
    ], function (
        $,
         _,
         mediator,
         router,
         LoadingMask,
         initSelect2
    ) {
        'use strict';
 
        // Allow using select2 search box in jquery ui dialog
        $.ui.dialog.prototype._allowInteraction = function (e) {
            return !!$(e.target).closest('.ui-dialog, .select2-drop').length;
        };
 
        return function (elementId, callback) {
            var $el = $(elementId);
            if (!$el.length) {
                return console.error('DialogForm: the element could not be found!');
            }
            var $dialog;
            var url = $el.attr('data-form-url');
            if (!url) {
                throw new Error('DialogForm: please specify the url');
            }
            var width = $el.attr('data-form-width') || 400;
 
            var loadingMask = null;
 
            function showLoadingMask() {
                if (!loadingMask) {
                    loadingMask = new LoadingMask();
                    loadingMask.render().$el.appendTo($('#container'));
                }
                loadingMask.show();
            }
 
            function destroyDialog() {
                if ($dialog && $dialog.length) {
                    $dialog.remove();
                }
                $dialog = null;
            }
 
            function createDialog(data) {
                destroyDialog();
                var $form = $(data);
                var formTitle = $form.data('title');
                var formId = '#' + $form.attr('id');
                var loadingMask = $('<div class="AknLoadingMask">').hide();
                $('body').append(loadingMask);
 
                var formButtons = [];
                var submitButton = $form.data('button-submit');
                var cancelButton = $form.data('button-cancel');
                if (submitButton) {
                    formButtons.push({
                        text: submitButton,
                        'class': 'btn btn-primary',
                        click: function () {
                            showLoadingMask();
                            $.ajax({
                                url: url,
                                type: 'post',
                                data: $(formId).serialize(),
                                success: function (data) {
                                    processResponse(data);
                                    mediator.trigger('dialog:open:after', this);
                                    loadingMask.remove();
                                }
                            });
                        }
                    });
                }
                if (cancelButton) {
                    formButtons.push({
                        text: cancelButton,
                        'class': 'btn',
                        click: function () {
                            destroyDialog();
                            loadingMask.remove();
                        }
                    });
                }
 
                $dialog = $form.dialog({
                    title: formTitle,
                    modal: true,
                    resizable: false,
                    width: width,
                    buttons: formButtons,
                    draggable: false,
 
                    open: function () {
                        $(this).parent().on('keypress', function (e) {
                            if (e.keyCode === $.ui.keyCode.ENTER) {
                                e.preventDefault();
                                e.stopPropagation();
                                $(this).find('button.btn-primary:eq(0)').click();
                            }
                        });
                        loadingMask.show();
                    },
 
                    close: function () {
                        $(this).remove();
                        loadingMask.remove();
                    }
                });
 
                initSelect2.init($(formId));
                $(formId + ' .switch').bootstrapSwitch();
 
                $(formId).find('[data-toggle="tooltip"]').tooltip();
            }
 
            function isJSON(str) {
                try {
                    JSON.parse(str);
                } catch (e) {
                    return false;
                }
 
                return true;
            }
 
            function processResponse(data) {
                loadingMask.hide();
                if (isJSON(data)) {
                    data = $.parseJSON(data);
                    destroyDialog();
                    if (callback) {
                        callback(data);
                    } else {
                        router.redirect(data.url);
                    }
                } else if (_.isObject(data)) {
                    destroyDialog();
                    if (callback) {
                        callback(data);
                    } else if (data.url) {
                        router.redirect(data.url);
                    } else if (data.route) {
                        router.redirectToRoute(data.route, data.params);
                    }
                } else if ($(data).prop('tagName').toLowerCase() === 'form') {
                    createDialog(data);
                }
            }
 
            $el.on('click', function (e) {
                e.preventDefault();
                showLoadingMask();
                $.ajax({
                    url: url,
                    type: 'get',
                    success: function (data) {
                        loadingMask.hide();
                        createDialog(data);
                        mediator.trigger('dialog:open:after', this);
                    }
                });
            });
        };
    }
);