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

96.97% Statements 64/66
93.75% Branches 15/16
90.48% Functions 19/21
96.97% Lines 64/66

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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262                98x                                             98x         308x               119x   119x             119x           119x             466x         466x   132x 132x   88x 88x   246x 246x     466x 466x                               466x     466x   466x                 364x   4156x   3792x                           246x 2800x                   118x   118x 118x   118x   118x                 3045x                     639x 2358x                   311x   1x 1x   9x 9x 9x   118x 118x 118x     118x   118x 118x 118x   99x   99x 99x 99x 99x 88x 88x 88x 88x       99x   99x   84x 84x   84x           84x   84x                               84x     84x                          
'use strict';
/**
 * Mass edit root form
 *
 * @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',
        'pim/router',
        'routing',
        'oro/messenger',
        'pim/form/common/edit-form',
        'oro/loading-mask',
        'pim/template/mass-edit/form'
    ],
    function (
        $,
        _,
        __,
        router,
        Routing,
        messenger,
        BaseForm,
        LoadingMask,
        template
    ) {
        return BaseForm.extend({
            template: _.template(template),
            currentStep: 'choose',
            events: {
                'click .wizard-action': function (event) {
                    this.applyAction(event.target.dataset.actionTarget);
                }
            },
 
            /**
             * {@inheritdoc}
             */
            initialize: function (meta) {
                this.config = _.extend({}, meta.config);
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.listenTo(
                    this.getRoot(),
                    'mass-edit:navigate:action',
                    this.applyAction.bind(this)
                );
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                var step = this.currentStep === 'choose' ?
                    this.getChooseExtension() :
                    this.getOperationExtension(this.getCurrentOperation());
 
                var currentStepNumber;
                switch (this.currentStep) {
                    case 'configure':
                        currentStepNumber = 1;
                        break;
                    case 'confirm':
                        currentStepNumber = 2;
                        break;
                    default:
                        currentStepNumber = 0;
                        break;
                }
 
                const itemsCount = this.getFormData().itemsCount;
                this.$el.html(this.template({
                    currentStep: this.currentStep,
                    currentStepNumber: currentStepNumber,
                    currentOperation: this.getCurrentOperation(),
                    label: step.getLabel(),
                    description: step.getDescription(),
                    title: step.getTitle(),
                    labelCount: step.getLabelCount(),
                    confirm: __(this.config.confirm, {itemsCount}, itemsCount),
                    previousLabel: __('pim_common.previous'),
                    nextLabel: __('pim_common.next'),
                    confirmLabel: __('pim_common.confirm'),
                    illustrationClass: step.getIllustrationClass(),
                    __: __
                }));
 
                this.$('.step').empty().append(step.$el);
                // We need to have the step in the DOM as soon as possible for extensions that call render() and
                // postRender()
                step.render();
 
                this.delegateEvents();
            },
 
            /**
             * Provide the list of operations available
             *
             * @return {array}
             */
            getOperations: function () {
                return _.chain(this.extensions)
                    .filter(function (extension) {
                        return extension.options.config.label !== undefined;
                    }).map(function (extension) {
                        return {
                            code: extension.getCode(),
                            label: extension.getLabel(),
                            icon: extension.getIcon()
                        };
                    }).value();
            },
 
            /**
             * Get the chose extension
             *
             * @return {object}
             */
            getChooseExtension: function () {
                return _.filter(this.extensions, function (extension) {
                    return extension.targetZone === 'choose';
                })[0];
            },
 
            /**
             * The the porvided extension as the current one
             *
             * @param {object} operation
             */
            setCurrentOperation: function (operation) {
                var data = this.getFormData();
 
                data.operation = operation;
                data.jobInstanceCode = this.getOperationExtension(operation).getJobInstanceCode();
 
                this.setData(data);
 
                this.render();
            },
 
            /**
             * Provide the current oparation
             *
             * @return {string}
             */
            getCurrentOperation: function () {
                return this.getFormData().operation;
            },
 
            /**
             * Get the operation module corresponding to the given parameter
             *
             * @param {string} operationCode
             *
             * @return {object}
             */
            getOperationExtension: function (operationCode) {
                return _.find(this.extensions, (extension) => {
                    return typeof extension.getCode === 'function' && extension.getCode() === operationCode;
                });
            },
 
            /**
             * Apply the action triggered by a dom event
             *
             * @param {String} action
             */
            applyAction: function (action) {
                switch (action) {
                    case 'grid':
                        router.redirectToRoute(this.config.backRoute);
                        break;
                    case 'choose':
                        this.currentStep = 'choose';
                        this.render();
                        break;
                    case 'configure':
                        var operationView = this.getOperationExtension(this.getCurrentOperation());
                        Eif ('choose' === this.currentStep) {
                            operationView.reset();
                        }
 
                        this.currentStep = 'configure';
 
                        operationView.setReadOnly(false);
                        this.render();
                        break;
                    case 'confirm':
                        var operationView = this.getOperationExtension(this.getCurrentOperation());
 
                        var loadingMask = new LoadingMask();
                        loadingMask.render().$el.appendTo(this.getRoot().$el).show();
                        operationView.validate().then((isValid) => {
                            if (isValid) {
                                operationView.setReadOnly(true);
                                this.currentStep = 'confirm';
                                this.render();
                                this.getRoot().trigger('mass-edit:action:confirm');
                            }
                        })
                        .always(() => {
                            loadingMask.hide().$el.remove();
                        });
                        break;
                    case 'validate':
                        var loadingMask = new LoadingMask();
                        loadingMask.render().$el.appendTo(this.getRoot().$el).show();
 
                        $.ajax({
                            method: 'POST',
                            contentType: 'application/json',
                            url: Routing.generate('pim_enrich_mass_edit_rest_launch'),
                            data: JSON.stringify(this.getFormData())
                        }).then(() => {
                            router.redirectToRoute(this.config.backRoute);
 
                            messenger.notify(
                                'success',
                                __(
                                    this.config.launchedLabel,
                                    {
                                        operation: this.getOperationExtension(this.getCurrentOperation()).getLabel()
                                    }
                                )
                            );
                        })
                        .fail(() => {
                            messenger.notify(
                                'error', __(this.config.launchErrorLabel)
                            );
                        })
                        .always(() => {
                            loadingMask.hide().$el.remove();
                        });
 
                        break;
                }
            },
 
            /**
             * Disables the next button when the next step can not be accessed.
             */
            disableNextButton: function() {
                this.$el.find('.next').addClass('AknButton--disabled').removeClass('wizard-action');
            }
        });
    }
);