UNPKG

8.31 kBJavaScriptView Raw
1/**
2 * ag-grid - Advanced Data Grid / Data Table supporting Javascript / React / AngularJS / Web Components
3 * @version v18.1.2
4 * @link http://www.ag-grid.com/
5 * @license MIT
6 */
7"use strict";
8var __extends = (this && this.__extends) || (function () {
9 var extendStatics = Object.setPrototypeOf ||
10 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
11 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
12 return function (d, b) {
13 extendStatics(d, b);
14 function __() { this.constructor = d; }
15 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
16 };
17})();
18var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
19 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
20 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
21 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
22 return c > 3 && r && Object.defineProperty(target, key, r), r;
23};
24var __metadata = (this && this.__metadata) || function (k, v) {
25 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
26};
27var __param = (this && this.__param) || function (paramIndex, decorator) {
28 return function (target, key) { decorator(target, key, paramIndex); }
29};
30Object.defineProperty(exports, "__esModule", { value: true });
31var context_1 = require("../../context/context");
32var eventService_1 = require("../../eventService");
33var events_1 = require("../../events");
34var logger_1 = require("../../logger");
35var infiniteBlock_1 = require("./infiniteBlock");
36var rowNodeCache_1 = require("../cache/rowNodeCache");
37var gridApi_1 = require("../../gridApi");
38var columnApi_1 = require("../../columnController/columnApi");
39var InfiniteCache = (function (_super) {
40 __extends(InfiniteCache, _super);
41 function InfiniteCache(params) {
42 return _super.call(this, params) || this;
43 }
44 InfiniteCache.prototype.setBeans = function (loggerFactory) {
45 this.logger = loggerFactory.create('InfiniteCache');
46 };
47 InfiniteCache.prototype.init = function () {
48 _super.prototype.init.call(this);
49 // start load of data, as the virtualRowCount will remain at 0 otherwise,
50 // so we need this to kick things off, otherwise grid would never call getRow()
51 this.getRow(0);
52 };
53 InfiniteCache.prototype.moveItemsDown = function (block, moveFromIndex, moveCount) {
54 var startRow = block.getStartRow();
55 var endRow = block.getEndRow();
56 var indexOfLastRowToMove = moveFromIndex + moveCount;
57 // all rows need to be moved down below the insertion index
58 for (var currentRowIndex = endRow - 1; currentRowIndex >= startRow; currentRowIndex--) {
59 // don't move rows at or before the insertion index
60 if (currentRowIndex < indexOfLastRowToMove) {
61 continue;
62 }
63 var indexOfNodeWeWant = currentRowIndex - moveCount;
64 var nodeForThisIndex = this.getRow(indexOfNodeWeWant, true);
65 if (nodeForThisIndex) {
66 block.setRowNode(currentRowIndex, nodeForThisIndex);
67 }
68 else {
69 block.setBlankRowNode(currentRowIndex);
70 block.setDirty();
71 }
72 }
73 };
74 InfiniteCache.prototype.insertItems = function (block, indexToInsert, items) {
75 var pageStartRow = block.getStartRow();
76 var pageEndRow = block.getEndRow();
77 var newRowNodes = [];
78 // next stage is insert the rows into this page, if applicable
79 for (var index = 0; index < items.length; index++) {
80 var rowIndex = indexToInsert + index;
81 var currentRowInThisPage = rowIndex >= pageStartRow && rowIndex < pageEndRow;
82 if (currentRowInThisPage) {
83 var dataItem = items[index];
84 var newRowNode = block.setNewData(rowIndex, dataItem);
85 newRowNodes.push(newRowNode);
86 }
87 }
88 return newRowNodes;
89 };
90 InfiniteCache.prototype.insertItemsAtIndex = function (indexToInsert, items) {
91 // get all page id's as NUMBERS (not strings, as we need to sort as numbers) and in descending order
92 var _this = this;
93 var newNodes = [];
94 this.forEachBlockInReverseOrder(function (block) {
95 var pageEndRow = block.getEndRow();
96 // if the insertion is after this page, then this page is not impacted
97 if (pageEndRow <= indexToInsert) {
98 return;
99 }
100 _this.moveItemsDown(block, indexToInsert, items.length);
101 var newNodesThisPage = _this.insertItems(block, indexToInsert, items);
102 newNodesThisPage.forEach(function (rowNode) { return newNodes.push(rowNode); });
103 });
104 if (this.isMaxRowFound()) {
105 this.hack_setVirtualRowCount(this.getVirtualRowCount() + items.length);
106 }
107 this.onCacheUpdated();
108 var event = {
109 type: events_1.Events.EVENT_ROW_DATA_UPDATED,
110 api: this.gridApi,
111 columnApi: this.columnApi
112 };
113 this.eventService.dispatchEvent(event);
114 };
115 // the rowRenderer will not pass dontCreatePage, meaning when rendering the grid,
116 // it will want new pages in the cache as it asks for rows. only when we are inserting /
117 // removing rows via the api is dontCreatePage set, where we move rows between the pages.
118 InfiniteCache.prototype.getRow = function (rowIndex, dontCreatePage) {
119 if (dontCreatePage === void 0) { dontCreatePage = false; }
120 var blockId = Math.floor(rowIndex / this.cacheParams.blockSize);
121 var block = this.getBlock(blockId);
122 if (!block) {
123 if (dontCreatePage) {
124 return null;
125 }
126 else {
127 block = this.createBlock(blockId);
128 }
129 }
130 return block.getRow(rowIndex);
131 };
132 InfiniteCache.prototype.createBlock = function (blockNumber) {
133 var newBlock = new infiniteBlock_1.InfiniteBlock(blockNumber, this.cacheParams);
134 this.context.wireBean(newBlock);
135 this.postCreateBlock(newBlock);
136 return newBlock;
137 };
138 // we have this on infinite row model only, not server side row model,
139 // because for server side, it would leave the children in inconsistent
140 // state - eg if a node had children, but after the refresh it had data
141 // for a different row, then the children would be with the wrong row node.
142 InfiniteCache.prototype.refreshCache = function () {
143 this.forEachBlockInOrder(function (block) { return block.setDirty(); });
144 this.checkBlockToLoad();
145 };
146 __decorate([
147 context_1.Autowired('eventService'),
148 __metadata("design:type", eventService_1.EventService)
149 ], InfiniteCache.prototype, "eventService", void 0);
150 __decorate([
151 context_1.Autowired('context'),
152 __metadata("design:type", context_1.Context)
153 ], InfiniteCache.prototype, "context", void 0);
154 __decorate([
155 context_1.Autowired('columnApi'),
156 __metadata("design:type", columnApi_1.ColumnApi)
157 ], InfiniteCache.prototype, "columnApi", void 0);
158 __decorate([
159 context_1.Autowired('gridApi'),
160 __metadata("design:type", gridApi_1.GridApi)
161 ], InfiniteCache.prototype, "gridApi", void 0);
162 __decorate([
163 __param(0, context_1.Qualifier('loggerFactory')),
164 __metadata("design:type", Function),
165 __metadata("design:paramtypes", [logger_1.LoggerFactory]),
166 __metadata("design:returntype", void 0)
167 ], InfiniteCache.prototype, "setBeans", null);
168 __decorate([
169 context_1.PostConstruct,
170 __metadata("design:type", Function),
171 __metadata("design:paramtypes", []),
172 __metadata("design:returntype", void 0)
173 ], InfiniteCache.prototype, "init", null);
174 return InfiniteCache;
175}(rowNodeCache_1.RowNodeCache));
176exports.InfiniteCache = InfiniteCache;