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

100% Statements 21/21
63.64% Branches 7/11
100% Functions 9/9
100% Lines 21/21

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                  1172x                                     1172x                     22220x   22220x 22220x   22220x                 22058x             22058x             77476x           77476x   77476x                 1x 1x 1x     1x     1x                   99535x                 99535x                 99534x                   67775x   67775x        
'use strict';
 
/**
 * Base extension for menu
 *
 * @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/item',
        'oro/mediator'
    ],
    function (
        _,
        __,
        BaseForm,
        router,
        Routing,
        template,
        mediator
    ) {
        return BaseForm.extend({
            template: _.template(template),
            events: {
                'click .navigation-item': 'redirect'
            },
            active: false,
 
            /**
             * {@inheritdoc}
             */
            initialize: function (config) {
                this.config = config.config;
 
                mediator.on('pim_menu:highlight:item', this.highlight, this);
                mediator.on('pim_menu:redirect:item', this.redirect, this);
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * On configure, this module triggers an event to register it to tabs.
             *
             * {@inheritdoc}
             */
            configure: function () {
                this.trigger('pim_menu:column:register_navigation_item', {
                    route: this.getRoute(),
                    label: this.getLabel(),
                    position: this.position,
                    routeParams: this.getRouteParams()
                });
 
                BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                this.$el.empty().append(this.template({
                    title: this.getLabel(),
                    url: Routing.generateHash(this.getRoute(), this.getRouteParams()),
                    active: this.active
                }));
 
                this.delegateEvents();
 
                return BaseForm.prototype.render.apply(this, arguments);
            },
 
            /**
             * Redirect the user to the config destination
             *
             * @param {Event} event
             */
            redirect: function (event) {
                Eif (!_.has(event, 'extension')) {
                    event.stopPropagation();
                    event.preventDefault();
                }
 
                Eif (!(event.metaKey || event.ctrlKey) &&
                    (!_.has(event, 'extension') || event.extension === this.code)
                ) {
                    router.redirectToRoute(this.getRoute(), this.getRouteParams());
                }
            },
 
            /**
             * Returns the route of the tab.
             *
             * @returns {string|undefined}
             */
            getRoute: function () {
                return this.config.to;
            },
 
            /**
             * Returns the route parameters.
             *
             * @returns {Object}
             */
            getRouteParams: function () {
                return this.config.routeParams !== 'undefined' ? this.config.routeParams : {};
            },
 
            /**
             * Returns the displayed label of the tab
             *
             * @returns {string}
             */
            getLabel: function () {
                return __(this.config.title);
            },
 
            /**
             * Highlight or un-highlight item
             *
             * @param {Event}  event
             * @param {string} event.extension The extension code to highlight
             */
            highlight: function (event) {
                this.active = (event.extension === this.code);
 
                this.render();
            }
        });
    });