-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveCubes.js
More file actions
59 lines (49 loc) · 1.74 KB
/
saveCubes.js
File metadata and controls
59 lines (49 loc) · 1.74 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
// saveCubes.js
import * as d3 from 'https://cdn.jsdelivr.net/npm/d3@7/+esm';
import { getData } from './dataManager.js';
const UI_ONLY_EXPORT_FIELDS = new Set([
'complete',
'combinedMeSH',
'combinedKeywords'
]);
const FIRST_EXPORT_FIELDS = ['ResearchQuestion', 'PubMedQuery'];
const TRAILING_EXPORT_FIELDS = ['Notes', 'Rating', 'Tags'];
function sanitizeCsvValue(value) {
if (value === null || value === undefined) return '';
return String(value).replace(/\r?\n/g, ' ').trim();
}
function getExportColumns(data) {
const guaranteedFields = [...FIRST_EXPORT_FIELDS, ...TRAILING_EXPORT_FIELDS];
if (!data.length) return guaranteedFields;
const firstRowColumns = Object.keys(data[0]).filter(
key => !UI_ONLY_EXPORT_FIELDS.has(key) && !guaranteedFields.includes(key)
);
return [...FIRST_EXPORT_FIELDS, ...firstRowColumns, ...TRAILING_EXPORT_FIELDS];
}
function normalizeExportData(data, columns) {
return data.map(item => {
const row = {};
columns.forEach(column => {
row[column] = sanitizeCsvValue(item[column] ?? '');
});
return row;
});
}
export function exportFilteredData(dataOverride = null) {
const data = dataOverride || getData();
if (!data || !data.length) {
alert("No data available to export");
return false;
}
const columns = getExportColumns(data);
const exportData = normalizeExportData(data, columns);
const blob = new Blob([d3.csvFormat(exportData, columns)], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `pubmed_export_${new Date().toISOString().slice(0,10)}.csv`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
return true;
}