-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAddingNewItemsToChartAtRuntime.jsx
159 lines (140 loc) · 3.9 KB
/
AddingNewItemsToChartAtRuntime.jsx
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
import React, { Component } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faUserPlus, faUserSlash } from '@fortawesome/free-solid-svg-icons'
import { OrgDiagram } from '../Diagrams';
import { LCA, Tree, PageFitMode, Enabled } from 'basicprimitives';
class Sample extends Component {
constructor(props) {
super(props);
this.onAddButtonClick = this.onAddButtonClick.bind(this);
this.onRemoveButtonClick = this.onRemoveButtonClick.bind(this);
this.state = {
cursorItem: 0,
highlightItem: 0,
items: [
{
id: 0,
parent: null,
title: "James Smith",
description: "VP, Public Sector",
image: "./photos/a.png"
},
{
id: 1,
parent: 0,
title: "Ted Lucas",
description: "VP, Human Resources",
image: "./photos/b.png"
},
{
id: 2,
parent: 0,
title: "Fritz Stuger",
description: "Business Solutions, US",
image: "./photos/c.png"
},
{
id: 3,
parent: 0,
title: "Lynne Rathinam",
description: "GM, Enterprise Services",
image: "./photos/c.png"
}
]
}
this.index = this.state.items.length;
}
onAddButtonClick(itemConfig) {
const { items } = this.state;
var newItem = {
id: this.index++,
parent: itemConfig.id,
title: "New Title",
description: "New Description",
image: "./photos/z.png"
};
this.setState({
items: [...items, newItem],
cursorItem: newItem.id
});
}
onRemoveButtonClick(itemConfig) {
const { items } = this.state;
this.setState(this.getDeletedItems(items, [itemConfig.id]));
}
getDeletedItems(items = [], deletedItems = []) {
const tree = this.getTree(items);
const hash = deletedItems.reduce((agg, itemid) => {
agg.add(itemid.toString());
return agg;
}, new Set());
const cursorParent = this.getDeletedItemsParent(tree, deletedItems, hash);
const result = [];
tree.loopLevels(this, (nodeid, node) => {
if (hash.has(nodeid.toString())) {
return tree.SKIP;
}
result.push(node);
});
return {
items: result,
cursorItem: cursorParent
};
}
getDeletedItemsParent(tree, deletedItems, deletedHash) {
let result = null;
const lca = LCA(tree);
result = deletedItems.reduce((agg, itemid) => {
if (agg == null) {
agg = itemid;
} else {
agg = lca.getLowestCommonAncestor(agg, itemid);
}
return agg;
}, null);
if (deletedHash.has(result.toString())) {
result = tree.parentid(result);
}
return result;
}
getTree(items = []) {
const tree = Tree();
// rebuild tree
for (let index = 0; index < items.length; index += 1) {
const item = items[index];
tree.add(item.parent, item.id, item);
}
return tree;
}
render() {
const config = {
...this.state,
pageFitMode: PageFitMode.None,
hasSelectorCheckbox: Enabled.True,
hasButtons: Enabled.True,
buttonsPanelSize: 40,
onButtonsRender: (({ context: itemConfig }) => {
return <>
<button key="1" className="StyledButton"
onClick={(event) => {
event.stopPropagation();
this.onAddButtonClick(itemConfig);
}}>
<FontAwesomeIcon icon={faUserPlus} />
</button>
<button key="2" className="StyledButton"
onClick={(event) => {
event.stopPropagation();
this.onRemoveButtonClick(itemConfig);
}}>
<FontAwesomeIcon icon={faUserSlash} />
</button>
</>
})
};
return <div className="placeholder">
<OrgDiagram centerOnCursor={true} config={config} />
</div >
}
}
export default Sample;