all files / spreadsheet/actions/ merge.js

90.08% Statements 118/131
77.78% Branches 105/135
91.67% Functions 11/12
90.08% Lines 118/131
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   121× 121×   175× 175×                                     175×                           293× 293× 293× 293× 293× 153× 13× 13×   10× 10× 10× 10×               153×   140× 20× 20×                                                         121× 121× 121× 121×   121× 121×   121×     18525×        
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) {
            var _this = this;
            Iif (args.showDialog) {
                this.parent.serviceLocator.getService(index_2.dialog).show({
                    target: this.parent.element,
                    height: 180, width: 400, isModal: true, showCloseIcon: true,
                    content: this.parent.serviceLocator.getService(index_2.locale).getConstant('PasteMergeAlert'),
                    beforeOpen: function (args) {
                        var dlgArgs = {
                            dialogName: 'MergeDialog',
                            element: args.element, target: args.target, cancel: args.cancel
                        };
                        _this.parent.trigger('dialogBeforeOpen', dlgArgs);
                        if (dlgArgs.cancel) {
                            args.cancel = true;
                        }
                        index_2.focus(_this.parent.element);
                    }
                });
                return;
            }
            this.parent.serviceLocator.getService('cell').refresh(args.rowIdx, args.colIdx, args.lastCell, args.element);
        };
        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.end < endIdx && (endIdx - args.end > 1 || cell.rowSpan > 1)) {
                        var cellEle = this.parent.getCell(args.rowIdx, args.end + 1);
                        Eif (cellEle) {
                            if (endIdx - args.end > 1) {
                                cellEle.colSpan = endIdx - args.end;
                            }
                            cellEle.style.display = '';
                        }
                        Eif (cell.rowSpan > 1) {
                            var rowSpan = cell.rowSpan - this.parent.hiddenCount(args.rowIdx, args.rowIdx + (cell.rowSpan - 1));
                            Eif (rowSpan > 1) {
                                cellEle.rowSpan = rowSpan;
                            }
                        }
                    }
                }
                else {
                    this.merge({ rowIdx: mergeArgs.range[0], colIdx: mergeArgs.range[1] });
                }
            }
        };
        Merge.prototype.checkPrevMerge = function (args) {
            var cell;
            var sheet = this.parent.getActiveSheet();
            var mergeArgs;
            var mergeCount;
            if (args.isRow) {
                if (args.rowIdx - 1 > -1 && index_4.isHiddenRow(sheet, args.rowIdx - 1)) {
                    cell = index_4.getCell(args.rowIdx - 1, args.colIdx, sheet) || {};
                    if ((cell.rowSpan !== undefined || cell.colSpan !== undefined) && (cell.colSpan === undefined || cell.colSpan > 1 ||
                        (args.colIdx - 1 > -1 && index_4.isHiddenCol(sheet, args.colIdx - 1)))) {
                        mergeArgs = { range: [args.rowIdx - 1, args.colIdx, args.rowIdx - 1, args.colIdx] };
                        this.parent.notify(index_1.activeCellMergedRange, mergeArgs);
                        mergeArgs.range = mergeArgs.range;
                        if (index_4.isHiddenRow(sheet, mergeArgs.range[0]) && mergeArgs.range[2] >= args.rowIdx) {
                            cell = index_4.getCell(mergeArgs.range[0], mergeArgs.range[1], sheet) || {};
                            Eif (cell.rowSpan > 1) {
                                mergeCount = (mergeArgs.range[2] - args.rowIdx) + 1 - this.parent.hiddenCount(args.rowIdx, args.rowIdx + (mergeArgs.range[2] - args.rowIdx));
                                if (mergeCount > 1) {
                                    args.td.rowSpan = mergeCount;
                                    args.td.style.display = '';
                                }
                                if (cell.colSpan > 1 && !(args.colIdx - 1 > -1 && index_4.isHiddenCol(sheet, args.colIdx - 1))) {
                                    mergeCount = cell.colSpan - this.parent.hiddenCount(args.colIdx, args.colIdx + (cell.colSpan - 1), 'columns');
                                    Eif (mergeCount > 1) {
                                        args.td.colSpan = mergeCount;
                                        args.td.style.display = '';
                                    }
                                }
                            }
                        }
                    }
                }
                return;
            }
            if (args.colIdx - 1 > -1 && index_4.isHiddenCol(sheet, args.colIdx - 1)) {
                cell = index_4.getCell(args.rowIdx, args.colIdx - 1, sheet) || {};
                if ((cell.colSpan !== undefined || cell.rowSpan !== undefined) && (cell.rowSpan === undefined || cell.rowSpan > 1 ||
                    (args.rowIdx - 1 > -1 && index_4.isHiddenRow(sheet, args.rowIdx - 1)))) {
                    mergeArgs = { range: [args.rowIdx, args.colIdx - 1, args.rowIdx, args.colIdx - 1] };
                    this.parent.notify(index_1.activeCellMergedRange, mergeArgs);
                    mergeArgs.range = mergeArgs.range;
                    if (index_4.isHiddenCol(sheet, mergeArgs.range[1]) && mergeArgs.range[3] >= args.colIdx) {
                        cell = index_4.getCell(mergeArgs.range[0], mergeArgs.range[1], sheet) || {};
                        Eif (cell.colSpan > 1) {
                            mergeCount = (mergeArgs.range[3] - args.colIdx) + 1 - this.parent.hiddenCount(args.colIdx, args.colIdx + (cell.colSpan - 1), 'columns');
                            Iif (mergeCount > 1) {
                                args.td.colSpan = mergeCount;
                                args.td.style.display = '';
                            }
                            if (cell.rowSpan > 1 && !(args.rowIdx - 1 > -1 && index_4.isHiddenRow(sheet, args.rowIdx - 1))) {
                                mergeCount = cell.rowSpan - this.parent.hiddenCount(args.rowIdx, args.rowIdx + (cell.rowSpan - 1));
                                Eif (mergeCount > 1) {
                                    args.td.rowSpan = mergeCount;
                                    args.td.style.display = '';
                                }
                            }
                        }
                    }
                }
            }
        };
        Merge.prototype.checkMerge = function (args) {
            var sheet = this.parent.getActiveSheet();
            var cell = index_4.getCell(args.rowIdx, args.colIdx, sheet) || {};
            if (args.isRow) {
                Eif (cell.colSpan === undefined || index_4.isHiddenCol(sheet, args.colIdx - 1)) {
                    var 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;
                    }
                    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 });
                        args.td.rowSpan = mergeCount;
                        args.td.style.display = '';
                    }
                }
            }
            else {
                if (cell.rowSpan === undefined || index_4.isHiddenRow(sheet, args.rowIdx - 1)) {
                    var 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;
                    }
                    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');
                    if (mergeCount > 1) {
                        this.merge({ rowIdx: mergeArgs.range[0], colIdx: mergeArgs.range[1], element: args.td });
                        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;
});