All files / web/bundles/pimdatagrid/js/datagrid/header-cell select-all-header-cell.js

65.96% Statements 31/47
61.11% Branches 11/18
68.18% Functions 15/22
65.96% Lines 31/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 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                            1123x                           1123x                                       1245x 1245x       1245x 1245x                 11250x 11250x                                   2094x 2094x                 279x                         11335x                 124x                   534x 410x   124x                       13x 13x 13x                                           5x     5x                 13x 120x                   5x 85x 80x                                                   1245x                   1245x       1245x       1245x         1245x        
/* global define */
 
/**
 * Contains mass-selection logic
 *  - watches models selection, keeps reference to selected
 *  - provides mass-selection actions
 *  - listening to models collection events,
 *      fills in 'obj' with proper data for
 *      `backgrid:isSelected` and `backgrid:getSelected`
 *
 * @export  oro/datagrid/select-all-header-cell
 * @class   oro.datagrid.SelectAllHeaderCell
 * @extends oro.datagrid.SelectRowCell
 */
define(
    [
        'jquery',
        'underscore',
        'backgrid',
        'oro/datagrid/select-row-cell'
    ], function (
        $,
        _,
        Backgrid,
        SelectRowCell
    ) {
    "use strict";
 
    return SelectRowCell.extend({
        /** @property */
        className: "AknGrid-headerCell AknGrid-headerCell--checkbox select-all-header-cell",
 
        /** @property */
        tagName: "th",
 
        events: {},
 
        /**
         * Initializer.
         * Subscribers on events listening
         *
         * @param {Object} options
         * @param {Backgrid.Column} options.column
         * @param {Backbone.Collection} options.collection
         */
        initialize: function (options) {
            //Backgrid.requireOptions(options, ["column", "collection"]);
 
            this.column = options.column;
            Iif (!(this.column instanceof Backgrid.Column)) {
                this.column = new Backgrid.Column(this.column);
            }
 
            this.initialState();
            this.listenTo(this.collection, {
                remove: this.removeModel,
                updateState: this.initialState,
                'backgrid:selected': this.selectModel,
                'backgrid:selectAll': this.selectAll,
                'backgrid:selectAllVisible': this.selectAllVisible,
                'backgrid:selectNone': this.selectNone,
 
                'backgrid:isSelected': _.bind(function (model, obj) {
                    Eif ($.isPlainObject(obj)) {
                        obj.selected = this.isSelectedModel(model);
                    }
                }, this),
                'backgrid:getSelected': _.bind(function (obj) {
                    if ($.isEmptyObject(obj)) {
                        obj.selected = _.keys(this.selectedModels);
                        obj.inset = this.inset;
                    }
                }, this)
            });
        },
 
        /**
         * Resets selection to initial conditions
         *  - clear selected models set
         *  - reset set type in-set/not-in-set
         */
        initialState: function () {
            this.selectedModels = {};
            this.inset = true;
        },
 
        /**
         * Gets selection state
         *
         * @returns {{selectedModels: *, inset: boolean}}
         */
        getSelectionState: function() {
            return {
                selectedModels: this.selectedModels,
                inset: this.inset
            };
        },
 
        /**
         * Checks if passed model have to be marked as selected
         *
         * @param {Backbone.Model} model
         * @returns {boolean}
         */
        isSelectedModel: function (model) {
            return this.inset === _.has(this.selectedModels, model.id || model.cid);
        },
 
        /**
         * Removes model from selected models set
         *
         * @param {Backbone.Model} model
         */
        removeModel: function (model) {
            delete this.selectedModels[model.id || model.cid];
        },
 
        /**
         * Adds/removes model to/from selected models set
         *
         * @param {Backbone.Model} model
         * @param {boolean} selected
         */
        selectModel: function (model, selected) {
            if (selected === this.inset) {
                this.selectedModels[model.id || model.cid] = model;
            } else {
                this.removeModel(model);
            }
        },
 
        /**
         * Performs selection of all possible models:
         *  - reset to initial state
         *  - change type of set type as not-inset
         *  - marks all models in collection as selected
         *  start to collect models which have to be excluded
         */
        selectAll: function () {
            this.initialState();
            this.inset = false;
            this._selectAll();
        },
 
        /**
         * Reset selection of all possible models:
         *  - reset to initial state
         *  - change type of set type as inset
         *  - marks all models in collection as not selected
         *  start to collect models which have to be included
         */
        selectNone: function () {
            this.initialState();
            this.inset = true;
            this._selectNone();
        },
 
        /**
         * Performs selection of all visible models:
         *  - if necessary reset to initial state
         *  - marks all models in collection as selected
         */
        selectAllVisible: function () {
            Iif (!this.inset) {
                this.initialState();
            }
            this._selectAllNotSelected();
        },
 
        /**
         * Marks all models in collection as selected
         *
         * @private
         */
        _selectAll: function () {
            this.collection.each(function (model) {
                model.trigger("backgrid:select", model, true);
            });
        },
 
        /**
         * Marks all non selected models in collection as selected
         *
         * @private
         */
        _selectAllNotSelected: function () {
            this.collection.each((model) => {
                if (!this.isSelectedModel(model)) {
                    model.trigger("backgrid:select", model, true);
                }
            });
        },
 
        /**
         * Marks all models in collection as not selected
         *
         * @private
         */
        _selectNone: function () {
            this.collection.each(function (model) {
                model.trigger("backgrid:select", model, false);
            });
        },
 
        /**
         *
         *
         * @returns {oro.datagrid.SelectAllHeaderCell}
         */
        render: function () {
            /* temp solution: start */
            // It's not clear for now, how mass selection will be designed,
            // thus implementation is done just to check functionality.
            // For future render method will depend on options or will be empty
            this.$el.empty().append('<div class="AknSeveralActionsButton AknSeveralActionsButton--small AknDropdown">\
                <button type="button" class="AknSeveralActionsButton-mainAction" data-select-all>'+_.__('pim_common.all')+'</button>\
                <button type="button" class="AknSeveralActionsButton-caretContainer dropdown-toggle" data-toggle="dropdown">\
                    <i class="AknCaret AknCaret--inverse"></i>\
                </button>\
                <ul class="AknSeveralActionsButton-menu AknDropdown-menu">\
                    <li><a class="AknDropdown-menuLink" href="#" data-select-all-visible>'+_.__('oro_datagrid.select.visible')+'</a></li>\
                    <li><a class="AknDropdown-menuLink" href="#" data-select-none>'+_.__('pim_common.none')+'</a></li>\
                </ul>\
            </div>');
            this.$el.find('[data-select-all]').on('click', _.bind(function (e) {
                this.collection.trigger('backgrid:selectAll');
                e.preventDefault();
            }, this));
            this.$el.find('[data-select-all-visible]').on('click', _.bind(function (e) {
                this.collection.trigger('backgrid:selectAllVisible');
                e.preventDefault();
            }, this));
            this.$el.find('[data-select-none]').on('click', _.bind(function (e) {
                this.collection.trigger('backgrid:selectNone');
                e.preventDefault();
            }, this));
            /* temp solution: end */
            return this;
        }
    });
});