all files / spreadsheet/actions/ wrap.js

91.79% Statements 179/195
88.83% Branches 167/188
100% Functions 12/12
91.79% Lines 179/195
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   884× 884× 884×   884× 884× 884× 884× 884×   884× 13× 13× 13× 13× 13×     1237× 1237×     1237× 642×   1237× 1237× 1237× 1237× 1237× 1237× 1237× 1237× 1237× 1237× 1237× 1237× 1237× 1237×     1231×       1237× 1237× 1237× 1237× 1237× 1237× 1237× 1237× 1237× 14×   1223× 1223× 1223× 887× 816× 816×     71× 71×   887×   112×   887×         336×   1223× 1122× 1122× 1122× 765× 765× 717×       1122× 611× 428×     428× 428× 428× 428× 26× 26× 26× 120× 120× 11×   120×   26×     402×   428× 428× 428×             428×     183×   183× 183× 183×     511× 511× 511×   1122× 1122× 1122×   576× 464× 178× 178×         286×             1223× 887× 81× 81× 79×           887× 816× 768×     48×       71×         1237× 652×       1023× 1023× 21× 21× 21× 21×       21× 21× 21× 21×       2654× 134× 134× 5770× 14× 14× 14× 14× 14×           86× 59× 59× 3260×         151× 121× 121× 121×   13×   121× 121×     121× 12× 12×           121× 120× 120× 120× 120×             121× 121×         126650×   884× 884× 884× 884×   884×        
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);
            this.parent.on(index_3.updateWrapCell, this.updateWrapCell, 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);
                this.parent.off(index_3.updateWrapCell, this.updateWrapCell);
            }
        };
        WrapText.prototype.wrapTextHandler = function (args) {
            Eif (args.initial || index_1.inView(this.parent, args.range, true)) {
                Iif (args.isPublic && index_1.isReadOnlyCells(this.parent, args.range)) {
                    return;
                }
                if (args.initial && !args.td && !args.outsideViewport && index_1.inView(this.parent, args.range, true)) {
                    args.initial = false;
                }
                var ele = void 0;
                var cell = void 0;
                var colwidth = void 0;
                var maxHgt = void 0;
                var hgt = void 0;
                var isCustomHgt = void 0;
                var rowCustomHeight = void 0;
                var lineHgt = void 0;
                var row = void 0;
                var visibleRow = void 0;
                var isLessStandardHgt = void 0;
                var filterRange = void 0;
                var frozenRow = this.parent.frozenRowCount(args.sheet);
                if (!ej2_base_1.isNullOrUndefined(args.sheet.standardHeight) && args.sheet.standardHeight < 20) {
                    isLessStandardHgt = true;
                }
                else {
                    filterRange = this.parent.allowFiltering &&
                        this.parent.filterModule.filterRange.has(this.parent.activeSheetIndex) &&
                        this.parent.filterModule.filterRange.get(this.parent.activeSheetIndex).range;
                }
                for (var i = args.range[0]; i <= args.range[2]; i++) {
                    maxHgt = 0;
                    row = index_4.getRow(args.sheet, i);
                    rowCustomHeight = row.customHeight;
                    isCustomHgt = rowCustomHeight || args.isCustomHgt;
                    visibleRow = !index_5.isHiddenRow(args.sheet, i);
                    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 : (visibleRow && !index_5.isHiddenCol(args.sheet, j) && this.parent.getCell(i, j));
                        if (ele) {
                            if (args.wrap) {
                                lineHgt = index_3.getLineHeight(cell.style && cell.style.fontFamily ? cell.style : this.parent.cellStyle);
                                ele.classList.add(index_1.WRAPTEXT);
                            }
                            else {
                                ele.classList.remove(index_1.WRAPTEXT);
                                lineHgt = null;
                            }
                            if (isCustomHgt || isMerge || row.height < 20 || isLessStandardHgt ||
                                (filterRange && i === filterRange[0] && j >= filterRange[1] && j <= filterRange[3])) {
                                this.updateWrapCell({ rowIdx: i, colIdx: j, sheet: args.sheet, ele: ele });
                            }
                            Iif (ej2_base_1.Browser.isIE) {
                                ele.classList.add('e-ie-wrap');
                            }
                        }
                        else {
                            lineHgt = null;
                        }
                        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 (ele && 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) {
                                if (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, lineHgt);
                                    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 (ele && 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, lineHgt);
                                    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);
                            }
                            Eif (j === args.range[3]) {
                                var prevHgt = index_5.getRowHeight(args.sheet, i);
                                if ((args.wrap && maxHgt > 20 && index_1.getMaxHgt(args.sheet, i) <= maxHgt) || ((!args.wrap || !displayText) &&
                                    index_1.getMaxHgt(args.sheet, i) < prevHgt && prevHgt > 20)) {
                                    if (prevHgt !== maxHgt) {
                                        if (ele) {
                                            index_1.setRowEleHeight(this.parent, args.sheet, maxHgt, i, args.row, args.hRow, visibleRow);
                                            Iif (ele && args.sheet.conditionalFormats && args.sheet.conditionalFormats.length) {
                                                this.parent.notify(index_4.applyCF, { indexes: [i, j], isAction: true });
                                            }
                                        }
                                        else {
                                            index_1.setRowEleHeight(this.parent, args.sheet, maxHgt, i, null, null, visibleRow, !visibleRow || i > this.parent.viewport.bottomIndex ||
                                                (i >= frozenRow && i < this.parent.viewport.topIndex + frozenRow));
                                        }
                                    }
                                }
                            }
                        }
                        if (ele) {
                            if (isCustomHgt && !isMerge) {
                                var displayText = this.parent.getDisplayText(cell);
                                if (args.wrap) {
                                    Iif (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.wrap) {
                                if (!rowCustomHeight) {
                                    ele.style.lineHeight = (parseFloat((cell.style && cell.style.fontSize) || this.parent.cellStyle.fontSize) *
                                        lineHgt) + 'pt';
                                }
                                else if (ele.style.lineHeight) {
                                    ele.style.lineHeight = '';
                                }
                            }
                            else {
                                ele.style.lineHeight = '';
                            }
                        }
                    }
                }
                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 };
                Iif (index_1.isReadOnlyCells(this.parent)) {
                    this.parent.notify(index_1.readonlyAlert, null);
                    return;
                }
                this.parent.notify(index_5.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) {
                args.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, args.sheet, false, true).wrap) {
                        args.colIdx = i;
                        args.ele = this.parent.getCell(args.rowIdx, i);
                        this.updateWrapCell(args);
                        Eif (args.ele.style.lineHeight) {
                            args.ele.style.lineHeight = '';
                        }
                    }
                }
            }
        };
        WrapText.prototype.colWidthChanged = function (args) {
            if (args.checkWrapCell) {
                args.sheet = this.parent.getActiveSheet();
                for (var i = this.parent.viewport.topIndex, len = this.parent.viewport.bottomIndex; i <= len; i++) {
                    if (index_4.getCell(i, args.colIdx, args.sheet, false, true).wrap) {
                        args.rowIdx = i;
                        args.ele = this.parent.getCell(i, args.colIdx);
                        this.updateWrapCell(args);
                    }
                }
            }
        };
        WrapText.prototype.updateWrapCell = function (args) {
            if (args.ele && !args.ele.querySelector('.e-wrap-content')) {
                var wrapSpan = this.wrapCell.cloneNode();
                var filterBtn = args.ele.querySelector('.e-filter-btn');
                while (args.ele.childElementCount && !ej2_base_1.isNullOrUndefined(args.ele.firstElementChild) &&
                    args.ele.firstElementChild.className.indexOf('e-addNoteIndicator') === -1) {
                    wrapSpan.appendChild(args.ele.firstElementChild);
                }
                var nodeElement = void 0;
                Iif (!ej2_base_1.isNullOrUndefined(args.ele.firstElementChild) && args.ele.firstElementChild.className.indexOf('e-addNoteIndicator') > -1) {
                    nodeElement = args.ele.firstElementChild;
                }
                if (filterBtn) {
                    Eif (args.ele.firstChild) {
                        args.ele.insertBefore(filterBtn, args.ele.firstChild);
                    }
                    else {
                        args.ele.appendChild(filterBtn);
                    }
                }
                if (!index_4.getCell(args.rowIdx, args.colIdx, args.sheet, false, true).hyperlink) {
                    var node = args.ele.lastChild;
                    Eif (node && node.nodeType === 3) {
                        wrapSpan.appendChild(document.createTextNode(node.textContent));
                        node.textContent = '';
                    }
                    else {
                        wrapSpan.appendChild(document.createTextNode(args.ele.textContent));
                        args.ele.textContent = '';
                    }
                }
                args.ele.appendChild(wrapSpan);
                Iif (!ej2_base_1.isNullOrUndefined(nodeElement)) {
                    args.ele.appendChild(nodeElement);
                }
            }
        };
        WrapText.prototype.getModuleName = function () {
            return 'wrapText';
        };
        WrapText.prototype.destroy = function () {
            this.removeEventListener();
            Eif (this.wrapCell) {
                this.wrapCell.remove();
                this.wrapCell = null;
            }
            this.parent = null;
        };
        return WrapText;
    }());
    exports.WrapText = WrapText;
});