all files / spreadsheet/actions/ merge.js

86.36% Statements 152/176
72.05% Branches 116/161
100% Functions 13/13
86.36% Lines 152/176
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   884× 884×   40×   16× 16× 16× 16× 16× 16× 16× 16×                       2511× 2511× 2511× 2511× 2511× 2511× 2511× 2511× 20× 20× 20×     20× 20× 20×                                     2511× 29× 29× 13× 13× 21×     13× 11× 11× 11× 11× 11× 11× 11× 11×       16× 10×     10×       2511× 1637× 41× 41× 41×   23× 23× 23× 23× 23× 14× 19×         14× 14× 14× 14×               874× 15× 15× 15× 15× 15× 15× 15×         10× 10× 10× 10×                                                                         884× 884× 884× 884×   884× 884×   884× 12× 12× 12× 12×     126650×        
define(["require", "exports", "../../workbook/common/index", "../common/index", "../common/index", "../../workbook/index"], function (require, exports, index_1, index_2, index_3, index_4) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Merge = (function () {
        function Merge(parent) {
            this.parent = parent;
            this.addEventListener();
        }
        Merge.prototype.merge = function (args) {
            this.parent.serviceLocator.getService('cell').refresh(args.rowIdx, args.colIdx, args.lastCell, args.element, false, false, index_4.isImported(this.parent));
        };
        Merge.prototype.hideHandler = function (args) {
            var sheet = this.parent.getActiveSheet();
            var mergeArgs = { range: [args.rowIdx, args.colIdx, args.rowIdx, args.colIdx] };
            this.parent.notify(index_1.activeCellMergedRange, mergeArgs);
            mergeArgs.range = mergeArgs.range;
            var cell = index_4.getCell(mergeArgs.range[0], mergeArgs.range[1], sheet) || {};
            var startIdx = args.model === 'row' ? mergeArgs.range[0] : mergeArgs.range[1];
            var endIdx = startIdx + ((cell[args.model + "Span"] || 1) - 1);
            if ((!args.isEnd && (args.start === startIdx || index_4.isHiddenCol(sheet, startIdx))) || (args.isEnd && (args.start > startIdx &&
                !index_4.isHiddenCol(sheet, startIdx)))) {
                return;
            }
            Eif (cell[args.model + "Span"] > 1 && endIdx >= args.start) {
                if (args.model === 'row' ? index_4.isHiddenRow(sheet, startIdx) : index_4.isHiddenCol(sheet, startIdx)) {
                    Eif (args.colIdx <= endIdx) {
                        var colIdx = index_1.skipHiddenIdx(sheet, mergeArgs.range[1], true, 'columns');
                        if (colIdx <= endIdx) {
                            var rowIdx = mergeArgs.range[0];
                            if (cell.rowSpan > 1) {
                                rowIdx = index_1.skipHiddenIdx(sheet, mergeArgs.range[0], true);
                                rowIdx = rowIdx <= mergeArgs.range[2] ? rowIdx : mergeArgs.range[0];
                            }
                            var cellEle = this.parent.getCell(rowIdx, colIdx);
                            Eif (cellEle) {
                                cellEle.style.display = '';
                                this.parent.serviceLocator.getService('cell').refresh(mergeArgs.range[0], mergeArgs.range[1], true, cellEle, true, true);
                            }
                        }
                    }
                }
                else {
                    var rowIdx = cell.rowSpan > 1 ? index_1.skipHiddenIdx(sheet, mergeArgs.range[0], true) : mergeArgs.range[0];
                    this.merge({ rowIdx: mergeArgs.range[0], colIdx: mergeArgs.range[1], element: this.parent.getCell(rowIdx, mergeArgs.range[1]) });
                }
            }
        };
        Merge.prototype.checkPrevMerge = function (args) {
            var _this = this;
            var cell;
            var mergeArgs;
            var mergeCount;
            var isMergeApplied;
            var isRowMergeCell;
            var sheet = this.parent.getActiveSheet();
            var clearMerge = function () {
                var contTable = _this.parent.getContentTable();
                var contRow = contTable && contTable.rows[0];
                Iif (!contRow) {
                    return;
                }
                var firstRowIdx = parseInt(contRow.getAttribute('aria-rowindex'), 10) - 1;
                mergeArgs.range = mergeArgs.range;
                Iif (firstRowIdx > _this.parent.viewport.topIndex + _this.parent.frozenRowCount(sheet) && firstRowIdx > mergeArgs.range[0] &&
                    firstRowIdx <= mergeArgs.range[2]) {
                    var cellEle = void 0;
                    var hdrTable = _this.parent.getRowHeaderTable();
                    var hdrRow = hdrTable && hdrTable.rows[0];
                    var frozenCol = _this.parent.frozenColCount(sheet);
                    if (frozenCol && !hdrRow) {
                        return;
                    }
                    for (var colIdx = args.colIdx; colIdx <= mergeArgs.range[3]; colIdx++) {
                        cellEle = _this.parent.getCell(firstRowIdx, colIdx, colIdx < frozenCol ? hdrRow : contRow);
                        if (cellEle && (cellEle.colSpan > 1 || cellEle.rowSpan > 1)) {
                            cellEle.style.display = 'none';
                            cellEle.removeAttribute('colSpan');
                            cellEle.removeAttribute('rowSpan');
                        }
                    }
                }
            };
            var checkRowMerge = function (isColMerge) {
                mergeArgs.range = mergeArgs.range;
                if (index_4.isHiddenRow(sheet, mergeArgs.range[0]) && args.rowIdx >= mergeArgs.range[0] && args.rowIdx <= mergeArgs.range[2]) {
                    isMergeApplied = false;
                    for (var rowIdx = mergeArgs.range[0]; rowIdx < args.rowIdx; rowIdx++) {
                        if (!index_4.isHiddenRow(sheet, rowIdx)) {
                            isMergeApplied = true;
                            break;
                        }
                    }
                    if (cell.rowSpan > 1 && !isMergeApplied) {
                        var rowMergeCount = cell.rowSpan - _this.parent.hiddenCount(mergeArgs.range[0], mergeArgs.range[2]);
                        Eif (rowMergeCount > 0) {
                            clearMerge();
                            args.td.style.display = '';
                            args.colSpan = mergeCount;
                            args.rowSpan = rowMergeCount;
                            args.cell = cell;
                            args.isMerged = false;
                        }
                    }
                }
                else if (isColMerge) {
                    for (var rowIdx = mergeArgs.range[0]; isRowMergeCell && rowIdx < args.rowIdx; rowIdx++) {
                        Eif (!index_4.isHiddenRow(sheet, rowIdx)) {
                            isMergeApplied = true;
                            break;
                        }
                    }
                    if (!isMergeApplied) {
                        clearMerge();
                        args.td.style.display = '';
                        args.colSpan = mergeCount;
                        args.cell = cell;
                        args.isMerged = false;
                    }
                }
            };
            if (args.cell.colSpan < 0) {
                if (args.colIdx - 1 > -1 && index_4.isHiddenCol(sheet, args.colIdx - 1)) {
                    cell = index_4.getCell(args.rowIdx, args.colIdx - 1, sheet, false, true);
                    isRowMergeCell = args.rowIdx - 1 > -1 && index_4.isHiddenRow(sheet, args.rowIdx - 1);
                    if ((cell.colSpan !== undefined || cell.rowSpan !== undefined) && (cell.rowSpan === undefined ||
                        cell.rowSpan > 1 || isRowMergeCell)) {
                        mergeArgs = { range: [args.rowIdx, args.colIdx - 1, args.rowIdx, args.colIdx - 1] };
                        this.parent.notify(index_1.activeCellMergedRange, mergeArgs);
                        mergeArgs.range = mergeArgs.range;
                        cell = index_4.getCell(mergeArgs.range[0], mergeArgs.range[1], sheet, false, true);
                        if (index_4.isHiddenCol(sheet, mergeArgs.range[1]) && args.colIdx >= mergeArgs.range[1] && args.colIdx <= mergeArgs.range[3]) {
                            for (var colIdx = mergeArgs.range[1]; colIdx < args.colIdx; colIdx++) {
                                Iif (!index_4.isHiddenCol(sheet, colIdx)) {
                                    isMergeApplied = true;
                                    break;
                                }
                            }
                            Eif (cell.colSpan > 1 && !isMergeApplied) {
                                mergeCount = cell.colSpan - this.parent.hiddenCount(mergeArgs.range[1], mergeArgs.range[3], 'columns');
                                Eif (mergeCount > 0) {
                                    checkRowMerge(true);
                                }
                            }
                        }
                    }
                }
            }
            else {
                if (args.rowIdx - 1 > -1 && index_4.isHiddenRow(sheet, args.rowIdx - 1)) {
                    cell = index_4.getCell(args.rowIdx - 1, args.colIdx, sheet, false, true);
                    Eif (cell.rowSpan !== undefined) {
                        mergeArgs = { range: [args.rowIdx - 1, args.colIdx, args.rowIdx - 1, args.colIdx] };
                        this.parent.notify(index_1.activeCellMergedRange, mergeArgs);
                        mergeArgs.range = mergeArgs.range;
                        cell = index_4.getCell(mergeArgs.range[0], mergeArgs.range[1], sheet, false, true);
                        checkRowMerge();
                    }
                }
            }
        };
        Merge.prototype.checkMerge = function (args) {
            var sheet = this.parent.getActiveSheet();
            var mergeArgs;
            var cell = index_4.getCell(args.rowIdx, args.colIdx, sheet) || {};
            if (args.isRow) {
                Eif (cell.colSpan === undefined || index_4.isHiddenCol(sheet, args.colIdx - 1)) {
                    mergeArgs = { range: [args.rowIdx, args.colIdx, args.rowIdx, args.colIdx] };
                    mergeArgs.range = mergeArgs.range;
                    this.parent.notify(index_1.activeCellMergedRange, mergeArgs);
                    Iif ((index_4.isHiddenCol(sheet, args.colIdx - 1) && !index_4.isHiddenCol(sheet, mergeArgs.range[1])) || (args.isFreezePane &&
                        mergeArgs.range[0] < this.parent.frozenRowCount(sheet))) {
                        args.insideFreezePane = mergeArgs.range[0] < this.parent.frozenRowCount(sheet);
                        return;
                    }
                    Iif (args.colIdx !== this.parent.viewport.leftIndex + this.parent.frozenColCount(sheet) &&
                        index_4.isHiddenCol(sheet, args.colIdx - 1)) {
                        for (var colIdx = mergeArgs.range[1]; cell.colSpan !== undefined && colIdx < args.colIdx; colIdx++) {
                            if (!index_4.isHiddenCol(sheet, colIdx)) {
                                return;
                            }
                        }
                    }
                    cell = index_4.getCell(mergeArgs.range[0], mergeArgs.range[1], sheet);
                    var mergeCount = (mergeArgs.range[2] - args.rowIdx) + 1 -
                        this.parent.hiddenCount(args.rowIdx, mergeArgs.range[2]);
                    Eif (mergeCount >= 1) {
                        this.merge({ rowIdx: mergeArgs.range[0], colIdx: mergeArgs.range[1], element: args.td });
                        Iif (mergeCount === 1) {
                            args.td.removeAttribute('rowspan');
                        }
                        else {
                            args.td.rowSpan = mergeCount;
                        }
                        args.td.style.display = '';
                    }
                }
            }
            else {
                if (cell.rowSpan === undefined || index_4.isHiddenRow(sheet, args.rowIdx - 1)) {
                    mergeArgs = { range: [args.rowIdx, args.colIdx, args.rowIdx, args.colIdx] };
                    mergeArgs.range = mergeArgs.range;
                    this.parent.notify(index_1.activeCellMergedRange, mergeArgs);
                    Iif ((index_4.isHiddenRow(sheet, args.rowIdx - 1) && !index_4.isHiddenRow(sheet, mergeArgs.range[0])) || (args.isFreezePane &&
                        mergeArgs.range[1] < this.parent.frozenColCount(sheet))) {
                        args.insideFreezePane = mergeArgs.range[1] < this.parent.frozenColCount(sheet);
                        return;
                    }
                    if (args.rowIdx !== this.parent.viewport.topIndex + this.parent.frozenRowCount(sheet) &&
                        index_4.isHiddenRow(sheet, args.rowIdx - 1)) {
                        for (var rowIdx = mergeArgs.range[0]; cell.rowSpan !== undefined && rowIdx < args.rowIdx; rowIdx++) {
                            Iif (!index_4.isHiddenRow(sheet, rowIdx)) {
                                return;
                            }
                        }
                    }
                    cell = index_4.getCell(mergeArgs.range[0], mergeArgs.range[1], sheet);
                    var mergeCount = (mergeArgs.range[3] - args.colIdx) + 1 - this.parent.hiddenCount(args.colIdx, mergeArgs.range[3], 'columns');
                    Eif (mergeCount >= 1) {
                        this.merge({ rowIdx: mergeArgs.range[0], colIdx: mergeArgs.range[1], element: args.td });
                        if (mergeCount === 1) {
                            args.td.removeAttribute('colspan');
                        }
                        else {
                            args.td.colSpan = mergeCount;
                        }
                        args.td.style.display = '';
                    }
                }
            }
        };
        Merge.prototype.addEventListener = function () {
            this.parent.on(index_1.applyMerge, this.merge, this);
            this.parent.on(index_2.hiddenMerge, this.hideHandler, this);
            this.parent.on(index_3.checkPrevMerge, this.checkPrevMerge, this);
            this.parent.on(index_3.checkMerge, this.checkMerge, this);
        };
        Merge.prototype.destroy = function () {
            this.removeEventListener();
            this.parent = null;
        };
        Merge.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(index_1.applyMerge, this.merge);
                this.parent.off(index_2.hiddenMerge, this.hideHandler);
                this.parent.off(index_3.checkPrevMerge, this.checkPrevMerge);
                this.parent.off(index_3.checkMerge, this.checkMerge);
            }
        };
        Merge.prototype.getModuleName = function () {
            return 'merge';
        };
        return Merge;
    }());
    exports.Merge = Merge;
});