-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrenderUI.js
259 lines (232 loc) · 8.86 KB
/
renderUI.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
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
import { compareVersions } from 'compare-versions';
import tippy, { hideAll } from 'tippy.js';
import 'tippy.js/dist/tippy.css';
import 'tippy.js/themes/light-border.css';
import { setZoomTransform } from './renderD3';
import { SvgSaver } from '../utils/svgsaver';
export const setupUI = (zoom, inner, svg) => {
const zoomInBtn = document.querySelector('#zoom_in');
const zoomOutBtn = document.querySelector('#zoom_out');
const downloadSVGBtn = document.querySelector('#download-svg');
const downloadPNGBtn = document.querySelector('#download-png');
const svgElement = document.querySelector('#bods-svg');
const svgsaver = new SvgSaver();
zoomInBtn.addEventListener('click', () => {
zoom.scaleBy(svg.transition().duration(750), 1.2);
});
zoomInBtn.addEventListener('keyup', (e) => {
if (e.key === 'Enter' || e.key === 'Space') {
zoom.scaleBy(svg.transition().duration(750), 1.2);
}
});
zoomOutBtn.addEventListener('click', () => {
zoom.scaleBy(svg.transition().duration(750), 0.8);
});
zoomOutBtn.addEventListener('keyup', (e) => {
if (e.key === 'Enter' || e.key === 'Space') {
zoom.scaleBy(svg.transition().duration(750), 0.8);
}
});
downloadSVGBtn.addEventListener('click', (e) => {
e.stopPropagation();
setZoomTransform(inner, svg);
svgsaver.asSvg(svgElement, 'bods.svg');
});
downloadSVGBtn.addEventListener('keyup', (e) => {
e.stopPropagation();
if (e.key === 'Enter' || e.key === 'Space') {
setZoomTransform(inner, svg);
svgsaver.asSvg(svgElement, 'bods.svg');
}
});
downloadPNGBtn.addEventListener('click', (e) => {
e.stopPropagation();
setZoomTransform(inner, svg);
svgsaver.asPng(svgElement, 'bods.png');
});
downloadPNGBtn.addEventListener('keyup', (e) => {
e.stopPropagation();
if (e.key === 'Enter' || e.key === 'Space') {
setZoomTransform(inner, svg);
svgsaver.asPng(svgElement, 'bods.png');
}
});
};
const getDescription = (description) => {
// Extract identifiers from description and output text on newlines
let identifiers = [];
let identifiersOutput = '';
if (description.identifiers) {
identifiers = description.identifiers.map((identifier, index) => ({
[`Identifier ${index + 1}`]: `${identifier.scheme ? '(' + identifier.scheme + ') ' : ''}${
identifier.id
}`,
}));
identifiers.forEach((item) => {
const key = Object.keys(item)[0];
const value = item[key];
identifiersOutput += `${key}: ${value}\n`;
});
}
// Extract interests from description and output text on newlines
let interests = [];
let interestsOutput = '';
if (description.interests) {
interests = description.interests.map((interest, index) => {
if (interest.startDate) {
return {
[`Interest ${index + 1} Type`]: `${interest.type}\n`,
[`Interest ${index + 1} Start Date`]: `${interest.startDate}\n`,
};
} else {
return {
[`Interest ${index + 1} Type`]: `${interest.type}\n`,
};
}
});
interests.forEach((item) => {
for (const key in item) {
if (item.hasOwnProperty(key)) {
interestsOutput += `${key}: ${item[key]}`;
}
}
});
}
// Output the descriptions subset as key value pairs on new lines
return `Statement date: ${description.statementDate}\n${
description.recordId !== null ? 'Record ID: ' + description.recordId + '\n' : ''
}${identifiers.length > 0 ? identifiersOutput : ''}${interests.length > 0 ? interestsOutput : ''}`;
};
// Configure tippy.js
const setTippyInstance = (element, content) => {
return tippy(element, {
content: `<div class="button-container"><button class="close-tooltip">×</button></div><pre>${content}</pre>`,
allowHTML: true,
trigger: 'manual',
hideOnClick: true,
interactive: true,
theme: 'light-border',
appendTo: document.body,
});
};
// Generic function to check if elements (multiple) exist in the DOM
function waitForElementsToExist(selector, callback) {
if (document.querySelectorAll(selector)) {
return callback(document.querySelectorAll(selector));
}
const intervalId = setInterval(() => {
if (document.querySelectorAll(selector)) {
clearInterval(intervalId);
callback(document.querySelectorAll(selector));
}
}, 500);
}
export const renderMessage = (message) => {
alert(message);
};
export const renderProperties = (inner, g, useTippy) => {
// Only use tippy.js if the useTippy property is true
if (useTippy) {
// Pre-emptively hide any rogue open tooltips
hideAll({ duration: 0 });
}
const disclosureWidget = document.querySelector('#disclosure-widget');
disclosureWidget.innerHTML = '';
const nodes = inner.selectAll('g.node');
nodes.each((d, i) => {
const node = g.node(d);
// Don't display statement properties if the node is unspecified
if (node?.class?.includes('unknown') || node?.class?.includes('unspecified')) {
return;
}
const description = getDescription(node.description);
const fullDescription = JSON.stringify(node.fullDescription, null, 2);
node.elem.addEventListener('click', () => {
// Populate disclosure widget with node `fullDescription` JSON
disclosureWidget.innerHTML = `<details open><summary>Properties</summary><pre>${fullDescription}</pre></details>`;
// Only use tippy.js if the useTippy property is true
if (useTippy) {
// Pre-emptively hide any rogue open tooltips
hideAll();
// Create tooltip instance and display it
const tippyInstance = setTippyInstance(node.elem, description);
tippyInstance.show();
// Wait until the tooltip button exists before attaching a close event
waitForElementsToExist('.close-tooltip', (elements) => {
elements.forEach((element) => {
element.addEventListener('click', () => {
hideAll({ duration: 0 });
});
});
});
}
});
});
const edges = inner.selectAll('g.edgePath');
edges.each((d, i) => {
const edge = g.edge(d.v, d.w);
const edgeInstances = document.querySelectorAll(`#${edge.elem.id}`);
const description = getDescription(edge.description);
const fullDescription = JSON.stringify(edge.fullDescription, null, 2);
edgeInstances.forEach((edgeInstance) => {
edgeInstance.addEventListener('click', () => {
// Populate disclosure widget with edge `fullDescription` JSON
disclosureWidget.innerHTML = `<details open><summary>Properties</summary><pre>${fullDescription}</pre></details>`;
// Only use tippy.js if the useTippy property is true
if (useTippy) {
// Pre-emptively hide any rogue open tooltips
hideAll();
// Create tooltip instance and display it
const tippyInstance = setTippyInstance(edgeInstance, description);
tippyInstance.show();
// Wait until the tooltip button exists before attaching a close event
waitForElementsToExist('.close-tooltip', (elements) => {
elements.forEach((element) => {
element.addEventListener('click', () => {
hideAll();
});
});
});
}
});
});
});
};
export const renderDateSlider = (dates, version, currentlySelectedDate) => {
const sliderContainer = document.querySelector('#slider-container');
let selectedDate = currentlySelectedDate ? currentlySelectedDate : dates[dates.length - 1];
if (compareVersions(version, '0.4') >= 0 && dates.length > 1) {
sliderContainer.style.display = 'block';
sliderContainer.innerHTML = `
<input id="slider-input" type="range" min="0" max="${dates.length - 1}" list="markers" step="1"></input>
<datalist id="markers">
</datalist>
<p>Date: <output id="slider-value"></output></p>
`;
const datalist = document.querySelector('#markers');
for (let i = 0; i < dates.length; i++) {
datalist.innerHTML += `
<option value="${i}"></option>
`;
}
const value = document.querySelector('#slider-value');
const input = document.querySelector('#slider-input');
input.value = currentlySelectedDate ? dates.indexOf(currentlySelectedDate) : dates.length - 1;
value.textContent = dates[input.value];
input.addEventListener('input', (event) => {
value.textContent = dates[event.target.value];
selectedDate = dates[event.target.value];
});
return selectedDate;
} else if (compareVersions(version, '0.4') <= 0 && dates.length > 1) {
sliderContainer.style.display = 'block';
sliderContainer.innerHTML = `
<input id="slider-input" type="range" disabled min="0" max="1"></input>
<p>Date: <output id="slider-value">${dates[dates.length - 1]}</output></p>
`;
const input = document.querySelector('#slider-input');
input.value = 1;
} else {
sliderContainer.style.display = 'none';
}
};