UNPKG

50.6 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 utils_1 = require("../utils");
32var gridOptionsWrapper_1 = require("../gridOptionsWrapper");
33var expressionService_1 = require("../valueService/expressionService");
34var templateService_1 = require("../templateService");
35var valueService_1 = require("../valueService/valueService");
36var eventService_1 = require("../eventService");
37var rowComp_1 = require("./rowComp");
38var events_1 = require("../events");
39var constants_1 = require("../constants");
40var cellComp_1 = require("./cellComp");
41var context_1 = require("../context/context");
42var gridCore_1 = require("../gridCore");
43var columnApi_1 = require("../columnController/columnApi");
44var columnController_1 = require("../columnController/columnController");
45var logger_1 = require("../logger");
46var focusedCellController_1 = require("../focusedCellController");
47var cellNavigationService_1 = require("../cellNavigationService");
48var gridCell_1 = require("../entities/gridCell");
49var beanStub_1 = require("../context/beanStub");
50var paginationProxy_1 = require("../rowModels/paginationProxy");
51var gridApi_1 = require("../gridApi");
52var pinnedRowModel_1 = require("../rowModels/pinnedRowModel");
53var beans_1 = require("./beans");
54var animationFrameService_1 = require("../misc/animationFrameService");
55var heightScaler_1 = require("./heightScaler");
56var RowRenderer = (function (_super) {
57 __extends(RowRenderer, _super);
58 function RowRenderer() {
59 var _this = _super !== null && _super.apply(this, arguments) || this;
60 // map of row ids to row objects. keeps track of which elements
61 // are rendered for which rows in the dom.
62 _this.rowCompsByIndex = {};
63 _this.floatingTopRowComps = [];
64 _this.floatingBottomRowComps = [];
65 // we only allow one refresh at a time, otherwise the internal memory structure here
66 // will get messed up. this can happen if the user has a cellRenderer, and inside the
67 // renderer they call an API method that results in another pass of the refresh,
68 // then it will be trying to draw rows in the middle of a refresh.
69 _this.refreshInProgress = false;
70 return _this;
71 }
72 RowRenderer.prototype.agWire = function (loggerFactory) {
73 this.logger = loggerFactory.create("RowRenderer");
74 };
75 RowRenderer.prototype.registerGridComp = function (gridPanel) {
76 this.gridPanel = gridPanel;
77 this.rowContainers = this.gridPanel.getRowContainers();
78 this.addDestroyableEventListener(this.eventService, events_1.Events.EVENT_PAGINATION_CHANGED, this.onPageLoaded.bind(this));
79 this.addDestroyableEventListener(this.eventService, events_1.Events.EVENT_PINNED_ROW_DATA_CHANGED, this.onPinnedRowDataChanged.bind(this));
80 this.addDestroyableEventListener(this.eventService, events_1.Events.EVENT_DISPLAYED_COLUMNS_CHANGED, this.onDisplayedColumnsChanged.bind(this));
81 this.addDestroyableEventListener(this.eventService, events_1.Events.EVENT_BODY_SCROLL, this.redrawAfterScroll.bind(this));
82 this.addDestroyableEventListener(this.eventService, events_1.Events.EVENT_BODY_HEIGHT_CHANGED, this.redrawAfterScroll.bind(this));
83 this.redrawAfterModelUpdate();
84 };
85 RowRenderer.prototype.onPageLoaded = function (refreshEvent) {
86 if (utils_1.Utils.missing(refreshEvent)) {
87 refreshEvent = {
88 type: events_1.Events.EVENT_MODEL_UPDATED,
89 api: this.gridApi,
90 columnApi: this.columnApi,
91 animate: false,
92 keepRenderedRows: false,
93 newData: false,
94 newPage: false
95 };
96 }
97 this.onModelUpdated(refreshEvent);
98 };
99 RowRenderer.prototype.getAllCellsForColumn = function (column) {
100 var eCells = [];
101 utils_1.Utils.iterateObject(this.rowCompsByIndex, callback);
102 utils_1.Utils.iterateObject(this.floatingBottomRowComps, callback);
103 utils_1.Utils.iterateObject(this.floatingTopRowComps, callback);
104 function callback(key, rowComp) {
105 var eCell = rowComp.getCellForCol(column);
106 if (eCell) {
107 eCells.push(eCell);
108 }
109 }
110 return eCells;
111 };
112 RowRenderer.prototype.refreshFloatingRowComps = function () {
113 this.refreshFloatingRows(this.floatingTopRowComps, this.pinnedRowModel.getPinnedTopRowData(), this.rowContainers.floatingTopPinnedLeft, this.rowContainers.floatingTopPinnedRight, this.rowContainers.floatingTop, this.rowContainers.floatingTopFullWidth);
114 this.refreshFloatingRows(this.floatingBottomRowComps, this.pinnedRowModel.getPinnedBottomRowData(), this.rowContainers.floatingBottomPinnedLeft, this.rowContainers.floatingBottomPinnedRight, this.rowContainers.floatingBottom, this.rowContainers.floatingBottomFullWith);
115 };
116 RowRenderer.prototype.refreshFloatingRows = function (rowComps, rowNodes, pinnedLeftContainerComp, pinnedRightContainerComp, bodyContainerComp, fullWidthContainerComp) {
117 var _this = this;
118 rowComps.forEach(function (row) {
119 row.destroy();
120 });
121 rowComps.length = 0;
122 if (rowNodes) {
123 rowNodes.forEach(function (node) {
124 var rowComp = new rowComp_1.RowComp(_this.$scope, bodyContainerComp, pinnedLeftContainerComp, pinnedRightContainerComp, fullWidthContainerComp, node, _this.beans, false, false);
125 rowComp.init();
126 rowComps.push(rowComp);
127 });
128 }
129 this.flushContainers(rowComps);
130 };
131 RowRenderer.prototype.onPinnedRowDataChanged = function () {
132 // recycling rows in order to ensure cell editing is not cancelled
133 var params = {
134 recycleRows: true
135 };
136 this.redrawAfterModelUpdate(params);
137 };
138 RowRenderer.prototype.onModelUpdated = function (refreshEvent) {
139 var params = {
140 recycleRows: refreshEvent.keepRenderedRows,
141 animate: refreshEvent.animate,
142 newData: refreshEvent.newData,
143 newPage: refreshEvent.newPage,
144 // because this is a model updated event (not pinned rows), we
145 // can skip updating the pinned rows. this is needed so that if user
146 // is doing transaction updates, the pinned rows are not getting constantly
147 // trashed - or editing cells in pinned rows are not refreshed and put into read mode
148 onlyBody: true
149 };
150 this.redrawAfterModelUpdate(params);
151 };
152 // if the row nodes are not rendered, no index is returned
153 RowRenderer.prototype.getRenderedIndexesForRowNodes = function (rowNodes) {
154 var result = [];
155 if (utils_1.Utils.missing(rowNodes)) {
156 return result;
157 }
158 utils_1.Utils.iterateObject(this.rowCompsByIndex, function (index, renderedRow) {
159 var rowNode = renderedRow.getRowNode();
160 if (rowNodes.indexOf(rowNode) >= 0) {
161 result.push(index);
162 }
163 });
164 return result;
165 };
166 RowRenderer.prototype.redrawRows = function (rowNodes) {
167 if (!rowNodes || rowNodes.length == 0) {
168 return;
169 }
170 // we only need to be worried about rendered rows, as this method is
171 // called to whats rendered. if the row isn't rendered, we don't care
172 var indexesToRemove = this.getRenderedIndexesForRowNodes(rowNodes);
173 // remove the rows
174 this.removeRowComps(indexesToRemove);
175 // add draw them again
176 this.redrawAfterModelUpdate({
177 recycleRows: true
178 });
179 };
180 RowRenderer.prototype.getCellToRestoreFocusToAfterRefresh = function (params) {
181 var focusedCell = params.suppressKeepFocus ? null : this.focusedCellController.getFocusCellToUseAfterRefresh();
182 if (utils_1.Utils.missing(focusedCell)) {
183 return null;
184 }
185 // if the dom is not actually focused on a cell, then we don't try to refocus. the problem this
186 // solves is with editing - if the user is editing, eg focus is on a text field, and not on the
187 // cell itself, then the cell can be registered as having focus, however it's the text field that
188 // has the focus and not the cell div. therefore, when the refresh is finished, the grid will focus
189 // the cell, and not the textfield. that means if the user is in a text field, and the grid refreshes,
190 // the focus is lost from the text field. we do not want this.
191 var activeElement = document.activeElement;
192 var domData = this.gridOptionsWrapper.getDomData(activeElement, cellComp_1.CellComp.DOM_DATA_KEY_CELL_COMP);
193 var elementIsNotACellDev = utils_1.Utils.missing(domData);
194 if (elementIsNotACellDev) {
195 return null;
196 }
197 return focusedCell;
198 };
199 // gets called after changes to the model.
200 RowRenderer.prototype.redrawAfterModelUpdate = function (params) {
201 if (params === void 0) { params = {}; }
202 this.getLockOnRefresh();
203 var focusedCell = this.getCellToRestoreFocusToAfterRefresh(params);
204 this.sizeContainerToPageHeight();
205 this.scrollToTopIfNewData(params);
206 var recycleRows = params.recycleRows;
207 var animate = params.animate && this.gridOptionsWrapper.isAnimateRows();
208 var rowsToRecycle = this.binRowComps(recycleRows);
209 this.redraw(rowsToRecycle, animate);
210 if (!params.onlyBody) {
211 this.refreshFloatingRowComps();
212 }
213 this.restoreFocusedCell(focusedCell);
214 this.releaseLockOnRefresh();
215 };
216 RowRenderer.prototype.scrollToTopIfNewData = function (params) {
217 var scrollToTop = params.newData || params.newPage;
218 var suppressScrollToTop = this.gridOptionsWrapper.isSuppressScrollOnNewData();
219 if (scrollToTop && !suppressScrollToTop) {
220 this.gridPanel.scrollToTop();
221 }
222 };
223 RowRenderer.prototype.sizeContainerToPageHeight = function () {
224 var containerHeight = this.paginationProxy.getCurrentPageHeight();
225 // we need at least 1 pixel for the horizontal scroll to work. so if there are now rows,
226 // we still want the scroll to be present, otherwise there would be no way to access the columns
227 // on the RHS - and if that was where the filter was that cause no rows to be presented, there
228 // is no way to remove the filter.
229 if (containerHeight === 0) {
230 containerHeight = 1;
231 }
232 this.heightScaler.setModelHeight(containerHeight);
233 var realHeight = this.heightScaler.getUiContainerHeight();
234 this.rowContainers.body.setHeight(realHeight);
235 this.rowContainers.fullWidth.setHeight(realHeight);
236 this.rowContainers.pinnedLeft.setHeight(realHeight);
237 this.rowContainers.pinnedRight.setHeight(realHeight);
238 };
239 RowRenderer.prototype.getLockOnRefresh = function () {
240 if (this.refreshInProgress) {
241 throw new Error("ag-Grid: cannot get grid to draw rows when it is in the middle of drawing rows. " +
242 "Your code probably called a grid API method while the grid was in the render stage. To overcome " +
243 "this, put the API call into a timeout, eg instead of api.refreshView(), " +
244 "call setTimeout(function(){api.refreshView(),0}). To see what part of your code " +
245 "that caused the refresh check this stacktrace.");
246 }
247 this.refreshInProgress = true;
248 };
249 RowRenderer.prototype.releaseLockOnRefresh = function () {
250 this.refreshInProgress = false;
251 };
252 // sets the focus to the provided cell, if the cell is provided. this way, the user can call refresh without
253 // worry about the focus been lost. this is important when the user is using keyboard navigation to do edits
254 // and the cellEditor is calling 'refresh' to get other cells to update (as other cells might depend on the
255 // edited cell).
256 RowRenderer.prototype.restoreFocusedCell = function (gridCell) {
257 if (gridCell) {
258 this.focusedCellController.setFocusedCell(gridCell.rowIndex, gridCell.column, gridCell.floating, true);
259 }
260 };
261 RowRenderer.prototype.stopEditing = function (cancel) {
262 if (cancel === void 0) { cancel = false; }
263 this.forEachRowComp(function (key, rowComp) {
264 rowComp.stopEditing(cancel);
265 });
266 };
267 RowRenderer.prototype.forEachCellComp = function (callback) {
268 this.forEachRowComp(function (key, rowComp) { return rowComp.forEachCellComp(callback); });
269 };
270 RowRenderer.prototype.forEachRowComp = function (callback) {
271 utils_1.Utils.iterateObject(this.rowCompsByIndex, callback);
272 utils_1.Utils.iterateObject(this.floatingTopRowComps, callback);
273 utils_1.Utils.iterateObject(this.floatingBottomRowComps, callback);
274 };
275 RowRenderer.prototype.addRenderedRowListener = function (eventName, rowIndex, callback) {
276 var rowComp = this.rowCompsByIndex[rowIndex];
277 if (rowComp) {
278 rowComp.addEventListener(eventName, callback);
279 }
280 };
281 RowRenderer.prototype.flashCells = function (params) {
282 if (params === void 0) { params = {}; }
283 this.forEachCellCompFiltered(params.rowNodes, params.columns, function (cellComp) { return cellComp.flashCell(); });
284 };
285 RowRenderer.prototype.refreshCells = function (params) {
286 if (params === void 0) { params = {}; }
287 var refreshCellParams = {
288 forceRefresh: params.force,
289 newData: false
290 };
291 this.forEachCellCompFiltered(params.rowNodes, params.columns, function (cellComp) { return cellComp.refreshCell(refreshCellParams); });
292 };
293 RowRenderer.prototype.getCellRendererInstances = function (params) {
294 var res = [];
295 this.forEachCellCompFiltered(params.rowNodes, params.columns, function (cellComp) {
296 var cellRenderer = cellComp.getCellRenderer();
297 if (cellRenderer) {
298 res.push(cellRenderer);
299 }
300 });
301 return res;
302 };
303 RowRenderer.prototype.getCellEditorInstances = function (params) {
304 var res = [];
305 this.forEachCellCompFiltered(params.rowNodes, params.columns, function (cellComp) {
306 var cellEditor = cellComp.getCellEditor();
307 if (cellEditor) {
308 res.push(cellEditor);
309 }
310 });
311 return res;
312 };
313 RowRenderer.prototype.getEditingCells = function () {
314 var res = [];
315 this.forEachCellComp(function (cellComp) {
316 if (cellComp.isEditing()) {
317 var gridCellDef = cellComp.getGridCell().getGridCellDef();
318 res.push(gridCellDef);
319 }
320 });
321 return res;
322 };
323 // calls the callback for each cellComp that match the provided rowNodes and columns. eg if one row node
324 // and two columns provided, that identifies 4 cells, so callback gets called 4 times, once for each cell.
325 RowRenderer.prototype.forEachCellCompFiltered = function (rowNodes, columns, callback) {
326 var _this = this;
327 var rowIdsMap;
328 if (utils_1.Utils.exists(rowNodes)) {
329 rowIdsMap = {
330 top: {},
331 bottom: {},
332 normal: {}
333 };
334 rowNodes.forEach(function (rowNode) {
335 if (rowNode.rowPinned === constants_1.Constants.PINNED_TOP) {
336 rowIdsMap.top[rowNode.id] = true;
337 }
338 else if (rowNode.rowPinned === constants_1.Constants.PINNED_BOTTOM) {
339 rowIdsMap.bottom[rowNode.id] = true;
340 }
341 else {
342 rowIdsMap.normal[rowNode.id] = true;
343 }
344 });
345 }
346 var colIdsMap;
347 if (utils_1.Utils.exists(columns)) {
348 colIdsMap = {};
349 columns.forEach(function (colKey) {
350 var column = _this.columnController.getGridColumn(colKey);
351 if (utils_1.Utils.exists(column)) {
352 colIdsMap[column.getId()] = true;
353 }
354 });
355 }
356 var processRow = function (rowComp) {
357 var rowNode = rowComp.getRowNode();
358 var id = rowNode.id;
359 var floating = rowNode.rowPinned;
360 // skip this row if it is missing from the provided list
361 if (utils_1.Utils.exists(rowIdsMap)) {
362 if (floating === constants_1.Constants.PINNED_BOTTOM) {
363 if (!rowIdsMap.bottom[id]) {
364 return;
365 }
366 }
367 else if (floating === constants_1.Constants.PINNED_TOP) {
368 if (!rowIdsMap.top[id]) {
369 return;
370 }
371 }
372 else {
373 if (!rowIdsMap.normal[id]) {
374 return;
375 }
376 }
377 }
378 rowComp.forEachCellComp(function (cellComp) {
379 var colId = cellComp.getColumn().getId();
380 var excludeColFromRefresh = colIdsMap && !colIdsMap[colId];
381 if (excludeColFromRefresh) {
382 return;
383 }
384 callback(cellComp);
385 });
386 };
387 utils_1.Utils.iterateObject(this.rowCompsByIndex, function (index, rowComp) {
388 processRow(rowComp);
389 });
390 if (this.floatingTopRowComps) {
391 this.floatingTopRowComps.forEach(processRow);
392 }
393 if (this.floatingBottomRowComps) {
394 this.floatingBottomRowComps.forEach(processRow);
395 }
396 };
397 RowRenderer.prototype.destroy = function () {
398 _super.prototype.destroy.call(this);
399 var rowIndexesToRemove = Object.keys(this.rowCompsByIndex);
400 this.removeRowComps(rowIndexesToRemove);
401 };
402 RowRenderer.prototype.binRowComps = function (recycleRows) {
403 var _this = this;
404 var indexesToRemove;
405 var rowsToRecycle = {};
406 if (recycleRows) {
407 indexesToRemove = [];
408 utils_1.Utils.iterateObject(this.rowCompsByIndex, function (index, rowComp) {
409 var rowNode = rowComp.getRowNode();
410 if (utils_1.Utils.exists(rowNode.id)) {
411 rowsToRecycle[rowNode.id] = rowComp;
412 delete _this.rowCompsByIndex[index];
413 }
414 else {
415 indexesToRemove.push(index);
416 }
417 });
418 }
419 else {
420 indexesToRemove = Object.keys(this.rowCompsByIndex);
421 }
422 this.removeRowComps(indexesToRemove);
423 return rowsToRecycle;
424 };
425 // takes array of row indexes
426 RowRenderer.prototype.removeRowComps = function (rowsToRemove) {
427 var _this = this;
428 // if no fromIndex then set to -1, which will refresh everything
429 // let realFromIndex = -1;
430 rowsToRemove.forEach(function (indexToRemove) {
431 var renderedRow = _this.rowCompsByIndex[indexToRemove];
432 renderedRow.destroy();
433 delete _this.rowCompsByIndex[indexToRemove];
434 });
435 };
436 // gets called when rows don't change, but viewport does, so after:
437 // 1) height of grid body changes, ie number of displayed rows has changed
438 // 2) grid scrolled to new position
439 // 3) ensure index visible (which is a scroll)
440 RowRenderer.prototype.redrawAfterScroll = function () {
441 this.getLockOnRefresh();
442 this.redraw(null, false, true);
443 this.releaseLockOnRefresh();
444 };
445 RowRenderer.prototype.removeRowCompsNotToDraw = function (indexesToDraw) {
446 // for speedy lookup, dump into map
447 var indexesToDrawMap = {};
448 indexesToDraw.forEach(function (index) { return (indexesToDrawMap[index] = true); });
449 var existingIndexes = Object.keys(this.rowCompsByIndex);
450 var indexesNotToDraw = utils_1.Utils.filter(existingIndexes, function (index) { return !indexesToDrawMap[index]; });
451 this.removeRowComps(indexesNotToDraw);
452 };
453 RowRenderer.prototype.calculateIndexesToDraw = function () {
454 var _this = this;
455 // all in all indexes in the viewport
456 var indexesToDraw = utils_1.Utils.createArrayOfNumbers(this.firstRenderedRow, this.lastRenderedRow);
457 // add in indexes of rows we want to keep, because they are currently editing
458 utils_1.Utils.iterateObject(this.rowCompsByIndex, function (indexStr, rowComp) {
459 var index = Number(indexStr);
460 if (index < _this.firstRenderedRow || index > _this.lastRenderedRow) {
461 if (_this.keepRowBecauseEditing(rowComp)) {
462 indexesToDraw.push(index);
463 }
464 }
465 });
466 indexesToDraw.sort(function (a, b) { return a - b; });
467 return indexesToDraw;
468 };
469 RowRenderer.prototype.redraw = function (rowsToRecycle, animate, afterScroll) {
470 var _this = this;
471 if (animate === void 0) { animate = false; }
472 if (afterScroll === void 0) { afterScroll = false; }
473 this.heightScaler.update();
474 this.workOutFirstAndLastRowsToRender();
475 // the row can already exist and be in the following:
476 // rowsToRecycle -> if model change, then the index may be different, however row may
477 // exist here from previous time (mapped by id).
478 // this.rowCompsByIndex -> if just a scroll, then this will contain what is currently in the viewport
479 // this is all the indexes we want, including those that already exist, so this method
480 // will end up going through each index and drawing only if the row doesn't already exist
481 var indexesToDraw = this.calculateIndexesToDraw();
482 this.removeRowCompsNotToDraw(indexesToDraw);
483 // add in new rows
484 var nextVmTurnFunctions = [];
485 var rowComps = [];
486 indexesToDraw.forEach(function (rowIndex) {
487 var rowComp = _this.createOrUpdateRowComp(rowIndex, rowsToRecycle, animate, afterScroll);
488 if (utils_1.Utils.exists(rowComp)) {
489 rowComps.push(rowComp);
490 utils_1.Utils.pushAll(nextVmTurnFunctions, rowComp.getAndClearNextVMTurnFunctions());
491 }
492 });
493 this.flushContainers(rowComps);
494 utils_1.Utils.executeNextVMTurn(nextVmTurnFunctions);
495 if (afterScroll && !this.gridOptionsWrapper.isSuppressAnimationFrame()) {
496 this.beans.taskQueue.addP2Task(this.destroyRowComps.bind(this, rowsToRecycle, animate));
497 }
498 else {
499 this.destroyRowComps(rowsToRecycle, animate);
500 }
501 this.checkAngularCompile();
502 };
503 RowRenderer.prototype.flushContainers = function (rowComps) {
504 utils_1.Utils.iterateObject(this.rowContainers, function (key, rowContainerComp) {
505 if (rowContainerComp) {
506 rowContainerComp.flushRowTemplates();
507 }
508 });
509 rowComps.forEach(function (rowComp) { return rowComp.afterFlush(); });
510 };
511 RowRenderer.prototype.onDisplayedColumnsChanged = function () {
512 var pinningLeft = this.columnController.isPinningLeft();
513 var pinningRight = this.columnController.isPinningRight();
514 var atLeastOneChanged = this.pinningLeft !== pinningLeft || pinningRight !== this.pinningRight;
515 if (atLeastOneChanged) {
516 this.pinningLeft = pinningLeft;
517 this.pinningRight = pinningRight;
518 if (this.gridOptionsWrapper.isEmbedFullWidthRows()) {
519 this.redrawFullWidthEmbeddedRows();
520 }
521 }
522 };
523 // when embedding, what gets showed in each section depends on what is pinned. eg if embedding group expand / collapse,
524 // then it should go into the pinned left area if pinning left, or the center area if not pinning.
525 RowRenderer.prototype.redrawFullWidthEmbeddedRows = function () {
526 // if either of the pinned panels has shown / hidden, then need to redraw the fullWidth bits when
527 // embedded, as what appears in each section depends on whether we are pinned or not
528 var rowsToRemove = [];
529 utils_1.Utils.iterateObject(this.rowCompsByIndex, function (id, rowComp) {
530 if (rowComp.isFullWidth()) {
531 var rowIndex = rowComp.getRowNode().rowIndex;
532 rowsToRemove.push(rowIndex.toString());
533 }
534 });
535 this.refreshFloatingRowComps();
536 this.removeRowComps(rowsToRemove);
537 this.redrawAfterScroll();
538 };
539 RowRenderer.prototype.createOrUpdateRowComp = function (rowIndex, rowsToRecycle, animate, afterScroll) {
540 var rowNode;
541 var rowComp = this.rowCompsByIndex[rowIndex];
542 // if no row comp, see if we can get it from the previous rowComps
543 if (!rowComp) {
544 rowNode = this.paginationProxy.getRow(rowIndex);
545 if (utils_1.Utils.exists(rowNode) && utils_1.Utils.exists(rowsToRecycle) && rowsToRecycle[rowNode.id]) {
546 rowComp = rowsToRecycle[rowNode.id];
547 rowsToRecycle[rowNode.id] = null;
548 }
549 }
550 var creatingNewRowComp = !rowComp;
551 if (creatingNewRowComp) {
552 // create a new one
553 if (!rowNode) {
554 rowNode = this.paginationProxy.getRow(rowIndex);
555 }
556 if (utils_1.Utils.exists(rowNode)) {
557 rowComp = this.createRowComp(rowNode, animate, afterScroll);
558 }
559 else {
560 // this should never happen - if somehow we are trying to create
561 // a row for a rowNode that does not exist.
562 return;
563 }
564 }
565 else {
566 // ensure row comp is in right position in DOM
567 rowComp.ensureDomOrder();
568 }
569 this.rowCompsByIndex[rowIndex] = rowComp;
570 return rowComp;
571 };
572 RowRenderer.prototype.destroyRowComps = function (rowCompsMap, animate) {
573 var delayedFuncs = [];
574 utils_1.Utils.iterateObject(rowCompsMap, function (nodeId, rowComp) {
575 // if row was used, then it's null
576 if (!rowComp) {
577 return;
578 }
579 rowComp.destroy(animate);
580 utils_1.Utils.pushAll(delayedFuncs, rowComp.getAndClearDelayedDestroyFunctions());
581 });
582 utils_1.Utils.executeInAWhile(delayedFuncs);
583 };
584 RowRenderer.prototype.checkAngularCompile = function () {
585 var _this = this;
586 // if we are doing angular compiling, then do digest the scope here
587 if (this.gridOptionsWrapper.isAngularCompileRows()) {
588 // we do it in a timeout, in case we are already in an apply
589 setTimeout(function () {
590 _this.$scope.$apply();
591 }, 0);
592 }
593 };
594 RowRenderer.prototype.workOutFirstAndLastRowsToRender = function () {
595 var newFirst;
596 var newLast;
597 if (!this.paginationProxy.isRowsToRender()) {
598 newFirst = 0;
599 newLast = -1; // setting to -1 means nothing in range
600 }
601 else {
602 var pageFirstRow = this.paginationProxy.getPageFirstRow();
603 var pageLastRow = this.paginationProxy.getPageLastRow();
604 var pixelOffset = this.paginationProxy ? this.paginationProxy.getPixelOffset() : 0;
605 var heightOffset = this.heightScaler.getOffset();
606 var bodyVRange = this.gridPanel.getVScrollPosition();
607 var topPixel = bodyVRange.top;
608 var bottomPixel = bodyVRange.bottom;
609 var realPixelTop = topPixel + pixelOffset + heightOffset;
610 var realPixelBottom = bottomPixel + pixelOffset + heightOffset;
611 var first = this.paginationProxy.getRowIndexAtPixel(realPixelTop);
612 var last = this.paginationProxy.getRowIndexAtPixel(realPixelBottom);
613 //add in buffer
614 var buffer = this.gridOptionsWrapper.getRowBuffer();
615 first = first - buffer;
616 last = last + buffer;
617 // adjust, in case buffer extended actual size
618 if (first < pageFirstRow) {
619 first = pageFirstRow;
620 }
621 if (last > pageLastRow) {
622 last = pageLastRow;
623 }
624 newFirst = first;
625 newLast = last;
626 }
627 var firstDiffers = newFirst !== this.firstRenderedRow;
628 var lastDiffers = newLast !== this.lastRenderedRow;
629 if (firstDiffers || lastDiffers) {
630 this.firstRenderedRow = newFirst;
631 this.lastRenderedRow = newLast;
632 var event_1 = {
633 type: events_1.Events.EVENT_VIEWPORT_CHANGED,
634 firstRow: newFirst,
635 lastRow: newLast,
636 api: this.gridApi,
637 columnApi: this.columnApi
638 };
639 this.eventService.dispatchEvent(event_1);
640 }
641 };
642 RowRenderer.prototype.getFirstVirtualRenderedRow = function () {
643 return this.firstRenderedRow;
644 };
645 RowRenderer.prototype.getLastVirtualRenderedRow = function () {
646 return this.lastRenderedRow;
647 };
648 // check that none of the rows to remove are editing or focused as:
649 // a) if editing, we want to keep them, otherwise the user will loose the context of the edit,
650 // eg user starts editing, enters some text, then scrolls down and then up, next time row rendered
651 // the edit is reset - so we want to keep it rendered.
652 // b) if focused, we want ot keep keyboard focus, so if user ctrl+c, it goes to clipboard,
653 // otherwise the user can range select and drag (with focus cell going out of the viewport)
654 // and then ctrl+c, nothing will happen if cell is removed from dom.
655 RowRenderer.prototype.keepRowBecauseEditing = function (rowComp) {
656 var REMOVE_ROW = false;
657 var KEEP_ROW = true;
658 var rowNode = rowComp.getRowNode();
659 var rowHasFocus = this.focusedCellController.isRowNodeFocused(rowNode);
660 var rowIsEditing = rowComp.isEditing();
661 var mightWantToKeepRow = rowHasFocus || rowIsEditing;
662 // if we deffo don't want to keep it,
663 if (!mightWantToKeepRow) {
664 return REMOVE_ROW;
665 }
666 // editing row, only remove if it is no longer rendered, eg filtered out or new data set.
667 // the reason we want to keep is if user is scrolling up and down, we don't want to loose
668 // the context of the editing in process.
669 var rowNodePresent = this.paginationProxy.isRowPresent(rowNode);
670 return rowNodePresent ? KEEP_ROW : REMOVE_ROW;
671 };
672 RowRenderer.prototype.createRowComp = function (rowNode, animate, afterScroll) {
673 var useAnimationFrameForCreate = afterScroll && !this.gridOptionsWrapper.isSuppressAnimationFrame();
674 var rowComp = new rowComp_1.RowComp(this.$scope, this.rowContainers.body, this.rowContainers.pinnedLeft, this.rowContainers.pinnedRight, this.rowContainers.fullWidth, rowNode, this.beans, animate, useAnimationFrameForCreate);
675 rowComp.init();
676 return rowComp;
677 };
678 RowRenderer.prototype.getRenderedNodes = function () {
679 var renderedRows = this.rowCompsByIndex;
680 return Object.keys(renderedRows).map(function (key) {
681 return renderedRows[key].getRowNode();
682 });
683 };
684 // we use index for rows, but column object for columns, as the next column (by index) might not
685 // be visible (header grouping) so it's not reliable, so using the column object instead.
686 RowRenderer.prototype.navigateToNextCell = function (event, key, previousCell, allowUserOverride) {
687 var nextCell = previousCell;
688 // we keep searching for a next cell until we find one. this is how the group rows get skipped
689 while (true) {
690 nextCell = this.cellNavigationService.getNextCellToFocus(key, nextCell);
691 if (utils_1.Utils.missing(nextCell)) {
692 break;
693 }
694 var skipGroupRows = this.gridOptionsWrapper.isGroupUseEntireRow();
695 if (skipGroupRows) {
696 var rowNode = this.paginationProxy.getRow(nextCell.rowIndex);
697 if (!rowNode.group) {
698 break;
699 }
700 }
701 else {
702 break;
703 }
704 }
705 // allow user to override what cell to go to next. when doing normal cell navigation (with keys)
706 // we allow this, however if processing 'enter after edit' we don't allow override
707 if (allowUserOverride) {
708 var userFunc = this.gridOptionsWrapper.getNavigateToNextCellFunc();
709 if (utils_1.Utils.exists(userFunc)) {
710 var params = {
711 key: key,
712 previousCellDef: previousCell,
713 nextCellDef: nextCell ? nextCell.getGridCellDef() : null,
714 event: event
715 };
716 var nextCellDef = userFunc(params);
717 if (utils_1.Utils.exists(nextCellDef)) {
718 nextCell = new gridCell_1.GridCell(nextCellDef);
719 }
720 else {
721 nextCell = null;
722 }
723 }
724 }
725 // no next cell means we have reached a grid boundary, eg left, right, top or bottom of grid
726 if (!nextCell) {
727 return;
728 }
729 this.ensureCellVisible(nextCell);
730 this.focusedCellController.setFocusedCell(nextCell.rowIndex, nextCell.column, nextCell.floating, true);
731 if (this.rangeController) {
732 var gridCell = new gridCell_1.GridCell({ rowIndex: nextCell.rowIndex, floating: nextCell.floating, column: nextCell.column });
733 this.rangeController.setRangeToCell(gridCell);
734 }
735 };
736 RowRenderer.prototype.ensureCellVisible = function (gridCell) {
737 // this scrolls the row into view
738 if (utils_1.Utils.missing(gridCell.floating)) {
739 this.gridPanel.ensureIndexVisible(gridCell.rowIndex);
740 }
741 if (!gridCell.column.isPinned()) {
742 this.gridPanel.ensureColumnVisible(gridCell.column);
743 }
744 // need to nudge the scrolls for the floating items. otherwise when we set focus on a non-visible
745 // floating cell, the scrolls get out of sync
746 this.gridPanel.horizontallyScrollHeaderCenterAndFloatingCenter();
747 // need to flush frames, to make sure the correct cells are rendered
748 this.animationFrameService.flushAllFrames();
749 };
750 RowRenderer.prototype.startEditingCell = function (gridCell, keyPress, charPress) {
751 var cell = this.getComponentForCell(gridCell);
752 if (cell) {
753 cell.startRowOrCellEdit(keyPress, charPress);
754 }
755 };
756 RowRenderer.prototype.getComponentForCell = function (gridCell) {
757 var rowComponent;
758 switch (gridCell.floating) {
759 case constants_1.Constants.PINNED_TOP:
760 rowComponent = this.floatingTopRowComps[gridCell.rowIndex];
761 break;
762 case constants_1.Constants.PINNED_BOTTOM:
763 rowComponent = this.floatingBottomRowComps[gridCell.rowIndex];
764 break;
765 default:
766 rowComponent = this.rowCompsByIndex[gridCell.rowIndex];
767 break;
768 }
769 if (!rowComponent) {
770 return null;
771 }
772 var cellComponent = rowComponent.getRenderedCellForColumn(gridCell.column);
773 return cellComponent;
774 };
775 RowRenderer.prototype.onTabKeyDown = function (previousRenderedCell, keyboardEvent) {
776 var backwards = keyboardEvent.shiftKey;
777 var success = this.moveToCellAfter(previousRenderedCell, backwards);
778 if (success) {
779 keyboardEvent.preventDefault();
780 }
781 };
782 RowRenderer.prototype.tabToNextCell = function (backwards) {
783 var focusedCell = this.focusedCellController.getFocusedCell();
784 // if no focus, then cannot navigate
785 if (utils_1.Utils.missing(focusedCell)) {
786 return false;
787 }
788 var renderedCell = this.getComponentForCell(focusedCell);
789 // if cell is not rendered, means user has scrolled away from the cell
790 if (utils_1.Utils.missing(renderedCell)) {
791 return false;
792 }
793 var result = this.moveToCellAfter(renderedCell, backwards);
794 return result;
795 };
796 RowRenderer.prototype.moveToCellAfter = function (previousRenderedCell, backwards) {
797 var editing = previousRenderedCell.isEditing();
798 var res;
799 if (editing) {
800 if (this.gridOptionsWrapper.isFullRowEdit()) {
801 res = this.moveToNextEditingRow(previousRenderedCell, backwards);
802 }
803 else {
804 res = this.moveToNextEditingCell(previousRenderedCell, backwards);
805 }
806 }
807 else {
808 res = this.moveToNextCellNotEditing(previousRenderedCell, backwards);
809 }
810 return res;
811 };
812 RowRenderer.prototype.moveToNextEditingCell = function (previousRenderedCell, backwards) {
813 var gridCell = previousRenderedCell.getGridCell();
814 // need to do this before getting next cell to edit, in case the next cell
815 // has editable function (eg colDef.editable=func() ) and it depends on the
816 // result of this cell, so need to save updates from the first edit, in case
817 // the value is referenced in the function.
818 previousRenderedCell.stopEditing();
819 // find the next cell to start editing
820 var nextRenderedCell = this.findNextCellToFocusOn(gridCell, backwards, true);
821 var foundCell = utils_1.Utils.exists(nextRenderedCell);
822 // only prevent default if we found a cell. so if user is on last cell and hits tab, then we default
823 // to the normal tabbing so user can exit the grid.
824 if (foundCell) {
825 nextRenderedCell.startEditingIfEnabled(null, null, true);
826 nextRenderedCell.focusCell(false);
827 }
828 return foundCell;
829 };
830 RowRenderer.prototype.moveToNextEditingRow = function (previousRenderedCell, backwards) {
831 var gridCell = previousRenderedCell.getGridCell();
832 // find the next cell to start editing
833 var nextRenderedCell = this.findNextCellToFocusOn(gridCell, backwards, true);
834 var foundCell = utils_1.Utils.exists(nextRenderedCell);
835 // only prevent default if we found a cell. so if user is on last cell and hits tab, then we default
836 // to the normal tabbing so user can exit the grid.
837 if (foundCell) {
838 this.moveEditToNextCellOrRow(previousRenderedCell, nextRenderedCell);
839 }
840 return foundCell;
841 };
842 RowRenderer.prototype.moveToNextCellNotEditing = function (previousRenderedCell, backwards) {
843 var gridCell = previousRenderedCell.getGridCell();
844 // find the next cell to start editing
845 var nextRenderedCell = this.findNextCellToFocusOn(gridCell, backwards, false);
846 var foundCell = utils_1.Utils.exists(nextRenderedCell);
847 // only prevent default if we found a cell. so if user is on last cell and hits tab, then we default
848 // to the normal tabbing so user can exit the grid.
849 if (foundCell) {
850 nextRenderedCell.focusCell(true);
851 }
852 return foundCell;
853 };
854 RowRenderer.prototype.moveEditToNextCellOrRow = function (previousRenderedCell, nextRenderedCell) {
855 var pGridCell = previousRenderedCell.getGridCell();
856 var nGridCell = nextRenderedCell.getGridCell();
857 var rowsMatch = pGridCell.rowIndex === nGridCell.rowIndex && pGridCell.floating === nGridCell.floating;
858 if (rowsMatch) {
859 // same row, so we don't start / stop editing, we just move the focus along
860 previousRenderedCell.setFocusOutOnEditor();
861 nextRenderedCell.setFocusInOnEditor();
862 }
863 else {
864 var pRow = previousRenderedCell.getRenderedRow();
865 var nRow = nextRenderedCell.getRenderedRow();
866 previousRenderedCell.setFocusOutOnEditor();
867 pRow.stopEditing();
868 nRow.startRowEditing();
869 nextRenderedCell.setFocusInOnEditor();
870 }
871 nextRenderedCell.focusCell();
872 };
873 // called by the cell, when tab is pressed while editing.
874 // @return: RenderedCell when navigation successful, otherwise null
875 RowRenderer.prototype.findNextCellToFocusOn = function (gridCell, backwards, startEditing) {
876 var nextCell = gridCell;
877 while (true) {
878 nextCell = this.cellNavigationService.getNextTabbedCell(nextCell, backwards);
879 // allow user to override what cell to go to next
880 var userFunc = this.gridOptionsWrapper.getTabToNextCellFunc();
881 if (utils_1.Utils.exists(userFunc)) {
882 var params = {
883 backwards: backwards,
884 editing: startEditing,
885 previousCellDef: gridCell.getGridCellDef(),
886 nextCellDef: nextCell ? nextCell.getGridCellDef() : null
887 };
888 var nextCellDef = userFunc(params);
889 if (utils_1.Utils.exists(nextCellDef)) {
890 nextCell = new gridCell_1.GridCell(nextCellDef);
891 }
892 else {
893 nextCell = null;
894 }
895 }
896 // if no 'next cell', means we have got to last cell of grid, so nothing to move to,
897 // so bottom right cell going forwards, or top left going backwards
898 if (!nextCell) {
899 return null;
900 }
901 // if editing, but cell not editable, skip cell. we do this before we do all of
902 // the 'ensure index visible' and 'flush all frames', otherwise if we are skipping
903 // a bunch of cells (eg 10 rows) then all the work on ensuring cell visible is useless
904 // (except for the last one) which causes grid to stall for a while.
905 if (startEditing) {
906 var rowNode = this.paginationProxy.getRow(nextCell.rowIndex);
907 var cellIsEditable = nextCell.column.isCellEditable(rowNode);
908 if (!cellIsEditable) {
909 continue;
910 }
911 }
912 // this scrolls the row into view
913 var cellIsNotFloating = utils_1.Utils.missing(nextCell.floating);
914 if (cellIsNotFloating) {
915 this.gridPanel.ensureIndexVisible(nextCell.rowIndex);
916 }
917 // pinned columns don't scroll, so no need to ensure index visible
918 if (!nextCell.column.isPinned()) {
919 this.gridPanel.ensureColumnVisible(nextCell.column);
920 }
921 // need to nudge the scrolls for the floating items. otherwise when we set focus on a non-visible
922 // floating cell, the scrolls get out of sync
923 this.gridPanel.horizontallyScrollHeaderCenterAndFloatingCenter();
924 // get the grid panel to flush all animation frames - otherwise the call below to get the cellComp
925 // could fail, if we just scrolled the grid (to make a cell visible) and the rendering hasn't finished.
926 this.animationFrameService.flushAllFrames();
927 // we have to call this after ensureColumnVisible - otherwise it could be a virtual column
928 // or row that is not currently in view, hence the renderedCell would not exist
929 var nextCellComp = this.getComponentForCell(nextCell);
930 // if next cell is fullWidth row, then no rendered cell,
931 // as fullWidth rows have no cells, so we skip it
932 if (utils_1.Utils.missing(nextCellComp)) {
933 continue;
934 }
935 if (nextCellComp.isSuppressNavigable()) {
936 continue;
937 }
938 // by default, when we click a cell, it gets selected into a range, so to keep keyboard navigation
939 // consistent, we set into range here also.
940 if (this.rangeController) {
941 var gridCell_2 = new gridCell_1.GridCell({ rowIndex: nextCell.rowIndex, floating: nextCell.floating, column: nextCell.column });
942 this.rangeController.setRangeToCell(gridCell_2);
943 }
944 // we successfully tabbed onto a grid cell, so return true
945 return nextCellComp;
946 }
947 };
948 __decorate([
949 context_1.Autowired("paginationProxy"),
950 __metadata("design:type", paginationProxy_1.PaginationProxy)
951 ], RowRenderer.prototype, "paginationProxy", void 0);
952 __decorate([
953 context_1.Autowired("columnController"),
954 __metadata("design:type", columnController_1.ColumnController)
955 ], RowRenderer.prototype, "columnController", void 0);
956 __decorate([
957 context_1.Autowired("gridOptionsWrapper"),
958 __metadata("design:type", gridOptionsWrapper_1.GridOptionsWrapper)
959 ], RowRenderer.prototype, "gridOptionsWrapper", void 0);
960 __decorate([
961 context_1.Autowired("gridCore"),
962 __metadata("design:type", gridCore_1.GridCore)
963 ], RowRenderer.prototype, "gridCore", void 0);
964 __decorate([
965 context_1.Autowired("$scope"),
966 __metadata("design:type", Object)
967 ], RowRenderer.prototype, "$scope", void 0);
968 __decorate([
969 context_1.Autowired("expressionService"),
970 __metadata("design:type", expressionService_1.ExpressionService)
971 ], RowRenderer.prototype, "expressionService", void 0);
972 __decorate([
973 context_1.Autowired("templateService"),
974 __metadata("design:type", templateService_1.TemplateService)
975 ], RowRenderer.prototype, "templateService", void 0);
976 __decorate([
977 context_1.Autowired("valueService"),
978 __metadata("design:type", valueService_1.ValueService)
979 ], RowRenderer.prototype, "valueService", void 0);
980 __decorate([
981 context_1.Autowired("eventService"),
982 __metadata("design:type", eventService_1.EventService)
983 ], RowRenderer.prototype, "eventService", void 0);
984 __decorate([
985 context_1.Autowired("pinnedRowModel"),
986 __metadata("design:type", pinnedRowModel_1.PinnedRowModel)
987 ], RowRenderer.prototype, "pinnedRowModel", void 0);
988 __decorate([
989 context_1.Autowired("context"),
990 __metadata("design:type", context_1.Context)
991 ], RowRenderer.prototype, "context", void 0);
992 __decorate([
993 context_1.Autowired("loggerFactory"),
994 __metadata("design:type", logger_1.LoggerFactory)
995 ], RowRenderer.prototype, "loggerFactory", void 0);
996 __decorate([
997 context_1.Autowired("focusedCellController"),
998 __metadata("design:type", focusedCellController_1.FocusedCellController)
999 ], RowRenderer.prototype, "focusedCellController", void 0);
1000 __decorate([
1001 context_1.Autowired("cellNavigationService"),
1002 __metadata("design:type", cellNavigationService_1.CellNavigationService)
1003 ], RowRenderer.prototype, "cellNavigationService", void 0);
1004 __decorate([
1005 context_1.Autowired("columnApi"),
1006 __metadata("design:type", columnApi_1.ColumnApi)
1007 ], RowRenderer.prototype, "columnApi", void 0);
1008 __decorate([
1009 context_1.Autowired("gridApi"),
1010 __metadata("design:type", gridApi_1.GridApi)
1011 ], RowRenderer.prototype, "gridApi", void 0);
1012 __decorate([
1013 context_1.Autowired("beans"),
1014 __metadata("design:type", beans_1.Beans)
1015 ], RowRenderer.prototype, "beans", void 0);
1016 __decorate([
1017 context_1.Autowired("heightScaler"),
1018 __metadata("design:type", heightScaler_1.HeightScaler)
1019 ], RowRenderer.prototype, "heightScaler", void 0);
1020 __decorate([
1021 context_1.Autowired("animationFrameService"),
1022 __metadata("design:type", animationFrameService_1.AnimationFrameService)
1023 ], RowRenderer.prototype, "animationFrameService", void 0);
1024 __decorate([
1025 context_1.Optional("rangeController"),
1026 __metadata("design:type", Object)
1027 ], RowRenderer.prototype, "rangeController", void 0);
1028 __decorate([
1029 __param(0, context_1.Qualifier("loggerFactory")),
1030 __metadata("design:type", Function),
1031 __metadata("design:paramtypes", [logger_1.LoggerFactory]),
1032 __metadata("design:returntype", void 0)
1033 ], RowRenderer.prototype, "agWire", null);
1034 __decorate([
1035 context_1.PreDestroy,
1036 __metadata("design:type", Function),
1037 __metadata("design:paramtypes", []),
1038 __metadata("design:returntype", void 0)
1039 ], RowRenderer.prototype, "destroy", null);
1040 RowRenderer = __decorate([
1041 context_1.Bean("rowRenderer")
1042 ], RowRenderer);
1043 return RowRenderer;
1044}(beanStub_1.BeanStub));
1045exports.RowRenderer = RowRenderer;