All files / web/bundles/pimui/js/product-model/form/add-child header.js

100% Statements 14/14
100% Branches 0/0
100% Functions 6/6
100% Lines 14/14

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                    15x                                         15x             35x   35x       35x       35x   35x 35x 82x     35x                                         35x 35x 42x     35x          
'use strict';
 
/**
 * Form header of the product model child creation modal.
 *
 * @author    Damien Carcel <damien.carcel@akeneo.com>
 * @author    Yohan Blain <yohan.blain@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(
    [
        'pim/form',
        'jquery',
        'underscore',
        'oro/translator',
        'pim/i18n',
        'pim/user-context',
        'pim/fetcher-registry',
        'pim/template/product-model-edit-form/add-child-form-header'
    ],
    (
        BaseForm,
        $,
        _,
        __,
        i18n,
        UserContext,
        FetcherRegistry,
        template
    ) => {
        return BaseForm.extend({
            template: _.template(template),
 
            /**
             * {@inheritdoc}
             */
            render() {
                const parentCode = this.getFormData().parent;
 
                FetcherRegistry
                    .getFetcher('product-model-by-code')
                    .fetch(parentCode)
                    .then((parent) => {
                        FetcherRegistry
                            .getFetcher('family-variant')
                            .fetch(parent.family_variant)
                            .then((familyVariant) => {
                                this.getAxesAttributes(familyVariant, parent.meta.level + 1)
                                    .then((axesAttributes) => {
                                        const catalogLocale = UserContext.get('catalogLocale');
                                        const axesLabels = axesAttributes.map((attribute) => {
                                            return i18n.getLabel(attribute.labels, catalogLocale, attribute.code);
                                        });
 
                                        this.$el.html(
                                            this.template({
                                                __: __,
                                                axes: axesLabels.sort().join(', ')
                                            })
                                        );
                                    });
                            });
                    });
            },
 
            /**
             * Looks for the attributes set corresponding to the specified level of the family variant
             * and fetches its axes attributes.
             *
             * @param {Object} familyVariant
             * @param {Number} level
             *
             * @returns {Promise}
             */
            getAxesAttributes(familyVariant, level) {
                const variantAttributeSets = familyVariant.variant_attribute_sets;
                const variantAttributeSetForLevel = variantAttributeSets.find(
                    (variantAttributeSet) => variantAttributeSet.level === level
                );
 
                return FetcherRegistry.getFetcher('attribute').fetchByIdentifiers(variantAttributeSetForLevel.axes);
            }
        });
    }
);