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

94.59% Statements 35/37
84.62% Branches 11/13
91.67% Functions 11/12
94.59% Lines 35/37

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                  354x                                             354x                     458x 458x 458x 458x           458x                 148x   148x 148x 108x     148x 148x   148x 148x 96x       96x       96x       148x 93x                   230x             1949x     1947x 1947x 1947x   1947x 3332x           1947x 1947x 1947x 1741x                         1947x     1949x                     1947x   1947x                            
'use strict';
/**
 * Attribute 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/form/common/group-selector',
        'pim/attribute-group-manager',
        'pim/template/form/tab/attribute/attribute-group-selector',
        'pim/user-context',
        'pim/i18n',
        'pim/provider/to-fill-field-provider'
    ],
    function (
        $,
        _,
        __,
        GroupSelectorForm,
        AttributeGroupManager,
        template,
        UserContext,
        i18n,
        toFillFieldProvider
    ) {
        return GroupSelectorForm.extend({
            tagName: 'div',
 
            className: 'AknDropdown AknButtonList-item nav nav-tabs group-selector',
 
            template: _.template(template),
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.listenTo(this.getRoot(), 'pim_enrich:form:entity:validation_error', this.onValidationError);
                this.listenTo(this.getRoot(), 'pim_enrich:form:entity:post_fetch', this.onPostFetch);
                this.listenTo(this.getRoot(), 'pim_enrich:form:to-fill:cleared', this.render);
                this.listenTo(
                    this.getRoot(),
                    'pim_enrich:form:switch_attribute_group',
                    this.setAttributeGroup.bind(this)
                );
 
                return GroupSelectorForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * Triggered on validation error
             *
             * @param {Event} event
             */
            onValidationError: function (event) {
                this.removeBadges();
 
                var object = event.sentData;
                var valuesErrors = _.uniq(event.response.values, function (error) {
                    return JSON.stringify(error);
                });
 
                Eif (valuesErrors) {
                    AttributeGroupManager.getAttributeGroupsForObject(object)
                        .then(function (attributeGroups) {
                            var globalErrors = [];
                            _.each(valuesErrors, function (error) {
                                Iif (error.global) {
                                    globalErrors.push(error);
                                }
 
                                var attributeGroup = AttributeGroupManager.getAttributeGroupForAttribute(
                                    attributeGroups,
                                    error.attribute
                                );
                                this.addToBadge(attributeGroup, 'invalid');
                            }.bind(this));
 
                            // Don't force attributes tab if only global errors
                            if (!_.isEmpty(valuesErrors) && valuesErrors.length > globalErrors.length) {
                                this.getRoot().trigger('pim_enrich:form:show_attribute', _.first(valuesErrors));
                            }
                        }.bind(this));
                }
            },
 
            /**
             * Triggered on post fetch
             */
            onPostFetch: function () {
                this.removeBadges();
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                $.when(
                    AttributeGroupManager.getAttributeGroupsForObject(this.getFormData())
                ).then(function (attributeGroups) {
                    const scope = UserContext.get('catalogScope');
                    const locale = UserContext.get('catalogLocale');
                    const attributes = toFillFieldProvider.getMissingRequiredFields(this.getFormData(), scope, locale);
 
                    const toFillAttributeGroups = _.uniq(_.map(attributes, function (attribute) {
                        return AttributeGroupManager.getAttributeGroupForAttribute(
                            attributeGroups,
                            attribute
                        );
                    }));
 
                    this.$el.empty();
                    this.ensureDefault();
                    if (this.shouldBeDisplayed(this.getElements())) {
                        this.$el.html(this.template({
                            current: this.getCurrent(),
                            elements: _.sortBy(this.getElements(), 'sort_order'),
                            badges: this.badges,
                            locale: UserContext.get('catalogLocale'),
                            toFillAttributeGroups: toFillAttributeGroups,
                            allAttributeCode: this.all.code,
                            currentElement: _.findWhere(this.getElements(), {code: this.getCurrent()}),
                            i18n: i18n,
                            label: __('pim_enrich.entity.attribute_group.uppercase_label')
                        }));
                    }
 
                    this.delegateEvents();
                }.bind(this));
 
                return this;
            },
 
            /**
             * Don't display the dropdown if there is no elements or if the only element is the "All" group.
             *
             * @param {Object} elements
             *
             * @returns {Boolean}
             */
            shouldBeDisplayed: function (elements) {
                const length = Object.keys(elements).length;
 
                return length > 1 || (1 === length && this.all.code !== Object.values(elements)[0].code);
            },
 
            /**
             * Set current group from event
             *
             * @param {[type]} attributeGroupCode [description]
             */
            setAttributeGroup: function (attributeGroupCode) {
                this.setCurrent(attributeGroupCode, {silent: true});
            }
        });
    }
);