all files / workbook/actions/ delete.js

95.56% Statements 215/225
86.74% Branches 157/181
100% Functions 11/11
95.54% Lines 214/224
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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335   881× 881×   152× 152× 152×   151× 151× 151×   151×   151× 151× 52× 52×       151× 151× 151× 151× 151× 151× 151× 64×     64× 64× 64×         59×   58×     62× 62× 61×   61×     62× 62×   62× 62× 62× 62× 51× 638×                     638×                               638×     62×   87× 77×     77× 77× 77×         72×   70×     74× 74× 73× 73×     74× 74×   74× 74× 74× 74× 74× 1035× 1035× 1035×                   1035×                             1035× 1035×     74× 74×     10×       10× 10×   146× 146× 146× 228× 228× 13×   228× 220× 220×       228× 146×   228× 16×     146× 146× 146× 136× 136× 136× 136× 136×   146× 146× 146× 146× 146×   136× 136× 136×     136×     136× 136× 128×     136× 136× 136× 88× 88× 88× 88×     80×         881×   880× 880×   880× 12×     126650×        
define(["require", "exports", "../index", "../../workbook/common/index", "../../workbook/common/index", "../../workbook/index", "../../workbook/index", "@syncfusion/ej2-base"], function (require, exports, index_1, index_2, index_3, index_4, index_5, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var WorkbookDelete = (function () {
        function WorkbookDelete(parent) {
            this.parent = parent;
            this.addEventListener();
        }
        WorkbookDelete.prototype.deleteModel = function (args) {
            var _this = this;
            var sheetLength = this.parent.sheets.length;
            if (args.modelType === 'Sheet' && sheetLength === 1) {
                return;
            }
            var modelName = args.modelType.toLowerCase() + "s";
            args.start = args.start;
            if (args.start > args.end) {
                var startIdx = args.start;
                args.start = args.end;
                args.end = startIdx;
            }
            var eventArgs = { startIndex: args.start, endIndex: args.end, modelType: args.modelType,
                cancel: false, isUndoRedo: args.isUndoRedo };
            var actionArgs = { eventArgs: eventArgs, action: 'delete' };
            if (args.isAction) {
                this.parent.notify(index_4.beginAction, actionArgs);
                Iif (eventArgs.cancel) {
                    return;
                }
            }
            var deletedCells;
            var prevCell;
            var mergeArgsCollection = [];
            var count = (args.end - args.start) + 1;
            var insertArgs = { startIndex: args.start, endIndex: args.end, modelType: args.modelType, sheet: args.model };
            var isFinite = this.parent.scrollSettings.isFinite;
            if (args.modelType === 'Row') {
                Iif (args.checkCount !== undefined && args.checkCount === args.model.usedRange.rowIndex) {
                    return;
                }
                this.parent.notify(index_4.refreshInsertDelete, insertArgs);
                args.model = args.model;
                if (isFinite) {
                    if (args.start >= args.model.rowCount) {
                        return;
                    }
                    if (args.end >= args.model.rowCount) {
                        args.end = args.model.rowCount - 1;
                    }
                }
                else {
                    if (args.start > args.model.usedRange.rowIndex) {
                        return;
                    }
                    if (args.end > args.model.usedRange.rowIndex) {
                        args.end -= (args.end - args.model.usedRange.rowIndex);
                    }
                }
                this.setRowColCount(args.start, args.end, args.model, 'row');
                if (args.start <= args.model.usedRange.rowIndex) {
                    this.parent.setSheetPropertyOnMute(args.model, 'usedRange', { rowIndex: args.model.usedRange.rowIndex - ((args.end - args.start) + 1),
                        colIndex: args.model.usedRange.colIndex });
                    if (args.model.usedRange.rowIndex < 0) {
                        this.parent.setSheetPropertyOnMute(args.model, 'usedRange', { rowIndex: 0, colIndex: args.model.usedRange.colIndex });
                    }
                }
                var frozenRow = this.parent.frozenRowCount(args.model);
                if (args.start < frozenRow) {
                    frozenRow = args.end < frozenRow ? (args.end - args.start) + 1 : frozenRow - args.start;
                    frozenRow = args.model.frozenRows - frozenRow;
                    this.parent.setSheetPropertyOnMute(args.model, 'frozenRows', frozenRow);
                    eventArgs.freezePane = true;
                }
                var curIdx = args.end + 1;
                var cell = void 0;
                var mergeArgs = void 0;
                if (args.model.rows[args.start] && args.model.rows[args.start].cells) {
                    for (var i = 0; i <= args.model.usedRange.colIndex; i++) {
                        if (args.model.rows[args.start].cells[i] &&
                            args.model.rows[args.start].cells[i].rowSpan !== undefined &&
                            args.model.rows[args.start].cells[i].rowSpan < 0 &&
                            args.model.rows[args.start].cells[i].colSpan === undefined) {
                            mergeArgs = { range: [args.start, i, args.start, i] };
                            this.parent.notify(index_3.activeCellMergedRange, mergeArgs);
                            mergeArgs.range = mergeArgs.range;
                            if (mergeArgs.range[2] <= args.end) {
                                prevCell = index_1.getCell(mergeArgs.range[0], i, args.model);
                                Eif (prevCell && prevCell.rowSpan > 1) {
                                    Iif (prevCell.rowSpan - ((mergeArgs.range[2] - args.start) + 1) > 1) {
                                        index_1.setCell(mergeArgs.range[0], i, args.model, { colSpan: prevCell.rowSpan - ((mergeArgs.range[2] - args.start) + 1) }, true);
                                    }
                                    else {
                                        delete args.model.rows[mergeArgs.range[0]].cells[i].rowSpan;
                                    }
                                }
                                mergeArgs = null;
                            }
                        }
                        if (args.model.rows[curIdx] && args.model.rows[curIdx].cells &&
                            args.model.rows[curIdx].cells[i] && args.model.rows[curIdx].cells[i].rowSpan
                            !== undefined && args.model.rows[curIdx].cells[i].rowSpan < 0 &&
                            args.model.rows[curIdx].cells[i].colSpan === undefined) {
                            if (!mergeArgs) {
                                mergeArgs = { range: [curIdx, i, curIdx, i] };
                                this.parent.notify(index_3.activeCellMergedRange, mergeArgs);
                            }
                            cell = new Object();
                            mergeArgs.range = mergeArgs.range;
                            Object.assign(cell, index_1.getCell(mergeArgs.range[0], mergeArgs.range[1], args.model));
                            Eif (cell && cell.rowSpan && (cell.rowSpan > 1 || cell.colSpan > 1)) {
                                var indexes = [];
                                indexes[1] = i;
                                indexes[3] = cell.colSpan > 1 ? i + (cell.colSpan - 1) : i;
                                mergeArgs.range = mergeArgs.range;
                                if (mergeArgs.range[0] < args.start) {
                                    indexes[0] = indexes[2] = mergeArgs.range[0];
                                    Eif (cell.rowSpan - count > 1) {
                                        indexes[2] += (cell.rowSpan - count - 1);
                                    }
                                }
                                else {
                                    indexes[0] = indexes[2] = args.start;
                                    Iif (cell.rowSpan - ((args.end - mergeArgs.range[0]) + 1) > 1) {
                                        indexes[2] += ((cell.rowSpan - ((args.end - mergeArgs.range[0]) + 1)) - 1);
                                    }
                                }
                                mergeArgsCollection.push({
                                    range: indexes, isAction: false, preventRefresh: true, merge: true,
                                    type: 'All', skipChecking: true
                                });
                            }
                        }
                        mergeArgs = null;
                    }
                }
                eventArgs.sheetCount = args.model.usedRange.rowIndex;
            }
            else if (args.modelType === 'Column') {
                Iif (args.checkCount !== undefined && args.checkCount === args.model.usedRange.colIndex) {
                    return;
                }
                this.parent.notify(index_4.refreshInsertDelete, insertArgs);
                args.model = args.model;
                if (isFinite) {
                    if (args.start >= args.model.colCount) {
                        return;
                    }
                    if (args.end >= args.model.colCount) {
                        args.end = args.model.colCount - 1;
                    }
                }
                else {
                    if (args.start > args.model.usedRange.colIndex) {
                        return;
                    }
                    if (args.end > args.model.usedRange.colIndex) {
                        args.end -= (args.end - args.model.usedRange.colIndex);
                    }
                }
                this.setRowColCount(args.start, args.end, args.model, 'col');
                if (args.start <= args.model.usedRange.colIndex) {
                    this.parent.setSheetPropertyOnMute(args.model, 'usedRange', { rowIndex: args.model.usedRange.rowIndex, colIndex: args.model.usedRange.colIndex - count });
                    if (args.model.usedRange.colIndex < 0) {
                        this.parent.setSheetPropertyOnMute(args.model, 'usedRange', { rowIndex: args.model.usedRange.rowIndex, colIndex: 0 });
                    }
                }
                var frozenCol = this.parent.frozenColCount(args.model);
                if (args.start < frozenCol) {
                    frozenCol = args.end < frozenCol ? (args.end - args.start) + 1 : frozenCol - args.start;
                    frozenCol = args.model.frozenColumns - frozenCol;
                    this.parent.setSheetPropertyOnMute(args.model, 'frozenColumns', frozenCol);
                    this.parent.updateTopLeftCell();
                    eventArgs.freezePane = true;
                }
                deletedCells = [];
                var curIdx = args.end + 1;
                var cell = void 0;
                var mergeArgs = void 0;
                for (var i = 0; i <= args.model.usedRange.rowIndex; i++) {
                    deletedCells.push({});
                    Eif (args.model.rows[i] && args.model.rows[i].cells) {
                        if (args.model.rows[i].cells[args.start] && args.model.rows[i].cells[args.start].colSpan !==
                            undefined && args.model.rows[i].cells[args.start].colSpan < 0 &&
                            args.model.rows[i].cells[args.start].rowSpan === undefined) {
                            mergeArgs = { range: [i, args.start, i, args.start] };
                            this.parent.notify(index_3.activeCellMergedRange, mergeArgs);
                            mergeArgs.range = mergeArgs.range;
                            if (mergeArgs.range[3] <= args.end) {
                                var prevCell_1 = index_1.getCell(i, mergeArgs.range[1], args.model);
                                Eif (prevCell_1 && prevCell_1.colSpan > 1) {
                                    Iif (prevCell_1.colSpan - ((mergeArgs.range[3] - args.start) + 1) > 1) {
                                        index_1.setCell(i, mergeArgs.range[1], args.model, { colSpan: prevCell_1.colSpan - ((mergeArgs.range[3] - args.start) + 1) }, true);
                                    }
                                    else {
                                        delete args.model.rows[i].cells[mergeArgs.range[1]].colSpan;
                                    }
                                }
                                mergeArgs = null;
                            }
                        }
                        if (args.model.rows[i].cells[curIdx] && args.model.rows[i].cells[curIdx].colSpan
                            !== undefined && args.model.rows[i].cells[curIdx].colSpan < 0 &&
                            args.model.rows[i].cells[curIdx].rowSpan === undefined) {
                            if (!mergeArgs) {
                                mergeArgs = { range: [i, curIdx, i, curIdx] };
                                this.parent.notify(index_3.activeCellMergedRange, mergeArgs);
                            }
                            cell = new Object();
                            mergeArgs.range = mergeArgs.range;
                            Object.assign(cell, index_1.getCell(mergeArgs.range[0], mergeArgs.range[1], args.model));
                            Eif (cell && cell.colSpan && (cell.colSpan > 1 || cell.rowSpan > 1)) {
                                var indexes = [];
                                indexes[0] = i;
                                indexes[2] = cell.rowSpan > 1 ? i + (cell.rowSpan - 1) : i;
                                mergeArgs.range = mergeArgs.range;
                                if (mergeArgs.range[1] < args.start) {
                                    indexes[1] = indexes[3] = mergeArgs.range[1];
                                    Eif (cell.colSpan - count > 1) {
                                        indexes[3] += (cell.colSpan - count - 1);
                                    }
                                }
                                else {
                                    indexes[1] = indexes[3] = args.start;
                                    Iif (cell.colSpan - ((args.end - mergeArgs.range[1]) + 1) > 1) {
                                        indexes[3] += ((cell.colSpan - ((args.end - mergeArgs.range[1]) + 1)) - 1);
                                    }
                                }
                                mergeArgsCollection.push({
                                    range: indexes, isAction: false, preventRefresh: true, merge: true,
                                    type: 'All', skipChecking: true
                                });
                            }
                        }
                        deletedCells[i].cells = args.model.rows[i].cells.splice(args.start, count);
                        mergeArgs = null;
                    }
                }
                eventArgs.sheetCount = args.model.usedRange.colIndex;
                eventArgs.deletedCellsModel = deletedCells;
            }
            else {
                Iif ((args.end - args.start === this.parent.sheets.length - 1) || (args.checkCount !== undefined && args.checkCount ===
                    this.parent.sheets.length)) {
                    return;
                }
                eventArgs.sheetCount = this.parent.sheets.length;
                eventArgs.activeSheetIndex = this.parent.activeSheetIndex;
            }
            var deletedModel = [];
            var deleteMaxHgt = args.modelType === 'Row' && args.start < args.model.maxHgts.length;
            for (var i = args.start; i <= args.end; i++) {
                var sheetsModel = args.model["" + modelName];
                if (args.modelType === 'Sheet' && sheetsModel[i]) {
                    this.parent.notify(index_3.workbookFormulaOperation, { action: 'deleteSheetTab', sheetId: sheetsModel[i].id });
                }
                if (sheetsModel[args.start] || args.start < sheetsModel.length) {
                    deletedModel.push(sheetsModel[args.start] || {});
                    sheetsModel.splice(args.start, 1);
                }
                else {
                    deletedModel.push({});
                }
                if (i === args.start) {
                    deletedModel[0].index = args.start;
                }
                if (deleteMaxHgt) {
                    args.model.maxHgts.splice(args.start, 1);
                }
            }
            mergeArgsCollection.forEach(function (merge) { return _this.parent.notify(index_3.setMerge, merge); });
            this.parent.notify(index_2.beforeDelete, args);
            if (args.modelType !== 'Sheet') {
                this.parent.notify(index_2.refreshClipboard, args);
                eventArgs.refreshSheet = args.refreshSheet;
                eventArgs.activeSheetIndex = index_4.getSheetIndex(this.parent, args.model.name);
                eventArgs['conditionalFormats'] = [];
                this.deleteConditionalFormats(args, eventArgs);
            }
            eventArgs.definedNames = insertArgs.definedNames;
            eventArgs.isAction = args.isAction;
            eventArgs.deletedModel = deletedModel;
            delete eventArgs.cancel;
            this.parent.notify(index_2.deleteAction, actionArgs);
        };
        WorkbookDelete.prototype.setRowColCount = function (startIdx, endIdx, sheet, layout) {
            var prop = layout + 'Count';
            var curCount = sheet["" + prop];
            Iif (endIdx >= curCount) {
                endIdx = curCount - 1;
            }
            Iif (endIdx < startIdx) {
                return;
            }
            this.parent.setSheetPropertyOnMute(sheet, prop, curCount - ((endIdx - startIdx) + 1));
            if (sheet.id === this.parent.getActiveSheet().id) {
                this.parent.notify(index_4.updateRowColCount, { index: curCount - 1, update: layout, isDelete: true, start: startIdx, end: endIdx });
            }
        };
        WorkbookDelete.prototype.deleteConditionalFormats = function (args, eventArgs) {
            var cfCollection = args.model.conditionalFormats;
            Eif (cfCollection) {
                for (var i = 0; i < cfCollection.length; i++) {
                    eventArgs['conditionalFormats'].push(ej2_base_1.extend({}, cfCollection[i], null, true));
                    var cfRange = index_5.getRangeIndexes(cfCollection[i].range);
                    var sltRangeIndex = index_5.getRangeIndexes(args.model.selectedRange);
                    if ((args.modelType === 'Column' && sltRangeIndex[1] <= cfRange[1] && sltRangeIndex[3] >= cfRange[3]) || (args.modelType === 'Row' && sltRangeIndex[0] <= cfRange[0] && sltRangeIndex[2] >= cfRange[2])) {
                        cfCollection.splice(cfCollection.indexOf(cfCollection[i]), 1);
                        i--;
                    }
                    else {
                        cfCollection[i].range = index_5.getRangeAddress(index_5.deleteFormatRange(args, cfRange));
                    }
                }
            }
        };
        WorkbookDelete.prototype.addEventListener = function () {
            this.parent.on(index_3.deleteModel, this.deleteModel, this);
        };
        WorkbookDelete.prototype.destroy = function () {
            this.removeEventListener();
            this.parent = null;
        };
        WorkbookDelete.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(index_3.deleteModel, this.deleteModel);
            }
        };
        WorkbookDelete.prototype.getModuleName = function () {
            return 'workbookdelete';
        };
        return WorkbookDelete;
    }());
    exports.WorkbookDelete = WorkbookDelete;
});