All files / web/bundles/pimui/js/product/form product-completeness.js

61.54% Statements 32/52
54.55% Branches 12/22
81.82% Functions 9/11
61.54% Lines 32/52

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 204 205 206 207 208 209 210 211 212 213 214 215 216 217                  265x                                     265x                     322x 33x 33x     322x 22x 19x       322x   322x             322x   322x                     523x         523x   523x 523x 398x                       125x     523x                     1319x                         921x 921x 122x     799x 799x 3x     796x                         398x 398x       398x 198x     200x                                                                                                                                
'use strict';
/**
 * Product completeness extension
 * Displays the global completeness of the product.
 *
 * @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/router',
        'pim/form',
        'pim/i18n',
        'pim/user-context',
        'pim/template/product/form/product-completeness'
    ],
    function (
        _,
        __,
        router,
        BaseForm,
        i18n,
        UserContext,
        template
    ) {
        return BaseForm.extend({
            className: 'AknDropdown AknButtonList-item',
            template: _.template(template),
            events: {
                'click .missing-attribute': 'showAttribute'
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.listenTo(this.getRoot(), 'pim_enrich:form:scope_switcher:change', function (scopeEvent) {
                    Eif ('base_product' === scopeEvent.context) {
                        this.renderCompleteness({ scope: scopeEvent.scopeCode });
                    }
                }.bind(this));
                this.listenTo(this.getRoot(), 'pim_enrich:form:locale_switcher:change', function (localeEvent) {
                    if ('base_product' === localeEvent.context) {
                        this.renderCompleteness({ locale: localeEvent.localeCode});
                    }
                }.bind(this));
 
                this.listenTo(this.getRoot(), 'pim_enrich:form:entity:post_fetch', this.renderCompleteness.bind(this));
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritDoc}
             */
            render: function() {
                this.renderCompleteness();
 
                return BaseForm.prototype.render.apply(this, arguments);
            },
 
            /**
             * {@inheritDoc}
             *
             * @param options Object
             * @param options.locale String
             * @param options.scope  String
             */
            renderCompleteness: function (event) {
                const options = Object.assign({}, {
                    locale: UserContext.get('catalogLocale'),
                    scope: UserContext.get('catalogScope')
                }, event);
 
                this.$el.empty();
 
                const ratio = this.getCurrentRatio(options);
                if (null !== ratio) {
                    this.$el.append(this.template({
                        __: __,
                        label: __('pim_enrich.entity.product.module.completeness.complete'),
                        ratio: ratio,
                        completenesses: this.getCurrentCompletenesses(options.scope),
                        badgeClass: this.getBadgeClass(options),
                        currentLocale: options.locale,
                        missingValues: 'pim_enrich.entity.product.module.completeness.missing_values',
                        i18n: i18n
                    }));
                } else {
                    // We drop the element for design issues, to avoid blank spaces.
                    this.$el.remove();
                }
 
                return this;
            },
 
            /**
             * Returns the completeness of the current scope
             *
             * @param scope String
             *
             * @return Object
             */
            getCurrentCompletenesses: function (scope) {
                return _.findWhere(this.getFormData().meta.completenesses, {channel: scope});
            },
 
            /**
             * Returns the ratio of the current scope and current locale
             *
             * @param options Object
             * @param options.locale String
             * @param options.scope  String
             *
             * @returns number|null
             */
            getCurrentRatio: function (options) {
                const completenesses = this.getCurrentCompletenesses(options.scope);
                if (undefined === completenesses) {
                    return null;
                }
 
                const completeness = completenesses.locales[options.locale];
                if (undefined === completeness) {
                    return null;
                }
 
                return Math.round(completeness.completeness.ratio);
            },
 
            /**
             * Returns the HTML class for the badge from the completeness ratio
             *
             * @param options Object
             * @param options.locale String
             * @param options.scope  String
             *
             * @returns string
             */
            getBadgeClass: function(options) {
                const ratio = this.getCurrentRatio(options);
                Iif (ratio <= 0) {
                    return 'AknBadge--important';
                }
 
                if (ratio >= 100) {
                    return 'AknBadge--enabled';
                }
 
                return 'AknBadge--warning';
            },
 
            /**
             * Set focus to the attribute given by the event
             *
             * @param event Event
             */
            showAttribute: function (event) {
                this.getRoot().trigger(
                    'pim_enrich:form:locale_switcher:change',
                    {
                        localeCode: event.currentTarget.dataset.locale,
                        context: 'base_product'
                    }
                );
 
                const product = this.getFormData();
                const familyVariant = product.meta.family_variant;
                const attributeCode = event.currentTarget.dataset.attribute;
 
                if (null !== familyVariant) {
                    if (!product.meta.attributes_for_this_level.includes(attributeCode)) {
                        let modelId = product.meta.variant_navigation[0].selected.id;
                        const comesFromParent = product.meta.parent_attributes.includes(attributeCode);
                        const hasTwoLevelsOfVariation = (3 === product.meta.variant_navigation.length);
 
                        if (comesFromParent && hasTwoLevelsOfVariation) {
                            modelId = product.meta.variant_navigation[1].selected.id;
                        }
 
                        this.redirectToModel(modelId);
 
                        return;
                    }
                }
 
                this.getRoot().trigger(
                    'pim_enrich:form:show_attribute',
                    {
                        attribute: event.currentTarget.dataset.attribute,
                        locale: event.currentTarget.dataset.locale,
                        scope: UserContext.get('catalogScope')
                    }
                );
                this.renderCompleteness();
            },
 
            /**
             * Redirect to the product model with the modelId
             *
             * @param modelId
             */
            redirectToModel: function(modelId) {
                const params = {id: modelId};
                const route = 'pim_enrich_product_model_edit';
 
                sessionStorage.setItem('filter_missing_required_attributes', true);
 
                router.redirectToRoute(route, params);
            }
        });
    }
);