All files / web/bundles/pimui/js/grid mass-actions.js

87.5% Statements 35/40
83.33% Branches 10/12
100% Functions 3/3
87.5% Lines 35/40

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                  327x                                 327x                               534x   534x             532x 1356x 532x     1356x 1356x   532x 532x   532x             532x               532x   532x                   534x 526x   8x     534x             13x 13x   13x             5x     5x   5x                                 13x     13x                       2440x 556x   556x 188x       368x         1884x   1884x         2440x          
'use strict';
 
/**
 * This extension will display the mass actions panel with all the actions available for checked items in a grid.
 *
 * @author    Pierre Allard <pierre.allard@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',
        'backbone',
        'pim/form',
        'pim/template/grid/mass-actions'
    ],
    function (
        $,
        _,
        __,
        Backbone,
        BaseForm,
        template
    ) {
        return BaseForm.extend({
            template: _.template(template),
            className: 'AknDefault-bottomPanel AknDefault-bottomPanel--hidden AknMassActions mass-actions',
            collection: null,
            count: 0,
            events: {
                'click .select-all': 'selectAll',
                'click .select-none': 'selectNone',
                'click .select-visible': 'selectVisible',
                'click .select-button': 'toggleButton'
            },
 
            /**
             * {@inheritdoc}
             */
            initialize: function (meta) {
                this.config = meta.config;
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            configure() {
                const setCollection = (collection) => {
                    if (null === this.collection) {
                        this.listenTo(collection, 'backgrid:selected', this.select.bind(this));
                    }
 
                    this.collection = collection;
                    this.updateView();
                };
                this.listenTo(this.getRoot(), 'grid_load:start', setCollection);
                this.listenTo(this.getRoot(), 'grid_load:complete', setCollection);
 
                BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render() {
                this.$el.html(this.template({
                    selectedProductsLabel: __(this.config.label),
                    select: __('pim_datagrid.select.title'),
                    selectAll: __('pim_common.all'),
                    selectVisible: __('pim_datagrid.select.all_visible'),
                    selectNone: __('pim_common.none')
                }));
 
                this.updateView();
 
                BaseForm.prototype.render.apply(this, arguments);
            },
 
            /**
             * Updates the count after clicking in a single event
             *
             * @param {Object}  model The selected model
             * @param {boolean} checked
             */
            select(model, checked) {
                if (checked) {
                    this.count = Math.min(this.count + 1, this.collection.state.totalRecords);
                } else {
                    this.count = Math.max(this.count - 1, 0);
                }
 
                this.updateView();
            },
 
            /**
             * Updates the count after clicking in "Select all" button
             */
            selectAll() {
                this.count = this.collection.state.totalRecords;
                this.collection.trigger('backgrid:selectAll');
 
                this.updateView();
            },
 
            /**
             * Updates the count after clicking in "Select all visible" button
             */
            selectVisible() {
                Iif (this.count === this.collection.state.totalRecords) {
                    this.count = 0;
                }
                this.collection.trigger('backgrid:selectAllVisible');
 
                this.updateView();
            },
 
            /**
             * Updates the count after clicking in "Select none" button
             */
            selectNone() {
                this.count = 0;
                this.collection.trigger('backgrid:selectNone');
 
                this.updateView();
            },
 
            /**
             * Updates the count (select all or select none), regarding the current count.
             */
            toggleButton() {
                Iif (this.count === this.collection.state.totalRecords) {
                    this.selectNone();
                } else {
                    this.selectAll();
                }
            },
 
            /**
             * Updates the current view.
             *
             * In this function, we do not use render() method because:
             * - We need to animate this extension (with CSS)
             * - The events of the sub extensions are lost after re-render.
             */
            updateView() {
                if (this.count > 0) {
                    this.$el.removeClass('AknDefault-bottomPanel--hidden');
 
                    if (this.count >= this.collection.state.totalRecords) {
                        this.$el.find('.AknSelectButton')
                            .removeClass('AknSelectButton--partial')
                            .addClass('AknSelectButton--selected');
                    } else {
                        this.$el.find('.AknSelectButton')
                            .removeClass('AknSelectButton--selected')
                            .addClass('AknSelectButton--partial');
                    }
                } else {
                    this.$el.addClass('AknDefault-bottomPanel--hidden');
 
                    this.$el.find('.AknSelectButton')
                        .removeClass('AknSelectButton--selected')
                        .removeClass('AknSelectButton--partial');
                }
 
                this.$el.find('.count').text(this.count);
            }
        });
    }
);