All files / web/bundles/pimui/js/menu tab.js

83.87% Statements 26/31
50% Branches 11/22
90.91% Functions 10/11
83.87% Lines 26/31

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                    1183x                                     1183x                         7093x 7093x   7093x 7093x   7093x             6967x   6967x               31361x   31361x 4x     31357x             31357x                                                                 31357x 15676x   15681x                   31357x 15676x   15681x                   31357x                   24396x   24396x                     132305x 22058x                   19171x        
'use strict';
 
/**
 * Base extension for tab
 * This represents a main tab of the application, associated with icon, text and column.
 *
 * @author    Julien Sanchez <julien@akeneo.com>
 * @copyright 2017 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
define(
    [
        'underscore',
        'oro/translator',
        'pim/form',
        'pim/router',
        'routing',
        'pim/template/menu/tab',
        'oro/mediator'
    ],
    function (
        _,
        __,
        BaseForm,
        router,
        Routing,
        template,
        mediator
    ) {
        return BaseForm.extend({
            template: _.template(template),
            events: {
                'click': 'redirect'
            },
            active: false,
            items: [],
            className: 'AknHeader-menuItemContainer',
 
            /**
             * {@inheritdoc}
             */
            initialize: function (config) {
                this.config = config.config;
                this.items = [];
 
                mediator.on('pim_menu:highlight:tab', this.highlight, this);
                mediator.on('pim_menu:redirect:tab', this.redirect, this);
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.listenTo(this.getRoot(), 'pim_menu:register_item', this.registerItem);
 
                BaseForm.prototype.configure.apply(this, arguments);
            },
 
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                this.$el.empty();
 
                if (!this.config.to && !this.hasChildren()) {
                    return this;
                }
 
                this.$el.append(this.template({
                    active: this.active,
                    title: this.getLabel(),
                    url: Routing.generateHash(this.getRoute(), this.getRouteParams()),
                    iconModifier: this.config.iconModifier
                }));
 
                return BaseForm.prototype.render.apply(this, arguments);
            },
 
            /**
             * Redirect the user to the config destination
             *
             * @param {Event} event
             */
            redirect: function (event) {
                if (!_.has(event, 'extension')) {
                    event.stopPropagation();
                    event.preventDefault();
                }
 
                if (!(event.metaKey || event.ctrlKey) &&
                    ((!_.has(event, 'extension') || event.extension === this.code) && undefined !== this.getRoute())
                ) {
                    router.redirectToRoute(this.getRoute(), this.getRouteParams());
                }
            },
 
            /**
             * Returns the route of the tab.
             *
             * There is 2 cases here:
             * - The configuration contains a `to` element, so we did a simple redirect to this route.
             * - There is no configuration, so we need to get the first available element of the associated column.
             *   For this, we simply register all the items of the column, sort them by priority then take the first
             *   one.
             *
             * @returns {string|undefined}
             */
            getRoute: function () {
                if (undefined !== this.config.to) {
                    return this.config.to;
                } else {
                    return _.first(_.sortBy(this.items, 'position')).route;
                }
            },
 
            /**
             * Returns the route parameters.
             *
             * @returns {json}
             */
            getRouteParams: function () {
                if (undefined !== this.config.to) {
                    return this.config.routeParams !== 'undefined' ? this.config.routeParams : {};
                } else {
                    return _.first(_.sortBy(this.items, 'position')).routeParams;
                }
            },
 
            /**
             * Returns the displayed label of the tab
             *
             * @returns {string}
             */
            getLabel: function () {
                return __(this.config.title);
            },
 
            /**
             * Highlight or un-highlight tab
             *
             * @param {Event} event
             * @param {string} event.extension The extension code to highlight
             */
            highlight: function (event) {
                this.active = (event.extension === this.code);
 
                this.render();
            },
 
            /**
             * Registers a new item attached to this tab.
             *
             * @param {Event}  event
             * @param {string} event.route
             * @param {number} event.position
             */
            registerItem: function (event) {
                if (event.target === this.code) {
                    this.items.push(event);
                }
            },
 
            /**
             * Does this tab have children elements
             *
             * @return {Boolean}
             */
            hasChildren: function () {
                return 0 < this.items.length;
            }
        });
    });