all files / grid/services/ summary-model-generator.js

98.6% Statements 141/143
92.86% Branches 78/84
100% Functions 44/44
99.24% Lines 130/131
9 statements, 5 functions, 9 branches Ignored     
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        564×   583× 583× 583× 1170× 1703×     1170× 545×     583×   443×   260× 260× 184×   260×   260× 260×   210× 210×   208× 208× 208× 198×   208×   198× 198× 198× 198× 198×   1168× 198× 198× 198× 680× 198×   1566× 1168× 1168× 88×   1168×     1168×                 1168×   208× 208× 208× 208× 208× 208× 198× 198× 255×   198×   208×   136×   255× 255× 255× 255× 255× 255× 248×   255× 255× 255× 255×   255× 255× 255× 255× 160× 160×     255× 255× 255×   374×     272×   533×   62× 62×   334×     272×   667×   215× 215× 186× 186×     91×   310×        
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
define(["require", "exports", "../models/row", "../models/column", "@syncfusion/ej2-base", "../base/util", "../base/enum", "../models/cell"], function (require, exports, row_1, column_1, ej2_base_1, util_1, enum_1, cell_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var SummaryModelGenerator = (function () {
        function SummaryModelGenerator(parent) {
            this.parent = parent;
        }
        SummaryModelGenerator.prototype.getData = function () {
            var _this = this;
            var rows = [];
            this.parent.aggregates.slice().forEach(function (row) {
                var columns = row.columns.filter(function (column) {
                    return !(column.footerTemplate || column.groupFooterTemplate || column.groupCaptionTemplate)
                        || _this.columnSelector(column);
                });
                if (columns.length) {
                    rows.push({ columns: columns });
                }
            });
            return rows;
        };
        SummaryModelGenerator.prototype.columnSelector = function (column) {
            return column.footerTemplate !== undefined;
        };
        SummaryModelGenerator.prototype.getColumns = function (start, end) {
            var columns = [];
            if (this.parent.allowGrouping) {
                this.parent.groupSettings.columns.forEach(function (value) { return columns.push(new column_1.Column({})); });
            }
            if (this.parent.detailTemplate || !ej2_base_1.isNullOrUndefined(this.parent.childGrid)) {
                columns.push(new column_1.Column({}));
            }
            columns.push.apply(columns, this.parent.getColumns());
            return ej2_base_1.isNullOrUndefined(start) ? columns : columns.slice(start, end);
        };
        SummaryModelGenerator.prototype.generateRows = function (input, args, start, end) {
            var _this = this;
            if (this.parent.currentViewData.length === 0) {
                return [];
            }
            var data = this.buildSummaryData(input, args);
            var rows = [];
            this.getData().forEach(function (row, index) {
                rows.push(_this.getGeneratedRow(row, data[index], args ? args.level : undefined, start, end));
            });
            return rows;
        };
        SummaryModelGenerator.prototype.getGeneratedRow = function (summaryRow, data, raw, start, end) {
            var _this = this;
            var tmp = [];
            var indents = this.getIndentByLevel(raw);
            var isDetailGridAlone = !ej2_base_1.isNullOrUndefined(this.parent.childGrid);
            var indentLength = this.parent.groupSettings.columns.length + (this.parent.detailTemplate ||
                !ej2_base_1.isNullOrUndefined(this.parent.childGrid) ? 1 : 0);
            this.getColumns(start, end).forEach(function (value, index) { return tmp.push(_this.getGeneratedCell(value, summaryRow, index >= indentLength ? _this.getCellType() : enum_1.CellType.Indent, indents[index], isDetailGridAlone)); });
            var row = new row_1.Row({ data: data, attributes: { class: 'e-summaryrow' } });
            row.cells = tmp;
            row.uid = util_1.getUid('grid-row');
            row.visible = tmp.some(function (cell) { return cell.isDataCell && cell.visible; });
            return row;
        };
        SummaryModelGenerator.prototype.getGeneratedCell = function (column, summaryRow, cellType, indent, isDetailGridAlone) {
            var sColumn = summaryRow.columns.filter(function (scolumn) { return scolumn.columnName === column.field; })[0];
            var attrs = { 'style': { 'textAlign': column.textAlign } };
            if (indent) {
                attrs.class = indent;
            }
            Iif (ej2_base_1.isNullOrUndefined(indent) && isDetailGridAlone) {
                attrs.class = 'e-detailindentcelltop';
            }
            var opt = {
                'visible': column.visible,
                'isDataCell': !ej2_base_1.isNullOrUndefined(sColumn),
                'isTemplate': sColumn && !ej2_base_1.isNullOrUndefined(sColumn.footerTemplate
                    || sColumn.groupFooterTemplate || sColumn.groupCaptionTemplate),
                'column': sColumn || {},
                'attributes': attrs,
                'cellType': cellType
            };
            return new cell_1.Cell(opt);
        };
        SummaryModelGenerator.prototype.buildSummaryData = function (data, args) {
            var _this = this;
            var dummy = [];
            var summaryRows = this.getData();
            var single = {};
            var key = '';
            summaryRows.forEach(function (row) {
                single = {};
                row.columns.forEach(function (column) {
                    single = _this.setTemplate(column, (args && args.aggregates) ? args : data, single);
                });
                dummy.push(single);
            });
            return dummy;
        };
        SummaryModelGenerator.prototype.getIndentByLevel = function (data) {
            return this.parent.groupSettings.columns.map(function () { return 'e-indentcelltop'; });
        };
        SummaryModelGenerator.prototype.setTemplate = function (column, data, single) {
            var _this = this;
            var types = column.type;
            var helper = {};
            var formatFn = column.getFormatter() || (function () { return function (a) { return a; }; })();
            var group = data;
            if (!(types instanceof Array)) {
                types = [column.type];
            }
            types.forEach(function (type) {
                var key = column.field + ' - ' + type.toLowerCase();
                var disp = column.columnName;
                var val = type !== 'Custom' && group.aggregates && key in group.aggregates ? group.aggregates[key] :
                    util_1.calculateAggregate(type, group.aggregates ? group : data, column, _this.parent);
                single[disp] = single[disp] || {};
                single[disp][key] = val;
                single[disp][type] = !ej2_base_1.isNullOrUndefined(val) ? formatFn(val) : ' ';
                if (group.field) {
                    single[disp].field = group.field;
                    single[disp].key = group.key;
                }
            });
            helper.format = column.getFormatter();
            column.setTemplate(helper);
            return single;
        };
        SummaryModelGenerator.prototype.getCellType = function () {
            return enum_1.CellType.Summary;
        };
        return SummaryModelGenerator;
    }());
    exports.SummaryModelGenerator = SummaryModelGenerator;
    var GroupSummaryModelGenerator = (function (_super) {
        __extends(GroupSummaryModelGenerator, _super);
        function GroupSummaryModelGenerator() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        GroupSummaryModelGenerator.prototype.columnSelector = function (column) {
            return column.groupFooterTemplate !== undefined;
        };
        GroupSummaryModelGenerator.prototype.getIndentByLevel = function (level) {
            Iif (level === void 0) { level = this.parent.groupSettings.columns.length; }
            return this.parent.groupSettings.columns.map(function (v, indx) { return indx <= level - 1 ? '' : 'e-indentcelltop'; });
        };
        GroupSummaryModelGenerator.prototype.getCellType = function () {
            return enum_1.CellType.GroupSummary;
        };
        return GroupSummaryModelGenerator;
    }(SummaryModelGenerator));
    exports.GroupSummaryModelGenerator = GroupSummaryModelGenerator;
    var CaptionSummaryModelGenerator = (function (_super) {
        __extends(CaptionSummaryModelGenerator, _super);
        function CaptionSummaryModelGenerator() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        CaptionSummaryModelGenerator.prototype.columnSelector = function (column) {
            return column.groupCaptionTemplate !== undefined;
        };
        CaptionSummaryModelGenerator.prototype.getData = function () {
            var initVal = { columns: [] };
            return [_super.prototype.getData.call(this).reduce(function (prev, cur) {
                    prev.columns = prev.columns.concat(cur.columns);
                    return prev;
                }, initVal)];
        };
        CaptionSummaryModelGenerator.prototype.isEmpty = function () {
            return (this.getData()[0].columns || []).length === 0;
        };
        CaptionSummaryModelGenerator.prototype.getCellType = function () {
            return enum_1.CellType.CaptionSummary;
        };
        return CaptionSummaryModelGenerator;
    }(SummaryModelGenerator));
    exports.CaptionSummaryModelGenerator = CaptionSummaryModelGenerator;
});