-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
114 lines (94 loc) · 2.85 KB
/
index.js
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
module.exports = createLegend;
function createLegend(allSettings, folderName, legend) {
var renderer = allSettings.renderer();
var gui = allSettings.gui();
var group = gui.addFolder(folderName);
var model = Object.create(null);
var hiddenLinks = Object.create(null);
for (var i = 0; i < legend.length; ++i) {
var item = legend[i];
model[item.name] = item.color;
hiddenLinks[item.name] = [];
group.addColor(model, item.name)
.onChange(colorLinks)
.name(toggle(item.name));
}
group.open();
listToToggleEvents();
colorLinks();
function listToToggleEvents() {
var checkboxes = group.domElement.querySelectorAll('input.toggle');
for (var i = 0; i < checkboxes.length; ++i) {
checkboxes[i].addEventListener('change', handleChange, false);
}
function handleChange(e) {
e.preventDefault();
e.stopPropagation();
if (this.checked) {
showGroup(this.id);
} else {
hideGroup(this.id);
}
}
}
function colorLinks() {
var graph = renderer.graph();
graph.forEachLink(colorLink);
renderer.focus();
function colorLink(link) {
for (var i = 0; i < legend.length; ++i) {
var item = legend[i];
if (!item.filter(link)) continue;
var ui = renderer.getLink(link.id);
if (ui) {
ui.fromColor = model[item.name];
ui.toColor = model[item.name];
}
return;
}
}
}
function showGroup(groupName) {
var links = hiddenLinks[groupName];
if (!links) return;
var graph = renderer.graph();
graph.beginUpdate();
for (var i = 0; i < links.length; ++i) {
var link = links[i];
graph.addLink(link.fromId, link.toId, link.data);
}
graph.endUpdate();
links.splice(0, links.length);
colorLinks();
}
function hideGroup(groupName) {
var links = hiddenLinks[groupName];
if (!links) return;
var legendItem = getLegendItemByName(groupName);
if (!legendItem) return;
var graph = renderer.graph();
graph.forEachLink(noteLinksToRemove);
graph.beginUpdate();
for (var i = 0; i < links.length; ++i) {
graph.removeLink(links[i]);
}
graph.endUpdate();
colorLinks();
function noteLinksToRemove(link) {
if (legendItem.filter(link)) links.push(link);
}
}
function getLegendItemByName(name) {
for (var i = 0; i < legend.length; ++i) {
if (legend[i].name === name) return legend[i];
}
}
function toggle(name) {
return [
'<span style="-webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none;">',
'<input type="checkbox" name="checkbox" id="' + name + '" class="toggle" value="value" checked>',
'<label for="' + name + '">' + name + '</label>',
'</span>'
].join('\n');
}
}