-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathOrgTreeTask.js
More file actions
128 lines (106 loc) · 3.7 KB
/
OrgTreeTask.js
File metadata and controls
128 lines (106 loc) · 3.7 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
import Tree from '../../algorithms/Tree';
import OrgItem from '../../models/OrgItem';
import OrgItemConfig from '../../configs/OrgItemConfig';
import { ItemType, ChildrenPlacementType } from '../../enums';
export default function OrgTreeTask(itemsOptionTask) {
var _data = {
orgTree: null, /*Tree OrgItem */
maximumId: null /* maximum of OrgItem.id */
};
function process() {
createOrgTree(itemsOptionTask.getItems());
return true;
}
function createOrgTree(items) {
var orgItem,
orgItemRoot,
userItem,
index, len,
maximumId = 0,
parsedId,
// Organizational chart definition
orgTree = Tree(),
rootItemConfig;
/* convert items to hash table */
for (index = 0, len = items.length; index < len; index += 1) {
userItem = items[index];
/* user should define unique id for every OrgItemConfig otherwise we ignore it
if parent does not exists in the tree then item is considered as root item
*/
if (userItem.id != null) {
/* Organizational chart OrgItemConfig is almost the same as OrgItem
except options used to draw connectors in multi parent chart
*/
orgItem = new OrgItem(userItem);
// OrgItem id coincides with OrgItemConfig id since we don't add any new org items to user's org chart definition
parsedId = parseInt(userItem.id, 10);
maximumId = Math.max(isNaN(parsedId) ? 0 : parsedId, maximumId);
// Collect org items
orgTree.add(userItem.parent, orgItem.id, orgItem);
/* We ignore looped items, it is applications responsibility to control data consistency */
}
}
/* create chart root item config */
maximumId += 1;
rootItemConfig = new OrgItemConfig();
rootItemConfig.id = maximumId;
rootItemConfig.title = "internal root";
rootItemConfig.isVisible = false;
rootItemConfig.isActive = false;
/* create chart org root item */
orgItemRoot = new OrgItem(rootItemConfig);
orgItemRoot.hideParentConnection = true;
orgItemRoot.hideChildrenConnection = true;
/* if user needs custom root item layout, it should explicitly create invisible node and set its children layout manually up */
orgItemRoot.childrenPlacementType = ChildrenPlacementType.Horizontal;
orgTree.add(null, orgItemRoot.id, orgItemRoot);
orgTree.loopLevels(this, function (nodeid, node, levelid) {
if (levelid > 0) {
return orgTree.BREAK;
}
if (orgItemRoot.id != nodeid) {
orgTree.adopt(orgItemRoot.id, nodeid);
/* root item must be regular */
node.itemType = ItemType.Regular;
}
});
hideRootConnectors(orgTree);
_data.orgTree = orgTree;
_data.maximumId = maximumId;
return true;
}
function hideRootConnectors(orgTree) {
orgTree.loopLevels(this, function (nodeid, node, levelid) {
var allRegular = true;
if (!node.isVisible) {
orgTree.loopChildren(this, nodeid, function (childid, child, index) {
if (child.itemType != ItemType.Regular) {
allRegular = false;
return true; // break
}
}); //ignore jslint
if (allRegular) {
node.hideChildrenConnection = true;
orgTree.loopChildren(this, nodeid, function (childid, child, index) {
child.hideParentConnection = true;
});
} else {
return orgTree.SKIP; // skip children
}
} else {
return orgTree.SKIP;
}
});
}
function getOrgTree() {
return _data.orgTree;
}
function getMaximumId() {
return _data.maximumId;
}
return {
process: process,
getOrgTree: getOrgTree,
getMaximumId: getMaximumId
};
};