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

91.49% Statements 43/47
80.77% Branches 21/26
81.82% Functions 9/11
91.49% Lines 43/47

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  326x                     326x                       9633x 9633x   9633x             134x 134x     134x                     145x 145x   145x 337x   145x         145x 132x   13x     145x   145x   145x   145x 9x 9x     145x 145x     145x                 154x 154x 154x   154x                     145x   145x 145x 145x     145x                   145x   145x 136x         145x                   145x   145x 145x 142x       145x                                                    
/* global define */
define(['underscore', 'oro/messenger', 'oro/translator', 'pim/dialog', 'oro/datagrid/abstract-action'],
function(_, messenger, __, Dialog, AbstractAction) {
    'use strict';
 
    /**
     * Basic mass action class.
     *
     * @export  oro/datagrid/mass-action
     * @class   oro.datagrid.MassAction
     * @extends oro.datagrid.AbstractAction
     */
    return AbstractAction.extend({
        /** @property {Object} */
        defaultMessages: {
            confirm_title: __('pim_datagrid.mass_action.default.confirmation.title'),
            confirm_content: __('pim_datagrid.mass_action.default.confirmation.content'),
            confirm_ok: __('pim_common.yes'),
            success: __('pim_datagrid.mass_action.default.success'),
            error: __('pim_datagrid.mass_action.default.error'),
            empty_selection: __('pim_datagrid.mass_action.default.no_items')
        },
 
        initialize: function(options) {
            AbstractAction.prototype.initialize.apply(this, arguments);
            this.route_parameters = _.extend(this.route_parameters, {gridName: this.datagrid.name, actionName: this.name});
 
            _.defaults(this.messages, this.defaultMessages);
        },
 
        /**
         * Ask a confirmation and execute mass action.
         */
        execute: function() {
            var selectionState = this.datagrid.getSelectionState();
            Iif (_.isEmpty(selectionState.selectedModels) && selectionState.inset) {
                messenger.notify('warning', this.messages.empty_selection);
            } else {
                AbstractAction.prototype.execute.call(this);
            }
        },
 
        /**
         * Get action parameters
         *
         * @returns {Object}
         * @private
         */
        getActionParameters: function() {
            var selectionState = this.datagrid.getSelectionState();
            var collection     = this.datagrid.collection;
 
            var idValues = _.map(selectionState.selectedModels, function(model) {
                return model.get(this.identifierFieldName);
            }, this);
            var params = {
                inset: selectionState.inset ? 1 : 0,
                values: idValues.join(',')
            };
 
            if (selectionState.inset) {
                params.itemsCount = idValues.length;
            } else {
                params.itemsCount = collection.state.totalRecords - idValues.length;
            }
 
            params = this.getExtraParameters(params, collection.state);
 
            params = collection.processFiltersParams(params, null, 'filters');
 
            var locale = this.getLocaleFromUrl('dataLocale');
 
            if ('family-grid' === this.datagrid.name) {
                locale = this.getLocaleFromUrl('localeCode');
                delete params['filters[label][value]'];
            }
 
            Eif (locale) {
                params.dataLocale = locale;
            }
 
            return params;
        },
 
        /**
         * Get the locale from the datagrid collection url with a given key
         * @param  {String} localeKey For example dataLocale or localeCode
         * @return {String} locale.   The returned locale e.g. en_US
         */
        getLocaleFromUrl: function(localeKey) {
            const url = this.datagrid.collection.url.split('?')[1];
            const urlParams = this.datagrid.collection.decodeStateData(url);
            const datagridParams = urlParams[this.datagrid.name] || {};
 
            return urlParams[localeKey] || datagridParams[localeKey];
        },
 
        /**
         * Get extra parameters (sorters and custom parameters)
         * @param {array}  params
         * @param {object} state
         *
         * @return {object}
         */
        getExtraParameters: function(params, state) {
            params[this.datagrid.name] = {};
 
            Eif (state !== undefined) {
                params[this.datagrid.name]._parameters = this.getActiveSorters(state);
                params[this.datagrid.name]._sort_by    = this.getActiveColumns(state);
            }
 
            return params;
        },
 
        /**
         * Get active sorters
         * @param {object} state
         *
         * @return {object}
         */
        getActiveSorters: function(state) {
            var result = {};
 
            if (state.parameters !== undefined && state.parameters.view !== undefined) {
                result.view = {
                    columns: state.parameters.view.columns
                };
            }
 
            return result;
        },
 
        /**
         * Get active columns
         * @param {object} state
         *
         * @return {object}
         */
        getActiveColumns: function(state) {
            var result = {};
 
            Eif (state.sorters !== undefined) {
                for (var sorterKey in state.sorters) {
                    result[sorterKey] = state.sorters[sorterKey] === 1 ? 'DESC' : 'ASC';
                }
            }
 
            return result;
        },
 
        _onAjaxSuccess: function(data, textStatus, jqXHR) {
            this.datagrid.resetSelectionState();
            AbstractAction.prototype._onAjaxSuccess.apply(this, arguments);
        },
 
        /**
         * Get view for confirm modal
         *
         * @return {oro.Modal}
         */
        getConfirmDialog: function(callback) {
            return Dialog.confirm(
              this.messages.confirm_content,
              this.messages.confirm_title,
              callback,
              this.getEntityHint(true),
              `${this.className} ok`,
              this.messages.confirm_ok,
              this.type
            );
        }
    });
});