all files / grid/actions/ show-hide.js

95.76% Statements 113/118
90.28% Branches 65/72
100% Functions 16/16
96.55% Lines 112/116
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   1195× 1195× 1195× 1195× 1195×   1196×   1195×     1195×           16× 16× 16× 16× 18×   16×   37× 37× 37× 37× 38×   37×   53× 53× 32×     21×   53×   53× 53× 56× 360× 56×   304×     53×   70× 70×   68×   46×       70× 70× 70× 70×   68× 68×         68× 68×     68× 68× 68× 68×     67×   67× 67×       67×     67×       67× 19×   67×   67×       67× 67× 67×     67×       68× 60×     60×            
define(["require", "exports", "@syncfusion/ej2-base", "../base/util", "../services/width-controller", "../base/constant"], function (require, exports, ej2_base_1, util_1, width_controller_1, events) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ShowHide = (function () {
        function ShowHide(parent) {
            this.colName = [];
            this.isShowHide = false;
            this.parent = parent;
            this.addEventListener();
            this.widthService = new width_controller_1.ColumnWidthService(parent);
        }
        ShowHide.prototype.addEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.evtHandlers = [{ event: events.batchCancel, handler: this.batchChanges },
                { event: events.batchCnfrmDlgCancel, handler: this.resetColumnState }
            ];
            util_1.addRemoveEventListener(this.parent, this.evtHandlers, true, this);
        };
        ShowHide.prototype.removeEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            util_1.addRemoveEventListener(this.parent, this.evtHandlers, false);
        };
        ShowHide.prototype.batchChanges = function () {
            if (this.isShowHide) {
                this.isShowHide = false;
                this.setVisible(this.colName, this.changedCol);
                this.changedCol = this.colName = [];
            }
        };
        ShowHide.prototype.show = function (columnName, showBy) {
            var keys = this.getToggleFields(columnName);
            var columns = this.getColumns(keys, showBy);
            this.parent.notify(events.tooltipDestroy, { module: 'edit' });
            for (var i = 0; i < columns.length; i++) {
                columns[parseInt(i.toString(), 10)].visible = true;
            }
            this.setVisible(columns);
        };
        ShowHide.prototype.hide = function (columnName, hideBy) {
            var keys = this.getToggleFields(columnName);
            var columns = this.getColumns(keys, hideBy);
            this.parent.notify(events.tooltipDestroy, { module: 'edit' });
            for (var i = 0; i < columns.length; i++) {
                columns[parseInt(i.toString(), 10)].visible = false;
            }
            this.setVisible(columns);
        };
        ShowHide.prototype.getToggleFields = function (key) {
            var finalized = [];
            if (typeof key === 'string') {
                finalized = [key];
            }
            else {
                finalized = key;
            }
            return finalized;
        };
        ShowHide.prototype.getColumns = function (keys, getKeyBy) {
            var _this = this;
            var columns = util_1.iterateArrayOrObject(keys, function (key) {
                return util_1.iterateArrayOrObject(_this.parent.columnModel, function (item) {
                    if (item["" + getKeyBy] === key) {
                        return item;
                    }
                    return undefined;
                })[0];
            });
            return columns;
        };
        ShowHide.prototype.batchActionPrevent = function (columns, changedStateColumns) {
            Iif (changedStateColumns === void 0) { changedStateColumns = []; }
            if (util_1.isActionPrevent(this.parent)) {
                this.colName = columns;
                this.changedCol = changedStateColumns;
                this.parent.closeEdit();
                return false;
            }
            return true;
        };
        ShowHide.prototype.resetColumnState = function () {
            if (this.isShowHide) {
                for (var i = 0; i < this.colName.length; i++) {
                    this.colName[parseInt(i.toString(), 10)].visible = !this.colName[parseInt(i.toString(), 10)].visible;
                }
            }
        };
        ShowHide.prototype.setVisible = function (columns, changedStateColumns) {
            var _this = this;
            if (changedStateColumns === void 0) { changedStateColumns = []; }
            this.isShowHide = true;
            if (!this.batchActionPrevent(columns, changedStateColumns)) {
                return;
            }
            changedStateColumns = (changedStateColumns.length > 0) ? changedStateColumns : columns;
            var args = {
                requestType: 'columnstate',
                cancel: false,
                columns: changedStateColumns
            };
            var cancel = 'cancel';
            if (this.parent.enableInfiniteScrolling && this.parent.allowGrouping
                && this.parent.groupModule.groupSettings.columns.length > 0) {
                this.parent.contentModule.visibleRows = [];
            }
            this.parent.trigger(events.actionBegin, args, function (showHideArgs) {
                var currentViewCols = _this.parent.getColumns();
                columns = ej2_base_1.isNullOrUndefined(columns) ? currentViewCols : columns;
                if (showHideArgs["" + cancel]) {
                    _this.parent.notify(events.resetColumns, { showHideArgs: showHideArgs });
                    Eif (columns.length > 0) {
                        columns[0].visible = true;
                    }
                    return;
                }
                if (util_1.isGroupAdaptive(_this.parent)) {
                    _this.parent.contentModule.emptyVcRows();
                }
                var addedRow = _this.parent.element.querySelector('.e-addedrow');
                if (_this.parent.editSettings.showAddNewRow && addedRow) {
                    ej2_base_1.remove(addedRow);
                    Iif (_this.parent.enableVirtualization || _this.parent.enableInfiniteScrolling) {
                        _this.parent.isAddNewRow = true;
                    }
                    _this.parent.addNewRowFocus = true;
                    _this.parent.isEdit = false;
                }
                if (_this.parent.allowSelection && _this.parent.getSelectedRecords().length &&
                    !_this.parent.selectionSettings.persistSelection) {
                    _this.parent.clearSelection();
                }
                Iif (_this.parent.enableColumnVirtualization) {
                    _this.parent.notify(events.refreshFrozenPosition, { isModeChg: true });
                }
                else {
                    if (_this.parent.isFrozenGrid() && columns.length) {
                        _this.parent.notify(events.refreshFrozenPosition, { isModeChg: true });
                    }
                    _this.parent.notify(events.columnVisibilityChanged, columns);
                }
                var params = {
                    requestType: 'columnstate',
                    columns: changedStateColumns
                };
                _this.parent.trigger(events.actionComplete, params);
                var startAdd = !_this.parent.element.querySelector('.e-addedrow');
                if (_this.parent.editSettings.showAddNewRow && startAdd) {
                    _this.parent.isEdit = false;
                    _this.parent.addRecord();
                    Eif (!(_this.parent.enableVirtualization || _this.parent.enableInfiniteScrolling)) {
                        _this.parent.notify(events.showAddNewRowFocus, {});
                    }
                }
                Iif (_this.parent.columnQueryMode !== 'All') {
                    _this.parent.refresh();
                }
            });
            if (!this.parent.groupSettings.columns.length) {
                Iif (this.parent.autoFit) {
                    this.parent.preventAdjustColumns();
                }
                else if (this.parent.allowResizing && this.parent.resizeSettings.mode === 'Normal') {
                    var isMaxWidth = this.parent.getHeaderTable().style.width.indexOf('px') === -1;
                    this.widthService.setWidthToTable(isMaxWidth);
                }
            }
        };
        return ShowHide;
    }());
    exports.ShowHide = ShowHide;
});