This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
130 lines (112 loc) · 4.15 KB
/
script.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
const nameInput = document.getElementById('name-input');
const dateInput = document.getElementById('date-input');
const uploaderInput = document.getElementById('uploader-input');
const searchButton = document.getElementById('search-button');
const resultsTable = document.getElementById('results-table');
const resultsTableBody = document.getElementById('results-table').querySelector('tbody');
let sortAscending = false;
let querydata;
let dataTable;
let line_data;
resultsTableBody.addEventListener('click', async (event) => {
const row = event.target.parentNode;
const lineIndex = row.querySelector('td:first-child').textContent-1;
const _id=querydata[lineIndex]._id
const db = database.value;
const url = `http://localhost:3000/search_one?_id=${_id}&database=${db}`;
try {
const response = await fetch(url);
line_data = await response.json();
let xyz=`${line_data.opt_xyz.length}\n Test\n`;
line_data.opt_xyz.forEach(line => {
xyz+=`${line.atom} ${line.x} ${line.y} ${line.z}\n`
});
let filename=line_data.name;
var molecule = ChemDoodle.readXYZ(xyz, 1);
molecularview.loadMolecule(molecule);
// Remove the previous highlighted row
const prevHighlightedRow = resultsTableBody.querySelector('.highlighted');
if (prevHighlightedRow) {
prevHighlightedRow.classList.remove('highlighted');
}
row.classList.add('highlighted');
var tableBody = document.getElementById("detail-values");
tableBody.innerHTML = "";
// Loop through the JSON data and create table rows for each key-value pair
for (var key in line_data) {
var value = line_data[key];
var detail_row = document.createElement("tr");
var keyCell = document.createElement("td");
var valueCell = document.createElement("td");
keyCell.textContent = key;
if (key === 'opt_xyz') {
var downloadLink = document.createElement("a");
downloadLink.textContent = "Download XYZ";
downloadLink.href = "data:text/plain;charset=utf-8," + encodeURIComponent(xyz);
downloadLink.download = `${filename}.xyz`;
valueCell.appendChild(downloadLink);
} else {
valueCell.textContent = JSON.stringify(value);
}
detail_row.appendChild(keyCell);
detail_row.appendChild(valueCell);
tableBody.appendChild(detail_row);
}
} catch (error) {
console.error(error);
resultsTable.innerHTML = '<tr><td colspan="4">An error occurred</td></tr>';
}
});
searchButton.addEventListener('click', async () => {
const db = database.value;
const namekeyword = nameInput.value.trim();
const datekeyword = dateInput.value.trim();
const uploaderkeyword = uploaderInput.value.trim();
const url = `http://localhost:3000/search?database=${db}&namekeyword=${namekeyword}&datekeyword=${datekeyword}&uploaderkeyword=${uploaderkeyword}`;
try {
document.getElementById("loading").style.display = "block";
const response = await fetch(url);
querydata = await response.json();
document.getElementById("loading").style.display = "none";
if (dataTable) {
// If DataTable already exists, destroy it before creating a new one
dataTable.destroy();
}
if (querydata.length === 0) {
resultsTable.innerHTML = '<tr><td colspan="4">No results found</td></tr>';
} else {
let ind=1;
rows = querydata.map(molecule => [
ind++,
molecule.name,
molecule.upload_date,
molecule.uploader,
molecule.basis_sets,
molecule.functional,
molecule.electronic_energy,
]);
const columns = [
{ title: "#" },
{ title: "Name" },
{ title: "Upload Date" },
{ title: "Uploader" },
{ title: "Basis Sets" },
{ title: "Functional" },
{ title: "Energy" },
];
dataTable = $(resultsTable).DataTable({
data: rows,
columns: columns,
order: [[0, "asc"]],
paging: true,
lengthChange: true,
searching: true,
info: true,
autoWidth: false,
});
}
} catch (error) {
console.error(error);
resultsTable.innerHTML = '<tr><td colspan="4">An error occurred</td></tr>';
}
});