-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmindmap.js
More file actions
420 lines (376 loc) · 13.1 KB
/
mindmap.js
File metadata and controls
420 lines (376 loc) · 13.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
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
const data = [
{
"title": "North America",
"salesLead": "Sarah Johnson",
"salesPlays": [
{
"title": "Enterprise Cloud Solutions",
"budget": 500000,
"currency": "USD",
"salesLead": "Michael Chen"
}
]
},
{
"title": "European Union",
"salesLead": "Marie Dubois",
"salesPlays": [
{
"title": "Manufacturing Innovation",
"budget": 400000,
"currency": "EUR",
"salesLead": "Andreas Schmidt"
},
{
"title": "Financial Services Modernization",
"budget": 450000,
"currency": "EUR",
"salesLead": "Isabella Romano"
}
]
},
{
"title": "Asia Pacific",
"salesLead": "James Wong",
"salesPlays": [
{
"title": "E-commerce Integration",
"budget": 800000,
"currency": "USD",
"salesLead": "Akiko Tanaka"
},
{
"title": "Smart Cities Initiative",
"budget": 600000,
"currency": "USD",
"salesLead": "Raj Patel"
},
{
"title": "Digital Banking Solutions",
"budget": 450000,
"currency": "USD",
"salesLead": "Li Wei"
}
]
},
{
"title": "Latin America",
"salesLead": "Carlos Ruiz",
"salesPlays": [
{
"title": "Telecom Infrastructure",
"budget": 300000,
"currency": "USD",
"salesLead": "Ana Silva"
},
{
"title": "Retail Digitalization",
"budget": 250000,
"currency": "USD",
"salesLead": "Diego Torres"
},
{
"title": "Fintech Solutions",
"budget": 400000,
"currency": "USD",
"salesLead": "Gabriela Santos"
},
{
"title": "Agriculture Tech",
"budget": 350000,
"currency": "USD",
"salesLead": "Roberto Mendoza"
}
]
},
{
"title": "Middle East",
"salesLead": "Ahmed Hassan",
"salesPlays": [
{
"title": "Smart Government",
"budget": 900000,
"currency": "USD",
"salesLead": "Fatima Al-Said"
},
{
"title": "Oil & Gas Digitalization",
"budget": 1200000,
"currency": "USD",
"salesLead": "Omar Sheikh"
},
{
"title": "Healthcare Innovation",
"budget": 600000,
"currency": "USD",
"salesLead": "Leila Mansour"
},
{
"title": "Education Technology",
"budget": 400000,
"currency": "USD",
"salesLead": "Yousef Rahman"
},
{
"title": "Sustainable Energy Solutions",
"budget": 800000,
"currency": "USD",
"salesLead": "Noor Ibrahim"
}
]
}
];
const fxConst = {
nodeX: 200,
spaceX: 100,
nodeY: 70,
spaceY: 25
};
function layoutMindmap(nodes, svgElement, title = "My Plan", relayout = false) {
layoutMindmap.balanceNodes = function(nodes) {
const sorted = [...nodes].sort((a, b) => b.salesPlays.length - a.salesPlays.length);
const right = [sorted[0]];
const left = sorted.length > 1 ? [sorted[1]] : [];
let rightSum = sorted[0].salesPlays.length;
let leftSum = sorted.length > 1 ? sorted[1].salesPlays.length : 0;
for (let i = 2; i < sorted.length; i++) {
if (rightSum <= leftSum) {
right.push(sorted[i]);
rightSum += sorted[i].salesPlays.length;
} else {
left.push(sorted[i]);
leftSum += sorted[i].salesPlays.length;
}
}
return { left, right, leftSum, rightSum };
}
layoutMindmap.getChildNodeSlots = function(amount, fxParent, left = false) {
const sign = left ? -1 : 1;
const slots = [];
if (amount < 1) {
return slots;
}
// Positions are from center of the node to avoid differences in left and right logic
// This may lead to rounding
const fxObj = {
w: fxConst.nodeX,
h: fxConst.nodeY
};
fxObj.x = fxParent.x + (sign * Math.abs(fxParent.w / 2 + fxObj.w / 2 + fxConst.spaceX));
if (amount === 1) {
slots.push({
y: fxParent.y,
...fxObj
})
} else {
// Negative Y is up to conform with SVG Canvas Y-axis
const totalHeight = amount * fxObj.h;
const startY = fxParent.y - (totalHeight / 2);
for (let i = 0; i < amount; i++) {
let o = {
y: startY + (i * fxConst.spaceY) + (i * fxObj.h),
...fxObj
};
slots.push(layoutMindmap.slot(o.x, o.y, o.w, o.h));
}
}
return slots;
}
layoutMindmap.updateDimensions = function(slot) {
// Update dimensions
const nodeXMin = slot.x - (slot.w / 2);
const nodeXMax = slot.x + (slot.w / 2);
const nodeYMin = slot.y - (slot.h / 2);
const nodeYMax = slot.y + (slot.h / 2);
if (dimensions.xMin > nodeXMin) dimensions.xMin = nodeXMin;
if (dimensions.xMax < nodeXMax) dimensions.xMax = nodeXMax;
if (dimensions.yMin > nodeYMin) dimensions.yMin = nodeYMin;
if (dimensions.yMax < nodeYMax) dimensions.yMax = nodeYMax;
}
layoutMindmap.slot = function(x, y, w = fxConst.nodeX, h = fxConst.nodeY, title = null) {
const obj = {
x: x,
y: y,
w: w,
h: h,
children: []
};
if (title) obj.title = title;
return obj;
}
layoutMindmap.distributeSide = function(_nodes, fxParent, left = false) {
const sign = left ? -1 : 1;
// If the nodes have children, then request that amount of slots to reserve space for them
// In this case, layout the node itself at the y-center of those slots
// For nodes with no children, request 1 slot
// +1 slot for spacing between branches
// Calculate total subnodes (min 1 for each node)
let totalSubnodes = 0;
for (let x = 0; x < _nodes.length; x++) {
if (_nodes[x].salesPlays) {
totalSubnodes += Math.max(1, _nodes[x].salesPlays.length);
}
}
// Slots are evenly (y axis) distributed coordinate sets relative to the parent node
const slots = layoutMindmap.getChildNodeSlots(totalSubnodes, fxParent, left);
let slotIndex = 0;
// Consume slots per node
for (let i = 0; i < _nodes.length; i++) {
// 'Consume' the node's slots
// Arrange the node in the y-center of the slots
// Offset the slots on x
// Distribute children into slots
let node = _nodes[i];
let size = Math.max(1, node.salesPlays ? node.salesPlays.length : 1);
let nodeSlots = slots.slice(slotIndex, slotIndex + size);
slotIndex += size;
// Find lowest and highest y-values
let minY = Math.min(...nodeSlots.map(slot => slot.y));
let maxY = Math.max(...nodeSlots.map(slot => slot.y));
const nodeSlot = { ...node, ...layoutMindmap.slot(nodeSlots[0].x, (minY + maxY) / 2) };
if (node.salesPlays) {
// Offset slots on x and fill
const newX = nodeSlot.x + (Math.abs(fxConst.spaceX + fxConst.nodeX) * sign);
for (let z = 0; z < node.salesPlays.length; z++) {
const cNode = node.salesPlays[z];
const cSlot = layoutMindmap.slot(newX, nodeSlots[z].y, fxConst.nodeX, fxConst.nodeY);
layoutMindmap.updateDimensions(cSlot);
nodeSlot.children.push({ ...cNode, ...cSlot });
}
}
layoutMindmap.updateDimensions(nodeSlot);
fxParent.children.push(nodeSlot);
}
}
const { left, right, leftSum, rightSum } = layoutMindmap.balanceNodes(data);
const dimensions = {
xMin: 0,
xMax: 0,
yMin: 0,
yMax: 0
};
const root = layoutMindmap.slot(0, 0, fxConst.nodeX, fxConst.nodeY, title);
layoutMindmap.distributeSide(left, root, true);
layoutMindmap.distributeSide(right, root, false);
// Re-center root node
const yCenter = (dimensions.yMax - Math.abs(dimensions.yMin)) / 2;
root.y = yCenter;
return { fxNodes: root, dimensions };
}
function drawMindmap(fx, dimensions, svgElement) {
drawMindmap.config = {
showTooltip: true,
showControlBar: true,
arrowColor: '#ff6a3d'
};
function getRect(x, y, w, h, text = '', fill = 'black') {
return `
<foreignObject x="${x}" y="${y}" width="${w}" height="${h}" style="overflow: visible;">
<div xmlns="http://www.w3.org/1999/xhtml" style="width: 100%; height: ${h}px; position: relative;">
<div class="foreign-div">
<div class="content">
<div class="content-text" ${drawMindmap.config.showTooltip ? `onmouseover="showTooltip('${text}')" onmouseout="hideTooltip()"` : ''}>
${text}
</div>
</div>
${drawMindmap.config.showControlBar ? `
<div class="control-bar">
<button>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18"/><path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6"/><path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"/></svg>
</button>
<div class="spacer"></div>
<button>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12h14"/></svg>
</button>
<input type="text" value="$1000" />
<button>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12h14"/><path d="M12 5v14"/></svg>
</button>
</div>` : ''}
</div>
</div>
</foreignObject>`;
}
function createArrow(fxNodeFrom, fxNodeTo, color = 'black', curveExclFactor = 0.3) {
const fromX = fxNodeFrom.x;
const fromY = fxNodeFrom.y;
const toX = fxNodeTo.x;
const toY = fxNodeTo.y;
const totalDistance = Math.abs(toX - fromX);
let straightLength = totalDistance * curveExclFactor;
// For arrows going left
if (fromX > toX) {
straightLength = -straightLength;
}
// For S-curve, use two control points
const startX = fromX;
const startY = fromY;
const straightStartX = fromX + straightLength;
const straightStartY = fromY;
const straightEndX = toX - straightLength;
const straightEndY = toY;
const endX = toX;
const endY = toY;
// Calculate path
const path = fromY === toY
? `M ${startX} ${startY} L ${endX} ${endY}` // Straight line if Y's match
: `M ${startX} ${startY}
L ${straightStartX} ${straightStartY}
C ${straightStartX + straightLength} ${straightStartY},
${straightEndX - straightLength} ${straightEndY},
${straightEndX} ${straightEndY}
L ${endX} ${endY}`;
// Create arrowhead
const arrowSize = 8;
const arrowhead = toX < fromX
? `M ${toX + arrowSize} ${toY - arrowSize}
L ${toX} ${toY}
L ${toX + arrowSize} ${toY + arrowSize}`
: `M ${toX - arrowSize} ${toY - arrowSize}
L ${toX} ${toY}
L ${toX - arrowSize} ${toY + arrowSize}`;
return `
<path d="${path}" fill="none" stroke="${color}" stroke-width="2"/>
<path d="${arrowhead}" fill="none" stroke="${color}" stroke-width="2"/>
<circle cx="${fromX}" cy="${fromY}" r="4" fill="white" stroke="${color}" stroke-width="2"/>
`;
}
function topLeftFor(fxNode) {
return { ...fxNode, x: fxNode.x - (fxNode.w / 2), y: fxNode.y - (fxNode.h / 2) };
}
function midRightFor(fxNode) {
return { ...fxNode, x: fxNode.x + (fxNode.w / 2) };
}
function midLeftFor(fxNode) {
return { ...fxNode, x: fxNode.x - (fxNode.w / 2) };
}
function getNodes(fxNode) {
let inner = '';
correctedPos = topLeftFor(fxNode);
inner += getRect(correctedPos.x, correctedPos.y, fxNode.w, fxNode.h, fxNode.title);
fxNode.children.forEach(child => {
inner += getNodes(child);
let arrowStart = null;
let arrowEnd = null;
if (fxNode.x > child.x) {
arrowStart = midLeftFor(fxNode);
arrowEnd = midRightFor(child);
} else {
arrowStart = midRightFor(fxNode);
arrowEnd = midLeftFor(child);
}
inner += createArrow(arrowStart, arrowEnd, drawMindmap.config.arrowColor);
});
return inner;
}
const canvas = {
x: dimensions.xMin,
y: dimensions.yMin,
w: dimensions.xMax - dimensions.xMin,
h: dimensions.yMax - dimensions.yMin
};
const inner = getNodes(fx);
svgElement.innerHTML = inner;
svgElement.setAttribute('viewBox', `${canvas.x} ${canvas.y} ${canvas.w} ${canvas.h}`);
}
layoutMindmap();