-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathOrgChart.java
More file actions
333 lines (279 loc) · 10.1 KB
/
OrgChart.java
File metadata and controls
333 lines (279 loc) · 10.1 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
/*-
* #%L
* OrgChart Add-on
* %%
* Copyright (C) 2017 - 2025 Flowing Code S.A.
* %%
* 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.
* #L%
*/
package com.flowingcode.vaadin.addons.orgchart;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.flowingcode.vaadin.addons.orgchart.client.OrgChartState;
import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.ClientCallable;
import com.vaadin.flow.component.ComponentEvent;
import com.vaadin.flow.component.ComponentEventListener;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.dependency.NpmPackage;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.shared.Registration;
import java.util.List;
import java.util.stream.Collectors;
/**
* OrgChart component definition. <br>
* Uses JQuery library <b>OrgChart</b> to show an organization chart. <br>
* More information about this library at <a href=
* "https://github.com/dabeng/OrgChart">https://github.com/dabeng/OrgChart</a>
*
* @author pbartolo
*/
@SuppressWarnings("serial")
@NpmPackage(value = "orgchart", version = "3.7.0")
@NpmPackage(value = "html2canvas", version = "1.4.1")
@NpmPackage(value = "jquery", version = "3.6.2")
@JsModule("jquery/dist/jquery.js")
@JsModule("orgchart/dist/js/jquery.orgchart.js")
@CssImport("orgchart/dist/css/jquery.orgchart.min.css")
@Tag("fc-orgchart")
@JsModule("./fc-orgchart.js")
@CssImport("./fc-orgchart-styles.css")
@NpmPackage(value = "json-digger", version = "2.0.2")
public class OrgChart extends Div {
private OrgChartItem orgChartItem;
private OrgChartState state = new OrgChartState();
public OrgChart(OrgChartItem orgChartItem) {
super();
this.orgChartItem = orgChartItem;
this.setValue(orgChartItem);
String identifier = "orgchart" + this.hashCode();
this.setId(identifier);
}
@Override
protected void onAttach(AttachEvent attachEvent) {
super.onAttach(attachEvent);
this.getElement()
.executeJs(
"this.initializeOrgChart($0,$1,$2)",
convertToJsonObj(state),
state.value,
this.getId().get());
}
/**
* @deprecated This method is no longer needed. Initialization is done in {@link
* #onAttach(AttachEvent)}.
*/
@Deprecated
public void initializeChart() {}
public void setValue(OrgChartItem orgChartItem) {
String value = convertToJsonObj(orgChartItem);
getState().value = value;
}
protected OrgChartState getState() {
return this.state;
}
private String convertToJsonObj(Object orgChartItem) {
String result = null;
ObjectMapper mapper = new ObjectMapper();
try {
result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(orgChartItem);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return result;
}
public void setChartNodeTitle(String chartNodeTitle) {
getState().chartNodeTitle = chartNodeTitle;
}
public void setChartNodeContent(String chartNodeContent) {
getState().chartNodeContent = chartNodeContent;
}
public void setChartDirection(String chartDirection) {
getState().chartDirection = chartDirection;
}
public void setChartTitle(String chartTitle) {
getState().chartTitle = chartTitle;
}
public void setChartZoom(Boolean chartZoom) {
getState().chartZoom = chartZoom;
}
public void setChartExportButton(Boolean chartExportButton) {
getState().chartExportButton = chartExportButton;
}
public void setChartExportFileName(String chartExportFileName) {
getState().chartExportFileName = chartExportFileName;
}
public void setChartExportFileExtension(String chartExportFileExtension) {
getState().chartExportFileExtension = chartExportFileExtension;
}
public void setChartPan(Boolean chartPan) {
getState().chartPan = chartPan;
}
public void setChartZoominLimit(Double chartZoominLimit) {
getState().chartZoominLimit = chartZoominLimit;
}
public void setChartZoomoutLimit(Double chartZoomoutLimit) {
getState().chartZoomoutLimit = chartZoomoutLimit;
}
/** orgchart visibleLevel option */
public void setChartDepth(Integer chartDepth) {
getState().chartDepth = chartDepth;
}
/** orgchart verticalLevel option */
public void setChartVerticalDepth(Integer chartVerticalDepth) {
getState().chartVerticalDepth = chartVerticalDepth;
}
public void setChartToggleSiblingsResp(Boolean chartToggleSiblingsResp) {
getState().chartToggleSiblingsResp = chartToggleSiblingsResp;
}
public void setChartExpandCollapse(Boolean chartExpandCollapse) {
getState().chartExpandCollapse = chartExpandCollapse;
}
public void setChartDraggable(Boolean chartDraggable) {
getState().chartDraggable = chartDraggable;
}
/** Returns latest value of the orgchart element. */
public OrgChartItem getOrgChartItem() {
return orgChartItem;
}
/**
* Fires event on node click.
*
* @param nodeId
*/
@ClientCallable
public void onClick(String nodeId) {
Integer clickedNodeId = Integer.valueOf(nodeId);
OrgChartItem clickedItem = getById(clickedNodeId, orgChartItem);
fireNodeClickEvent(clickedItem, true);
}
/**
* Updates chart after drag and drop event.
*
* @param draggedNode
* @param dragZone
* @param dropZone
*/
@ClientCallable
public void updateDraggedNode(String draggedNode, String dragZone, String dropZone) {
Integer draggedNodeId = Integer.valueOf(draggedNode);
OrgChartItem draggedItem = getById(draggedNodeId, orgChartItem);
OrgChartItem oldParentItem = getById(Integer.valueOf(dragZone), orgChartItem);
OrgChartItem newParentItem = getById(Integer.valueOf(dropZone), orgChartItem);
// update old parent (remove item)
List<OrgChartItem> oldParentUpdated =
oldParentItem.getChildren().stream()
.filter(i -> !draggedNodeId.equals(i.getId()))
.collect(Collectors.toList());
oldParentItem.setChildren(oldParentUpdated);
// update new parent (add item)
newParentItem.addChildren(draggedItem);
fireDragAndDropEvent(draggedItem, true);
}
private OrgChartItem getById(Integer id, OrgChartItem item) {
if (id.equals(item.getId())) {
return item;
} else {
return getChildItemById(id, item.getChildren());
}
}
private OrgChartItem getChildItemById(Integer id, List<OrgChartItem> children) {
OrgChartItem result = null;
for (int i = 0; i < children.size() && result == null; i++) {
result = getById(id, children.get(i));
}
return result;
}
/**
* Sets the template generation function used to customize the internal structure of nodes. {@code
* functionBody} is the body of a javascript function that recieves one parameter (the JSON
* datasoure representing a node) and returns an HTML snippet. The name of this parameter is given
* by {@code parameterName}.
*
* <p>Example: <code>
* setNodeTemplate("item","return '<span>'+item.name+'</span>';")
* </code> configures the following JS function as node template: <code>
* function(item) { return '<span>'+item.name+'</span>'; }
* </code> {@linkplain OrgChartItem#setData(String, String) custom properties} are accessible
* through {@code item.data} <br>
*
* @param parameterName the name of the parameter of a javascript function
* @param functionBody the body of a javascript function
*/
public void setNodeTemplate(String parameterName, String functionBody) {
getState().nodeTemplateParam = parameterName;
getState().nodeTemplate = functionBody;
}
/**
* Adds a {@link DragAndDropEvent} listener to the component.
*
* @param listener the listener to be added.
*/
public void addDragAndDropListener(ComponentEventListener<DragAndDropEvent> listener) {
addListener(DragAndDropEvent.class, listener);
}
/**
* Fires a {@link DragAndDropEvent}.
*
* @param draggedItem the item being dragged.
*/
protected void fireDragAndDropEvent(OrgChartItem draggedItem, boolean fromClient) {
fireEvent(new DragAndDropEvent(this, draggedItem, fromClient));
}
/** Event thrown when a node is dragged and dropped. */
public static class DragAndDropEvent extends ComponentEvent<OrgChart> {
private final OrgChartItem draggedItem;
public DragAndDropEvent(OrgChart source, OrgChartItem draggedItem, boolean fromClient) {
super(source, fromClient);
this.draggedItem = draggedItem;
}
public OrgChartItem getDraggedItem() {
return draggedItem;
}
public OrgChart getOrgChart() {
return (OrgChart) source;
}
}
public Registration addOnNodeClickListener(ComponentEventListener<NodeClickEvent> listener) {
return addListener(NodeClickEvent.class, listener);
}
/**
* Fires a {@link NodeClickEvent}.
*
* @param clickedItem the item being clicked.
*/
protected void fireNodeClickEvent(OrgChartItem clickedItem, boolean fromClient) {
fireEvent(new NodeClickEvent(this, clickedItem, fromClient));
}
/** Event thrown when a node is clicked. */
public static class NodeClickEvent extends ComponentEvent<OrgChart> {
private final OrgChartItem clickedItem;
public NodeClickEvent(OrgChart source, OrgChartItem clickedItem, boolean fromClient) {
super(source, fromClient);
this.clickedItem = clickedItem;
}
public OrgChartItem getClickedItem() {
return clickedItem;
}
public OrgChart getOrgChart() {
return (OrgChart) source;
}
}
/** Collapses all nodes except the root */
public void setCollapsedNodes() {
setChartDepth(1);
}
}