UNPKG

17.2 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};
27Object.defineProperty(exports, "__esModule", { value: true });
28var utils_1 = require("../../utils");
29var gridOptionsWrapper_1 = require("../../gridOptionsWrapper");
30var context_1 = require("../../context/context");
31var eventService_1 = require("../../eventService");
32var selectionController_1 = require("../../selectionController");
33var events_1 = require("../../events");
34var sortController_1 = require("../../sortController");
35var filterManager_1 = require("../../filter/filterManager");
36var constants_1 = require("../../constants");
37var infiniteCache_1 = require("./infiniteCache");
38var beanStub_1 = require("../../context/beanStub");
39var rowNodeCache_1 = require("../cache/rowNodeCache");
40var rowNodeBlockLoader_1 = require("../cache/rowNodeBlockLoader");
41var gridApi_1 = require("../../gridApi");
42var columnApi_1 = require("../../columnController/columnApi");
43var InfiniteRowModel = (function (_super) {
44 __extends(InfiniteRowModel, _super);
45 function InfiniteRowModel() {
46 return _super !== null && _super.apply(this, arguments) || this;
47 }
48 InfiniteRowModel.prototype.getRowBounds = function (index) {
49 return {
50 rowHeight: this.rowHeight,
51 rowTop: this.rowHeight * index
52 };
53 };
54 InfiniteRowModel.prototype.init = function () {
55 var _this = this;
56 if (!this.gridOptionsWrapper.isRowModelInfinite()) {
57 return;
58 }
59 this.rowHeight = this.gridOptionsWrapper.getRowHeightAsNumber();
60 this.addEventListeners();
61 this.setDatasource(this.gridOptionsWrapper.getDatasource());
62 this.addDestroyFunc(function () { return _this.destroyCache(); });
63 };
64 InfiniteRowModel.prototype.destroyDatasource = function () {
65 if (this.datasource && this.datasource.destroy) {
66 this.datasource.destroy();
67 }
68 this.datasource = null;
69 };
70 InfiniteRowModel.prototype.isLastRowFound = function () {
71 return this.infiniteCache ? this.infiniteCache.isMaxRowFound() : false;
72 };
73 InfiniteRowModel.prototype.addEventListeners = function () {
74 this.addDestroyableEventListener(this.eventService, events_1.Events.EVENT_FILTER_CHANGED, this.onFilterChanged.bind(this));
75 this.addDestroyableEventListener(this.eventService, events_1.Events.EVENT_SORT_CHANGED, this.onSortChanged.bind(this));
76 this.addDestroyableEventListener(this.eventService, events_1.Events.EVENT_COLUMN_EVERYTHING_CHANGED, this.onColumnEverything.bind(this));
77 };
78 InfiniteRowModel.prototype.onFilterChanged = function () {
79 if (this.gridOptionsWrapper.isEnableServerSideFilter()) {
80 this.reset();
81 }
82 };
83 InfiniteRowModel.prototype.onSortChanged = function () {
84 if (this.gridOptionsWrapper.isEnableServerSideSorting()) {
85 this.reset();
86 }
87 };
88 InfiniteRowModel.prototype.onColumnEverything = function () {
89 // if the columns get reset, then this means the sort order could be impacted
90 if (this.gridOptionsWrapper.isEnableServerSideSorting()) {
91 this.reset();
92 }
93 };
94 InfiniteRowModel.prototype.destroy = function () {
95 _super.prototype.destroy.call(this);
96 };
97 InfiniteRowModel.prototype.getType = function () {
98 return constants_1.Constants.ROW_MODEL_TYPE_INFINITE;
99 };
100 InfiniteRowModel.prototype.setDatasource = function (datasource) {
101 this.destroyDatasource();
102 this.datasource = datasource;
103 // only reset if we have a valid datasource to working with
104 if (datasource) {
105 this.checkForDeprecated();
106 this.reset();
107 }
108 };
109 InfiniteRowModel.prototype.checkForDeprecated = function () {
110 var ds = this.datasource;
111 // the number of concurrent loads we are allowed to the server
112 if (utils_1.Utils.exists(ds.maxConcurrentRequests)) {
113 console.error('ag-Grid: since version 5.1.x, maxConcurrentRequests is replaced with grid property maxConcurrentDatasourceRequests');
114 }
115 if (utils_1.Utils.exists(ds.maxPagesInCache)) {
116 console.error('ag-Grid: since version 5.1.x, maxPagesInCache is replaced with grid property maxPagesInPaginationCache');
117 }
118 if (utils_1.Utils.exists(ds.overflowSize)) {
119 console.error('ag-Grid: since version 5.1.x, overflowSize is replaced with grid property paginationOverflowSize');
120 }
121 if (utils_1.Utils.exists(ds.blockSize)) {
122 console.error('ag-Grid: since version 5.1.x, pageSize/blockSize is replaced with grid property infinitePageSize');
123 }
124 };
125 InfiniteRowModel.prototype.isEmpty = function () {
126 return utils_1.Utils.missing(this.infiniteCache);
127 };
128 InfiniteRowModel.prototype.isRowsToRender = function () {
129 return utils_1.Utils.exists(this.infiniteCache);
130 };
131 InfiniteRowModel.prototype.getNodesInRangeForSelection = function (firstInRange, lastInRange) {
132 return this.infiniteCache.getRowNodesInRange(firstInRange, lastInRange);
133 };
134 InfiniteRowModel.prototype.reset = function () {
135 // important to return here, as the user could be setting filter or sort before
136 // data-source is set
137 if (utils_1.Utils.missing(this.datasource)) {
138 return;
139 }
140 // if user is providing id's, then this means we can keep the selection between datsource hits,
141 // as the rows will keep their unique id's even if, for example, server side sorting or filtering
142 // is done.
143 var userGeneratingIds = utils_1.Utils.exists(this.gridOptionsWrapper.getRowNodeIdFunc());
144 if (!userGeneratingIds) {
145 this.selectionController.reset();
146 }
147 this.resetCache();
148 var event = this.createModelUpdatedEvent();
149 this.eventService.dispatchEvent(event);
150 };
151 InfiniteRowModel.prototype.createModelUpdatedEvent = function () {
152 return {
153 type: events_1.Events.EVENT_MODEL_UPDATED,
154 api: this.gridApi,
155 columnApi: this.columnApi,
156 // not sure if these should all be false - noticed if after implementing,
157 // maybe they should be true?
158 newPage: false,
159 newData: false,
160 keepRenderedRows: false,
161 animate: false
162 };
163 };
164 InfiniteRowModel.prototype.resetCache = function () {
165 // if not first time creating a cache, need to destroy the old one
166 this.destroyCache();
167 var maxConcurrentRequests = this.gridOptionsWrapper.getMaxConcurrentDatasourceRequests();
168 // there is a bi-directional dependency between the loader and the cache,
169 // so we create loader here, and then pass dependencies in setDependencies() method later
170 this.rowNodeBlockLoader = new rowNodeBlockLoader_1.RowNodeBlockLoader(maxConcurrentRequests);
171 this.context.wireBean(this.rowNodeBlockLoader);
172 var cacheSettings = {
173 // the user provided datasource
174 datasource: this.datasource,
175 // sort and filter model
176 filterModel: this.filterManager.getFilterModel(),
177 sortModel: this.sortController.getSortModel(),
178 rowNodeBlockLoader: this.rowNodeBlockLoader,
179 // properties - this way we take a snapshot of them, so if user changes any, they will be
180 // used next time we create a new cache, which is generally after a filter or sort change,
181 // or a new datasource is set
182 maxConcurrentRequests: maxConcurrentRequests,
183 overflowSize: this.gridOptionsWrapper.getCacheOverflowSize(),
184 initialRowCount: this.gridOptionsWrapper.getInfiniteInitialRowCount(),
185 maxBlocksInCache: this.gridOptionsWrapper.getMaxBlocksInCache(),
186 blockSize: this.gridOptionsWrapper.getCacheBlockSize(),
187 rowHeight: this.gridOptionsWrapper.getRowHeightAsNumber(),
188 // the cache could create this, however it is also used by the pages, so handy to create it
189 // here as the settings are also passed to the pages
190 lastAccessedSequence: new utils_1.NumberSequence()
191 };
192 // set defaults
193 if (!(cacheSettings.maxConcurrentRequests >= 1)) {
194 cacheSettings.maxConcurrentRequests = 2;
195 }
196 // page size needs to be 1 or greater. having it at 1 would be silly, as you would be hitting the
197 // server for one page at a time. so the default if not specified is 100.
198 if (!(cacheSettings.blockSize >= 1)) {
199 cacheSettings.blockSize = 100;
200 }
201 // if user doesn't give initial rows to display, we assume zero
202 if (!(cacheSettings.initialRowCount >= 1)) {
203 cacheSettings.initialRowCount = 0;
204 }
205 // if user doesn't provide overflow, we use default overflow of 1, so user can scroll past
206 // the current page and request first row of next page
207 if (!(cacheSettings.overflowSize >= 1)) {
208 cacheSettings.overflowSize = 1;
209 }
210 this.infiniteCache = new infiniteCache_1.InfiniteCache(cacheSettings);
211 this.context.wireBean(this.infiniteCache);
212 this.infiniteCache.addEventListener(rowNodeCache_1.RowNodeCache.EVENT_CACHE_UPDATED, this.onCacheUpdated.bind(this));
213 };
214 InfiniteRowModel.prototype.destroyCache = function () {
215 if (this.infiniteCache) {
216 this.infiniteCache.destroy();
217 this.infiniteCache = null;
218 }
219 if (this.rowNodeBlockLoader) {
220 this.rowNodeBlockLoader.destroy();
221 this.rowNodeBlockLoader = null;
222 }
223 };
224 InfiniteRowModel.prototype.onCacheUpdated = function () {
225 var event = this.createModelUpdatedEvent();
226 this.eventService.dispatchEvent(event);
227 };
228 InfiniteRowModel.prototype.getRow = function (rowIndex) {
229 return this.infiniteCache ? this.infiniteCache.getRow(rowIndex) : null;
230 };
231 InfiniteRowModel.prototype.getRowNode = function (id) {
232 var result = null;
233 this.forEachNode(function (rowNode) {
234 if (rowNode.id === id) {
235 result = rowNode;
236 }
237 });
238 return result;
239 };
240 InfiniteRowModel.prototype.forEachNode = function (callback) {
241 if (this.infiniteCache) {
242 this.infiniteCache.forEachNodeDeep(callback, new utils_1.NumberSequence());
243 }
244 };
245 InfiniteRowModel.prototype.getCurrentPageHeight = function () {
246 return this.getRowCount() * this.rowHeight;
247 };
248 InfiniteRowModel.prototype.getRowIndexAtPixel = function (pixel) {
249 if (this.rowHeight !== 0) {
250 var rowIndexForPixel = Math.floor(pixel / this.rowHeight);
251 if (rowIndexForPixel > this.getPageLastRow()) {
252 return this.getPageLastRow();
253 }
254 else {
255 return rowIndexForPixel;
256 }
257 }
258 else {
259 return 0;
260 }
261 };
262 InfiniteRowModel.prototype.getPageFirstRow = function () {
263 return 0;
264 };
265 InfiniteRowModel.prototype.getPageLastRow = function () {
266 return this.infiniteCache ? this.infiniteCache.getVirtualRowCount() - 1 : 0;
267 };
268 InfiniteRowModel.prototype.getRowCount = function () {
269 return this.infiniteCache ? this.infiniteCache.getVirtualRowCount() : 0;
270 };
271 InfiniteRowModel.prototype.updateRowData = function (transaction) {
272 if (utils_1.Utils.exists(transaction.remove) || utils_1.Utils.exists(transaction.update)) {
273 console.warn('ag-Grid: updateRowData for InfiniteRowModel does not support remove or update, only add');
274 return;
275 }
276 if (utils_1.Utils.missing(transaction.addIndex)) {
277 console.warn('ag-Grid: updateRowData for InfiniteRowModel requires add and addIndex to be set');
278 return;
279 }
280 if (this.infiniteCache) {
281 this.infiniteCache.insertItemsAtIndex(transaction.addIndex, transaction.add);
282 }
283 };
284 InfiniteRowModel.prototype.isRowPresent = function (rowNode) {
285 return false;
286 };
287 InfiniteRowModel.prototype.refreshCache = function () {
288 if (this.infiniteCache) {
289 this.infiniteCache.refreshCache();
290 }
291 };
292 InfiniteRowModel.prototype.purgeCache = function () {
293 if (this.infiniteCache) {
294 this.infiniteCache.purgeCache();
295 }
296 };
297 InfiniteRowModel.prototype.getVirtualRowCount = function () {
298 if (this.infiniteCache) {
299 return this.infiniteCache.getVirtualRowCount();
300 }
301 else {
302 return null;
303 }
304 };
305 InfiniteRowModel.prototype.isMaxRowFound = function () {
306 if (this.infiniteCache) {
307 return this.infiniteCache.isMaxRowFound();
308 }
309 };
310 InfiniteRowModel.prototype.setVirtualRowCount = function (rowCount, maxRowFound) {
311 if (this.infiniteCache) {
312 this.infiniteCache.setVirtualRowCount(rowCount, maxRowFound);
313 }
314 };
315 InfiniteRowModel.prototype.getBlockState = function () {
316 if (this.rowNodeBlockLoader) {
317 return this.rowNodeBlockLoader.getBlockState();
318 }
319 else {
320 return null;
321 }
322 };
323 __decorate([
324 context_1.Autowired('gridOptionsWrapper'),
325 __metadata("design:type", gridOptionsWrapper_1.GridOptionsWrapper)
326 ], InfiniteRowModel.prototype, "gridOptionsWrapper", void 0);
327 __decorate([
328 context_1.Autowired('filterManager'),
329 __metadata("design:type", filterManager_1.FilterManager)
330 ], InfiniteRowModel.prototype, "filterManager", void 0);
331 __decorate([
332 context_1.Autowired('sortController'),
333 __metadata("design:type", sortController_1.SortController)
334 ], InfiniteRowModel.prototype, "sortController", void 0);
335 __decorate([
336 context_1.Autowired('selectionController'),
337 __metadata("design:type", selectionController_1.SelectionController)
338 ], InfiniteRowModel.prototype, "selectionController", void 0);
339 __decorate([
340 context_1.Autowired('eventService'),
341 __metadata("design:type", eventService_1.EventService)
342 ], InfiniteRowModel.prototype, "eventService", void 0);
343 __decorate([
344 context_1.Autowired('context'),
345 __metadata("design:type", context_1.Context)
346 ], InfiniteRowModel.prototype, "context", void 0);
347 __decorate([
348 context_1.Autowired('gridApi'),
349 __metadata("design:type", gridApi_1.GridApi)
350 ], InfiniteRowModel.prototype, "gridApi", void 0);
351 __decorate([
352 context_1.Autowired('columnApi'),
353 __metadata("design:type", columnApi_1.ColumnApi)
354 ], InfiniteRowModel.prototype, "columnApi", void 0);
355 __decorate([
356 context_1.PostConstruct,
357 __metadata("design:type", Function),
358 __metadata("design:paramtypes", []),
359 __metadata("design:returntype", void 0)
360 ], InfiniteRowModel.prototype, "init", null);
361 __decorate([
362 context_1.PreDestroy,
363 __metadata("design:type", Function),
364 __metadata("design:paramtypes", []),
365 __metadata("design:returntype", void 0)
366 ], InfiniteRowModel.prototype, "destroyDatasource", null);
367 __decorate([
368 context_1.PreDestroy,
369 __metadata("design:type", Function),
370 __metadata("design:paramtypes", []),
371 __metadata("design:returntype", void 0)
372 ], InfiniteRowModel.prototype, "destroy", null);
373 InfiniteRowModel = __decorate([
374 context_1.Bean('rowModel')
375 ], InfiniteRowModel);
376 return InfiniteRowModel;
377}(beanStub_1.BeanStub));
378exports.InfiniteRowModel = InfiniteRowModel;