all files / spreadsheet/actions/ wrap.js

86.13% Statements 118/137
78.57% Branches 88/112
100% Functions 12/12
86.13% Lines 118/137
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   121× 121× 121×   121× 121× 121× 121×   121×     22× 22× 22× 22× 22× 22× 22× 22× 24× 24× 24× 28× 28×   22× 22× 22× 22× 22×         22×     22×     22× 16× 16× 16× 11× 11×       16× 14× 14×     14× 14× 14× 14× 16× 16×   16×         14× 14× 14×             14×                       16×   14×     22×                       22× 13×       95× 95×       241× 172×         58×                               18525×   121× 121×        
define(["require", "exports", "@syncfusion/ej2-base", "../common/index", "../common/index", "../common/index", "../../workbook/index", "../../workbook/index"], function (require, exports, ej2_base_1, index_1, index_2, index_3, index_4, index_5) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var WrapText = (function () {
        function WrapText(parent) {
            this.parent = parent;
            this.wrapCell = this.parent.createElement('span', { className: 'e-wrap-content' });
            this.addEventListener();
        }
        WrapText.prototype.addEventListener = function () {
            this.parent.on(index_1.ribbonClick, this.ribbonClickHandler, this);
            this.parent.on(index_4.wrapEvent, this.wrapTextHandler, this);
            this.parent.on(index_1.rowHeightChanged, this.rowHeightChangedHandler, this);
            this.parent.on(index_3.colWidthChanged, this.colWidthChanged, this);
        };
        WrapText.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(index_1.ribbonClick, this.ribbonClickHandler);
                this.parent.off(index_4.wrapEvent, this.wrapTextHandler);
                this.parent.off(index_1.rowHeightChanged, this.rowHeightChangedHandler);
                this.parent.off(index_3.colWidthChanged, this.colWidthChanged);
            }
        };
        WrapText.prototype.wrapTextHandler = function (args) {
            Eif (args.initial || index_1.inView(this.parent, args.range, true)) {
                var ele = void 0;
                var cell = void 0;
                var colwidth = void 0;
                var isCustomHgt = void 0;
                var maxHgt = void 0;
                var hgt = void 0;
                for (var i = args.range[0]; i <= args.range[2]; i++) {
                    maxHgt = 0;
                    isCustomHgt = index_4.getRow(args.sheet, i).customHeight || args.isCustomHgt;
                    for (var j = args.range[1]; j <= args.range[3]; j++) {
                        cell = index_4.getCell(i, j, args.sheet, null, true);
                        if (cell.rowSpan < 0 || cell.colSpan < 0) {
                            continue;
                        }
                        var isMerge = (cell.rowSpan > 1 || cell.colSpan > 1);
                        ele = args.initial ? args.td : this.parent.getCell(i, j);
                        Eif (ele) {
                            Eif (args.wrap) {
                                ele.classList.add(index_1.WRAPTEXT);
                            }
                            else {
                                ele.classList.remove(index_1.WRAPTEXT);
                            }
                            if (isCustomHgt || isMerge) {
                                this.updateWrapCell(i, j, args.sheet, ele);
                            }
                        }
                        Iif (ej2_base_1.Browser.isIE) {
                            ele.classList.add('e-ie-wrap');
                        }
                        if (!(isCustomHgt || isMerge)) {
                            colwidth = index_2.getExcludedColumnWidth(args.sheet, i, j, cell.colSpan > 1 ? j + cell.colSpan - 1 : j);
                            var displayText = this.parent.getDisplayText(cell).toString();
                            if (displayText.indexOf('\n') < 0) {
                                var editElem = this.parent.element.querySelector('.e-spreadsheet-edit');
                                if (editElem) {
                                    if (editElem.textContent.indexOf('\n') > -1) {
                                        displayText = editElem.textContent;
                                    }
                                }
                            }
                            if (displayText) {
                                Eif (args.wrap) {
                                    Iif (ele && ele.classList.contains('e-alt-unwrap')) {
                                        ele.classList.remove('e-alt-unwrap');
                                    }
                                    var lines = void 0;
                                    var n = 0;
                                    var p = void 0;
                                    if (displayText.indexOf('\n') > -1) {
                                        var splitVal = displayText.split('\n');
                                        var valLength = splitVal.length;
                                        for (p = 0; p < valLength; p++) {
                                            lines = index_2.getLines(splitVal[p], colwidth, cell.style, this.parent.cellStyle);
                                            if (lines === 0) {
                                                lines = 1;
                                            }
                                            n = n + lines;
                                        }
                                        lines = n;
                                    }
                                    else {
                                        lines = index_2.getLines(displayText, colwidth, cell.style, this.parent.cellStyle);
                                    }
                                    hgt = index_2.getTextHeightWithBorder(this.parent, i, j, args.sheet, cell.style || this.parent.cellStyle, lines);
                                    maxHgt = Math.max(maxHgt, hgt);
                                    Iif (cell.rowSpan > 1) {
                                        var prevHeight = index_4.getRowsHeight(args.sheet, i, i + (cell.rowSpan - 1));
                                        if (prevHeight >= maxHgt) {
                                            return;
                                        }
                                        hgt = maxHgt = index_5.getRowHeight(args.sheet, i) + (maxHgt - prevHeight);
                                    }
                                    index_1.setMaxHgt(args.sheet, i, j, hgt);
                                }
                                else {
                                    if (displayText.indexOf('\n') > -1) {
                                        ele.classList.add('e-alt-unwrap');
                                    }
                                    hgt = index_2.getTextHeightWithBorder(this.parent, i, j, args.sheet, cell.style || this.parent.cellStyle, 1);
                                    index_1.setMaxHgt(args.sheet, i, j, hgt);
                                    maxHgt = Math.max(index_1.getMaxHgt(args.sheet, i), 20);
                                }
                            }
                            else Eif (!args.wrap || !displayText) {
                                index_1.setMaxHgt(args.sheet, i, j, 20);
                                maxHgt = Math.max(index_1.getMaxHgt(args.sheet, i), 20);
                            }
                            if (j === args.range[3] && ((args.wrap && maxHgt > 20 && index_1.getMaxHgt(args.sheet, i) <= maxHgt) || ((!args.wrap ||
                                !displayText) && index_1.getMaxHgt(args.sheet, i) < index_5.getRowHeight(args.sheet, i) && index_5.getRowHeight(args.sheet, i) > 20))) {
                                index_1.setRowEleHeight(this.parent, args.sheet, maxHgt, i, args.row, args.hRow);
                            }
                        }
                        if (isCustomHgt && !isMerge) {
                            var displayText = this.parent.getDisplayText(cell);
                            Eif (args.wrap) {
                                Iif (ele && ele.classList.contains('e-alt-unwrap')) {
                                    ele.classList.remove('e-alt-unwrap');
                                }
                            }
                            else {
                                if (displayText.indexOf('\n') > -1) {
                                    ele.classList.add('e-alt-unwrap');
                                }
                            }
                        }
                    }
                }
                if (!args.initial) {
                    this.parent.notify(index_3.positionAutoFillElement, null);
                }
            }
        };
        WrapText.prototype.ribbonClickHandler = function (args) {
            var target = ej2_base_1.closest(args.originalEvent.target, '.e-btn');
            if (target && target.id === this.parent.element.id + '_wrap') {
                var wrap = target.classList.contains('e-active');
                var address = index_5.getAddressFromSelectedRange(this.parent.getActiveSheet());
                var eventArgs = { address: address, wrap: wrap, cancel: false };
                this.parent.notify(index_1.beginAction, { action: 'beforeWrap', eventArgs: eventArgs });
                Eif (!eventArgs.cancel) {
                    index_4.wrap(this.parent.getActiveSheet().selectedRange, wrap, this.parent);
                    this.parent.notify(index_2.completeAction, { action: 'wrap', eventArgs: { address: address, wrap: wrap } });
                }
            }
        };
        WrapText.prototype.rowHeightChangedHandler = function (args) {
            if (args.isCustomHgt) {
                var sheet = this.parent.getActiveSheet();
                for (var i = this.parent.viewport.leftIndex, len = this.parent.viewport.rightIndex; i <= len; i++) {
                    if (index_4.getCell(args.rowIdx, i, sheet, false, true).wrap) {
                        this.updateWrapCell(args.rowIdx, i, sheet, this.parent.getCell(args.rowIdx, i));
                    }
                }
            }
        };
        WrapText.prototype.colWidthChanged = function (args) {
            Eif (args.checkWrapCell) {
                var sheet = this.parent.getActiveSheet();
                for (var i = this.parent.viewport.topIndex, len = this.parent.viewport.bottomIndex; i <= len; i++) {
                    Iif (index_4.getCell(i, args.colIdx, sheet, false, true).wrap) {
                        this.updateWrapCell(i, args.colIdx, sheet, this.parent.getCell(i, args.colIdx));
                    }
                }
            }
        };
        WrapText.prototype.updateWrapCell = function (rowIdx, colIdx, sheet, ele) {
            if (ele && !ele.querySelector('.e-wrap-content')) {
                var wrapSpan = this.wrapCell.cloneNode();
                while (ele.childElementCount) {
                    wrapSpan.appendChild(ele.firstElementChild);
                }
                Eif (!index_4.getCell(rowIdx, colIdx, sheet, false, true).hyperlink) {
                    var node = ele.lastChild;
                    Eif (node && node.nodeType === 3) {
                        wrapSpan.appendChild(document.createTextNode(node.textContent));
                        node.textContent = '';
                    }
                    else {
                        wrapSpan.appendChild(document.createTextNode(ele.textContent));
                        ele.textContent = '';
                    }
                }
                ele.appendChild(wrapSpan);
            }
        };
        WrapText.prototype.getModuleName = function () {
            return 'wrapText';
        };
        WrapText.prototype.destroy = function () {
            this.removeEventListener();
            this.parent = null;
        };
        return WrapText;
    }());
    exports.WrapText = WrapText;
});