All files / web/bundles/pimui/js/form/common group-selector.js

83.72% Statements 36/43
88.89% Branches 16/18
88.89% Functions 16/18
83.72% Lines 36/43

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 198 199 200 201 202 203                  354x                       354x                             459x 459x   459x             458x 458x 458x           458x 1071x                                                           1054x 1054x 1054x                 162x                 11212x                   527x   527x 436x   436x 71x     436x               3001x     365x     365x                   147x                 8577x                   96x 93x   96x 93x     96x   96x                 378x 378x             378x       1054x          
'use strict';
/**
 * Group selector 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',
        'oro/translator',
        'pim/user-context',
        'pim/form',
        'oro/mediator',
        'pim/fetcher-registry',
        'pim/template/form/group-selector'
    ],
    function ($, _, __, UserContext, BaseForm, mediator, fetcherRegistry, template) {
        return BaseForm.extend({
            tagName: 'ul',
            className: 'AknVerticalNavtab nav nav-tabs group-selector',
            all: {},
            template: _.template(template),
            elements: [],
            badges: {},
            events: {
                'click li': 'change'
            },
 
            /**
             * {@inheritdoc}
             */
            initialize: function () {
                this.badges   = {};
                this.elements = [];
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                return BaseForm.prototype.configure.apply(this, arguments).then(() => {
                    return fetcherRegistry.getFetcher('locale').fetchActivated().then((locales) => {
                        this.all = {
                            code: 'all_attribute_groups',
                            labels: {},
                            sort_order: -1
                        };
 
                        locales.forEach((locale) => {
                            this.all.labels[locale.code] = __(
                                'pim_common.all'
                            );
                        });
                    });
                });
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                this.$el.empty();
                this.$el.html(this.template({
                    current: this.getCurrent(),
                    elements: this.getElements(),
                    badges: this.badges
                }));
 
                this.delegateEvents();
 
                return this;
            },
 
            /**
             * Set the element collection
             *
             * @param {Array} elements
             */
            setElements: function (elements) {
                this.elements = elements;
                this.elements[this.all.code] = this.all;
                this.ensureDefault();
            },
 
            /**
             * On attribute group change
             *
             * @param {Event} event
             */
            change: function (event) {
                this.setCurrent(event.currentTarget.dataset.element);
            },
 
            /**
             * Get current attribute group
             *
             * @return {String}
             */
            getCurrent: function () {
                return sessionStorage.getItem('current_select_group_' + this.code);
            },
 
            /**
             * Set current attribute group
             *
             * @param {String} current
             * @param {Object} options
             */
            setCurrent: function (current, options) {
                options = options || {silent: false};
 
                if (current !== this.getCurrent()) {
                    sessionStorage.setItem('current_select_group_' + this.code, current);
 
                    if (!options.silent) {
                        this.trigger('group:change');
                    }
 
                    this.render();
                }
            },
 
            /**
             * Ensure default values for the current attribute group
             */
            ensureDefault: function () {
                if (_.isUndefined(this.getCurrent()) ||
                    !this.getElements()[this.getCurrent()]
                ) {
                    Iif (!this.elements[this.all.code]) {
                        this.elements[this.all.code] = this.all;
                    }
                    this.setCurrent(this.all.code, {silent: true});
                }
            },
 
            /**
             * Get the current attribute group
             *
             * @return {String}
             */
            getCurrentElement: function () {
                return this.getElements()[this.getCurrent()];
            },
 
            /**
             * Get all attribute groups
             *
             * @return {object}
             */
            getElements: function () {
                return this.elements;
            },
 
            /**
             * Increment count on attribute group for the given code
             *
             * @param {String} element
             * @param {String} code
             */
            addToBadge: function (element, code) {
                if (!this.badges[element]) {
                    this.badges[element] = {};
                }
                if (!this.badges[element][code]) {
                    this.badges[element][code] = 0;
                }
 
                this.badges[element][code]++;
 
                this.render();
            },
 
            /**
             * Remove badges for all attribute groups
             *
             * @param {String} code
             */
            removeBadges: function (code) {
                Eif (!code) {
                    this.badges = {};
                } else {
                    _.each(this.badges, function (badge) {
                        delete badge[code];
                    }.bind(this));
                }
 
                this.render();
            },
 
            isAll: function () {
                return this.getCurrent() === this.all.code;
            }
        });
    }
);