-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathgraph-util.js
More file actions
249 lines (204 loc) · 6.21 KB
/
graph-util.js
File metadata and controls
249 lines (204 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// @flow
/*
Copyright(c) 2018 Uber Technologies, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { type IEdge } from '../components/edge';
import { type INode } from '../components/node';
import { type IPoint } from '../components/graph-view-props';
import fastDeepEqual from 'fast-deep-equal';
import ReactDOM from 'react-dom';
export type INodeMapNode = {
node: INode,
originalArrIndex: number,
incomingEdges: IEdge[],
outgoingEdges: IEdge[],
parents: INode[],
children: INode[],
};
class GraphUtils {
static getNodesMap(nodes: any, key: string) {
const map = {};
const arr = Object.keys(nodes).map(key => nodes[key]);
let item = null;
for (let i = 0; i < arr.length; i++) {
item = arr[i];
map[`key-${item[key]}`] = {
children: [],
incomingEdges: [],
node: item,
originalArrIndex: i,
outgoingEdges: [],
parents: [],
};
}
return map;
}
static getEdgesMap(arr: IEdge[]) {
const map = {};
let item = null;
for (let i = 0; i < arr.length; i++) {
item = arr[i];
if (item.target == null) {
continue;
}
map[`${item.source != null ? item.source : ''}_${item.target}`] = {
edge: item,
originalArrIndex: i,
};
}
return map;
}
static linkNodesAndEdges(nodesMap: any, edges: IEdge[]) {
let nodeMapSourceNode = null;
let nodeMapTargetNode = null;
let edge = null;
for (let i = 0; i < edges.length; i++) {
edge = edges[i];
if (edge.target == null) {
continue;
}
const sourceID = `key-${edge.source != null ? edge.source : ''}`;
const targetID = `key-${edge.target}`;
nodeMapSourceNode = nodesMap[sourceID];
nodeMapTargetNode = nodesMap[targetID];
// avoid an orphaned edge
if (nodeMapSourceNode && nodeMapTargetNode) {
nodeMapSourceNode.outgoingEdges.push(edge);
nodeMapTargetNode.incomingEdges.push(edge);
nodeMapSourceNode.children.push(nodeMapTargetNode);
nodeMapTargetNode.parents.push(nodeMapSourceNode);
} else {
// This can get noisy because linkNodesAndEdges runs a lot.
// The consumer should have cleared out the edges before rendering react-digraph
console.warn('react-digraph: Found orphaned edges');
}
}
}
static removeElementFromDom(id: string, searchElement?: any = document) {
const container = searchElement.querySelector(`[id='${id}']`);
if (container && container.parentNode) {
ReactDOM.unmountComponentAtNode(container);
container.parentNode.removeChild(container);
return true;
}
return false;
}
static findParent(element: any, selector: string, stopAtSelector?: string) {
if (!element || (stopAtSelector && element?.matches?.(stopAtSelector))) {
return null;
}
if (element?.matches?.(selector)) {
return element;
} else if (element?.parentNode) {
return GraphUtils.findParent(
element.parentNode,
selector,
stopAtSelector
);
}
return null;
}
static classNames(...args: any[]) {
let className = '';
for (const arg of args) {
if (typeof arg === 'string' || typeof arg === 'number') {
className += ` ${arg}`;
} else if (
typeof arg === 'object' &&
!Array.isArray(arg) &&
arg !== null
) {
Object.keys(arg).forEach(key => {
if (arg[key]) {
className += ` ${key}`;
}
});
} else if (Array.isArray(arg)) {
className += ` ${arg.join(' ')}`;
}
}
return className.trim();
}
static yieldingLoop(count, chunksize, callback, finished) {
let i = 0;
(function chunk() {
const end = Math.min(i + chunksize, count);
for (; i < end; ++i) {
callback.call(null, i);
}
if (i < count) {
setTimeout(chunk, 0);
} else {
finished && finished.call(null);
}
})();
}
// retained for backwards compatibility
static hasNodeShallowChanged(prevNode: INode, newNode: INode) {
return !this.isEqual(prevNode, newNode);
}
static isEqual(prevNode: any, newNode: any) {
return fastDeepEqual(prevNode, newNode);
}
static findNodesWithinArea(
start: IPoint,
end: IPoint,
nodes: INode[],
nodeKey: string
): Map<string, INode> {
const smallerX = Math.min(start.x, end.x);
const smallerY = Math.min(start.y, end.y);
const largerX = Math.max(end.x, start.x);
const largerY = Math.max(end.y, start.y);
const foundNodesMap = new Map();
nodes.forEach(node => {
if (
node.x >= smallerX &&
node.x <= largerX &&
node.y >= smallerY &&
node.y <= largerY
) {
foundNodesMap.set(node[nodeKey], node);
}
});
return foundNodesMap;
}
static findConnectedEdgesForNodes(
nodes: Map<string, INode>,
edgesMap: any,
nodeKey: string
): Map<string, IEdge> {
const foundEdgesMap = new Map();
for (const nodeA of nodes) {
for (const nodeB of nodes) {
// nodeA and nodeB are map arrays: ["key", node]
// Find edges where A is connected to B or B is connected to A
const edgeAB = edgesMap[`${nodeA[1][nodeKey]}_${nodeB[1][nodeKey]}`];
const edgeBA = edgesMap[`${nodeB[1][nodeKey]}_${nodeA[1][nodeKey]}`];
if (edgeAB != null) {
foundEdgesMap.set(
`${edgeAB.edge.source}_${edgeAB.edge.target}`,
edgeAB.edge
);
}
if (edgeBA != null) {
foundEdgesMap.set(
`${edgeBA.edge.source}_${edgeBA.edge.target}`,
edgeBA.edge
);
}
}
}
return foundEdgesMap;
}
}
export default GraphUtils;