all files / workbook/actions/ insert.js

89.96% Statements 224/249
88.54% Branches 170/192
91.67% Functions 22/24
90.32% Lines 224/248
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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366   882× 882×   259× 259× 259×     259× 259× 259× 259× 259× 259× 217× 217× 217×       217× 13×   217× 315× 315× 13×         42× 36× 36× 36×       42×       259×   259× 259× 94× 94×     94× 94×   259× 259× 112×     112× 112× 112×     112×       112× 112×   112× 112× 15×   112× 112×     106×   112× 112× 112× 112× 112× 1348×   679× 679×         1348× 1348× 1348× 120× 120× 392× 324×     120× 24×   120× 160× 24×   160× 132×   160× 150×   160×       112× 112× 1348×   112×   147× 117×     117× 117× 117×     117× 20×   117× 117× 117×   117× 117×     108×   117×     117× 117× 103×   117× 166×   117× 117× 117× 117× 117× 1666×   1659× 18×   1666× 457×   1666×   1666× 1666× 1159× 1159×         1666× 1666× 1666× 89× 89× 284× 232×     89× 40×   89× 138× 113×   138× 116×   138×       117×   117×     30×     30× 30× 30× 30× 45×               30×     30× 30× 30× 30× 12× 12×   18×     30×   30× 45×   45× 45× 45×   45×   45× 45× 45×       30× 30×   259× 229× 229× 229×   259×   229× 229× 229× 219×           64×   64× 14×   64×   64× 13×                                 229× 229× 29×   200× 197× 124×       882×   881× 881×   881× 13×     126650×        
define(["require", "exports", "../base/index", "../../workbook/common/index", "../../workbook/common/index", "../../workbook/common/index", "../../workbook/index"], function (require, exports, index_1, index_2, index_3, index_4, index_5) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var WorkbookInsert = (function () {
        function WorkbookInsert(parent) {
            this.parent = parent;
            this.addEventListener();
        }
        WorkbookInsert.prototype.insertModel = function (args) {
            var _this = this;
            var _a, _b, _c, _d;
            Iif (!args.model) {
                return;
            }
            var index;
            var model = [];
            var mergeCollection;
            var isModel;
            var maxHgtObj;
            if (typeof (args.start) === 'number') {
                index = args.start;
                args.end = args.end || index;
                Iif (index > args.end) {
                    index = args.end;
                    args.end = args.start;
                }
                if (args.modelType === 'Row' && index < args.model.maxHgts.length) {
                    maxHgtObj = [];
                }
                for (var i = index; i <= args.end; i++) {
                    model.push({});
                    if (maxHgtObj) {
                        maxHgtObj.push(null);
                    }
                }
            }
            else {
                if (args.start) {
                    index = args.start[0].index || 0;
                    model = args.start;
                    isModel = true;
                }
                else {
                    index = 0;
                    model.push({});
                }
                if (args.modelType === 'Row' && index < args.model.maxHgts.length) {
                    maxHgtObj = [];
                    model.forEach(function () {
                        maxHgtObj.push(null);
                    });
                }
            }
            var eventArgs = { model: model, index: index, modelType: args.modelType, insertType: args.insertType,
                cancel: false, isUndoRedo: args.isUndoRedo };
            var actionArgs = { eventArgs: eventArgs, action: 'insert' };
            if (args.isAction) {
                this.parent.notify(index_4.beginAction, actionArgs);
                Iif (eventArgs.cancel) {
                    return;
                }
                delete eventArgs.cancel;
                eventArgs.isAction = args.isAction;
            }
            var insertArgs = { startIndex: index, endIndex: index + model.length - 1, modelType: args.modelType, sheet: args.model, isInsert: true };
            if (args.modelType === 'Row') {
                Iif (args.checkCount !== undefined && args.model.rows && args.checkCount === args.model.rows.length) {
                    return;
                }
                this.parent.notify(index_3.refreshInsertDelete, insertArgs);
                args.model = args.model;
                Iif (!args.model.rows) {
                    args.model.rows = [];
                }
                if (isModel && args.model.usedRange.rowIndex > -1 && index > args.model.usedRange.rowIndex) {
                    for (var i = args.model.usedRange.rowIndex; i < index - 1; i++) {
                        model.splice(0, 0, {});
                    }
                }
                var frozenRow = this.parent.frozenRowCount(args.model);
                if (index < frozenRow) {
                    this.parent.setSheetPropertyOnMute(args.model, 'frozenRows', args.model.frozenRows + model.length);
                    eventArgs.freezePane = true;
                }
                (_a = args.model.rows).splice.apply(_a, [index, 0].concat(model));
                if (maxHgtObj) {
                    (_b = args.model.maxHgts).splice.apply(_b, [index, 0].concat(maxHgtObj));
                }
                this.setRowColCount(insertArgs.startIndex, insertArgs.endIndex, args.model, 'row');
                if (index > args.model.usedRange.rowIndex) {
                    this.parent.setUsedRange(index + (model.length - 1), args.model.usedRange.colIndex, args.model, true);
                }
                else {
                    this.parent.setUsedRange(args.model.usedRange.rowIndex + model.length, args.model.usedRange.colIndex, args.model, true);
                }
                var curIdx = index + model.length;
                var style_1;
                var cell = void 0;
                var newStyle_1;
                var _loop_1 = function (i) {
                    if (args.model.rows[curIdx] && args.model.rows[curIdx].cells &&
                        args.model.rows[curIdx].cells[i]) {
                        cell = args.model.rows[curIdx].cells[i];
                        if (cell.rowSpan !== undefined && cell.rowSpan < 0 && cell.colSpan === undefined) {
                            this_1.parent.notify(index_3.insertMerge, {
                                range: [curIdx, i, curIdx, i], insertCount: model.length, insertModel: 'Row'
                            });
                        }
                    }
                    style_1 = index_1.getCell(index - 1, i, args.model, false, true).style;
                    cell = index_1.getCell(index + 1, i, args.model, false, true);
                    if (style_1) {
                        newStyle_1 = {};
                        Object.keys(style_1).forEach(function (key) {
                            if (!(key === 'borderLeft' || key === 'borderRight' || key === 'borderTop' || key === 'borderBottom')) {
                                newStyle_1[key] = style_1[key];
                            }
                        });
                        if (cell.style) {
                            this_1.checkBorder(cell.style, args.model.rows[index - 1].cells[i].style, newStyle_1);
                        }
                        model.forEach(function (row) {
                            if (!row.cells) {
                                row.cells = [];
                            }
                            if (!row.cells[i]) {
                                row.cells[i] = {};
                            }
                            if (!row.cells[i].style) {
                                row.cells[i].style = {};
                            }
                            Object.assign(row.cells[i].style, newStyle_1);
                        });
                    }
                };
                var this_1 = this;
                for (var i = 0; i <= args.model.usedRange.colIndex; i++) {
                    _loop_1(i);
                }
                eventArgs.sheetCount = args.model.rows.length;
            }
            else if (args.modelType === 'Column') {
                Iif (args.checkCount !== undefined && args.model.columns && args.checkCount === args.model.columns.length) {
                    return;
                }
                this.parent.notify(index_3.refreshInsertDelete, insertArgs);
                args.model = args.model;
                Iif (!args.model.columns) {
                    args.model.columns = [];
                }
                if (index && !args.model.columns[index - 1]) {
                    args.model.columns[index - 1] = {};
                }
                (_c = args.model.columns).splice.apply(_c, [index, 0].concat(model));
                var frozenCol = this.parent.frozenColCount(args.model);
                if (index < frozenCol) {
                    this.parent.setSheetPropertyOnMute(args.model, 'frozenColumns', args.model.frozenColumns + model.length);
                    eventArgs.freezePane = true;
                }
                this.setRowColCount(insertArgs.startIndex, insertArgs.endIndex, args.model, 'col');
                if (index > args.model.usedRange.colIndex) {
                    this.parent.setUsedRange(args.model.usedRange.rowIndex, index + (model.length - 1), args.model, true);
                }
                else {
                    this.parent.setUsedRange(args.model.usedRange.rowIndex, args.model.usedRange.colIndex + model.length, args.model, true);
                }
                Iif (!args.model.rows) {
                    args.model.rows = [];
                }
                var cellModel = [];
                if (!args.columnCellsModel) {
                    args.columnCellsModel = [];
                }
                for (var i = 0; i < model.length; i++) {
                    cellModel.push(null);
                }
                mergeCollection = [];
                var cell = void 0;
                var style_2;
                var newStyle_2;
                for (var i = 0; i <= args.model.usedRange.rowIndex; i++) {
                    if (!args.model.rows[i]) {
                        args.model.rows[i] = { cells: [] };
                    }
                    else if (!args.model.rows[i].cells) {
                        args.model.rows[i].cells = [];
                    }
                    if (index && !args.model.rows[i].cells[index - 1]) {
                        args.model.rows[i].cells[index - 1] = {};
                    }
                    (_d = args.model.rows[i].cells).splice.apply(_d, [index, 0].concat((args.columnCellsModel[i] &&
                        args.columnCellsModel[i].cells ? args.columnCellsModel[i].cells : cellModel)));
                    var curIdx = index + model.length;
                    if (args.model.rows[i].cells[curIdx]) {
                        cell = args.model.rows[i].cells[curIdx];
                        if (cell.colSpan !== undefined && cell.colSpan < 0 && cell.rowSpan === undefined) {
                            mergeCollection.push({
                                range: [i, curIdx, i, curIdx], insertCount: cellModel.length, insertModel: 'Column'
                            });
                        }
                    }
                    style_2 = index_1.getCell(i, index - 1, args.model, false, true).style;
                    cell = index_1.getCell(i, index + 1, args.model, false, true);
                    if (style_2) {
                        newStyle_2 = {};
                        Object.keys(style_2).forEach(function (key) {
                            if (!(key === 'borderLeft' || key === 'borderRight' || key === 'borderTop' || key === 'borderBottom')) {
                                newStyle_2[key] = style_2[key];
                            }
                        });
                        if (cell.style) {
                            this.checkBorder(cell.style, args.model.rows[i].cells[index - 1].style, newStyle_2);
                        }
                        for (var j = index; j < curIdx; j++) {
                            if (!args.model.rows[i].cells[j]) {
                                args.model.rows[i].cells[j] = {};
                            }
                            if (!args.model.rows[i].cells[j].style) {
                                args.model.rows[i].cells[j].style = {};
                            }
                            Object.assign(args.model.rows[i].cells[j].style, newStyle_2);
                        }
                    }
                }
                mergeCollection.forEach(function (mergeArgs) {
                    _this.parent.notify(index_3.insertMerge, mergeArgs);
                });
                eventArgs.sheetCount = args.model.columns.length;
            }
            else {
                Iif (args.checkCount !== undefined && args.checkCount === this.parent.sheets.length) {
                    return;
                }
                var sheetModel = model;
                var sheetName = index_1.getSheetName(this.parent);
                var isFromUpdateAction = args.isFromUpdateAction;
                for (var i = 0; i < sheetModel.length; i++) {
                    if (sheetModel[i].name) {
                        for (var j = 0; j < this.parent.sheets.length; j++) {
                            Iif (sheetModel[i].name === this.parent.sheets[j].name) {
                                sheetModel.splice(i, 1);
                                i--;
                                break;
                            }
                        }
                    }
                }
                Iif (!sheetModel.length) {
                    return;
                }
                delete model[0].index;
                this.parent.createSheet(index, model);
                var id_1;
                if (args.activeSheetIndex) {
                    eventArgs.activeSheetIndex = args.activeSheetIndex;
                    this.parent.setProperties({ activeSheetIndex: args.activeSheetIndex }, true);
                }
                else Iif (!args.isAction && args.start < this.parent.activeSheetIndex) {
                    this.parent.setProperties({ activeSheetIndex: this.parent.skipHiddenSheets(this.parent.activeSheetIndex) }, true);
                }
                if (isFromUpdateAction) {
                    this.parent.setProperties({ activeSheetIndex: index_1.getSheetIndex(this.parent, sheetName) }, true);
                }
                model.forEach(function (sheet) {
                    if (isModel) {
                        _this.updateRangeModel(sheet.ranges);
                    }
                    var viewport = _this.parent.viewport;
                    var refreshRange = [viewport.topIndex, viewport.leftIndex, viewport.bottomIndex, viewport.rightIndex];
                    var args = {
                        sheet: sheet, resolveAfterFullDataLoaded: true,
                        indexes: refreshRange, promise: new Promise(function (resolve) { resolve((function () { })()); })
                    };
                    _this.parent.notify(index_2.updateSheetFromDataSource, args);
                    id_1 = sheet.id;
                    _this.parent.notify(index_2.workbookFormulaOperation, {
                        action: 'addSheet', visibleName: sheet.name, sheetName: 'Sheet' + id_1, sheetId: id_1
                    });
                });
                eventArgs.activeSheetIndex = args.activeSheetIndex;
                eventArgs.sheetCount = this.parent.sheets.length;
            }
            if (args.modelType !== 'Sheet') {
                this.insertConditionalFormats(args);
                this.parent.notify(index_3.refreshClipboard, { start: index, end: index + model.length - 1, modelType: args.modelType, model: args.model, isInsert: true });
                eventArgs.activeSheetIndex = index_1.getSheetIndex(this.parent, args.model.name);
            }
            this.parent.notify(index_3.insert, actionArgs);
        };
        WorkbookInsert.prototype.setRowColCount = function (startIdx, endIdx, sheet, layout) {
            var prop = layout + 'Count';
            this.parent.setSheetPropertyOnMute(sheet, prop, sheet["" + prop] + ((endIdx - startIdx) + 1));
            if (sheet.id === this.parent.getActiveSheet().id) {
                this.parent.notify(index_4.updateRowColCount, { index: sheet["" + prop] - 1, update: layout, isInsert: true, start: startIdx, end: endIdx });
            }
        };
        WorkbookInsert.prototype.updateRangeModel = function (ranges) {
            ranges.forEach(function (range) {
                Eif (range.dataSource) {
                    range.startCell = range.startCell || 'A1';
                    range.showFieldAsHeader = range.showFieldAsHeader === undefined || range.showFieldAsHeader;
                    range.template = range.template || '';
                    range.address = range.address || 'A1';
                }
            });
        };
        WorkbookInsert.prototype.checkBorder = function (style, adjStyle, newStyle) {
            if (style.borderLeft && style.borderLeft === adjStyle.borderLeft) {
                newStyle.borderLeft = style.borderLeft;
            }
            if (style.borderRight && style.borderRight === adjStyle.borderRight) {
                newStyle.borderRight = style.borderRight;
            }
            if (style.borderTop && style.borderTop === adjStyle.borderTop) {
                newStyle.borderTop = style.borderTop;
            }
            if (style.borderBottom && style.borderBottom === adjStyle.borderBottom) {
                newStyle.borderBottom = style.borderBottom;
            }
        };
        WorkbookInsert.prototype.setInsertInfo = function (sheet, startIndex, count, totalKey, modelType) {
            if (modelType === void 0) { modelType = 'Row'; }
            var endIndex = count = startIndex + (count - 1);
            sheet.ranges.forEach(function (range) {
                if (range.info && startIndex < range.info["" + totalKey]) {
                    if (!range.info["insert" + modelType + "Range"]) {
                        range.info["insert" + modelType + "Range"] = [[startIndex, endIndex]];
                    }
                    else {
                        range.info["insert" + modelType + "Range"].push([startIndex, endIndex]);
                    }
                    range.info["" + totalKey] += ((endIndex - startIndex) + 1);
                }
            });
        };
        WorkbookInsert.prototype.insertConditionalFormats = function (args) {
            var cfCollection = args.model.conditionalFormats;
            if (args.prevAction === 'delete') {
                this.parent.setSheetPropertyOnMute(args.model, 'conditionalFormats', args.conditionalFormats);
            }
            else if (cfCollection) {
                for (var i = 0, cfLength = cfCollection.length; i < cfLength; i++) {
                    cfCollection[i].range = index_4.getRangeAddress(index_5.insertFormatRange(args, index_4.getRangeIndexes(cfCollection[i].range), !args.isAction && !args.isUndoRedo));
                }
            }
        };
        WorkbookInsert.prototype.addEventListener = function () {
            this.parent.on(index_2.insertModel, this.insertModel, this);
        };
        WorkbookInsert.prototype.destroy = function () {
            this.removeEventListener();
            this.parent = null;
        };
        WorkbookInsert.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(index_2.insertModel, this.insertModel);
            }
        };
        WorkbookInsert.prototype.getModuleName = function () {
            return 'workbookinsert';
        };
        return WorkbookInsert;
    }());
    exports.WorkbookInsert = WorkbookInsert;
});