All files / web/bundles/pimui/js/form/common column.js

96.3% Statements 26/27
100% Branches 12/12
91.67% Functions 11/12
96.3% Lines 26/27

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                1183x                             1183x                                     4431x 4431x   4431x             4366x   4366x             3100x   3100x 2602x           3100x 7x     3100x             15x                     3115x   3115x 3105x     10x                 22x   22x 19x   3x                   3137x                       24234x                 5702x 23754x                          
'use strict';
/**
 * Display a vertical column for navigation or filters
 *
 * @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(
    [
        'underscore',
        'oro/translator',
        'pim/form',
        'pim/template/form/column',
        'pim/template/form/column-navigation'
    ],
    function (
        _,
        __,
        BaseForm,
        template,
        navigationTemplate
    ) {
        return BaseForm.extend({
            className: 'AknColumn',
            template: _.template(template),
            navigationTemplate: _.template(navigationTemplate),
            events: {
                'click .AknColumn-collapseButton': 'toggleColumn',
                'click .navigation-link': 'redirect'
            },
            navigationItems: [],
 
            /**
             * @param {string} meta.config.navigationTitle Title of the navigation dropdown
             * @param {string} meta.config.stateCode       This is a key to identify each module using column, to
             *                 store if the column is collapsed or not. If you want to use the different collapsed
             *                 states, use different "stateCode" value.
             *
             * {@inheritdoc}
             */
            initialize: function (meta) {
                this.config = meta.config;
                this.navigationItems = [];
 
                return BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.onExtensions('pim_menu:column:register_navigation_item', this.registerNavigationItem);
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                this.$el.empty().append(this.template());
 
                if (!_.isEmpty(this.getNavigationItems())) {
                    this.$el.find('.column-inner').prepend(this.navigationTemplate({
                        navigationItems: this.getNavigationItems(),
                        title: __(this.config.navigationTitle)
                    }));
                }
 
                if (this.isCollapsed()) {
                    this.setCollapsed(true);
                }
 
                return BaseForm.prototype.render.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            toggleColumn: function () {
                this.setCollapsed(!this.isCollapsed());
            },
 
            /**
             * Returns true if the column is collapsed.
             * It uses the session storage with a key attached to this module.
             * If no key was found, returns false by default.
             *
             * @returns {boolean}
             */
            isCollapsed: function () {
                var result = sessionStorage.getItem(this.getSessionStorageKey());
 
                if (null === result) {
                    return false;
                }
 
                return '1' === result;
            },
 
            /**
             * Stores in the session storage if the column is collapsed or not.
             *
             * @param {boolean} value
             */
            setCollapsed: function (value) {
                sessionStorage.setItem(this.getSessionStorageKey(), value ? '1' : '0');
 
                if (value) {
                    this.$el.addClass('AknColumn--collapsed');
                } else {
                    this.$el.removeClass('AknColumn--collapsed');
                }
            },
 
            /**
             * Returns the key used by the session storage for this module.
             *
             * @returns {string}
             */
            getSessionStorageKey: function () {
                return 'collapsedColumn_' + this.config.stateCode;
            },
 
            /**
             * Registers a new item to display on navigation template
             *
             * @param {Event}    navigationItem
             * @param {string}   navigationItem.label
             * @param {function} navigationItem.isVisible
             * @param {string}   navigationItem.code
             */
            registerNavigationItem: function (navigationItem) {
                this.navigationItems.push(navigationItem);
            },
 
            /**
             * Returns the visible navigation items
             *
             * @returns {Array}
             */
            getNavigationItems: function () {
                return _.filter(this.navigationItems, function (navigationItem) {
                    return !_.isFunction(navigationItem.isVisible) || navigationItem.isVisible();
                });
            },
 
            /**
             * @param {Event} event
             */
            redirect: function (event) {
                this.getRoot().trigger('column-tab:select-tab', event);
            }
        });
    }
);