All files / web/bundles/pimdatagrid/js/datagrid/action mass-delete-action.js

95.45% Statements 21/22
100% Branches 0/0
90.91% Functions 10/11
95.45% Lines 21/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 123 124 125 126 127 128 129 130 131 132 133 134  299x                                           299x                       1369x   1369x             5x   5x                   5x 5x 5x 5x   5x       5x                                 5x             5x                 5x 5x   5x             5x   5x 5x                               5x            
/* global define */
define([
        'jquery',
        'underscore',
        'oro/translator',
        'routing',
        'oro/datagrid/mass-action',
        'pim/router',
        'oro/messenger',
        'oro/loading-mask',
        'pim/dialog'
    ],
    function($, _, __, Routing, MassAction, router, messenger, LoadingMask, Dialog) {
        'use strict';
 
        /**
         * Mass delete action
         *
         * @export  oro/datagrid/mass-delete-action
         * @class   oro.datagrid.MassDeleteAction
         *
         * @extends oro.datagrid.MassAction
         */
        return MassAction.extend({
 
            /** @type oro.Modal */
            errorModal: undefined,
 
            /** @type oro.Modal */
            confirmModal: undefined,
 
            /** @type {Object} */
            config: undefined,
 
            initialize: function(options) {
                this.config = __moduleConfig;
 
                MassAction.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * Displays a confirm dialog and mass delete if action is confirmed.
             */
            execute: function() {
                this.getData().then((data) => {
 
                    this.getConfirmDialog(data);
                });
            },
 
            /**
             * Converts grid data into pqb filters and gathers job instance code, actions and items count.
             *
             * @return {Promise}
             */
            getData: function () {
                let actionParameters = this.getActionParameters();
                actionParameters.actionName = this.route_parameters['actionName'];
                actionParameters.gridName = this.route_parameters['gridName'];
                const query = `?${$.param(actionParameters)}`;
 
                return $.ajax({
                    url: Routing.generate('pim_enrich_mass_edit_rest_get_filter') + query,
                    method: 'POST'
                }).then((response) => {
                    return {
                        filters: response.filters,
                        jobInstanceCode: this.config.jobInstanceCode,
                        actions: [this.route_parameters['actionName']],
                        itemsCount: response.itemsCount
                    };
                });
            },
 
            /**
             * Get view for confirm modal.
             *
             * @param {Object} data
             *
             * @return {oro.Modal}
             */
            getConfirmDialog: function(data) {
                this.confirmModal = Dialog.confirmDelete(
                    __(this.config.confirmLabel),
                    __('pim_common.confirm_deletion'),
                    this.doMassDelete.bind(this, data),
                    this.getEntityHint(true)
                );
 
                return this.confirmModal;
            },
 
            /**
             * Sends request to mass delete items.
             *
             * @param {Object} data
             */
            doMassDelete: function(data) {
                const loadingMask = new LoadingMask();
                loadingMask.render().$el.appendTo($('.hash-loading-mask')).show();
 
                $.ajax({
                    method: 'POST',
                    contentType: 'application/json',
                    url: Routing.generate(this.config.route),
                    data: JSON.stringify(data)
                })
                .then(() => {
                    router.redirectToRoute(this.config.backRoute);
 
                    const translatedAction = __('pim_datagrid.mass_action.mass_delete');
                    messenger.notify(
                        'success',
                        __(
                            this.config.launchedLabel,
                            {
                                operation: translatedAction
                            }
                        )
                    );
                })
                .fail(() => {
                    messenger.notify(
                        'error', __(this.config.launchErrorLabel)
                    );
                })
                .always(() => {
                    loadingMask.hide().$el.remove();
                });
            }
        });
    }
);