All files / web/bundles/pimui/js/mass-edit/form/product edit-common-attributes.js

95.24% Statements 40/42
80% Branches 8/10
100% Functions 13/13
95.24% Lines 40/42

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                89x                                                         89x                       220x         220x 220x               76x             142x         142x 75x 75x 75x 75x   75x       142x 142x 142x 142x     142x 142x   142x         142x 66x         66x       142x                 235x             131x                                     311x   311x             311x                 208x   208x                 66x 66x   66x         66x           66x 11x   11x   55x     66x              
'use strict';
/**
 * Edit common attributes 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(
    [
        'jquery',
        'underscore',
        'oro/translator',
        'oro/messenger',
        'routing',
        'pim/mass-edit-form/product/operation',
        'pim/user-context',
        'pim/form-builder',
        'pim/fetcher-registry',
        'pim/i18n',
        'pim/common/property',
        'pim/template/mass-edit/product/edit-common-attributes'
    ],
    function (
        $,
        _,
        __,
        messenger,
        Routing,
        BaseOperation,
        UserContext,
        FormBuilder,
        FetcherRegistry,
        i18n,
        propertyAccessor,
        template
    ) {
        return BaseOperation.extend({
            className: 'AknGridContainer--withoutNoDataPanel',
            template: _.template(template),
            errors: null,
            formPromise: null,
            channel: null,
            locales: [],
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                return $.when(
                    FetcherRegistry.getFetcher('channel')
                        .fetch(UserContext.get('catalogScope'), {force_list_method: true}),
                    FetcherRegistry.getFetcher('locale').search({ activated: true, cached: false })
                ).then((channel, locales) => {
                    this.channel = channel;
                    this.locales = locales;
                });
            },
 
            /**
             * {@inheritdoc}
             */
            reset: function () {
                this.setValue({});
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                var product = {
                    meta: {},
                    values: this.getValue()
                };
 
                if (!this.formPromise) {
                    this.formPromise = FormBuilder.build(this.config.innerForm).then(function (form) {
                        form.setData(product);
                        form.trigger('pim_enrich:form:entity:post_fetch', product);
                        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.$('.edit-common-attributes')).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-thirdColumnContainer').remove();
 
                    this.$el.find('.AknDefault-mainContent')
                        .addClass('AknDefault-mainContent--withoutPadding')
                        .css({'overflow-x': 'hidden'})
                    ;
 
                    if (this.errors) {
                        const event = {
                            sentData: product,
                            response: {values: this.errors}
                        };
 
                        form.trigger('pim_enrich:form:entity:bad_request', event);
                    }
                }.bind(this));
 
                return this;
            },
 
            /**
             * Update the mass edit model
             *
             * @param {Event} event
             */
            updateModel: function (event) {
                this.setValue(event.values);
            },
 
            /**
             * {@inheritdoc}
             */
            getDescription: function () {
                return __(
                    this.config.description,
                    {
                        locale: _.findWhere(this.locales, {code: UserContext.get('catalogLocale')}).label,
                        scope: i18n.getLabel(
                            this.channel.labels,
                            UserContext.get('catalogLocale'),
                            this.channel.code
                        )
                    }
                );
            },
 
            /**
             * Update the model after dom event triggered
             *
             * @param {string} group
             */
            setValue: function (values) {
                var data = this.getFormData();
 
                data.actions = [{
                    normalized_values: values,
                    ui_locale: UserContext.get('uiLocale'),
                    attribute_locale: UserContext.get('catalogLocale'),
                    attribute_channel: UserContext.get('catalogScope')
                }];
 
                this.setData(data);
            },
 
            /**
             * Get current value from mass edit model
             *
             * @return {string}
             */
            getValue: function () {
                var action = _.first(this.getFormData().actions);
 
                return action ? action.normalized_values : null;
            },
 
            /**
             * Validate the model before confirmation
             *
             * @return {Promise}
             */
            validate: function () {
                const data = this.getFormData();
                const actions = propertyAccessor.accessProperty(data, 'actions.0.normalized_values', {});
 
                Iif (0 === Object.keys(actions).length) {
                    messenger.notify('error', __('pim_enrich.mass_edit.product.operation.edit_common.no_update'));
 
                    return $.Deferred().resolve(false);
                } else {
                    return $.ajax({
                        method: 'POST',
                        contentType: 'application/json',
                        data: JSON.stringify(this.getValue()),
                        url: Routing.generate('pim_enrich_value_rest_validate')
                    }).then(function (response) {
                        if (!_.isEmpty(response.values)) {
                            this.errors = response.values;
 
                            this.render();
                        } else {
                            this.errors = {};
                        }
 
                        return _.isEmpty(this.errors);
                    }.bind(this));
                }
            }
        });
    }
);