All files / web/bundles/pimui/js/mass-edit/form/family set-requirements.js

96.43% Statements 27/28
75% Branches 3/4
100% Functions 10/10
96.43% Lines 27/28

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                9x                                             9x               12x       12x         12x 6x 6x 6x 6x   6x       12x 12x 12x 12x     12x 12x     12x                 12x 12x   12x 15x 30x                       12x                   12x   12x   12x                 12x          
'use strict';
/**
 * Set attributes requirements operation
 *
 * @author    Julien Sanchez <julien@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',
        'routing',
        'pim/mass-edit-form/product/operation',
        'pim/user-context',
        'pim/form-builder',
        'pim/common/property',
        'pim/fetcher-registry',
        'pim/template/mass-edit/family/set-requirements'
    ],
    function (
        _,
        __,
        Routing,
        BaseOperation,
        UserContext,
        FormBuilder,
        propertyAccessor,
        FetcherRegistry,
        template
    ) {
        return BaseOperation.extend({
            template: _.template(template),
            formPromise: null,
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                Iif (null === this.getValue()) {
                    this.setValue([]);
                }
 
                var family = {
                    attributes: [],
                    attribute_requirements: {},
                    meta: {}
                };
                if (!this.formPromise) {
                    this.formPromise = FormBuilder.build('pim-mass-family-edit-form').then(function (form) {
                        form.setData(family);
                        form.trigger('pim_enrich:form:entity:post_fetch', family);
                        this.listenTo(form, 'pim_enrich:mass_edit:model_updated', this.updateModel);
 
                        return form;
                    }.bind(this));
                }
 
                this.formPromise.then(function (form) {
                    this.$el.html(this.template());
                    form.setElement(this.$('.set-requirements')).render();
                    form.trigger('pim_enrich:form:update_read_only', this.readOnly);
 
                    // This method renders a complete PEF page, we need to remove useless elements manually.
                    this.$el.find('.navigation').remove();
                    this.$el.find('.AknDefault-mainContent').addClass('AknDefault-mainContent--withoutPadding');
                }.bind(this));
 
                return this;
            },
 
            /**
             * Update the mass edit model each time a requirement is changed
             *
             * @param {object} data
             */
            updateModel: function (data) {
                FetcherRegistry.getFetcher('channel').fetchAll().then(function (channels) {
                    var attributeRequirements = [];
 
                    _.each(data.attributes, function (attributeCode) {
                        _.each(channels, function (channel) {
                            attributeRequirements.push({
                                attribute_code: attributeCode,
                                channel_code: channel.code,
                                is_required: _.contains(propertyAccessor.accessProperty(
                                    data.attribute_requirements,
                                    channel.code,
                                    []
                                ), attributeCode)
                            });
                        });
                    });
 
                    this.setValue(attributeRequirements);
                }.bind(this));
            },
 
            /**
             * Update the general model
             *
             * @param {Object} values
             */
            setValue: function (values) {
                var data = this.getFormData();
 
                data.actions = values;
 
                this.setData(data);
            },
 
            /**
             * Get the value of the model
             *
             * @return {object}
             */
            getValue: function () {
                return this.getFormData().actions;
            }
        });
    }
);