This repository was archived by the owner on Jul 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmind-map.tsx
More file actions
562 lines (544 loc) · 19 KB
/
mind-map.tsx
File metadata and controls
562 lines (544 loc) · 19 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
import * as ReactDOM from "react-dom";
import * as React from "react";
import {
MindMap as MindMapModule,
HierarchicalTree,
ConnectorConstraints,
ToolBase,
MouseEventArgs,
randomId,
ISelectionChangeEventArgs,
PointPort,
UserHandleModel,
SelectorConstraints,
SnapConstraints,
PointPortModel,
PortVisibility,
DiagramComponent,
NodeConstraints,
NodeModel,
ConnectorModel,
Node,
Connector,
Diagram,
DiagramTools,
Inject,
DataBinding,
Side,
MarginModel,
HorizontalAlignment,
VerticalAlignment,
TextModel
} from "@syncfusion/ej2-react-diagrams";
import { SampleBase } from "../common/sample-base";
import { DataManager, Query } from "@syncfusion/ej2-data";
export let mindMap: object[] = [
{ id: 1, Label: "Creativity", fill: "red", branch: "Root" },
{ id: 3, Label: "Brainstorming", parentId: 1, branch: "Right", fill: "red" },
{ id: 4, Label: "Complementing", parentId: 1, branch: "Left", fill: "red" },
{ id: 22, Label: "Sessions", parentId: 3, branch: "subRight", fill: "red" },
{ id: 23, Label: "Generate", parentId: 3, branch: "subRight", fill: "red" },
{ id: 25, Label: "Local", parentId: 22, branch: "subRight" },
{ id: 26, Label: "Remote", parentId: 22, branch: "subRight" },
{ id: 27, Label: "Individual", parentId: 22, branch: "subRight" },
{ id: 28, Label: "Teams", parentId: 22, branch: "subRight" },
{ id: 29, Label: "Ideas", parentId: 23, branch: "subRight" },
{ id: 30, Label: "Engagement", parentId: 23, branch: "subRight" },
{ id: 31, Label: "Product", parentId: 29, branch: "subRight" },
{ id: 32, Label: "Service", parentId: 29, branch: "subRight" },
{ id: 33, Label: "Business Direction", parentId: 29, branch: "subRight" },
{ id: 34, Label: "Empowering", parentId: 30, branch: "subRight" },
{ id: 35, Label: "Ownership", parentId: 30, branch: "subRight" },
{ id: 50, Label: "Information", parentId: 4, branch: "subLeft" },
{ id: 51, Label: "Expectations", parentId: 4, branch: "subLeft" },
{ id: 53, Label: "Competetors", parentId: 50, branch: "subLeft" },
{ id: 54, Label: "Products", parentId: 50, branch: "subLeft" },
{ id: 55, Label: "Features", parentId: 50, branch: "subLeft" },
{ id: 56, Label: "Other Data", parentId: 50, branch: "subLeft" },
{ id: 59, Label: "Organization", parentId: 51, branch: "subLeft" },
{ id: 60, Label: "Customer", parentId: 51, branch: "subLeft" },
{ id: 61, Label: "Staff", parentId: 51, branch: "subLeft" },
{ id: 62, Label: "Stakeholders", parentId: 51, branch: "subLeft" }
];
let items: DataManager = new DataManager(
mindMap as JSON[],
new Query().take(7)
);
let diagramInstance: Diagram;
export class MindMap extends SampleBase<{}, {}> {
rendereComplete() {
diagramInstance.fitToPage();
}
render() {
return (
<div className="control-pane">
<div className="control-section">
<div className="content-wrapper" style={{width: "100%"}}>
<DiagramComponent
ref={diagram => (diagramInstance = diagram)}
id="diagram"
style={{ width: "74%", height: "550px", float: "left" }}
width={"100%"}
height={"550px"}
snapSettings={{ constraints: SnapConstraints.None }}
tool={DiagramTools.SingleSelect}
layout={{
type: "MindMap",
getBranch: (node: NodeModel, nodes: NodeModel[]) => {
return ((node as Node).data as EmployeeInfo).branch;
},
horizontalSpacing: 50
}}
//Selectionchange event for Node and connector
selectionChange={(arg: ISelectionChangeEventArgs) => {
if (arg.state === "Changing") {
if (arg.newValue[0] instanceof Node) {
for (let handle of diagramInstance.selectedItems
.userHandles) {
handle.visible = true;
}
if (
((arg.newValue[0] as Node).data as EmployeeInfo)
.branch === "Left" ||
((arg.newValue[0] as Node).data as EmployeeInfo)
.branch === "subLeft"
) {
hideUserHandle("leftHandle");
changeUserHandlePosition("leftHandle");
} else if (
((arg.newValue[0] as Node).data as EmployeeInfo)
.branch === "Right" ||
((arg.newValue[0] as Node).data as EmployeeInfo)
.branch === "subRight"
) {
hideUserHandle("rightHandle");
changeUserHandlePosition("rightHandle");
} else if (
((arg.newValue[0] as Node).data as EmployeeInfo)
.branch === "Root"
) {
hideUserHandle("delete");
}
} else {
hideUserHandle("leftHandle");
hideUserHandle("rightHandle");
hideUserHandle("delete");
}
}
}}
selectedItems={{
constraints: SelectorConstraints.UserHandle,
userHandles: handle
}}
dataSourceSettings={{
id: "id",
parentId: "parentId",
dataManager: items,
root: String(1)
}}
//sets node default value
getNodeDefaults={(obj: Node) => {
obj.constraints =
NodeConstraints.Default & ~NodeConstraints.Drag;
if (
(obj.data as EmployeeInfo).branch === "Left" ||
(obj.data as EmployeeInfo).branch === "Right" ||
(obj.data as EmployeeInfo).branch === "Root"
) {
obj.shape = { type: "Basic", shape: "Ellipse" };
obj.borderColor =
"black"; /* tslint:disable:no-string-literal */
obj.style = {
fill:
(obj.data as EmployeeInfo).branch === "Root"
? "#E74C3C"
: "#F39C12",
strokeColor: "none",
strokeWidth: 2
};
obj.annotations = [
{
content: (obj.data as EmployeeInfo).Label,
margin: { left: 10, right: 10, top: 10, bottom: 10 },
style: { color: "white" }
}
];
let port: PointPortModel[] = getPort();
for (let i: number = 0; i < port.length; i++) {
obj.ports.push(new PointPort(obj, "ports", port[i], true));
}
} else {
let color: string; /* tslint:disable:no-string-literal */
if (
(obj.data as EmployeeInfo).branch === "Right" ||
(obj.data as EmployeeInfo).branch === "subRight"
) {
color = "#8E44AD";
} else {
color = "#3498DB";
}
obj.shape = { type: "Basic", shape: "Rectangle" };
obj.style = { fill: color, strokeWidth: 0 };
obj.minWidth = 100;
obj.height = 4;
let port: PointPortModel[] = getPort();
for (let i: number = 0; i < port.length; i++) {
obj.ports.push(new PointPort(obj, "ports", port[i], true));
}
obj.annotations = [
{
content: (obj.data as EmployeeInfo).Label,
offset: { x: 0.5, y: 0 },
verticalAlignment: "Bottom"
}
];
(obj.shape as TextModel).margin = {
left: 0,
right: 0,
top: 0,
bottom: 0
};
}
return obj;
}}
//sets connector default value
getConnectorDefaults={(
connector: ConnectorModel,
diagram: Diagram
) => {
connector.type = "Bezier";
connector.targetDecorator = { shape: "None" };
let sourceNode: Node = diagram.getObject(
connector.sourceID
) as Node;
let targetNode: Node = diagram.getObject(
connector.targetID
) as Node;
if (
(targetNode.data as EmployeeInfo).branch === "Right" ||
(targetNode.data as EmployeeInfo).branch === "subRight"
) {
connector.sourcePortID = sourceNode.ports[0].id;
connector.targetPortID = targetNode.ports[1].id;
connector.style = { strokeWidth: 5, strokeColor: "#8E44AD" };
} else if (
(targetNode.data as EmployeeInfo).branch === "Left" ||
(targetNode.data as EmployeeInfo).branch === "subLeft"
) {
connector.sourcePortID = sourceNode.ports[1].id;
connector.targetPortID = targetNode.ports[0].id;
connector.style = { strokeWidth: 5, strokeColor: "#3498DB" };
}
connector.constraints &= ~ConnectorConstraints.Select;
return connector;
}}
getCustomTool={getTool}
>
<Inject
services={[DataBinding, MindMapModule, HierarchicalTree]}
/>
</DiagramComponent>
<input
id="palette"
style={{ visibility: "hidden", position: "absolute" }}
type="color"
name="favcolor"
value="#000000"
/>
</div>
</div>
<div id="action-description">
<p>
This sample demonstrates the concept of creativity using mind map
layout algorithm. User handles are used to extend the mind map
interactively.
</p>
</div>
<div id="description">
<p>
This example shows how to generate a mind map from an external data
source. The <code>type</code> property of the <code>layout</code>{" "}
can be used to enable the mind map layout algorithm. The
<code>getBranch</code> property can also be used to define the
branches at both left and right sides.
</p>
<p style={{ fontWeight: 500 }}>Injecting Module</p>
<p>
The diagram component’s features are segregated into individual
feature-wise modules. To generate diagrams from an external data
source, inject <code>DataBinding</code> module into{" "}
<code>services</code>. To automatically generate a mind map, inject
<code>Mindmap</code> module into <code>services</code>.
</p>
<br />
</div>
</div>
);
}
}
//creation of the Ports
function getPort(): PointPortModel[] {
let port: PointPortModel[] = [
{
id: "port1",
offset: { x: 0, y: 0.5 },
visibility: PortVisibility.Hidden,
style: { fill: "black" }
},
{
id: "port2",
offset: { x: 1, y: 0.5 },
visibility: PortVisibility.Hidden,
style: { fill: "black" }
}
];
return port;
}
function addNode(): NodeModel {
let obj: NodeModel = {};
obj.id = randomId();
obj.data = {};
(obj.data as EmployeeInfo).Label = "Node";
return obj;
}
function addConnector(source: NodeModel, target: NodeModel): ConnectorModel {
let connector: ConnectorModel = {};
connector.id = randomId();
connector.sourceID = source.id;
connector.targetID = target.id;
return connector;
}
//Tool for Userhandles.
function getTool(action: string): ToolBase {
let tool: ToolBase;
if (action === "leftHandle") {
tool = new LeftExtendTool(diagramInstance.commandHandler);
} else if (action === "rightHandle") {
tool = new RightExtendTool(diagramInstance.commandHandler);
} else if (action === "delete") {
tool = new DeleteClick(diagramInstance.commandHandler);
}
return tool;
}
class LeftExtendTool extends ToolBase {
public mouseDown(args: MouseEventArgs): void {
super.mouseDown(args);
this.inAction = true;
}
public mouseUp(args: MouseEventArgs): void {
if (this.inAction) {
let selectedObject: any = this.commandHandler.getSelectedObject();
if (selectedObject[0]) {
if (selectedObject[0] instanceof Node) {
let node: NodeModel = addNode();
if ((selectedObject[0].data as EmployeeInfo).branch === "Root") {
(node.data as EmployeeInfo).branch = "Right";
} else if (
(selectedObject[0].data as EmployeeInfo).branch === "Right" ||
(selectedObject[0].data as EmployeeInfo).branch === "subRight"
) {
(node.data as EmployeeInfo).branch = "subRight";
}
let connector: ConnectorModel = addConnector(selectedObject[0], node);
diagramInstance.clearSelection();
let nd: Node = diagramInstance.add(node) as Node;
diagramInstance.add(connector);
diagramInstance.doLayout();
diagramInstance.bringIntoView(nd.wrapper.bounds);
diagramInstance.startTextEdit(nd);
}
}
}
}
}
class RightExtendTool extends ToolBase {
//mouseDown event
public mouseDown(args: MouseEventArgs): void {
super.mouseDown(args);
this.inAction = true;
}
//mouseDown event
public mouseUp(args: MouseEventArgs): void {
if (this.inAction) {
let selectedObject: any = this.commandHandler.getSelectedObject();
if (selectedObject[0]) {
if (selectedObject[0] instanceof Node) {
let node: NodeModel = addNode();
if ((selectedObject[0].data as EmployeeInfo).branch === "Root") {
(node.data as EmployeeInfo).branch = "Left";
} else if (
(selectedObject[0].data as EmployeeInfo).branch === "Left" ||
(selectedObject[0].data as EmployeeInfo).branch === "subLeft"
) {
(node.data as EmployeeInfo).branch = "subLeft";
}
let connector: ConnectorModel = addConnector(selectedObject[0], node);
diagramInstance.clearSelection();
let nd: Node = diagramInstance.add(node) as Node;
diagramInstance.add(connector);
diagramInstance.doLayout();
diagramInstance.bringIntoView(nd.wrapper.bounds);
diagramInstance.startTextEdit(nd);
}
}
}
}
}
class DeleteClick extends ToolBase {
//mouseDown event
public mouseDown(args: MouseEventArgs): void {
super.mouseDown(args);
this.inAction = true;
}
//mouseup event
public mouseUp(args: MouseEventArgs): void {
if (this.inAction) {
let selectedObject: any = this.commandHandler.getSelectedObject();
if (selectedObject[0]) {
if (selectedObject[0] instanceof Node) {
let node: Node = selectedObject[0] as Node;
this.removeSubChild(node);
}
diagramInstance.doLayout();
}
}
}
//Remove the subchild Elements
private removeSubChild(node: Node): void {
for (let i: number = node.outEdges.length - 1; i >= 0; i--) {
let connector: Connector = diagramInstance.getObject(
node.outEdges[i]
) as Connector;
let childNode: Node = diagramInstance.getObject(
connector.targetID
) as Node;
if (childNode.outEdges.length > 0) {
this.removeSubChild(childNode);
} else {
diagramInstance.remove(childNode);
}
}
diagramInstance.remove(node);
}
}
//hide the require userhandle.
function hideUserHandle(name: string): void {
for (let handle of diagramInstance.selectedItems.userHandles) {
if (handle.name === name) {
handle.visible = false;
}
}
}
let leftarrow: string =
"M11.924,6.202 L4.633,6.202 L4.633,9.266 L0,4.633 L4.632,0 L4.632,3.551 L11.923,3.551 L11.923,6.202Z";
let rightarrow: string =
"M0,3.063 L7.292,3.063 L7.292,0 L11.924,4.633 L7.292,9.266 L7.292,5.714 L0.001,5.714 L0.001,3.063Z";
let deleteicon: string =
"M 7.04 22.13 L 92.95 22.13 L 92.95 88.8 C 92.95 91.92 91.55 94.58 88.76" +
"96.74 C 85.97 98.91 82.55 100 78.52 100 L 21.48 100 C 17.45 100 14.03 98.91 11.24 96.74 C 8.45 94.58 7.04" +
"91.92 7.04 88.8 z M 32.22 0 L 67.78 0 L 75.17 5.47 L 100 5.47 L 100 16.67 L 0 16.67 L 0 5.47 L 24.83 5.47 z";
let leftuserhandle: UserHandleModel = setUserHandle(
//it is in dedicated line here.
"leftHandle",
leftarrow,
"Left",
1,
{ top: 0, bottom: 0, left: 0, right: 10 },
"Left",
"Top"
);
let rightuserhandle: UserHandleModel = setUserHandle(
//it is in dedicated line here.
"rightHandle",
rightarrow,
"Right",
1,
{ top: 0, bottom: 0, left: 10, right: 0 },
"Right",
"Top"
);
let deleteuserhandle: UserHandleModel = setUserHandle(
//it is in dedicated line here.
"delete",
deleteicon,
"Top",
0.5,
{ top: 0, bottom: 10, left: 0, right: 0 },
"Center",
"Center"
);
let handle: UserHandleModel[] = [
leftuserhandle,
rightuserhandle,
deleteuserhandle
];
//set and creation of the Userhandle.
function setUserHandle( //it is in dedicated line here.
name: string,
pathData: string,
side: Side,
offset: number,
margin: MarginModel,
halignment: HorizontalAlignment,
valignment: VerticalAlignment
): UserHandleModel {
let userhandle: UserHandleModel = {
name: name,
pathData: pathData,
backgroundColor: "black",
pathColor: "white",
side: side,
offset: offset,
margin: margin,
horizontalAlignment: halignment,
verticalAlignment: valignment
};
return userhandle;
}
//Change the Position of the UserHandle.
function changeUserHandlePosition(change: string): void {
for (let handle of diagramInstance.selectedItems.userHandles) {
if (handle.name === "delete" && change === "leftHandle") {
applyHandle(
handle,
"Left",
1,
{ top: 0, bottom: 0, left: 0, right: 10 },
"Left",
"Top"
);
} else if (handle.name === "delete" && change === "rightHandle") {
applyHandle(
handle,
"Right",
1,
{ top: 0, bottom: 0, left: 10, right: 0 },
"Right",
"Top"
);
}
}
}
//set the value for UserHandle element.
function applyHandle( //it is in dedicated line here.
handle: UserHandleModel,
side: Side,
offset: number,
margin: MarginModel,
halignment: HorizontalAlignment,
valignment: VerticalAlignment
): void {
handle.side = side;
handle.offset = offset;
handle.margin = margin;
handle.horizontalAlignment = halignment;
handle.verticalAlignment = valignment;
}
export interface EmployeeInfo {
branch: string;
color: string;
Left: string;
Right: string;
Root: string;
Label: string;
}