-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataManager.js
More file actions
272 lines (228 loc) · 8.12 KB
/
dataManager.js
File metadata and controls
272 lines (228 loc) · 8.12 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
// dataManager.js
import * as d3 from 'https://cdn.jsdelivr.net/npm/d3@7/+esm';
import { fetchPubMedData, DEFAULT_API_KEY } from './pubmedFetcher.js';
import { showPubMedFetchOverlay, hidePubMedFetchOverlay } from './pubmedOverlay.js';
import { deleteSelectedFromData } from './deleteCubes.js';
import { exportFilteredData } from './saveCubes.js';
let data = [];
/* ========== CORE DATA FUNCTIONS ========== */
export async function loadData(url) {
try {
data = await d3.csv(url);
data.forEach(initializeItemFields);
return data;
} catch (error) {
console.error("Error loading data:", error);
return [];
}
}
export function getData() {
return data;
}
export function setData(newData) {
data = newData;
}
export function addAnnotation(pmid, field, value) {
const item = data.find(d => d.PMID === pmid);
if (item) {
item[field] = value;
updateCompletionStatus(item);
return true;
}
return false;
}
/* ========== UI TABLE FUNCTIONS ========== */
export function populateDataTable(data, onSelect) {
const tbody = d3.select('#data-table tbody');
tbody.selectAll('tr').remove();
// Process data to combine MeSH and Keyword columns
const processedData = data.map(item => {
// Combine all MeSH terms (MeSH_1 to MeSH_30)
const meshTerms = [];
for (let i = 1; i <= 30; i++) {
const term = item[`MeSH_${i}`];
if (term) meshTerms.push(term);
}
// Combine all Keywords (Keyword_1 to Keyword_30)
const keywords = [];
for (let i = 1; i <= 30; i++) {
const keyword = item[`Keyword_${i}`];
if (keyword) keywords.push(keyword);
}
return {
...item,
combinedMeSH: meshTerms.join('; '),
combinedKeywords: keywords.join('; ')
};
});
const rows = tbody.selectAll('tr')
.data(processedData)
.enter()
.append('tr')
.attr('data-pmid', d => d.PMID)
.classed('complete', d => d.complete);
// Title column
rows.append('td')
.text(d => d.Title?.substring(0, 50) + (d.Title?.length > 50 ? '...' : ''));
// Checkbox column
rows.append('td')
.append('input')
.attr('type', 'checkbox')
.attr('class', 'select-checkbox')
.on('change', function(event) {
const pmid = d3.select(this.closest('tr')).attr('data-pmid');
onSelect(pmid, event.target.checked);
});
// Notes column
rows.append('td')
.append('input')
.attr('type', 'text')
.attr('class', 'notes-input')
.attr('value', d => d.Notes || '')
.on('change', function(event, d) {
addAnnotation(d.PMID, 'Notes', event.target.value);
});
// Rating column
const ratingSelect = rows.append('td')
.append('select')
.attr('class', 'rating-select');
ratingSelect.selectAll('option')
.data([...Array(6).keys()].slice(1))
.enter()
.append('option')
.attr('value', d => d)
.text(d => d)
.filter((d, i, nodes) => d === nodes[i].__data__.Rating)
.attr('selected', true);
ratingSelect.on('change', function(event, d) {
addAnnotation(d.PMID, 'Rating', event.target.value);
});
// Tags column
rows.append('td')
.append('input')
.attr('type', 'text')
.attr('class', 'tags-input')
.attr('value', d => d.Tags || '')
.on('change', function(event, d) {
addAnnotation(d.PMID, 'Tags', event.target.value);
});
// MeSH Terms column
rows.append('td')
.text(d => d.combinedMeSH || '')
.attr('title', d => d.combinedMeSH || '')
.classed('mesh-cell', true);
// Keywords column
rows.append('td')
.text(d => d.combinedKeywords || '')
.attr('title', d => d.combinedKeywords || '')
.classed('keyword-cell', true);}
/* ========== TEXT ZONE FUNCTIONS ========== */
export function updateTextZone(article) {
console.groupCollapsed('[updateTextZone] Updating text zone');
// Input validation logging
if (!article || typeof article !== 'object') {
console.error('Invalid article data:', article);
console.log('Clearing text zone due to invalid input');
console.groupEnd();
clearTextZone();
return;
}
console.log('Raw article data:', JSON.parse(JSON.stringify(article)));
// Create safe article data with fallbacks
const safeArticle = {
Title: article.Title || 'No title available',
PMID: article.PMID || '-',
PubYear: article.PubYear || '-',
Source: article.Source || '-',
DOI: article.DOI || '',
PMC_ID: article.PMC_ID || '',
Abstract: article.Abstract || 'No abstract available'
};
console.log('Processed article data:', safeArticle);
// Update DOM elements with logging
const updateField = (id, value) => {
const element = document.getElementById(id);
if (!element) {
console.error(`Element with ID ${id} not found`);
return;
}
console.log(`Updating ${id}:`, value);
element.textContent = value;
};
updateField('selected-title', safeArticle.Title);
updateField('pmid-text', safeArticle.PMID);
updateField('year-text', safeArticle.PubYear);
updateField('source-text', safeArticle.Source);
updateField('abstract-text', safeArticle.Abstract);
// Handle DOI link
const doiLink = document.getElementById('doi-link');
if (doiLink) {
console.log('Updating DOI link:', {
text: safeArticle.DOI || '-',
href: safeArticle.DOI ? `https://doi.org/${safeArticle.DOI}` : '#'
});
doiLink.textContent = safeArticle.DOI || '-';
doiLink.href = safeArticle.DOI ? `https://doi.org/${safeArticle.DOI}` : '#';
} else {
console.error('DOI link element not found');
}
// Handle PMC link
const pmcLink = document.getElementById('pmc-link');
if (pmcLink) {
console.log('Updating PMC link:', {
text: safeArticle.PMC_ID ? `PMC${safeArticle.PMC_ID}` : '-',
href: safeArticle.PMC_ID ? `https://www.ncbi.nlm.nih.gov/pmc/articles/PMC${safeArticle.PMC_ID}/` : '#'
});
pmcLink.textContent = safeArticle.PMC_ID ? `PMC${safeArticle.PMC_ID}` : '-';
pmcLink.href = safeArticle.PMC_ID ? `https://www.ncbi.nlm.nih.gov/pmc/articles/PMC${safeArticle.PMC_ID}/` : '#';
} else {
console.error('PMC link element not found');
}
console.groupEnd();
}
export function clearTextZone() {
document.getElementById('selected-title').textContent = 'No article selected';
document.getElementById('pmid-text').textContent = '-';
document.getElementById('year-text').textContent = '-';
document.getElementById('source-text').textContent = '-';
document.getElementById('doi-link').textContent = '-';
document.getElementById('pmc-link').textContent = '-';
document.getElementById('abstract-text').textContent = 'Select an article to view its abstract';
}
/* ========== PUBMED FETCH FUNCTIONS ========== */
export async function attemptPubMedFetch() {
const overlay = showPubMedFetchOverlay();
return new Promise((resolve) => {
overlay.querySelector('button').onclick = async () => {
const fetchButton = overlay.querySelector('button');
fetchButton.disabled = true;
fetchButton.textContent = 'Fetching...';
try {
const searchTerm = overlay.querySelector('#pubmed-search-term').value.trim();
const apiKey = overlay.querySelector('#pubmed-api-key').value.trim() || DEFAULT_API_KEY;
const result = await fetchPubMedData(searchTerm, apiKey);
hidePubMedFetchOverlay();
resolve(result);
} catch (error) {
console.error("PubMed fetch failed:", error);
fetchButton.textContent = 'Try Again';
fetchButton.disabled = false;
}
};
overlay.querySelectorAll('button')[1].onclick = () => {
hidePubMedFetchOverlay();
resolve(null);
};
});
}
/* ========== PRIVATE HELPER FUNCTIONS ========== */
function initializeItemFields(item) {
item.includeArticle = item.includeArticle || "true";
item.rationale = item.rationale || "";
item.tags = item.tags || "";
return item;
}
function updateCompletionStatus(item) {
item.complete = item.Notes && item.Rating && item.Tags;
return item;
}