UNPKG

9.72 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};
17Object.defineProperty(exports, "__esModule", { value: true });
18var columnUtils_1 = require("./columnUtils");
19var columnGroup_1 = require("../entities/columnGroup");
20var originalColumnGroup_1 = require("../entities/originalColumnGroup");
21var context_1 = require("../context/context");
22var utils_1 = require("../utils");
23var context_2 = require("../context/context");
24// takes in a list of columns, as specified by the column definitions, and returns column groups
25var DisplayedGroupCreator = (function () {
26 function DisplayedGroupCreator() {
27 }
28 DisplayedGroupCreator.prototype.createDisplayedGroups = function (
29 // all displayed columns sorted - this is the columns the grid should show
30 sortedVisibleColumns,
31 // the tree of columns, as provided by the users, used to know what groups columns roll up into
32 balancedColumnTree,
33 // create's unique id's for the group
34 groupInstanceIdCreator,
35 // we try to reuse old groups if we can, to allow gui to do animation
36 oldDisplayedGroups) {
37 var _this = this;
38 var result = [];
39 var previousRealPath;
40 var previousOriginalPath;
41 var oldColumnsMapped = this.mapOldGroupsById(oldDisplayedGroups);
42 // go through each column, then do a bottom up comparison to the previous column, and start
43 // to share groups if they converge at any point.
44 sortedVisibleColumns.forEach(function (currentColumn) {
45 var currentOriginalPath = _this.getOriginalPathForColumn(balancedColumnTree, currentColumn);
46 var currentRealPath = [];
47 var firstColumn = !previousOriginalPath;
48 for (var i = 0; i < currentOriginalPath.length; i++) {
49 if (firstColumn || currentOriginalPath[i] !== previousOriginalPath[i]) {
50 // new group needed
51 var newGroup = _this.createColumnGroup(currentOriginalPath[i], groupInstanceIdCreator, oldColumnsMapped);
52 currentRealPath[i] = newGroup;
53 // if top level, add to result, otherwise add to parent
54 if (i == 0) {
55 result.push(newGroup);
56 }
57 else {
58 currentRealPath[i - 1].addChild(newGroup);
59 }
60 }
61 else {
62 // reuse old group
63 currentRealPath[i] = previousRealPath[i];
64 }
65 }
66 var noColumnGroups = currentRealPath.length === 0;
67 if (noColumnGroups) {
68 // if we are not grouping, then the result of the above is an empty
69 // path (no groups), and we just add the column to the root list.
70 result.push(currentColumn);
71 }
72 else {
73 var leafGroup = currentRealPath[currentRealPath.length - 1];
74 leafGroup.addChild(currentColumn);
75 }
76 previousRealPath = currentRealPath;
77 previousOriginalPath = currentOriginalPath;
78 });
79 this.setupParentsIntoColumns(result, null);
80 return result;
81 };
82 DisplayedGroupCreator.prototype.createColumnGroup = function (originalGroup, groupInstanceIdCreator, oldColumnsMapped) {
83 var groupId = originalGroup.getGroupId();
84 var instanceId = groupInstanceIdCreator.getInstanceIdForKey(groupId);
85 var uniqueId = columnGroup_1.ColumnGroup.createUniqueId(groupId, instanceId);
86 var columnGroup = oldColumnsMapped[uniqueId];
87 // if the user is setting new colDefs, it is possible that the id's overlap, and we
88 // would have a false match from above. so we double check we are talking about the
89 // same original column group.
90 if (columnGroup && columnGroup.getOriginalColumnGroup() !== originalGroup) {
91 columnGroup = null;
92 }
93 if (utils_1.Utils.exists(columnGroup)) {
94 // clean out the old column group here, as we will be adding children into it again
95 columnGroup.reset();
96 }
97 else {
98 columnGroup = new columnGroup_1.ColumnGroup(originalGroup, groupId, instanceId);
99 this.context.wireBean(columnGroup);
100 }
101 return columnGroup;
102 };
103 // returns back a 2d map of ColumnGroup as follows: groupId -> instanceId -> ColumnGroup
104 DisplayedGroupCreator.prototype.mapOldGroupsById = function (displayedGroups) {
105 var result = {};
106 var recursive = function (columnsOrGroups) {
107 columnsOrGroups.forEach(function (columnOrGroup) {
108 if (columnOrGroup instanceof columnGroup_1.ColumnGroup) {
109 var columnGroup = columnOrGroup;
110 result[columnOrGroup.getUniqueId()] = columnGroup;
111 recursive(columnGroup.getChildren());
112 }
113 });
114 };
115 if (displayedGroups) {
116 recursive(displayedGroups);
117 }
118 return result;
119 };
120 DisplayedGroupCreator.prototype.setupParentsIntoColumns = function (columnsOrGroups, parent) {
121 var _this = this;
122 columnsOrGroups.forEach(function (columnsOrGroup) {
123 columnsOrGroup.setParent(parent);
124 if (columnsOrGroup instanceof columnGroup_1.ColumnGroup) {
125 var columnGroup = columnsOrGroup;
126 _this.setupParentsIntoColumns(columnGroup.getChildren(), columnGroup);
127 }
128 });
129 };
130 // private createFakePath(balancedColumnTree: OriginalColumnGroupChild[], column: Column): OriginalColumnGroup[] {
131 // let fakePath: OriginalColumnGroup[] = [];
132 // let currentChildren = balancedColumnTree;
133 // // this while loop does search on the balanced tree, so our result is the right length
134 // let index = 0;
135 // while (currentChildren && currentChildren[0] && currentChildren[0] instanceof OriginalColumnGroup) {
136 // // putting in a deterministic fake id, in case the API in the future needs to reference the col
137 // let fakeGroup = new OriginalColumnGroup(null, 'FAKE_PATH_' + index, true);
138 // this.context.wireBean(fakeGroup);
139 //
140 // // fakePath.setChildren(children);
141 //
142 // fakePath.push(fakeGroup);
143 // currentChildren = (<OriginalColumnGroup>currentChildren[0]).getChildren();
144 // index++;
145 // }
146 //
147 // fakePath.forEach( (fakePathGroup: OriginalColumnGroup, i: number) => {
148 // let lastItemInList = i === fakePath.length-1;
149 // let child = lastItemInList ? column : fakePath[i+1];
150 // fakePathGroup.setChildren([child]);
151 // });
152 //
153 // return fakePath;
154 // }
155 DisplayedGroupCreator.prototype.getOriginalPathForColumn = function (balancedColumnTree, column) {
156 var result = [];
157 var found = false;
158 recursePath(balancedColumnTree, 0);
159 // it's possible we didn't find a path. this happens if the column is generated
160 // by the grid (auto-group), in that the definition didn't come from the client. in this case,
161 // we create a fake original path.
162 if (found) {
163 return result;
164 }
165 else {
166 console.log('could not get path');
167 return null;
168 // return this.createFakePath(balancedColumnTree, column);
169 }
170 function recursePath(balancedColumnTree, dept) {
171 for (var i = 0; i < balancedColumnTree.length; i++) {
172 if (found) {
173 // quit the search, so 'result' is kept with the found result
174 return;
175 }
176 var node = balancedColumnTree[i];
177 if (node instanceof originalColumnGroup_1.OriginalColumnGroup) {
178 var nextNode = node;
179 recursePath(nextNode.getChildren(), dept + 1);
180 result[dept] = node;
181 }
182 else {
183 if (node === column) {
184 found = true;
185 }
186 }
187 }
188 }
189 };
190 __decorate([
191 context_2.Autowired('columnUtils'),
192 __metadata("design:type", columnUtils_1.ColumnUtils)
193 ], DisplayedGroupCreator.prototype, "columnUtils", void 0);
194 __decorate([
195 context_2.Autowired('context'),
196 __metadata("design:type", context_1.Context)
197 ], DisplayedGroupCreator.prototype, "context", void 0);
198 DisplayedGroupCreator = __decorate([
199 context_1.Bean('displayedGroupCreator')
200 ], DisplayedGroupCreator);
201 return DisplayedGroupCreator;
202}());
203exports.DisplayedGroupCreator = DisplayedGroupCreator;