all files / grid/renderer/ cell-merge-renderer.js

100% Statements 85/85
92.86% Branches 52/56
100% Functions 14/14
100% Lines 84/84
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   170× 170×   123× 123×   123×   123× 123× 123× 123× 96× 96×   96× 96× 699×   123×   123× 134×     127×     123×       123× 16× 16×   123× 91×   123× 10× 10× 10× 10×     123× 11× 11× 11×   123×   16×   123×   17×   35×   17×   18×   65×   17×   46× 46× 16× 16× 16× 16× 16× 16× 16× 16× 136× 136× 16× 16× 16×         46×   16× 8000× 16×   7984×   16×        
define(["require", "exports", "@syncfusion/ej2-base", "../base/util", "../base/enum"], function (require, exports, ej2_base_1, util_1, enum_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var CellMergeRender = (function () {
        function CellMergeRender(serviceLocator, parent) {
            this.serviceLocator = serviceLocator;
            this.parent = parent;
        }
        CellMergeRender.prototype.render = function (cellArgs, row, i, td) {
            var cellRendererFact = this.serviceLocator.getService('cellRendererFactory');
            var cellRenderer = cellRendererFact.getCellRenderer(row.cells[parseInt(i.toString(), 10)].cellType
                || enum_1.CellType.Data);
            var colSpan = row.cells[parseInt(i.toString(), 10)].cellSpan ? row.cells[parseInt(i.toString(), 10)].cellSpan :
                (cellArgs.colSpan + i) <= row.cells.length ? cellArgs.colSpan : row.cells.length - i;
            var rowSpan = cellArgs.rowSpan;
            var visible = 0;
            var spannedCell;
            if (row.index > 0) {
                var rowsObject = this.parent.getRowsObject();
                var cells = this.parent.groupSettings.columns.length > 0 &&
                    !rowsObject[row.index - 1].isDataRow ? rowsObject[row.index].cells : rowsObject[row.index - 1].cells;
                var targetCell_1 = row.cells[parseInt(i.toString(), 10)];
                var uid_1 = 'uid';
                spannedCell = cells.filter(function (cell) { return cell.column.uid === targetCell_1.column["" + uid_1]; })[0];
            }
            var colSpanLen = spannedCell && spannedCell.colSpanRange > 1 && spannedCell.rowSpanRange > 1 ?
                spannedCell.colSpanRange : colSpan;
            for (var j = i + 1; j < i + colSpanLen && j < row.cells.length; j++) {
                if (row.cells[parseInt(j.toString(), 10)].visible === false) {
                    visible++;
                }
                else {
                    row.cells[parseInt(j.toString(), 10)].isSpanned = true;
                }
            }
            if (visible > 0) {
                for (var j = i + colSpan; j < i + colSpan + visible && j < row.cells.length; j++) {
                    row.cells[parseInt(j.toString(), 10)].isSpanned = true;
                }
                if (i + colSpan + visible >= row.cells.length) {
                    colSpan -= (i + colSpan + visible) - row.cells.length;
                }
            }
            if (row.cells[parseInt(i.toString(), 10)].cellSpan) {
                row.data[cellArgs.column.field] = row.cells[parseInt(i.toString(), 10)].spanText;
                td = cellRenderer.render(row.cells[parseInt(i.toString(), 10)], row.data, { 'index': !ej2_base_1.isNullOrUndefined(row.index) ? row.index.toString() : '' });
            }
            if (colSpan > 1) {
                ej2_base_1.attributes(td, { 'colSpan': colSpan.toString(), 'aria-colSpan': colSpan.toString() });
            }
            if (rowSpan > 1) {
                ej2_base_1.attributes(td, { 'rowspan': rowSpan.toString(), 'aria-rowspan': rowSpan.toString() });
                row.cells[parseInt(i.toString(), 10)].isRowSpanned = true;
                row.cells[parseInt(i.toString(), 10)].rowSpanRange = Number(rowSpan);
                if (colSpan > 1) {
                    row.cells[parseInt(i.toString(), 10)].colSpanRange = Number(colSpan);
                }
            }
            if (row.index > 0 && (spannedCell.rowSpanRange > 1)) {
                row.cells[parseInt(i.toString(), 10)].isSpanned = true;
                row.cells[parseInt(i.toString(), 10)].rowSpanRange = Number(spannedCell.rowSpanRange - 1);
                row.cells[parseInt(i.toString(), 10)].colSpanRange = spannedCell.rowSpanRange > 0 ? spannedCell.colSpanRange : 1;
            }
            if (this.parent.enableColumnVirtualization && !row.cells[parseInt(i.toString(), 10)].cellSpan &&
                !this.containsKey(cellArgs.column.field, cellArgs.data[cellArgs.column.field])) {
                this.backupMergeCells(cellArgs.column.field, cellArgs.data[cellArgs.column.field], cellArgs.colSpan);
            }
            return td;
        };
        CellMergeRender.prototype.backupMergeCells = function (fName, data, span) {
            this.setMergeCells(this.generteKey(fName, data), span);
        };
        CellMergeRender.prototype.generteKey = function (fname, data) {
            return fname + '__' + data.toString();
        };
        CellMergeRender.prototype.splitKey = function (key) {
            return key.split('__');
        };
        CellMergeRender.prototype.containsKey = function (fname, data) {
            return this.getMergeCells().hasOwnProperty(this.generteKey(fname, data));
        };
        CellMergeRender.prototype.getMergeCells = function () {
            return this.parent.mergeCells;
        };
        CellMergeRender.prototype.setMergeCells = function (key, span) {
            this.parent.mergeCells["" + key] = span;
        };
        CellMergeRender.prototype.updateVirtualCells = function (rows) {
            var mCells = this.getMergeCells();
            for (var _i = 0, _a = Object.keys(mCells); _i < _a.length; _i++) {
                var key = _a[_i];
                var value = mCells["" + key];
                var merge = this.splitKey(key);
                var columnIndex = this.getIndexFromAllColumns(merge[0]);
                var vColumnIndices = this.parent.getColumnIndexesInView();
                var span = value - (vColumnIndices[0] - columnIndex);
                Eif (columnIndex < vColumnIndices[0] && span > 1) {
                    for (var _b = 0, rows_1 = rows; _b < rows_1.length; _b++) {
                        var row = rows_1[_b];
                        if (row.data[merge[0]].toString() === merge[1].toString()) {
                            row.cells[0].cellSpan = span;
                            row.cells[0].spanText = merge[1];
                            break;
                        }
                    }
                }
            }
            return rows;
        };
        CellMergeRender.prototype.getIndexFromAllColumns = function (field) {
            var index = util_1.iterateArrayOrObject(this.parent.getVisibleColumns(), function (item, index) {
                if (item.field === field) {
                    return index;
                }
                return undefined;
            })[0];
            return index;
        };
        return CellMergeRender;
    }());
    exports.CellMergeRender = CellMergeRender;
});