UNPKG

14.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";
8Object.defineProperty(exports, "__esModule", { value: true });
9var rowNode_1 = require("../../entities/rowNode");
10var utils_1 = require("../../utils");
11var events_1 = require("../../events");
12var ClientSideNodeManager = (function () {
13 function ClientSideNodeManager(rootNode, gridOptionsWrapper, context, eventService, columnController, gridApi, columnApi, selectionController) {
14 this.nextId = 0;
15 // when user is provide the id's, we also keep a map of ids to row nodes for convenience
16 this.allNodesMap = {};
17 this.rootNode = rootNode;
18 this.gridOptionsWrapper = gridOptionsWrapper;
19 this.context = context;
20 this.eventService = eventService;
21 this.columnController = columnController;
22 this.gridApi = gridApi;
23 this.columnApi = columnApi;
24 this.selectionController = selectionController;
25 this.rootNode.group = true;
26 this.rootNode.level = -1;
27 this.rootNode.id = ClientSideNodeManager.ROOT_NODE_ID;
28 this.rootNode.allLeafChildren = [];
29 this.rootNode.childrenAfterGroup = [];
30 this.rootNode.childrenAfterSort = [];
31 this.rootNode.childrenAfterFilter = [];
32 // if we make this class a bean, then can annotate postConstruct
33 this.postConstruct();
34 }
35 // @PostConstruct - this is not a bean, so postConstruct called by constructor
36 ClientSideNodeManager.prototype.postConstruct = function () {
37 // func below doesn't have 'this' pointer, so need to pull out these bits
38 this.getNodeChildDetails = this.gridOptionsWrapper.getNodeChildDetailsFunc();
39 this.suppressParentsInRowNodes = this.gridOptionsWrapper.isSuppressParentsInRowNodes();
40 this.doesDataFlower = this.gridOptionsWrapper.getDoesDataFlowerFunc();
41 this.isRowMasterFunc = this.gridOptionsWrapper.getIsRowMasterFunc();
42 this.doingLegacyTreeData = utils_1.Utils.exists(this.getNodeChildDetails);
43 this.doingMasterDetail = this.gridOptionsWrapper.isMasterDetail();
44 };
45 ClientSideNodeManager.prototype.getCopyOfNodesMap = function () {
46 var result = utils_1.Utils.cloneObject(this.allNodesMap);
47 return result;
48 };
49 ClientSideNodeManager.prototype.getRowNode = function (id) {
50 return this.allNodesMap[id];
51 };
52 ClientSideNodeManager.prototype.setRowData = function (rowData) {
53 this.rootNode.childrenAfterFilter = null;
54 this.rootNode.childrenAfterGroup = null;
55 this.rootNode.childrenAfterSort = null;
56 this.rootNode.childrenMapped = null;
57 this.nextId = 0;
58 this.allNodesMap = {};
59 if (!rowData) {
60 this.rootNode.allLeafChildren = [];
61 this.rootNode.childrenAfterGroup = [];
62 return;
63 }
64 // kick off recursion
65 var result = this.recursiveFunction(rowData, null, ClientSideNodeManager.TOP_LEVEL);
66 if (this.doingLegacyTreeData) {
67 this.rootNode.childrenAfterGroup = result;
68 this.setLeafChildren(this.rootNode);
69 }
70 else {
71 this.rootNode.allLeafChildren = result;
72 }
73 };
74 ClientSideNodeManager.prototype.updateRowData = function (rowDataTran, rowNodeOrder) {
75 var _this = this;
76 if (this.isLegacyTreeData()) {
77 return null;
78 }
79 var add = rowDataTran.add, addIndex = rowDataTran.addIndex, remove = rowDataTran.remove, update = rowDataTran.update;
80 var rowNodeTransaction = {
81 remove: [],
82 update: [],
83 add: []
84 };
85 if (utils_1.Utils.exists(add)) {
86 var useIndex = typeof addIndex === 'number' && addIndex >= 0;
87 if (useIndex) {
88 // items get inserted in reverse order for index insertion
89 add.reverse().forEach(function (item) {
90 var newRowNode = _this.addRowNode(item, addIndex);
91 rowNodeTransaction.add.push(newRowNode);
92 });
93 }
94 else {
95 add.forEach(function (item) {
96 var newRowNode = _this.addRowNode(item);
97 rowNodeTransaction.add.push(newRowNode);
98 });
99 }
100 }
101 if (utils_1.Utils.exists(remove)) {
102 var anyNodesSelected_1 = false;
103 remove.forEach(function (item) {
104 var rowNode = _this.lookupRowNode(item);
105 if (!rowNode) {
106 return;
107 }
108 if (rowNode.isSelected()) {
109 anyNodesSelected_1 = true;
110 }
111 _this.updatedRowNode(rowNode, item, false);
112 rowNodeTransaction.remove.push(rowNode);
113 });
114 if (anyNodesSelected_1) {
115 this.selectionController.updateGroupsFromChildrenSelections();
116 var event_1 = {
117 type: events_1.Events.EVENT_SELECTION_CHANGED,
118 api: this.gridApi,
119 columnApi: this.columnApi
120 };
121 this.eventService.dispatchEvent(event_1);
122 }
123 }
124 if (utils_1.Utils.exists(update)) {
125 update.forEach(function (item) {
126 var rowNode = _this.lookupRowNode(item);
127 if (!rowNode) {
128 return;
129 }
130 _this.updatedRowNode(rowNode, item, true);
131 rowNodeTransaction.update.push(rowNode);
132 });
133 }
134 if (rowNodeOrder) {
135 utils_1.Utils.sortRowNodesByOrder(this.rootNode.allLeafChildren, rowNodeOrder);
136 }
137 return rowNodeTransaction;
138 };
139 ClientSideNodeManager.prototype.addRowNode = function (data, index) {
140 var newNode = this.createNode(data, null, ClientSideNodeManager.TOP_LEVEL);
141 if (utils_1.Utils.exists(index)) {
142 utils_1.Utils.insertIntoArray(this.rootNode.allLeafChildren, newNode, index);
143 }
144 else {
145 this.rootNode.allLeafChildren.push(newNode);
146 }
147 return newNode;
148 };
149 ClientSideNodeManager.prototype.lookupRowNode = function (data) {
150 var rowNodeIdFunc = this.gridOptionsWrapper.getRowNodeIdFunc();
151 var rowNode;
152 if (utils_1.Utils.exists(rowNodeIdFunc)) {
153 // find rowNode using id
154 var id = rowNodeIdFunc(data);
155 rowNode = this.allNodesMap[id];
156 if (!rowNode) {
157 console.error("ag-Grid: could not find row id=" + id + ", data item was not found for this id");
158 return null;
159 }
160 }
161 else {
162 // find rowNode using object references
163 rowNode = utils_1.Utils.find(this.rootNode.allLeafChildren, function (rowNode) { return rowNode.data === data; });
164 if (!rowNode) {
165 console.error("ag-Grid: could not find data item as object was not found", data);
166 return null;
167 }
168 }
169 return rowNode;
170 };
171 ClientSideNodeManager.prototype.updatedRowNode = function (rowNode, data, update) {
172 if (update) {
173 // do update
174 rowNode.updateData(data);
175 }
176 else {
177 // do delete - setting 'tailingNodeInSequence = true' to ensure EVENT_SELECTION_CHANGED is not raised for
178 // each row node updated, instead it is raised once by the calling code if any selected nodes exist.
179 rowNode.setSelected(false, false, true);
180 // so row renderer knows to fade row out (and not reposition it)
181 rowNode.clearRowTop();
182 utils_1.Utils.removeFromArray(this.rootNode.allLeafChildren, rowNode);
183 this.allNodesMap[rowNode.id] = undefined;
184 }
185 };
186 ClientSideNodeManager.prototype.recursiveFunction = function (rowData, parent, level) {
187 var _this = this;
188 // make sure the rowData is an array and not a string of json - this was a commonly reported problem on the forum
189 if (typeof rowData === 'string') {
190 console.warn('ag-Grid: rowData must be an array, however you passed in a string. If you are loading JSON, make sure you convert the JSON string to JavaScript objects first');
191 return;
192 }
193 var rowNodes = [];
194 rowData.forEach(function (dataItem) {
195 var node = _this.createNode(dataItem, parent, level);
196 rowNodes.push(node);
197 });
198 return rowNodes;
199 };
200 ClientSideNodeManager.prototype.createNode = function (dataItem, parent, level) {
201 var node = new rowNode_1.RowNode();
202 this.context.wireBean(node);
203 var doingTreeData = this.gridOptionsWrapper.isTreeData();
204 var doingLegacyTreeData = !doingTreeData && utils_1.Utils.exists(this.getNodeChildDetails);
205 var nodeChildDetails = doingLegacyTreeData ? this.getNodeChildDetails(dataItem) : null;
206 if (nodeChildDetails && nodeChildDetails.group) {
207 node.group = true;
208 node.childrenAfterGroup = this.recursiveFunction(nodeChildDetails.children, node, level + 1);
209 node.expanded = nodeChildDetails.expanded === true;
210 node.field = nodeChildDetails.field;
211 node.key = nodeChildDetails.key;
212 node.canFlower = node.master; // deprecated, is now 'master'
213 // pull out all the leaf children and add to our node
214 this.setLeafChildren(node);
215 }
216 else {
217 node.group = false;
218 if (doingTreeData) {
219 node.master = false;
220 node.expanded = false;
221 }
222 else {
223 // this is the default, for when doing grid data
224 if (this.doesDataFlower) {
225 node.master = this.doesDataFlower(dataItem);
226 }
227 else if (this.doingMasterDetail) {
228 // if we are doing master detail, then the
229 // default is that everything can flower.
230 if (this.isRowMasterFunc) {
231 node.master = this.isRowMasterFunc(dataItem);
232 }
233 else {
234 node.master = true;
235 }
236 }
237 else {
238 node.master = false;
239 }
240 var rowGroupColumns = this.columnController.getRowGroupColumns();
241 var numRowGroupColumns = rowGroupColumns ? rowGroupColumns.length : 0;
242 // need to take row group into account when determining level
243 var masterRowLevel = level + numRowGroupColumns;
244 node.expanded = node.master ? this.isExpanded(masterRowLevel) : false;
245 }
246 }
247 // support for backwards compatibility, canFlow is now called 'master'
248 node.canFlower = node.master;
249 if (parent && !this.suppressParentsInRowNodes) {
250 node.parent = parent;
251 }
252 node.level = level;
253 node.setDataAndId(dataItem, this.nextId.toString());
254 this.allNodesMap[node.id] = node;
255 this.nextId++;
256 return node;
257 };
258 ClientSideNodeManager.prototype.isExpanded = function (level) {
259 var expandByDefault = this.gridOptionsWrapper.getGroupDefaultExpanded();
260 if (expandByDefault === -1) {
261 return true;
262 }
263 else {
264 return level < expandByDefault;
265 }
266 };
267 // this is only used for doing legacy tree data
268 ClientSideNodeManager.prototype.setLeafChildren = function (node) {
269 node.allLeafChildren = [];
270 if (node.childrenAfterGroup) {
271 node.childrenAfterGroup.forEach(function (childAfterGroup) {
272 if (childAfterGroup.group) {
273 if (childAfterGroup.allLeafChildren) {
274 childAfterGroup.allLeafChildren.forEach(function (leafChild) { return node.allLeafChildren.push(leafChild); });
275 }
276 }
277 else {
278 node.allLeafChildren.push(childAfterGroup);
279 }
280 });
281 }
282 };
283 ClientSideNodeManager.prototype.insertItemsAtIndex = function (index, rowData) {
284 if (this.isLegacyTreeData()) {
285 return null;
286 }
287 var nodeList = this.rootNode.allLeafChildren;
288 if (index > nodeList.length) {
289 console.warn("ag-Grid: invalid index " + index + ", max index is " + nodeList.length);
290 return;
291 }
292 var newNodes = [];
293 // go through the items backwards, otherwise they get added in reverse order
294 for (var i = rowData.length - 1; i >= 0; i--) {
295 var data = rowData[i];
296 var newNode = this.createNode(data, null, ClientSideNodeManager.TOP_LEVEL);
297 utils_1.Utils.insertIntoArray(nodeList, newNode, index);
298 newNodes.push(newNode);
299 }
300 return newNodes.length > 0 ? newNodes : null;
301 };
302 ClientSideNodeManager.prototype.addItems = function (items) {
303 var nodeList = this.rootNode.allLeafChildren;
304 return this.insertItemsAtIndex(nodeList.length, items);
305 };
306 ClientSideNodeManager.prototype.isLegacyTreeData = function () {
307 var rowsAlreadyGrouped = utils_1.Utils.exists(this.gridOptionsWrapper.getNodeChildDetailsFunc());
308 if (rowsAlreadyGrouped) {
309 console.warn('ag-Grid: adding and removing rows is not supported when using nodeChildDetailsFunc, ie it is not ' +
310 'supported for legacy tree data. Please see the docs on the new preferred way of providing tree data that works with delta updates.');
311 return true;
312 }
313 else {
314 return false;
315 }
316 };
317 ClientSideNodeManager.TOP_LEVEL = 0;
318 ClientSideNodeManager.ROOT_NODE_ID = 'ROOT_NODE_ID';
319 return ClientSideNodeManager;
320}());
321exports.ClientSideNodeManager = ClientSideNodeManager;