All files / web/bundles/pimui/js/mass-edit/form/product add-to-group.js

77.27% Statements 17/22
50% Branches 3/6
62.5% Functions 5/8
77.27% Lines 17/22

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                89x                                                 89x             3x             9x 9x   9x   9x                                   6x   6x         6x                                                       3x 3x 3x 3x   3x   3x       3x          
'use strict';
/**
 * Add to group 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',
        'pim/i18n',
        'pim/user-context',
        'pim/mass-edit-form/product/operation',
        'pim/fetcher-registry',
        'pim/common/property',
        'pim/template/mass-edit/product/add-to-group'
    ],
    function (
        $,
        _,
        __,
        messenger,
        i18n,
        UserContext,
        BaseOperation,
        FetcherRegistry,
        propertyAccessor,
        template
    ) {
        return BaseOperation.extend({
            template: _.template(template),
 
            /**
             * {@inheritdoc}
             */
            reset: function () {
                this.setValue([]);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                this.$el.html(this.template());
                this.renderExtensions();
 
                this.$el.find('input[name=group]').attr('disabled', this.readOnly ? 'disabled' : null);
 
                return this;
            },
 
            /**
             * Update the mass edit model
             *
             * @param {Event} event
             */
            updateModel: function (event) {
                this.transformValue(event.target.value, event.target.checked ? _.union : _.without);
            },
 
            /**
             * Update the model after dom event triggered
             *
             * @param {array} groups
             */
            setValue: function (groups) {
                var data = this.getFormData();
 
                data.actions = [{
                    field: 'groups',
                    value: groups
                }];
 
                this.setData(data);
            },
 
            /**
             * Transform dom event to proper group array
             *
             * @param {string}   group
             * @param {function} method
             */
            transformValue: function (group, method) {
                var value = this.getValue();
 
                this.setValue(method(value, [group]));
            },
 
            /**
             * Get current value from mass edit model
             *
             * @return {array}
             */
            getValue: function () {
                return _.findWhere(this.getFormData().actions, {field: 'group'});
            },
 
            /**
             * Checks there is at least one group selected to go to the next step
             */
            validate: function () {
                const data = this.getFormData();
                const groupsStr = propertyAccessor.accessProperty(data, 'group', '');
                const groups = groupsStr.split(',');
                this.setValue(groups);
 
                const hasUpdates = 0 !== groups.length;
 
                Iif (!hasUpdates) {
                    messenger.notify('error', __('pim_enrich.mass_edit.product.operation.add_to_group.no_update'));
                }
 
                return $.Deferred().resolve(hasUpdates);
            }
        });
    }
);