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

95.92% Statements 47/49
92.59% Branches 25/27
100% Functions 13/13
95.92% Lines 47/49

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 190 191 192 193 194 195 196 197                  546x                 546x                                         708x     708x   708x             706x 706x   706x                 3230x           3230x             4923x 3230x     1693x 1693x   1693x             1693x 1693x   1693x 1693x 1693x 1693x 1693x     1693x 1693x         1693x                 3386x 3386x 15048x     3386x                 342x                 1033x 873x 873x     1033x                 9257x             1693x   1693x     2x   2x     1693x 1693x 1693x 595x                       1693x 2429x     2429x   2429x            
'use strict';
/**
 * Form tabs extension
 *
 * @author    Julien Sanchez <julien@akeneo.com>
 * @author    Filips Alpe <filips@akeneo.com>
 * @copyright 2015 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
define(
    [
        'jquery',
        'underscore',
        'backbone',
        'pim/form',
        'pim/template/form/form-tabs'
    ],
    function ($, _, Backbone, BaseForm, template) {
        return BaseForm.extend({
            template: _.template(template),
 
            className: 'tabbable tabs-top',
 
            tabs: [],
 
            urlParsed: false,
 
            events: {
                'click header ul.nav-tabs li': 'selectTab'
            },
 
            currentKey: 'current_form_tab',
 
            config: {},
 
            /**
             * {@inheritdoc}
             */
            initialize: function (meta) {
                this.config = _.extend({}, {
                    centered: false
                }, meta.config);
                this.tabs = [];
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.onExtensions('tab:register', this.registerTab.bind(this));
                this.listenTo(this.getRoot(), 'pim_enrich:form:form-tabs:change', this.setCurrentTab);
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * Register a tab into the form tab extension
             *
             * @param {Event} event
             */
            registerTab: function (event) {
                this.tabs.push({
                    code: event.code,
                    isVisible: event.isVisible,
                    label: event.label
                });
 
                this.render();
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                if (!this.configured || _.isEmpty(this.tabs)) {
                    return this;
                }
 
                var tabs = this.getTabs();
                this.ensureDefault();
 
                this.$el.html(
                    this.template({
                        tabs: tabs,
                        currentTab: this.getCurrentTab(),
                        centered: this.config.centered
                    })
                );
                this.delegateEvents();
                this.initializeDropZones();
 
                var currentTab = this.getTabExtension(this.getCurrentTab());
                Eif (currentTab) {
                    var zone = this.getZone('container');
                    zone.appendChild(currentTab.el);
                    this.renderExtension(currentTab);
                }
 
                var panelsExtension = this.getExtension('panels');
                Iif (panelsExtension) {
                    this.getZone('panels').appendChild(panelsExtension.el);
                    this.renderExtension(panelsExtension);
                }
 
                return this;
            },
 
            /**
             * Get visible tabs
             *
             * @return {Array}
             */
            getTabs: function () {
                var tabs = _.clone(this.tabs);
                tabs = _.filter(tabs, function (tab) {
                    return !_.isFunction(tab.isVisible) || tab.isVisible();
                });
 
                return tabs;
            },
 
            /**
             * Select a tab in the form-tabs
             *
             * @param {Event} event
             */
            selectTab: function (event) {
                this.setCurrentTab(event.currentTarget.dataset.tab);
            },
 
            /**
             * Set the current tab and ask render if needed
             *
             * @param {string} tab
             */
            setCurrentTab: function (tab) {
                if (this.getCurrentTab() !== tab) {
                    sessionStorage.setItem(this.currentKey, tab);
                    this.render();
                }
 
                return this;
            },
 
            /**
             * get the current tab
             *
             * @return {string}
             */
            getCurrentTab: function () {
                return sessionStorage.getItem(this.currentKey);
            },
 
            /**
             * Ensure default value for the current tab
             */
            ensureDefault: function () {
                var tabs = this.getTabs();
 
                if (!_.isNull(sessionStorage.getItem('redirectTab')) &&
                    _.findWhere(tabs, {code: sessionStorage.getItem('redirectTab').substring(1)})
                ) {
                    this.setCurrentTab(sessionStorage.redirectTab.substring(1));
 
                    sessionStorage.removeItem('redirectTab');
                }
 
                var currentTabIsNotDefined = _.isNull(this.getCurrentTab());
                var currentTabDoesNotExist = !_.findWhere(tabs, {code: this.getCurrentTab()});
                if ((currentTabIsNotDefined || currentTabDoesNotExist) && _.first(tabs)) {
                    this.setCurrentTab(_.first(tabs).code);
                }
            },
 
            /**
             * Get a child tab extension
             *
             * @param {string} code
             *
             * @return {Object}
             */
            getTabExtension: function (code) {
                return this.extensions[_.find(this.extensions, function (extension) {
                    var extensionCode = extension.config && extension.config.tabCode ?
                        extension.config.tabCode :
                        extension.code;
                    var expectedPosition = extensionCode.length - code.length;
 
                    return expectedPosition >= 0 && expectedPosition === extensionCode.indexOf(code, expectedPosition);
                }).code];
            }
        });
    }
);