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

79.49% Statements 62/78
44.44% Branches 12/27
76.19% Functions 16/21
79.49% Lines 62/78

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 1901183x                                                         1183x   1183x   1162x     1162x 1162x 4908x       1162x 4895x 4895x 4895x 58x         4895x             4895x               4895x             4895x   4895x         4895x 11x       4895x 3733x 4x 4x             4895x   4895x     1162x 1162x       1162x 13x 13x 13x       1162x 1162x   1162x 12945x 12945x 12945x 897x   12945x           1162x 5x 5x 5x 5x 5x   5x 2x 2x                 2x   2x           2x 2x 2x 2x                       5x   5x   5x 5x         5x     1162x   1162x 3733x          
define(
    [
    'jquery',
    'underscore',
    'backbone',
    'oro/mediator',
    'oro/messenger',
    'pim/dialog',
    'pim/saveformstate',
    'pim/asynctab',
    'pim/ui',
    'oro/loading-mask',
    'pim/router',
    'require-polyfill'
],
    function (
        $,
        _,
        Backbone,
        mediator,
        messenger,
        Dialog,
        saveformstate,
        loadTab,
        UI,
        LoadingMask,
        router
    ) {
        'use strict';
        var initialized = false;
 
        return function () {
 
            Iif (initialized) {
                return;
            }
            initialized = true;
            var setFullHeight = function ($target) {
                Iif (!$target) {
                    $target = $('body');
                }
            };
            var pageInit = function ($target) {
                Eif (!$target) {
                    $target = $('body');
                    $target.find('form.form-horizontal, [data-saveformstate]').each(function () {
                        saveformstate($(this).attr('id'), loadTab);
                    });
                }
                // Place code that we need to run on every page load here
 
                $target.find('.remove-attribute').each(function () {
                    var target = $(this).parent().find('.icons-container');
                    if (target.length) {
                        $(this).appendTo(target).attr('tabIndex', -1);
                    }
                });
 
                var $localizableIcon = $('<i>', {
                    'class': 'icon-globe',
                    'attr': {
                        'data-original-title': _.__('Localized value'),
                        'data-toggle': 'tooltip',
                        'data-placement': 'right'
                    }
                });
                $target.find('.attribute-field.localizable').each(function () {
                    var $iconsContainers = $(this).find('div.controls').find('.icons-container');
                    if (!$iconsContainers.find('i.icon-globe').length) {
                        $iconsContainers.prepend($localizableIcon.clone());
                    }
                });
 
                UI($target);
 
                $target.find('a[data-form-toggle]').on('click', function () {
                    $('#' + $(this).attr('data-form-toggle')).show();
                    $(this).hide();
                });
 
                $target.find('a[data-toggle="tab"]').on('show.bs.tab', function () {
                    loadTab(this);
                });
 
                //Flash messages:
                if (window.flashMessages) {
                    _.each(window.flashMessages, function (messages, type) {
                        _.each(messages, function (message) {
                            messenger.notify(
                                type,
                                message
                            );
                        });
                    });
                }
                window.flashMessages = [];
 
                setFullHeight($target);
            };
 
            $(function () {
                $(document).on('tab.loaded', 'form.form-horizontal, [data-saveformstate]', function (e, tab) {
                    pageInit($(tab));
                });
 
                $(document).on('shown', 'a[data-toggle="tab"]', function () {
                    var target = $(this).attr('href');
                    Eif (target && target !== '#' && target.indexOf('javascript') !== 0) {
                        setFullHeight($(target).parent());
                    }
                });
 
                var secret = '38384040373937396665';
                var input = '';
                var timer;
                $(document).on('keyup', function (e) {
                    input += e.which;
                    clearTimeout(timer);
                    timer = setTimeout(function () {
                        input = '';
                    }, 500);
                    Iif (input === secret) {
                        $(document.body).addClass('konami');
                    }
                });
 
                // DELETE request for delete buttons
                $(document).on('click', '[data-dialog]', function () {
                    const $el      = $(this);
                    const message  = $el.data('message');
                    const title    = $el.data('title');
                    const subTitle = $el.data('subtitle');
                    const buttonLabel = $el.data('buttonlabel');
 
                    const doAction = function () {
                        const loadingMask = new LoadingMask();
                        loadingMask.render().$el.appendTo($(document.body)).css(
                            {
                                'position': 'absolute',
                                'top': '0px',
                                'left': '0px',
                                'width': '100%',
                                'height': '100%'
                            }
                        );
                        loadingMask.show();
 
                        $.ajax({
                            url: $el.attr('data-url'),
                            type: 'POST',
                            headers: { accept: 'application/json' },
                            data: { _method: $el.data('method') },
                            success: function () {
                                loadingMask.hide().$el.remove();
                                const targetUrl = $el.attr('data-redirect-url');
                                router.redirect(targetUrl, {trigger: true});
                                messenger.notify('success', $el.attr('data-success-message'));
                            },
                            error: function (xhr) {
                                loadingMask.hide().$el.remove();
                                messenger.notify(
                                    'error',
                                    (xhr.responseJSON && xhr.responseJSON.message) ?
                                        xhr.responseJSON.message :
                                        $el.attr('data-error-message'));
                            }
                        });
                    };
                    $el.off('click');
 
                    Iif ($el.data('dialog') === 'confirm') {
                        Dialog.confirm(message, title, doAction, subTitle, '', buttonLabel);
                    } else Eif ($el.data('dialog') === 'delete') {
                        Dialog.confirmDelete(message, title, doAction, subTitle);
                    } else {
                        Dialog.alert(message, title, subTitle);
                    }
 
                    return false;
                });
 
                pageInit();
            });
            mediator.on('route_complete pim:reinit', function () {
                pageInit();
            });
        };
    }
);