-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathrender.js
More file actions
221 lines (192 loc) · 5.63 KB
/
render.js
File metadata and controls
221 lines (192 loc) · 5.63 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
const { wrapText, helpers } = require('../utils')
const renderLines = require('./render-lines')
const onClick = require('./on-click')
const iconLink = require('./components/icon-link')
const CHART_NODE_CLASS = 'org-chart-node'
const PERSON_LINK_CLASS = 'org-chart-person-link'
const PERSON_NAME_CLASS = 'org-chart-person-name'
const PERSON_TITLE_CLASS = 'org-chart-person-title'
const PERSON_DEPARTMENT_CLASS = 'org-chart-person-dept'
const PERSON_REPORTS_CLASS = 'org-chart-person-reports'
function render(config) {
const {
svgroot,
svg,
tree,
animationDuration,
nodeWidth,
nodeHeight,
nodePaddingX,
nodePaddingY,
nodeBorderRadius,
backgroundColor,
nameColor,
titleColor,
reportsColor,
borderColor,
avatarWidth,
lineDepthY,
treeData,
sourceNode,
onPersonLinkClick
} = config
// Compute the new tree layout.
const nodes = tree.nodes(treeData).reverse()
const links = tree.links(nodes)
config.links = links
config.nodes = nodes
// Normalize for fixed-depth.
nodes.forEach(function(d) {
d.y = d.depth * lineDepthY
})
// Update the nodes
const node = svg
.selectAll('g.' + CHART_NODE_CLASS)
.data(nodes.filter(d => d.id), d => d.id)
const parentNode = sourceNode || treeData
// Enter any new nodes at the parent's previous position.
const nodeEnter = node
.enter()
.insert('g')
.attr('class', CHART_NODE_CLASS)
.attr('transform', `translate(${parentNode.x0}, ${parentNode.y0})`)
.on('click', onClick(config))
// Person Card Shadow
nodeEnter
.append('rect')
.attr('width', nodeWidth)
.attr('height', nodeHeight)
.attr('fill', backgroundColor)
.attr('stroke', borderColor)
.attr('rx', nodeBorderRadius)
.attr('ry', nodeBorderRadius)
.attr('fill-opacity', 0.05)
.attr('stroke-opacity', 0.025)
.attr('filter', 'url(#boxShadow)')
// Person Card Container
nodeEnter
.append('rect')
.attr('width', nodeWidth)
.attr('height', nodeHeight)
.attr('id', d => d.id)
.attr('fill', backgroundColor)
.attr('stroke', borderColor)
.attr('rx', nodeBorderRadius)
.attr('ry', nodeBorderRadius)
.style('cursor', helpers.getCursorForNode)
.attr('class', 'box')
const namePos = {
x: nodePaddingX * 1.4 + avatarWidth,
y: nodePaddingY * 1.8
}
// Person's Name
nodeEnter
.append('text')
.attr('class', PERSON_NAME_CLASS)
.attr('x', namePos.x)
.attr('y', namePos.y)
.attr('dy', '.3em')
.style('cursor', 'pointer')
.style('fill', nameColor)
.style('font-size', 16)
.text(d => d.person.name)
// Person's Title
nodeEnter
.append('text')
.attr('class', PERSON_TITLE_CLASS + ' unedited')
.attr('x', namePos.x)
.attr('y', namePos.y + nodePaddingY * 1.2)
.attr('dy', '0.1em')
.style('font-size', 14)
.style('cursor', 'pointer')
.style('fill', titleColor)
.text(d => d.person.title)
const heightForTitle = 45 // getHeightForText(d.person.title)
// Person's Reports
// nodeEnter
// .append('text')
// .attr('class', PERSON_REPORTS_CLASS)
// .attr('x', namePos.x)
// .attr('y', namePos.y + nodePaddingY + heightForTitle)
// .attr('dy', '.9em')
// .style('font-size', 14)
// .style('font-weight', 500)
// .style('cursor', 'pointer')
// .style('fill', reportsColor)
// .text(helpers.getTextForTitle)
// Person's Avatar
nodeEnter
.append('image')
.attr('width', 80)
.attr('height', 80)
.attr('x', 0)
.attr('y', 1)
.attr('stroke', borderColor)
.attr('src', d => d.person.avatar)
.attr('xlink:href', d => d.person.avatar)
.attr('clip-path', 'url(#avatarClip)')
// Person's Department
// nodeEnter
// .append('text')
// .attr('class', getDepartmentClass)
// .attr('x', 34)
// .attr('y', avatarWidth + nodePaddingY * 1.2)
// .attr('dy', '.9em')
// .style('cursor', 'pointer')
// .style('fill', titleColor)
// .style('font-weight', 600)
// .style('font-size', 8)
// .attr('text-anchor', 'middle')
// .text(helpers.getTextForDepartment)
// Person's Link
const nodeLink = nodeEnter
.append('p')
.attr('class', PERSON_LINK_CLASS)
// .attr('xlink:href', d => d.person.link || 'https://lattice.com')
// .on('click', datum => {
// d3.event.stopPropagation()
// // TODO: fire link click handler
// if (onPersonLinkClick) {
// onPersonLinkClick(datum, d3.event)
// }
// })
iconLink({
svg: nodeLink,
x: nodeWidth - 28,
y: nodeHeight - 28
})
// Transition nodes to their new position.
const nodeUpdate = node
.transition()
.duration(animationDuration)
.attr('transform', d => `translate(${d.x},${d.y})`)
nodeUpdate
.select('rect.box')
.attr('fill', backgroundColor)
.attr('stroke', borderColor)
// Transition exiting nodes to the parent's new position.
const nodeExit = node
.exit()
.transition()
.duration(animationDuration)
.attr('transform', d => `translate(${parentNode.x},${parentNode.y})`)
.remove()
// Update the links
const link = svg.selectAll('path.link').data(links, d => d.target.id)
// Wrap the title texts
const wrapWidth = 140
svg.selectAll('text.unedited.' + PERSON_TITLE_CLASS).call(wrapText, wrapWidth)
// Render lines connecting nodes
renderLines(config)
// Stash the old positions for transition.
nodes.forEach(function(d) {
d.x0 = d.x
d.y0 = d.y
})
}
function getDepartmentClass(d) {
const { person } = d
const deptClass = person.department ? person.department.toLowerCase() : ''
return [PERSON_DEPARTMENT_CLASS, deptClass].join(' ')
}
module.exports = render