All files / web/bundles/pimui/js/form/common column-tabs-navigation.js

100% Statements 22/22
100% Branches 4/4
100% Functions 10/10
100% Lines 22/22

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                        302x                             302x                               384x   384x             383x   383x   383x 383x   383x             2688x                             2176x           2176x 2176x   2176x                 129x 129x 129x                 258x               2688x   2688x             5376x 20620x            
'use strict';
 
/**
 * Display navigation links in column for the tab display
 *
 * Even if this module has the same design than `navigation-block`, it does not works like it, because this module is
 * not composed of extensions, but listen to the product edit form events to register its own tabs.
 *
 * @author    Pierre Allard <pierre.allard@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(
    [
        'jquery',
        'underscore',
        'oro/translator',
        'pim/form',
        'pim/template/form/column-tabs-navigation'
    ],
    function (
        $,
        _,
        __,
        BaseForm,
        template
    ) {
        return BaseForm.extend({
            className: 'AknColumn-block',
            template: _.template(template),
            tabs: [],
            currentTab: null,
            events: {
                'click .column-navigation-link': 'selectTab'
            },
            currentKey: 'current_column_tab',
 
            /**
             * @param {string} meta.config.title Translation key of the block title
             *
             * {@inheritdoc}
             */
            initialize: function (meta) {
                this.config = meta.config;
 
                return BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.tabs = [];
 
                this.currentTab = sessionStorage.getItem(this.currentKey);
 
                this.listenTo(this.getRoot(), 'column-tab:register', this.registerTab);
                this.listenTo(this.getRoot(), 'column-tab:select-tab', this.setCurrentTab);
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                this.$el
                    .empty()
                    .html(this.template({
                        tabs: this.getTabs(),
                        currentTab: this.getCurrentTabOrDefault(),
                        title: __(this.config.title)
                    }));
            },
 
            /**
             * Registers a new tab
             *
             * @param event
             */
            registerTab: function (event) {
                var tab = {
                    code: event.code,
                    isVisible: event.isVisible,
                    label: event.label,
                    route: event.code
                };
                this.tabs.push(tab);
                this.trigger('pim_menu:column:register_navigation_item', tab);
 
                this.render();
            },
 
            /**
             * Displays another tab
             *
             * @param event
             */
            selectTab: function (event) {
                this.getRoot().trigger('column-tab:select-tab', event);
                this.setCurrentTab(event.currentTarget.dataset.tab);
                this.render();
            },
 
            /**
             * Set the current tab
             *
             * @param {string} tabCode
             */
            setCurrentTab: function (tabCode) {
                this.currentTab = tabCode;
            },
 
            /**
             * Returns the current tab.
             * If there is no selected tab, returns the first available tab.
             */
            getCurrentTabOrDefault: function () {
                var result = _.findWhere(this.getTabs(), {code: this.currentTab});
 
                return (undefined !== result) ? result.code : _.first(_.pluck(this.tabs, 'code'));
            },
 
            /**
             * Returns the list of visible tabs
             */
            getTabs: function () {
                return _.filter(this.tabs, function (tab) {
                    return !_.isFunction(tab.isVisible) || tab.isVisible();
                });
            }
        });
    }
);