All files / web/bundles/pimdatagrid/js/datagrid actions-panel.js

84% Statements 42/50
83.33% Branches 5/6
76.19% Functions 16/21
83.67% Lines 41/49

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                  327x                                   327x                                               534x 534x   534x 534x   534x             1438x 1438x 1438x                 1438x   1438x 9633x   1438x 9633x     1438x 1419x 4157x       1438x 1369x     1438x       1438x                     5476x 1369x   1369x 1369x                 1369x 1369x 1369x 5476x       1369x                     2738x                 1438x 1438x 1438x 9633x                   9633x 9633x 9633x                                                           327x       327x    
/* global define */
 
/**
 * Panel with action buttons
 *
 * @export  oro/datagrid/actions-panel
 * @class   oro.datagrid.ActionsPanel
 * @extends BaseForm
 */
define(
    [
        'underscore',
        'oro/translator',
        'backbone',
        'pim/template/datagrid/actions-group',
        'pim/form',
        'oro/mediator'
    ], function(
        _,
        __,
        Backbone,
        groupTemplate,
        BaseForm,
        mediator
    ) {
    'use strict';
 
    const ActionsPanel = BaseForm.extend({
        appendToGrid: false,
 
        /** @property {Array} */
        actionsGroups: [],
 
        /** @property {Array.<oro.datagrid.AbstractAction>} */
        actions: [],
 
        /** @property {Array.<oro.datagrid.ActionLauncher>} */
        launchers: [],
 
        className: 'AknButtonList mass-actions-panel',
 
        /** @property {Function} */
        groupTemplate: _.template(groupTemplate),
 
        /**
         * Initialize view
         *
         * @param {Object} options
         * @param {Array} [options.actions] List of actions
         */
        initialize: function(options) {
            this.appendToGrid = options.appendToGrid;
            this.gridElement = options.gridElement;
 
            mediator.once('grid_load:start', this.setupActions.bind(this));
            mediator.on('grid_load:complete', this.setupActions.bind(this));
 
            return BaseForm.prototype.initialize.apply(this, arguments);
        },
 
        /**
         * Get the action options from the datagrid
         */
        setupActions: function(collection, datagrid) {
            this.actionsGroups = datagrid.massActionsGroups;
            this.setActions(datagrid.massActions, datagrid);
            this.renderActions();
        },
 
        /**
         * Renders panel
         *
         * @return {*}
         */
        renderActions: function () {
            this.$el.empty();
 
            var simpleLaunchers = _.filter(this.launchers, function (launcher) {
                return undefined === launcher.getGroup();
            });
            var groupedLaunchers = _.filter(this.launchers, function (launcher) {
                return undefined !== launcher.getGroup();
            });
 
            if (simpleLaunchers.length) {
                _.each(simpleLaunchers, (launcher) => {
                    this.$el.append(launcher.render().$el);
                });
            }
 
            if (groupedLaunchers.length) {
                this.renderGroupedLaunchers(groupedLaunchers);
            }
 
            Iif (this.appendToGrid) {
                this.gridElement.prepend(this.$el);
            }
 
            return this;
        },
 
        /**
         * Render launchers belonging to actions groups
         *
         * @param {Array} launchers
         *
         * @return {*}
         */
        renderGroupedLaunchers: function (launchers) {
            var groupedLaunchers = _.groupBy(launchers, function (launcher) { return launcher.getGroup() });
            var activeGroups = _.pick(this.actionsGroups, _.keys(groupedLaunchers));
 
            _.each(activeGroups, function (group, name) {
                this.$el.append(
                    this.groupTemplate({
                        __,
                        classname: this.getGroupClassname(name),
                        group: group
                    })
                );
            }.bind(this));
 
            _.each(groupedLaunchers, function (groupLaunchers, groupName) {
                const $dropdown = this.$el.find('.' + this.getGroupClassname(groupName) + ' .AknDropdown-menu');
                _.each(groupLaunchers, (launcher) => {
                    $dropdown.append(launcher.renderAsListItem().$el);
                });
            }.bind(this));
 
            return this;
        },
 
        /**
         * Build the class name for the specified action group
         *
         * @param {String} groupName
         *
         * @return {String}
         */
        getGroupClassname: function (groupName) {
            return groupName.replace('_', '-') + '-actions-group';
        },
 
        /**
         * Set actions
         *
         * @param {Array.<oro.datagrid.AbstractAction>} actions
         */
        setActions: function(actions, datagrid) {
            this.actions = [];
            this.launchers = [];
            _.each(actions, function(action) {
                this.addAction(action, datagrid);
            }, this);
        },
 
        /**
         * Adds action to toolbar
         *
         * @param {oro.datagrid.AbstractAction} action
         */
        addAction: function(Action, datagrid) {
            const actionModule = new Action({ datagrid });
            this.actions.push(actionModule);
            this.launchers.push(actionModule.createLauncher());
        },
 
        /**
         * Disable
         *
         * @return {*}
         */
        disable: function() {
            _.each(this.launchers, function(launcher) {
                launcher.disable();
            });
 
            return this;
        },
 
        /**
         * Enable
         *
         * @return {*}
         */
        enable: function() {
            _.each(this.launchers, function(launcher) {
                launcher.enable();
            });
 
            return this;
        }
    });
 
    ActionsPanel.init = (gridContainer, gridName) => {
        return new ActionsPanel({ appendToGrid: true, gridElement: $(gridContainer).find('.grid-container') });
    }
 
    return ActionsPanel;
});