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

97.29% Statements 251/258
88.4% Branches 160/181
96.97% Functions 32/33
97.58% Lines 242/248
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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364   114× 114× 114× 114× 114× 114× 114× 114× 114× 114×   42× 42×             40×   42×   198× 198× 42×   156× 156×     156× 156× 156× 156× 156× 156× 156×     156× 156× 8796× 11× 11×     145× 40× 80× 18×         145× 145× 301× 128×   128×   128×     128× 15× 15× 15× 15×   128× 128× 15×     113×   112×                     109× 109× 109×   109× 109×         301×   34×     301× 301× 301× 301× 41× 935× 814×       301×   301× 301×     145× 145× 301×   145×       145× 145× 145× 605×     4468×   145× 78×     145× 29×     143×           439×   274×   1055×   112×             129× 129× 129× 129× 129×   75× 75× 75× 75× 75× 75× 75× 75× 75× 75× 75× 75× 19× 19× 12× 12×           19×     75× 886× 886× 886× 886× 480×   886×   75×   296×   2597× 296× 72× 72× 72×   296×   116× 116× 116× 116× 116× 116× 8187×   116× 116× 18×   8187× 116× 8187× 8187×         36× 36× 36× 36× 2368× 19×     36× 19×   17×   18× 18× 18×   17× 311×           17×   17×   2640× 2640× 2640× 7856×   2640×   38× 38× 38× 406×   38×   15× 15× 15× 1179× 1179× 1179× 18693× 1160×   17533× 17533× 8870×              
define(["require", "exports", "@syncfusion/ej2-base", "../base/util", "../services/row-model-generator", "../services/group-model-generator", "../base/constant", "../base/string-literals"], function (require, exports, ej2_base_1, util_1, row_model_generator_1, group_model_generator_1, events, literals) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var VirtualRowModelGenerator = (function () {
        function VirtualRowModelGenerator(parent) {
            this.cOffsets = {};
            this.cache = {};
            this.rowCache = {};
            this.data = {};
            this.groups = {};
            this.currentInfo = {};
            this.prevInfo = {};
            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.columnInfiniteRows = function (data, e) {
            var result = [];
            if (e.requestType === 'virtualscroll') {
                var rows = this.parent.getRowsObject();
                result.push.apply(result, this.rowModelGenerator.refreshRows(rows));
                Eif (this.parent.infiniteScrollSettings.enableCache) {
                    var currentRowStartIndex = this.parent.frozenRows && this.parent.pageSettings.currentPage === 1 ? 0
                        : util_1.getRowIndexFromElement(this.parent.getContentTable().querySelector('.e-row:not(.e-addedrow)'));
                    var newResult = result
                        .slice(currentRowStartIndex, currentRowStartIndex + (this.parent.pageSettings.pageSize * 3));
                    Eif (this.parent.frozenRows && this.parent.pageSettings.currentPage !== 1) {
                        newResult = result.slice(0, this.parent.frozenRows).concat(newResult);
                    }
                    result = newResult;
                }
            }
            else {
                result.push.apply(result, this.rowModelGenerator.generateRows(data, e));
            }
            return result;
        };
        VirtualRowModelGenerator.prototype.generateRows = function (data, e) {
            var _this = this;
            if (this.parent.enableColumnVirtualization && this.parent.enableInfiniteScrolling) {
                return this.columnInfiniteRows(data, e);
            }
            var isManualRefresh = false;
            var info = e.virtualInfo = e.virtualInfo
                || ((e.requestType === 'sorting' || e.requestType === 'delete') && util_1.checkIsVirtual(this.parent) && this.prevInfo)
                || this.getData();
            this.prevInfo = info;
            var xAxis = info.sentinelInfo && info.sentinelInfo.axis === 'X';
            var page = !xAxis && info.loadNext && !info.loadSelf ? info.nextInfo.page : info.page;
            var result = [];
            var indexes = this.getBlockIndexes(page);
            var loadedBlocks = [];
            if (this.currentInfo.blockIndexes) {
                indexes = info.blockIndexes = e.virtualInfo.blockIndexes = this.includePrevPage ? this.currentInfo.blockIndexes.slice(1)
                    : this.currentInfo.blockIndexes.slice(0, this.currentInfo.blockIndexes.length - 1);
                isManualRefresh = true;
            }
            this.checkAndResetCache(e.requestType);
            if (util_1.isGroupAdaptive(this.parent) && this.parent.vcRows.length) {
                var dataRows = this.parent.vcRows.filter(function (row) { return row.isDataRow; });
                Eif ((this.parent.isManualRefresh && dataRows.length === data['records'].length) || !this.parent.isManualRefresh) {
                    return result = this.parent.vcRows;
                }
            }
            if (this.parent.enableColumnVirtualization) {
                for (var i = 0; i < info.blockIndexes.length; i++) {
                    if (this.isBlockAvailable(info.blockIndexes[parseInt(i.toString(), 10)])) {
                        this.cache[info.blockIndexes[parseInt(i.toString(), 10)]] =
                            this.rowModelGenerator.refreshRows(this.cache[info.blockIndexes[parseInt(i.toString(), 10)]]);
                    }
                }
            }
            var values = info.blockIndexes;
            var _loop_1 = function (i) {
                if (!this_1.isBlockAvailable(values[parseInt(i.toString(), 10)])) {
                    var startIdx = !ej2_base_1.isNullOrUndefined(this_1.startIndex) ? this_1.startIndex :
                        this_1.getStartIndex(values[parseInt(i.toString(), 10)], data);
                    startIdx = util_1.isGroupAdaptive(this_1.parent) && !this_1.parent.vcRows.length && (e.requestType === 'sorting'
                        || e.requestType === 'delete') ? 0 : startIdx;
                    var rows = this_1.rowModelGenerator.generateRows(data, {
                        virtualInfo: info, startIndex: startIdx
                    });
                    if (util_1.isGroupAdaptive(this_1.parent) && !this_1.parent.vcRows.length) {
                        this_1.recordsCount = data.records.length;
                        this_1.parent.vRows = rows;
                        this_1.parent.vcRows = rows;
                        this_1.parent.notify(events.refreshVirtualMaxPage, {});
                    }
                    var median = void 0;
                    if (util_1.isGroupAdaptive(this_1.parent)) {
                        this_1.getGroupVirtualRecordsByIndex(rows);
                    }
                    else {
                        if (isManualRefresh) {
                            this_1.setBlockForManualRefresh(this_1.cache, indexes, rows);
                        }
                        else if ((e.requestType === 'sorting' || e.requestType === 'delete') && util_1.checkIsVirtual(this_1.parent)) {
                            var visiblePage = util_1.getVisiblePage(info.blockIndexes);
                            var prevEndIndex = 0;
                            for (var i_1 = 0; i_1 < visiblePage.length; i_1++) {
                                var indexes_1 = this_1.getBlockIndexes(visiblePage[parseInt(i_1.toString(), 10)]);
                                var startIndex = this_1.model.pageSize * i_1;
                                var endIndex = startIndex + this_1.model.pageSize;
                                Iif (this_1.parent.allowGrouping && this_1.parent.groupSettings.columns.length) {
                                    var dataRowObject = rows.filter(function (row) { return row.isDataRow; })
                                        .slice(startIndex, endIndex);
                                    startIndex = prevEndIndex;
                                    endIndex = rows.indexOf(dataRowObject[dataRowObject.length - 1]) + 1;
                                }
                                var pageRecord = rows.slice(startIndex, endIndex);
                                var median_1 = ~~Math.max(pageRecord.length, this_1.model.pageSize) / 2;
                                Eif (!this_1.isBlockAvailable(indexes_1[0])) {
                                    this_1.cache[indexes_1[0]] = pageRecord.slice(0, median_1);
                                }
                                Eif (!this_1.isBlockAvailable(indexes_1[1])) {
                                    this_1.cache[indexes_1[1]] = pageRecord.slice(median_1);
                                }
                                prevEndIndex = endIndex;
                            }
                        }
                        else {
                            median = ~~Math.max(rows.length, this_1.model.pageSize) / 2;
                            Eif (!this_1.isBlockAvailable(indexes[0])) {
                                this_1.cache[indexes[0]] = rows.slice(0, median);
                            }
                            Eif (!this_1.isBlockAvailable(indexes[1])) {
                                this_1.cache[indexes[1]] = rows.slice(median);
                            }
                        }
                    }
                }
                if (this_1.parent.groupSettings.columns.length && !xAxis && this_1.cache[values[parseInt(i.toString(), 10)]] &&
                    !this_1.parent.groupSettings.enableLazyLoading) {
                    this_1.cache[values[parseInt(i.toString(), 10)]] =
                        this_1.updateGroupRow(this_1.cache[values[parseInt(i.toString(), 10)]], values[parseInt(i.toString(), 10)]);
                }
                Eif (!e.renderMovableContent && !e.renderFrozenRightContent && this_1.cache[values[parseInt(i.toString(), 10)]]) {
                    result.push.apply(result, this_1.cache[values[parseInt(i.toString(), 10)]]);
                    var DataRecord_1 = [];
                    if (this_1.parent.enableVirtualization && this_1.parent.groupSettings.columns.length) {
                        result.forEach(function (data) {
                            if (!DataRecord_1.includes(data)) {
                                DataRecord_1.push(data);
                            }
                        });
                    }
                    result = DataRecord_1.length ? DataRecord_1 : result;
                }
                Eif (this_1.isBlockAvailable(values[parseInt(i.toString(), 10)])) {
                    loadedBlocks.push(values[parseInt(i.toString(), 10)]);
                }
            };
            var this_1 = this;
            for (var i = 0; i < values.length; i++) {
                _loop_1(i);
            }
            Iif (util_1.isGroupAdaptive(this.parent) && this.parent.vcRows.length && e.requestType === 'sorting'
                && e.scrollTop.top !== 0) {
                return result = this.parent.vcRows;
            }
            info.blockIndexes = loadedBlocks;
            var grouping = 'records';
            if (this.parent.allowGrouping && this.parent.groupSettings.columns.length) {
                this.parent.currentViewData["" + grouping] = result.map(function (m) { return m.data; });
            }
            else {
                this.parent.currentViewData = result.map(function (m) { return m.data; });
            }
            if (e.requestType === 'grouping') {
                this.parent.currentViewData["" + grouping] = this.parent.currentViewData["" + grouping].filter(function (item, index) {
                    return _this.parent.currentViewData["" + grouping].indexOf(item) === index;
                });
            }
            if (util_1.isGroupAdaptive(this.parent) && this.parent.vcRows.length) {
                if (['save', 'delete'].some(function (value) { return e.requestType === value; })) {
                    return result = this.parent.vcRows;
                }
            }
            return result;
        };
        VirtualRowModelGenerator.prototype.setBlockForManualRefresh = function (cache, blocks, rows) {
            var size = this.model.pageSize / 2;
            if (this.includePrevPage) {
                cache[blocks[0] - 1] = rows.slice(0, size);
                cache[blocks[0]] = rows.slice(size, size * 2);
                cache[blocks[1]] = rows.slice(size * 2, size * 3);
                cache[blocks[2]] = rows.slice(size * 3, size * 4);
            }
            else {
                cache[blocks[0]] = rows.slice(0, size);
                cache[blocks[1]] = rows.slice(size, size * 2);
                cache[blocks[2]] = rows.slice(size * 2, size * 3);
                cache[blocks[2] + 1] = rows.slice(size * 3, size * 4);
            }
        };
        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().querySelector('.' + literals.headerContent); }
            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);
            var frzLeftWidth = 0;
            var diffWidth = sLeft - calWidth;
            if (this.parent.isFrozenGrid()) {
                frzLeftWidth = this.parent.leftrightColumnWidth('left');
                if (diffWidth > 0) {
                    for (var i = this.parent.getVisibleFrozenLeftCount() - 1; i >= 0; i--) {
                        Iif (diffWidth <= this.cOffsets[parseInt(i.toString(), 10)]) {
                            frzLeftWidth = frzLeftWidth - this.cOffsets[parseInt(i.toString(), 10)];
                            break;
                        }
                    }
                }
                if (this.parent.getFrozenMode() === literals.leftRight) {
                    var rightCol = this.parent.getVisibleFrozenRightCount();
                    keys.splice((keys.length - 1) - rightCol, rightCol);
                }
            }
            keys.some(function (offset) {
                var iOffset = Number(offset);
                var offsetVal = _this.cOffsets["" + offset];
                var border = (diffWidth + frzLeftWidth) <= offsetVal && (left + calWidth) >= offsetVal;
                if (border) {
                    indexes.push(iOffset);
                }
                return left + calWidth < offsetVal;
            });
            return indexes;
        };
        VirtualRowModelGenerator.prototype.checkAndResetCache = function (action) {
            var actions = ['paging', 'refresh', 'sorting', 'filtering', 'searching', 'grouping', 'ungrouping', 'reorder',
                'save', 'delete'];
            var clear = actions.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.getVisibleColumns();
            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); };
            var c = this.parent.groupSettings.columns;
            for (var i = 0; i < c.length; i++) {
                this.cOffsets[parseInt(i.toString(), 10)] = (this.cOffsets[i - 1] | 0) + 30;
            }
            var blocks = Array.apply(null, Array(cLen)).map(function () { return col++; });
            for (var j = 0; j < blocks.length; j++) {
                blocks[parseInt(j.toString(), 10)] = blocks[parseInt(j.toString(), 10)] + gLen;
                this.cOffsets[blocks[parseInt(j.toString(), 10)]] =
                    (this.cOffsets[blocks[parseInt(j.toString(), 10)] - 1] | 0) + (isVisible(cols[parseInt(j.toString(), 10)]) ?
                        parseInt(cols[parseInt(j.toString(), 10)].width, 10) : 0);
            }
        };
        VirtualRowModelGenerator.prototype.updateGroupRow = function (current, block) {
            var currentFirst = current[0];
            var rows = [];
            var keys = Object.keys(this.cache);
            for (var i = 0; i < keys.length; i++) {
                if (Number(keys[parseInt(i.toString(), 10)]) < block) {
                    rows = rows.concat(this.cache[keys[parseInt(i.toString(), 10)]]);
                }
            }
            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 rows = [];
            var keys = Object.keys(this.cache);
            for (var i = 0; i < keys.length; i++) {
                rows = rows.concat(this.cache[keys[parseInt(i.toString(), 10)]]);
            }
            return rows;
        };
        VirtualRowModelGenerator.prototype.generateCells = function (foreignKeyData) {
            var cells = [];
            var cols = this.parent.columnModel;
            for (var i = 0; i < cols.length; i++) {
                cells.push(this.rowModelGenerator.generateCell(cols[parseInt(i.toString(), 10)], null, null, null, null, foreignKeyData));
            }
            return cells;
        };
        VirtualRowModelGenerator.prototype.getGroupVirtualRecordsByIndex = function (rows) {
            var blocks = this.parent.contentModule.getGroupedTotalBlocks();
            var blockSize = this.parent.contentModule.getBlockSize();
            for (var i = 1; i <= blocks; i++) {
                var count = 0;
                this.cache[parseInt(i.toString(), 10)] = [];
                for (var j = ((i - 1) * blockSize); j < rows.length; j++) {
                    if (count === blockSize) {
                        break;
                    }
                    this.cache[parseInt(i.toString(), 10)].push(rows[parseInt(j.toString(), 10)]);
                    if (rows[parseInt(j.toString(), 10)].isDataRow) {
                        count++;
                    }
                }
            }
        };
        return VirtualRowModelGenerator;
    }());
    exports.VirtualRowModelGenerator = VirtualRowModelGenerator;
});