all files / grid/services/ virtual-row-model-generator.js

100% Statements 136/136
90.67% Branches 68/75
100% Functions 29/29
100% Lines 128/128
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   16× 16× 16× 16× 16× 16× 16×   17× 17× 17× 17× 17× 17× 17× 17× 17× 17× 16×       17× 35× 13×     13× 13× 13×   13× 13×     35×   35× 35× 35×     17× 17× 17× 142×     184×   17×   41×   26×   121×   13×             15× 15× 15× 15× 15×   13× 13× 13× 13× 13× 13× 13× 13× 13× 13× 101× 101× 101× 101× 59×   101×   13×   32× 238× 32×   32×   15× 15× 15× 15× 15× 15× 6510×   15× 6510× 6510× 6510×     14×           34×             20× 20× 42× 20×        
define(["require", "exports", "@syncfusion/ej2-base", "../services/row-model-generator", "../services/group-model-generator"], function (require, exports, ej2_base_1, row_model_generator_1, group_model_generator_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var VirtualRowModelGenerator = (function () {
        function VirtualRowModelGenerator(parent) {
            this.cOffsets = {};
            this.cache = {};
            this.data = {};
            this.groups = {};
            this.parent = parent;
            this.model = this.parent.pageSettings;
            this.rowModelGenerator = this.parent.allowGrouping ? new group_model_generator_1.GroupModelGenerator(this.parent) : new row_model_generator_1.RowModelGenerator(this.parent);
        }
        VirtualRowModelGenerator.prototype.generateRows = function (data, notifyArgs) {
            var _this = this;
            var info = notifyArgs.virtualInfo = notifyArgs.virtualInfo || this.getData();
            var xAxis = info.sentinelInfo && info.sentinelInfo.axis === 'X';
            var page = !xAxis && info.loadNext && !info.loadSelf ? info.nextInfo.page : info.page;
            var result = [];
            var center = ~~(this.model.pageSize / 2);
            var indexes = this.getBlockIndexes(page);
            var loadedBlocks = [];
            this.checkAndResetCache(notifyArgs.requestType);
            if (this.parent.enableColumnVirtualization) {
                info.blockIndexes.forEach(function (value) {
                    if (_this.isBlockAvailable(value)) {
                        _this.cache[value] = _this.rowModelGenerator.refreshRows(_this.cache[value]);
                    }
                });
            }
            info.blockIndexes.forEach(function (value) {
                if (!_this.isBlockAvailable(value)) {
                    var rows = _this.rowModelGenerator.generateRows(data, {
                        virtualInfo: info, startIndex: _this.getStartIndex(value, data)
                    });
                    var median = ~~Math.max(rows.length, _this.model.pageSize) / 2;
                    Eif (!_this.isBlockAvailable(indexes[0])) {
                        _this.cache[indexes[0]] = rows.slice(0, median);
                    }
                    Eif (!_this.isBlockAvailable(indexes[1])) {
                        _this.cache[indexes[1]] = rows.slice(median);
                    }
                }
                if (_this.parent.groupSettings.columns.length && !xAxis && _this.cache[value]) {
                    _this.cache[value] = _this.updateGroupRow(_this.cache[value], value);
                }
                result.push.apply(result, _this.cache[value]);
                Eif (_this.isBlockAvailable(value)) {
                    loadedBlocks.push(value);
                }
            });
            info.blockIndexes = loadedBlocks;
            var grouping = 'records';
            if (this.parent.allowGrouping) {
                this.parent.currentViewData[grouping] = result.map(function (m) { return m.data; });
            }
            else {
                this.parent.currentViewData = result.map(function (m) { return m.data; });
            }
            return result;
        };
        VirtualRowModelGenerator.prototype.getBlockIndexes = function (page) {
            return [page + (page - 1), page * 2];
        };
        VirtualRowModelGenerator.prototype.getPage = function (block) {
            return block % 2 === 0 ? block / 2 : (block + 1) / 2;
        };
        VirtualRowModelGenerator.prototype.isBlockAvailable = function (value) {
            return value in this.cache;
        };
        VirtualRowModelGenerator.prototype.getData = function () {
            return {
                page: this.model.currentPage,
                blockIndexes: this.getBlockIndexes(this.model.currentPage),
                direction: 'down',
                columnIndexes: this.parent.getColumnIndexesInView()
            };
        };
        VirtualRowModelGenerator.prototype.getStartIndex = function (blk, data, full) {
            if (full === void 0) { full = true; }
            var page = this.getPage(blk);
            var even = blk % 2 === 0;
            var index = (page - 1) * this.model.pageSize;
            return full || !even ? index : index + ~~(this.model.pageSize / 2);
        };
        VirtualRowModelGenerator.prototype.getColumnIndexes = function (content) {
            var _this = this;
            if (content === void 0) { content = this.parent.getHeaderContent().firstChild; }
            var indexes = [];
            var sLeft = content.scrollLeft | 0;
            var keys = Object.keys(this.cOffsets);
            var cWidth = content.getBoundingClientRect().width;
            sLeft = Math.min(this.cOffsets[keys.length - 1] - cWidth, sLeft);
            var calWidth = ej2_base_1.Browser.isDevice ? 2 * cWidth : cWidth / 2;
            var left = sLeft + cWidth + (sLeft === 0 ? calWidth : 0);
            keys.some(function (offset, indx, input) {
                var iOffset = Number(offset);
                var offsetVal = _this.cOffsets[offset];
                var border = sLeft - calWidth <= offsetVal && left + calWidth >= offsetVal;
                if (border) {
                    indexes.push(iOffset);
                }
                return left + calWidth < offsetVal;
            });
            return indexes;
        };
        VirtualRowModelGenerator.prototype.checkAndResetCache = function (action) {
            var clear = ['paging', 'refresh', 'sorting', 'filtering', 'searching', 'grouping', 'ungrouping', 'reorder']
                .some(function (value) { return action === value; });
            if (clear) {
                this.cache = {};
                this.data = {};
                this.groups = {};
            }
            return clear;
        };
        VirtualRowModelGenerator.prototype.refreshColOffsets = function () {
            var _this = this;
            var col = 0;
            this.cOffsets = {};
            var gLen = this.parent.groupSettings.columns.length;
            var cols = this.parent.columns;
            var cLen = cols.length;
            var isVisible = function (column) { return column.visible &&
                (!_this.parent.groupSettings.showGroupedColumn ? _this.parent.groupSettings.columns.indexOf(column.field) < 0 : column.visible); };
            this.parent.groupSettings.columns.forEach(function (c, n) { return _this.cOffsets[n] = (_this.cOffsets[n - 1] | 0) + 30; });
            Array.apply(null, Array(cLen)).map(function () { return col++; }).forEach(function (block, i) {
                block = block + gLen;
                _this.cOffsets[block] = (_this.cOffsets[block - 1] | 0) + (isVisible(cols[i]) ? parseInt(cols[i].width, 10) : 0);
            });
        };
        VirtualRowModelGenerator.prototype.updateGroupRow = function (current, block) {
            var _this = this;
            var currentFirst = current[0];
            var rows = [];
            Object.keys(this.cache).forEach(function (key) {
                if (Number(key) < block) {
                    rows = rows.concat(_this.cache[key]);
                }
            });
            if ((currentFirst && currentFirst.isDataRow) || block % 2 === 0) {
                return current;
            }
            return this.iterateGroup(current, rows);
        };
        VirtualRowModelGenerator.prototype.iterateGroup = function (current, rows) {
            var currentFirst = current[0];
            var offset = 0;
            if (currentFirst && currentFirst.isDataRow) {
                return current;
            }
            var isPresent = current.some(function (row) {
                return rows.some(function (oRow, index) {
                    var res = oRow && oRow.data.field !== undefined && oRow.data.field === row.data.field &&
                        oRow.data.key === row.data.key;
                    Eif (res) {
                        offset = index;
                    }
                    return res;
                });
            });
            if (isPresent) {
                current.shift();
                current = this.iterateGroup(current, rows.slice(offset));
            }
            return current;
        };
        VirtualRowModelGenerator.prototype.getRows = function () {
            var _this = this;
            var rows = [];
            Object.keys(this.cache).forEach(function (key) { return rows = rows.concat(_this.cache[key]); });
            return rows;
        };
        return VirtualRowModelGenerator;
    }());
    exports.VirtualRowModelGenerator = VirtualRowModelGenerator;
});