UNPKG

14.9 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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
9 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
10 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
11 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;
12 return c > 3 && r && Object.defineProperty(target, key, r), r;
13};
14var __metadata = (this && this.__metadata) || function (k, v) {
15 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
16};
17var __param = (this && this.__param) || function (paramIndex, decorator) {
18 return function (target, key) { decorator(target, key, paramIndex); }
19};
20Object.defineProperty(exports, "__esModule", { value: true });
21var utils_1 = require("./utils");
22var context_1 = require("./context/context");
23var context_2 = require("./context/context");
24var logger_1 = require("./logger");
25var eventService_1 = require("./eventService");
26var events_1 = require("./events");
27var context_3 = require("./context/context");
28var gridOptionsWrapper_1 = require("./gridOptionsWrapper");
29var context_4 = require("./context/context");
30var constants_1 = require("./constants");
31var columnApi_1 = require("./columnController/columnApi");
32var gridApi_1 = require("./gridApi");
33var SelectionController = (function () {
34 function SelectionController() {
35 }
36 SelectionController.prototype.setBeans = function (loggerFactory) {
37 this.logger = loggerFactory.create('SelectionController');
38 this.reset();
39 if (this.gridOptionsWrapper.isRowModelDefault()) {
40 this.eventService.addEventListener(events_1.Events.EVENT_ROW_DATA_CHANGED, this.reset.bind(this));
41 }
42 else {
43 this.logger.log('dont know what to do here');
44 }
45 };
46 SelectionController.prototype.init = function () {
47 this.groupSelectsChildren = this.gridOptionsWrapper.isGroupSelectsChildren();
48 this.eventService.addEventListener(events_1.Events.EVENT_ROW_SELECTED, this.onRowSelected.bind(this));
49 };
50 SelectionController.prototype.setLastSelectedNode = function (rowNode) {
51 this.lastSelectedNode = rowNode;
52 };
53 SelectionController.prototype.getLastSelectedNode = function () {
54 return this.lastSelectedNode;
55 };
56 SelectionController.prototype.getSelectedNodes = function () {
57 var selectedNodes = [];
58 utils_1.Utils.iterateObject(this.selectedNodes, function (key, rowNode) {
59 if (rowNode) {
60 selectedNodes.push(rowNode);
61 }
62 });
63 return selectedNodes;
64 };
65 SelectionController.prototype.getSelectedRows = function () {
66 var selectedRows = [];
67 utils_1.Utils.iterateObject(this.selectedNodes, function (key, rowNode) {
68 if (rowNode && rowNode.data) {
69 selectedRows.push(rowNode.data);
70 }
71 });
72 return selectedRows;
73 };
74 SelectionController.prototype.removeGroupsFromSelection = function () {
75 var _this = this;
76 utils_1.Utils.iterateObject(this.selectedNodes, function (key, rowNode) {
77 if (rowNode && rowNode.group) {
78 _this.selectedNodes[rowNode.id] = undefined;
79 }
80 });
81 };
82 // should only be called if groupSelectsChildren=true
83 SelectionController.prototype.updateGroupsFromChildrenSelections = function () {
84 // we only do this when group selection state depends on selected children
85 if (!this.gridOptionsWrapper.isGroupSelectsChildren()) {
86 return;
87 }
88 // also only do it if CSRM (code should never allow this anyway)
89 if (this.rowModel.getType() !== constants_1.Constants.ROW_MODEL_TYPE_CLIENT_SIDE) {
90 return;
91 }
92 var clientSideRowModel = this.rowModel;
93 clientSideRowModel.getTopLevelNodes().forEach(function (rowNode) {
94 rowNode.depthFirstSearch(function (rowNode) {
95 if (rowNode.group) {
96 rowNode.calculateSelectedFromChildren();
97 }
98 });
99 });
100 };
101 SelectionController.prototype.getNodeForIdIfSelected = function (id) {
102 return this.selectedNodes[id];
103 };
104 SelectionController.prototype.clearOtherNodes = function (rowNodeToKeepSelected) {
105 var _this = this;
106 var groupsToRefresh = {};
107 var updatedCount = 0;
108 utils_1.Utils.iterateObject(this.selectedNodes, function (key, otherRowNode) {
109 if (otherRowNode && otherRowNode.id !== rowNodeToKeepSelected.id) {
110 var rowNode = _this.selectedNodes[otherRowNode.id];
111 updatedCount += rowNode.setSelectedParams({ newValue: false, clearSelection: false, suppressFinishActions: true });
112 if (_this.groupSelectsChildren && otherRowNode.parent) {
113 groupsToRefresh[otherRowNode.parent.id] = otherRowNode.parent;
114 }
115 }
116 });
117 utils_1.Utils.iterateObject(groupsToRefresh, function (key, group) {
118 group.calculateSelectedFromChildren();
119 });
120 return updatedCount;
121 };
122 SelectionController.prototype.onRowSelected = function (event) {
123 var rowNode = event.node;
124 // we do not store the group rows when the groups select children
125 if (this.groupSelectsChildren && rowNode.group) {
126 return;
127 }
128 if (rowNode.isSelected()) {
129 this.selectedNodes[rowNode.id] = rowNode;
130 }
131 else {
132 this.selectedNodes[rowNode.id] = undefined;
133 }
134 };
135 SelectionController.prototype.syncInRowNode = function (rowNode, oldNode) {
136 this.syncInOldRowNode(rowNode, oldNode);
137 this.syncInNewRowNode(rowNode);
138 };
139 // if the id has changed for the node, then this means the rowNode
140 // is getting used for a different data item, which breaks
141 // our selectedNodes, as the node now is mapped by the old id
142 // which is inconsistent. so to keep the old node as selected,
143 // we swap in the clone (with the old id and old data). this means
144 // the oldNode is effectively a daemon we keep a reference to,
145 // so if client calls api.getSelectedNodes(), it gets the daemon
146 // in the result. when the client un-selects, the reference to the
147 // daemon is removed. the daemon, because it's an oldNode, is not
148 // used by the grid for rendering, it's a copy of what the node used
149 // to be like before the id was changed.
150 SelectionController.prototype.syncInOldRowNode = function (rowNode, oldNode) {
151 var oldNodeHasDifferentId = utils_1.Utils.exists(oldNode) && (rowNode.id !== oldNode.id);
152 if (oldNodeHasDifferentId) {
153 var oldNodeSelected = utils_1.Utils.exists(this.selectedNodes[oldNode.id]);
154 if (oldNodeSelected) {
155 this.selectedNodes[oldNode.id] = oldNode;
156 }
157 }
158 };
159 SelectionController.prototype.syncInNewRowNode = function (rowNode) {
160 if (utils_1.Utils.exists(this.selectedNodes[rowNode.id])) {
161 rowNode.setSelectedInitialValue(true);
162 this.selectedNodes[rowNode.id] = rowNode;
163 }
164 else {
165 rowNode.setSelectedInitialValue(false);
166 }
167 };
168 SelectionController.prototype.reset = function () {
169 this.logger.log('reset');
170 this.selectedNodes = {};
171 this.lastSelectedNode = null;
172 };
173 // returns a list of all nodes at 'best cost' - a feature to be used
174 // with groups / trees. if a group has all it's children selected,
175 // then the group appears in the result, but not the children.
176 // Designed for use with 'children' as the group selection type,
177 // where groups don't actually appear in the selection normally.
178 SelectionController.prototype.getBestCostNodeSelection = function () {
179 if (this.rowModel.getType() !== constants_1.Constants.ROW_MODEL_TYPE_CLIENT_SIDE) {
180 console.warn('getBestCostNodeSelection is only avilable when using normal row model');
181 }
182 var clientSideRowModel = this.rowModel;
183 var topLevelNodes = clientSideRowModel.getTopLevelNodes();
184 if (topLevelNodes === null) {
185 console.warn('selectAll not available doing rowModel=virtual');
186 return;
187 }
188 var result = [];
189 // recursive function, to find the selected nodes
190 function traverse(nodes) {
191 for (var i = 0, l = nodes.length; i < l; i++) {
192 var node = nodes[i];
193 if (node.isSelected()) {
194 result.push(node);
195 }
196 else {
197 // if not selected, then if it's a group, and the group
198 // has children, continue to search for selections
199 if (node.group && node.children) {
200 traverse(node.children);
201 }
202 }
203 }
204 }
205 traverse(topLevelNodes);
206 return result;
207 };
208 SelectionController.prototype.setRowModel = function (rowModel) {
209 this.rowModel = rowModel;
210 };
211 SelectionController.prototype.isEmpty = function () {
212 var count = 0;
213 utils_1.Utils.iterateObject(this.selectedNodes, function (nodeId, rowNode) {
214 if (rowNode) {
215 count++;
216 }
217 });
218 return count === 0;
219 };
220 SelectionController.prototype.deselectAllRowNodes = function (justFiltered) {
221 if (justFiltered === void 0) { justFiltered = false; }
222 var callback = function (rowNode) { return rowNode.selectThisNode(false); };
223 var rowModelClientSide = this.rowModel.getType() === constants_1.Constants.ROW_MODEL_TYPE_CLIENT_SIDE;
224 if (justFiltered) {
225 if (!rowModelClientSide) {
226 console.error('ag-Grid: selecting just filtered only works with In Memory Row Model');
227 return;
228 }
229 var clientSideRowModel = this.rowModel;
230 clientSideRowModel.forEachNodeAfterFilter(callback);
231 }
232 else {
233 utils_1.Utils.iterateObject(this.selectedNodes, function (id, rowNode) {
234 // remember the reference can be to null, as we never 'delete' from the map
235 if (rowNode) {
236 callback(rowNode);
237 }
238 });
239 // this clears down the map (whereas above only sets the items in map to 'undefined')
240 this.reset();
241 }
242 // the above does not clean up the parent rows if they are selected
243 if (rowModelClientSide && this.groupSelectsChildren) {
244 this.updateGroupsFromChildrenSelections();
245 }
246 var event = {
247 type: events_1.Events.EVENT_SELECTION_CHANGED,
248 api: this.gridApi,
249 columnApi: this.columnApi
250 };
251 this.eventService.dispatchEvent(event);
252 };
253 SelectionController.prototype.selectAllRowNodes = function (justFiltered) {
254 if (justFiltered === void 0) { justFiltered = false; }
255 if (this.rowModel.getType() !== constants_1.Constants.ROW_MODEL_TYPE_CLIENT_SIDE) {
256 throw "selectAll only available with normal row model, ie not " + this.rowModel.getType();
257 }
258 var clientSideRowModel = this.rowModel;
259 var callback = function (rowNode) { return rowNode.selectThisNode(true); };
260 if (justFiltered) {
261 clientSideRowModel.forEachNodeAfterFilter(callback);
262 }
263 else {
264 clientSideRowModel.forEachNode(callback);
265 }
266 // the above does not clean up the parent rows if they are selected
267 if (this.rowModel.getType() === constants_1.Constants.ROW_MODEL_TYPE_CLIENT_SIDE && this.groupSelectsChildren) {
268 this.updateGroupsFromChildrenSelections();
269 }
270 var event = {
271 type: events_1.Events.EVENT_SELECTION_CHANGED,
272 api: this.gridApi,
273 columnApi: this.columnApi
274 };
275 this.eventService.dispatchEvent(event);
276 };
277 // Deprecated method
278 SelectionController.prototype.selectNode = function (rowNode, tryMulti) {
279 rowNode.setSelectedParams({ newValue: true, clearSelection: !tryMulti });
280 };
281 // Deprecated method
282 SelectionController.prototype.deselectIndex = function (rowIndex) {
283 var node = this.rowModel.getRow(rowIndex);
284 this.deselectNode(node);
285 };
286 // Deprecated method
287 SelectionController.prototype.deselectNode = function (rowNode) {
288 rowNode.setSelectedParams({ newValue: false, clearSelection: false });
289 };
290 // Deprecated method
291 SelectionController.prototype.selectIndex = function (index, tryMulti) {
292 var node = this.rowModel.getRow(index);
293 this.selectNode(node, tryMulti);
294 };
295 __decorate([
296 context_3.Autowired('eventService'),
297 __metadata("design:type", eventService_1.EventService)
298 ], SelectionController.prototype, "eventService", void 0);
299 __decorate([
300 context_3.Autowired('rowModel'),
301 __metadata("design:type", Object)
302 ], SelectionController.prototype, "rowModel", void 0);
303 __decorate([
304 context_3.Autowired('gridOptionsWrapper'),
305 __metadata("design:type", gridOptionsWrapper_1.GridOptionsWrapper)
306 ], SelectionController.prototype, "gridOptionsWrapper", void 0);
307 __decorate([
308 context_3.Autowired('columnApi'),
309 __metadata("design:type", columnApi_1.ColumnApi)
310 ], SelectionController.prototype, "columnApi", void 0);
311 __decorate([
312 context_3.Autowired('gridApi'),
313 __metadata("design:type", gridApi_1.GridApi)
314 ], SelectionController.prototype, "gridApi", void 0);
315 __decorate([
316 __param(0, context_2.Qualifier('loggerFactory')),
317 __metadata("design:type", Function),
318 __metadata("design:paramtypes", [logger_1.LoggerFactory]),
319 __metadata("design:returntype", void 0)
320 ], SelectionController.prototype, "setBeans", null);
321 __decorate([
322 context_4.PostConstruct,
323 __metadata("design:type", Function),
324 __metadata("design:paramtypes", []),
325 __metadata("design:returntype", void 0)
326 ], SelectionController.prototype, "init", null);
327 SelectionController = __decorate([
328 context_1.Bean('selectionController')
329 ], SelectionController);
330 return SelectionController;
331}());
332exports.SelectionController = SelectionController;