all files / treegrid/actions/ summary.js

96.84% Statements 153/158
84.75% Branches 50/59
100% Functions 17/17
96.77% Lines 150/155
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   27× 27× 27× 27×   11395×   27× 27×     41× 41× 41× 41× 41× 1140× 1140× 1140×     41× 41× 41× 41× 41× 41× 42× 37× 251× 251× 251× 251× 260× 260× 260× 1280×   1280×   260× 260× 251× 251× 9094× 251× 251×     251× 251× 251× 251× 251× 251× 251× 251× 251× 251× 242× 242× 242×     251×         251× 251× 260×   251×         19×   19×       41×   476× 476× 476× 17608× 17608× 630926× 17608× 1840× 1840× 1840× 225×     1615×       476×   264× 264× 404×   404× 404× 1978× 1978× 323× 319×           1655×       264×   323× 323× 323× 323×   323× 323× 323× 323× 323× 323× 323× 323× 323× 323× 323× 323× 323× 323× 323×   323× 323× 323× 323× 323×   323× 323×     323×           323×   323× 323× 323×         323×     323× 316×             27×        
define(["require", "exports", "@syncfusion/ej2-data", "@syncfusion/ej2-grids", "../utils", "@syncfusion/ej2-base"], function (require, exports, ej2_data_1, ej2_grids_1, utils_1, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Aggregate = (function () {
        function Aggregate(parent) {
            ej2_grids_1.Grid.Inject(ej2_grids_1.Aggregate);
            this.parent = parent;
            this.flatChildRecords = [];
            this.summaryQuery = [];
        }
        Aggregate.prototype.getModuleName = function () {
            return 'summary';
        };
        Aggregate.prototype.removeEventListener = function () {
            Eif (this.parent.isDestroyed) {
                return;
            }
        };
        Aggregate.prototype.calculateSummaryValue = function (summaryQuery, filteredData, isSort) {
            this.summaryQuery = summaryQuery;
            var parentRecord;
            var parentDataLength = Object.keys(filteredData).length;
            var parentData = [];
            for (var p = 0, len = parentDataLength; p < len; p++) {
                var summaryRow = ej2_grids_1.getObject('isSummaryRow', filteredData[parseInt(p.toString(), 10)]);
                Eif (!summaryRow) {
                    parentData.push(filteredData[parseInt(p.toString(), 10)]);
                }
            }
            var parentRecords = utils_1.findParentRecords(parentData);
            var flatRecords = parentData.slice();
            var summaryLength = Object.keys(this.parent.aggregates).length;
            var dataLength = Object.keys(parentRecords).length;
            var childRecordsLength;
            var columns = this.parent.getColumns();
            if (this.parent.aggregates.filter(function (x) { return x.showChildSummary; }).length) {
                for (var i = 0, len = dataLength; i < len; i++) {
                    parentRecord = parentRecords[parseInt(i.toString(), 10)];
                    childRecordsLength = this.getChildRecordsLength(parentRecord, flatRecords);
                    Eif (childRecordsLength) {
                        var _loop_1 = function (summaryRowIndex, len_1) {
                            var item = void 0;
                            item = {};
                            for (var i_1 = 0; i_1 < columns.length; i_1++) {
                                var field = (ej2_base_1.isNullOrUndefined(ej2_grids_1.getObject('field', columns[parseInt(i_1.toString(), 10)]))) ?
                                    columns[parseInt(i_1.toString(), 10)] : ej2_grids_1.getObject('field', (columns[parseInt(i_1.toString(), 10)]));
                                item["" + field] = null;
                            }
                            item = this_1.createSummaryItem(item, this_1.parent.aggregates[summaryRowIndex - 1]);
                            if (this_1.parent.aggregates[summaryRowIndex - 1].showChildSummary) {
                                var idx_1;
                                flatRecords.map(function (e, i) {
                                    if (e.uniqueID === parentRecord.uniqueID) {
                                        idx_1 = i;
                                        return;
                                    }
                                });
                                var currentIndex = idx_1 + childRecordsLength + summaryRowIndex;
                                var summaryParent = ej2_base_1.extend({}, parentRecord);
                                delete summaryParent.childRecords;
                                delete summaryParent[this_1.parent.childMapping];
                                ej2_base_1.setValue('parentItem', summaryParent, item);
                                var level = ej2_grids_1.getObject('level', summaryParent);
                                ej2_base_1.setValue('level', level + 1, item);
                                ej2_base_1.setValue('isSummaryRow', true, item);
                                ej2_base_1.setValue('parentUniqueID', summaryParent.uniqueID, item);
                                if (isSort) {
                                    var childRecords = ej2_grids_1.getObject('childRecords', parentRecord);
                                    Eif (childRecords.length) {
                                        childRecords.push(item);
                                    }
                                }
                                flatRecords.splice(currentIndex, 0, item);
                            }
                            else {
                                return "continue";
                            }
                        };
                        var this_1 = this;
                        for (var summaryRowIndex = 1, len_1 = summaryLength; summaryRowIndex <= len_1; summaryRowIndex++) {
                            _loop_1(summaryRowIndex, len_1);
                        }
                        this.flatChildRecords = [];
                    }
                }
            }
            else {
                var items = {};
                for (var columnIndex = 0, length_1 = columns.length; columnIndex < length_1; columnIndex++) {
                    var fields = ej2_base_1.isNullOrUndefined(ej2_grids_1.getObject('field', columns[parseInt(columnIndex.toString(), 10)])) ?
                        columns[parseInt(columnIndex.toString(), 10)] : ej2_grids_1.getObject('field', columns[parseInt(columnIndex.toString(), 10)]);
                    items["" + fields] = null;
                }
                for (var summaryRowIndex = 1, length_2 = summaryLength; summaryRowIndex <= length_2; summaryRowIndex++) {
                    this.createSummaryItem(items, this.parent.aggregates[summaryRowIndex - 1]);
                }
            }
            return flatRecords;
        };
        Aggregate.prototype.getChildRecordsLength = function (parentData, flatData) {
            var recordLength = Object.keys(flatData).length;
            var record;
            for (var i = 0, len = recordLength; i < len; i++) {
                record = flatData[parseInt(i.toString(), 10)];
                var parent_1 = ej2_base_1.isNullOrUndefined(record.parentItem) ? null :
                    flatData.filter(function (e) { return e.uniqueID === record.parentItem.uniqueID; })[0];
                if (parentData === parent_1) {
                    this.flatChildRecords.push(record);
                    var hasChild = ej2_grids_1.getObject('hasChildRecords', record);
                    if (hasChild) {
                        this.getChildRecordsLength(record, flatData);
                    }
                    else {
                        continue;
                    }
                }
            }
            return this.flatChildRecords.length;
        };
        Aggregate.prototype.createSummaryItem = function (itemData, summary) {
            var summaryColumnLength = Object.keys(summary.columns).length;
            for (var i = 0, len = summaryColumnLength; i < len; i++) {
                var displayColumn = ej2_base_1.isNullOrUndefined(summary.columns[parseInt(i.toString(), 10)].columnName) ?
                    summary.columns[parseInt(i.toString(), 10)].field : summary.columns[parseInt(i.toString(), 10)].columnName;
                var keys = Object.keys(itemData);
                for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
                    var key = keys_1[_i];
                    if (key === displayColumn) {
                        if (this.flatChildRecords.length) {
                            itemData["" + key] = this.getSummaryValues(summary.columns[parseInt(i.toString(), 10)], this.flatChildRecords);
                        }
                        else Eif (this.parent.isLocalData) {
                            var data = this.parent.dataSource instanceof ej2_data_1.DataManager ? this.parent.dataSource.dataSource.json
                                : this.parent.flatData;
                            itemData["" + key] = this.getSummaryValues(summary.columns[parseInt(i.toString(), 10)], data);
                        }
                    }
                    else {
                        continue;
                    }
                }
            }
            return itemData;
        };
        Aggregate.prototype.getSummaryValues = function (summaryColumn, summaryData) {
            var qry = new ej2_data_1.Query();
            var single = {};
            var helper = {};
            var type = !ej2_base_1.isNullOrUndefined(summaryColumn.field) ?
                this.parent.getColumnByField(summaryColumn.field).type : undefined;
            summaryColumn.setPropertiesSilent({ format: this.getFormatFromType(summaryColumn.format, type) });
            summaryColumn.setFormatter(this.parent.grid.locale);
            var formatFn = summaryColumn.getFormatter() || (function () { return function (a) { return a; }; })();
            summaryColumn.setTemplate(helper);
            var tempObj = summaryColumn.getTemplate(2);
            qry.queries = this.summaryQuery;
            qry.requiresCount();
            var sumData = new ej2_data_1.DataManager(summaryData).executeLocal(qry);
            var types = summaryColumn.type;
            var summaryKey;
            types = [summaryColumn.type];
            for (var i = 0; i < types.length; i++) {
                summaryKey = types[parseInt(i.toString(), 10)];
                var key = summaryColumn.field + ' - ' + types[parseInt(i.toString(), 10)].toLowerCase();
                var val = types[parseInt(i.toString(), 10)] !== 'Custom' ? ej2_grids_1.getObject('aggregates', sumData) :
                    ej2_grids_1.calculateAggregate(types[parseInt(i.toString(), 10)], sumData, summaryColumn, this.parent);
                var disp = summaryColumn.columnName;
                var value_1 = types[parseInt(i.toString(), 10)] !== 'Custom' ? val["" + key] : val;
                single["" + disp] = single["" + disp] || {};
                single["" + disp]["" + key] = value_1;
                single["" + disp][types[parseInt(i.toString(), 10)]] = !ej2_base_1.isNullOrUndefined(val) ? formatFn(value_1) : ' ';
            }
            helper.format = summaryColumn.getFormatter();
            var cellElement = ej2_base_1.createElement('td', {
                className: 'e-summary'
            });
            Iif (this.parent.isReact) {
                var renderReactTemplates = 'renderReactTemplates';
                tempObj.fn(single[summaryColumn.columnName], this.parent, tempObj.property, '', null, null, cellElement);
                this.parent["" + renderReactTemplates]();
            }
            else {
                ej2_grids_1.appendChildren(cellElement, tempObj.fn(single[summaryColumn.columnName], this.parent, tempObj.property));
            }
            var value = single["" + summaryColumn.columnName]["" + summaryKey];
            var summaryValue;
            Iif (cellElement.innerHTML.indexOf(value) === -1) {
                summaryValue = cellElement.innerHTML + value;
                return summaryValue;
            }
            else {
                return cellElement.innerHTML;
            }
        };
        Aggregate.prototype.getFormatFromType = function (summaryformat, type) {
            if (ej2_base_1.isNullOrUndefined(type) || typeof summaryformat !== 'string') {
                return summaryformat;
            }
            var obj;
            switch (type) {
                case 'number':
                    obj = { format: summaryformat };
                    break;
                case 'datetime':
                    obj = { type: 'dateTime', skeleton: summaryformat };
                    break;
                case 'date':
                    obj = { type: type, skeleton: summaryformat };
                    break;
            }
            return obj;
        };
        Aggregate.prototype.destroy = function () {
            this.removeEventListener();
        };
        return Aggregate;
    }());
    exports.Aggregate = Aggregate;
});