all files / grid/renderer/ footer-renderer.js

97.69% Statements 169/173
85.94% Branches 110/128
96.55% Functions 28/29
97.62% Lines 164/168
11 statements, 6 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 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          79× 79× 79× 79× 79× 79× 79×   79× 79× 79× 79× 79× 36×     43×     79× 79× 79× 79× 79×   178×   178× 178×   178×   178× 178× 178× 178× 187× 187×   179× 179×     179× 38×   179×   178×   178×       177×   178×   178×   178× 177×   178× 178×     178×   71×   71× 71×   473× 473× 11×     178×     178×   473×     79×         79×   78×   11× 11× 11×     40× 40×   416× 416× 522× 56×     360×       40× 40× 40× 40× 40× 40× 40× 40×       40× 40×       40×         40×     40× 38×   40× 32× 440× 440× 22× 22×   440× 12×   428× 406×     32× 13× 13×         5132× 5132× 5132× 5132× 5132×   5132×           40× 40×   40× 40× 40× 40× 64× 79× 79×   79× 79× 79× 79× 79×   79×       40×         40×        
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        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 extendStatics(d, b);
    };
    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", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "../base/constant", "./content-renderer", "./row-renderer", "../services/summary-model-generator", "../base/util", "@syncfusion/ej2-data", "../base/string-literals"], function (require, exports, ej2_base_1, ej2_base_2, constant_1, content_renderer_1, row_renderer_1, summary_model_generator_1, util_1, ej2_data_1, literals) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var FooterRenderer = (function (_super) {
        __extends(FooterRenderer, _super);
        function FooterRenderer(gridModule, serviceLocator) {
            var _this = _super.call(this, gridModule, serviceLocator) || this;
            _this.aggregates = {};
            _this.parent = gridModule;
            _this.locator = serviceLocator;
            _this.modelGenerator = new summary_model_generator_1.SummaryModelGenerator(_this.parent);
            _this.addEventListener();
            return _this;
        }
        FooterRenderer.prototype.renderPanel = function () {
            var div = this.parent.createElement('div', { className: literals.gridFooter });
            var innerDiv = this.parent.createElement('div', { className: 'e-summarycontent' });
            div.appendChild(innerDiv);
            this.setPanel(div);
            if (this.parent.getPager() != null) {
                this.parent.element.insertBefore(div, this.parent.getPager());
            }
            else {
                this.parent.element.appendChild(div);
            }
        };
        FooterRenderer.prototype.renderTable = function () {
            var innerDiv = this.createContentTable('_footer_table');
            var table = innerDiv.querySelector('.' + literals.table);
            var tFoot = this.parent.createElement('tfoot');
            table.appendChild(tFoot);
            this.setTable(table);
        };
        FooterRenderer.prototype.renderSummaryContent = function (e, table, cStart, cEnd) {
            var input = this.parent.dataSource instanceof Array ? !this.parent.getDataModule().isRemote() &&
                this.parent.parentDetails ? this.getData() : this.parent.dataSource : this.parent.currentViewData;
            var summaries = this.modelGenerator.getData();
            var dummies = ej2_base_1.isNullOrUndefined(cStart) ? this.modelGenerator.getColumns() :
                this.modelGenerator.getColumns(cStart);
            var rows = ej2_base_1.isNullOrUndefined(cStart) ? this.modelGenerator.generateRows(input, e || this.aggregates) :
                this.modelGenerator.generateRows(input, e || this.aggregates, cStart, cEnd);
            var fragment = document.createDocumentFragment();
            var rowrenderer = new row_renderer_1.RowRenderer(this.locator, null, this.parent);
            rowrenderer.element = this.parent.createElement('TR', { className: 'e-summaryrow', attrs: { role: 'row' } });
            for (var srow = 0, len = summaries.length; srow < len; srow++) {
                var row = rows[parseInt(srow.toString(), 10)];
                if (!row) {
                    continue;
                }
                var tr = rowrenderer.render(row, dummies);
                if (tr.querySelectorAll('.e-leftfreeze').length && tr.querySelectorAll('.e-indentcell').length) {
                    var td = tr.querySelectorAll('.e-indentcell');
                    for (var i = 0; i < td.length; i++) {
                        td[parseInt(i.toString(), 10)].classList.add('e-leftfreeze');
                        util_1.applyStickyLeftRightPosition(td[parseInt(i.toString(), 10)], i * 30, this.parent.enableRtl, 'Left');
                    }
                }
                if (this.parent.isFrozenGrid() && tr.querySelectorAll('.e-summarycell').length) {
                    ej2_base_2.removeClass([].slice.call(tr.querySelectorAll('.e-summarycell')), ['e-freezeleftborder', 'e-freezerightborder']);
                }
                fragment.appendChild(tr);
            }
            var isReactChild = this.parent.parentDetails && this.parent.parentDetails.parentInstObj &&
                this.parent.parentDetails.parentInstObj.isReact;
            if ((this.parent.isReact || isReactChild) && summaries.length && this.parent.isInitialLoad) {
                this.parent.renderTemplates(function () {
                    table.tFoot.innerHTML = '';
                    table.tFoot.appendChild(fragment);
                });
            }
            else {
                table.tFoot.appendChild(fragment);
            }
            this.aggregates = !ej2_base_1.isNullOrUndefined(e) ? e : this.aggregates;
        };
        FooterRenderer.prototype.refresh = function (e) {
            var isReactChild = this.parent.parentDetails && this.parent.parentDetails.parentInstObj &&
                this.parent.parentDetails.parentInstObj.isReact;
            if (!(this.parent.isReact || isReactChild) || !this.parent.isInitialLoad) {
                this.getTable().tFoot.innerHTML = '';
            }
            this.renderSummaryContent(e, this.getTable(), undefined, undefined);
            Iif (ej2_base_1.isNullOrUndefined(e) && this.parent.isAutoFitColumns) {
                this.parent.autoFitColumns();
            }
            this.onScroll();
        };
        FooterRenderer.prototype.refreshCol = function () {
            var mheaderCol = this.parent.enableColumnVirtualization ? this.parent.headerModule.getColGroup() :
                this.parent.element.querySelector('.' + literals.gridHeader).querySelector(literals.colGroup).cloneNode(true);
            this.getTable().replaceChild(mheaderCol, this.getColGroup());
            this.setColGroup(mheaderCol);
        };
        FooterRenderer.prototype.onWidthChange = function (args) {
            this.getColFromIndex(args.index).style.width = ej2_base_2.formatUnit(args.width);
            if (this.parent.allowResizing && args.module === 'resize') {
                this.updateFooterTableWidth(this.getTable());
            }
        };
        FooterRenderer.prototype.onScroll = function (e) {
            Eif (e === void 0) { e = {
                left: this.parent.getContent().firstChild.scrollLeft
            }; }
            this.getTable().parentElement.scrollLeft = e.left;
        };
        FooterRenderer.prototype.getColFromIndex = function (index) {
            return this.getColGroup().children[parseInt(index.toString(), 10)];
        };
        FooterRenderer.prototype.columnVisibilityChanged = function () {
            this.refresh();
        };
        FooterRenderer.prototype.addEventListener = function () {
            this.evtHandlers = [{ event: constant_1.colGroupRefresh, handler: this.refreshCol },
                { event: constant_1.columnWidthChanged, handler: this.onWidthChange },
                { event: constant_1.scroll, handler: this.onScroll },
                { event: constant_1.columnVisibilityChanged, handler: this.columnVisibilityChanged },
                { event: constant_1.refreshFooterRenderer, handler: this.refreshFooterRenderer }];
            util_1.addRemoveEventListener(this.parent, this.evtHandlers, true, this);
        };
        FooterRenderer.prototype.removeEventListener = function () {
            util_1.addRemoveEventListener(this.parent, this.evtHandlers, false);
        };
        FooterRenderer.prototype.updateFooterTableWidth = function (tFoot) {
            var tHead = this.parent.getHeaderTable();
            Eif (tHead && tFoot) {
                tFoot.style.width = tHead.style.width;
            }
        };
        FooterRenderer.prototype.refreshFooterRenderer = function (editedData) {
            var aggregates = this.onAggregates(editedData);
            this.refresh(aggregates);
        };
        FooterRenderer.prototype.getIndexByKey = function (data, ds) {
            var key = this.parent.getPrimaryKeyFieldNames()[0];
            for (var i = 0; i < ds.length; i++) {
                if (ds[parseInt(i.toString(), 10)]["" + key] === data["" + key]) {
                    return i;
                }
            }
            return -1;
        };
        FooterRenderer.prototype.getData = function () {
            return this.parent.getDataModule().dataManager.executeLocal(this.parent.getDataModule().generateQuery(true));
        };
        FooterRenderer.prototype.onAggregates = function (editedData) {
            editedData = editedData instanceof Array ? editedData : [];
            var field = this.parent.getPrimaryKeyFieldNames()[0];
            var dataSource = [];
            var isModified = false;
            var batchChanges = {};
            var gridData = 'dataSource';
            var isFiltered = false;
            if (!(this.parent.renderModule.data.isRemote() || (!ej2_base_1.isNullOrUndefined(this.parent.dataSource)
                && this.parent.dataSource.result)) && ((this.parent.allowFiltering
                && this.parent.filterSettings.columns.length) || this.parent.searchSettings.key.length)) {
                isFiltered = true;
            }
            var currentViewData;
            Iif (!ej2_base_1.isNullOrUndefined(this.parent.dataSource) && this.parent.dataSource.result) {
                currentViewData = this.parent.getCurrentViewRecords();
            }
            else {
                currentViewData = this.parent.dataSource instanceof Array ?
                    (isFiltered ? this.parent.getFilteredRecords() : this.parent.dataSource) : (this.parent.dataSource["" + gridData].json.length ?
                    (isFiltered ? this.parent.getFilteredRecords() : this.parent.dataSource["" + gridData].json)
                    : this.parent.getCurrentViewRecords());
            }
            Iif (this.parent.parentDetails && !this.parent.getDataModule().isRemote()) {
                currentViewData = this.getData();
            }
            if (this.parent.editModule) {
                batchChanges = this.parent.editModule.getBatchChanges();
            }
            if (Object.keys(batchChanges).length) {
                for (var i = 0; i < currentViewData.length; i++) {
                    isModified = false;
                    if (batchChanges[literals.changedRecords].length && this.getIndexByKey(currentViewData[parseInt(i.toString(), 10)], batchChanges[literals.changedRecords]) > -1) {
                        isModified = true;
                        dataSource.push(batchChanges[literals.changedRecords][this.getIndexByKey(currentViewData[parseInt(i.toString(), 10)], batchChanges[literals.changedRecords])]);
                    }
                    if (batchChanges[literals.deletedRecords].length && this.getIndexByKey(currentViewData[parseInt(i.toString(), 10)], batchChanges[literals.deletedRecords]) > -1) {
                        isModified = true;
                    }
                    else if (!isModified) {
                        dataSource.push(currentViewData[parseInt(i.toString(), 10)]);
                    }
                }
                if (batchChanges[literals.addedRecords].length) {
                    for (var i = 0; i < batchChanges[literals.addedRecords].length; i++) {
                        dataSource.push(batchChanges[literals.addedRecords][parseInt(i.toString(), 10)]);
                    }
                }
            }
            else {
                if (editedData.length) {
                    var data = util_1.iterateExtend(currentViewData);
                    dataSource = data.map(function (item) {
                        var idVal = ej2_data_1.DataUtil.getObject(field, item);
                        var value;
                        var hasVal = editedData.some(function (cItem) {
                            value = cItem;
                            return idVal === ej2_data_1.DataUtil.getObject(field, cItem);
                        });
                        return hasVal ? value : item;
                    });
                }
                else {
                    dataSource = currentViewData;
                }
            }
            var eData = editedData;
            if ((eData.type && eData.type === 'cancel')) {
                dataSource = currentViewData;
            }
            var aggregate = {};
            var agrVal;
            var aggregateRows = this.parent.aggregates;
            for (var i = 0; i < aggregateRows.length; i++) {
                for (var j = 0; j < aggregateRows[parseInt(i.toString(), 10)].columns.length; j++) {
                    var data = [];
                    var type = aggregateRows[parseInt(i.toString(), 10)]
                        .columns[parseInt(j.toString(), 10)].type.toString();
                    data = dataSource;
                    var types = type.split(',').map(function (t) { return t.trim(); });
                    for (var _i = 0, types_1 = types; _i < types_1.length; _i++) {
                        var aggregateType = types_1[_i];
                        agrVal = util_1.calculateAggregate(aggregateType, data, aggregateRows[parseInt(i.toString(), 10)]
                            .columns[parseInt(j.toString(), 10)], this.parent);
                        aggregate[aggregateRows[parseInt(i.toString(), 10)].columns[parseInt(j.toString(), 10)].field + ' - ' + aggregateType.toLowerCase()] = agrVal;
                    }
                }
            }
            var result = {
                result: dataSource,
                count: dataSource.length,
                aggregates: aggregate
            };
            return result;
        };
        return FooterRenderer;
    }(content_renderer_1.ContentRender));
    exports.FooterRenderer = FooterRenderer;
});