-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaskDetails.html
executable file
·235 lines (192 loc) · 6.37 KB
/
MaskDetails.html
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
<body>
<div id="spin1" class="spinner"></div>
<div style="overflow-x: auto;">
<table class="mui-table" id="MaskDetails1" style="display: none;">
<tbody></tbody>
</table>
<hr/>
<hr/>
<table class="mui-table" id="BasicMaskTable2" style="display: none;">
<tbody></tbody>
</table>
</div>
<h2>Additional Information</h2>
<div id="spin3" class="spinner"></div>
<div id="remainingTables"></div>
<script type="module">
async function queryApi(url) {
try {
const response = await fetch(url, {
mode: 'cors',
credentials: 'include'
});
return await response.json();
} catch (error) {
console.error('Error getting data:', error);
return null;
}
}
async function getResults() {
const designId = getDesignID();
if (!designId) {
console.error('No design ID found in URL parameters');
hideAllSpinners();
return;
}
// get the api url from the config file
const response = await fetch('js/MaskConfig.json');
const config = await response.json();
const apiRootUrl = config.apiRootUrl;
const apiUrl = apiRootUrl + `/mask-detail?design-id=${designId}`;
showAllSpinners();
const results = await queryApi(apiUrl);
if (!results || !results.data || Object.keys(results.data).length === 0) {
hideAllSpinners();
return;
}
const [maskInfo, ...additionInfo] = results.data;
// Get the keys of the maskInfo object
const keys = Object.keys(maskInfo[1][0]);
const halfLength = Math.ceil(keys.length / 2);
// Split the keys into two halves
const firstHalfKeys = keys.slice(0, halfLength);
const secondHalfKeys = keys.slice(halfLength);
// Create the first half object
const firstHalf = {};
firstHalfKeys.forEach(key => {
firstHalf[key] = maskInfo[1][0][key];
});
// Create the second half object
const secondHalf = {};
secondHalfKeys.forEach(key => {
secondHalf[key] = maskInfo[1][0][key];
});
// Now you can use firstHalf and secondHalf as needed
createHorizontalTable(firstHalf, 'MaskDetails1');
createHorizontalTable(secondHalf, 'BasicMaskTable2');
const remainingTablesContainer = document.getElementById('remainingTables');
if (additionInfo.length === 0) {
hideAllSpinners();
return;
}
// create the remaining tables dynamically
additionInfo.forEach(function([title, tableData], index) {
if (Array.isArray(tableData) && tableData.length === 0) {
return;
}
const tableId = `table${index + 3}`;
const table = document.createElement('table');
table.id = tableId; // Corrected assignment
table.style.display = 'none';
const tbody = document.createElement('tbody');
table.appendChild(tbody);
// collapsible table as a button
const button = document.createElement('button');
button.classList.add('collapsible', 'detail-button-width');
button.textContent = title;
button.addEventListener('click', function () {
const content = document.getElementById(tableId);
if (content.style.display === 'table') {
content.style.display = 'none';
} else {
content.style.display = 'table';
}
});
remainingTablesContainer.appendChild(button);
remainingTablesContainer.appendChild(table);
// different tables for different content
if (/Slit/.test(title)) {
createHeaderTable(tableData, tableId);
} else {
createHorizontalTable(tableData[0], tableId);
}
});
hideAllSpinners();
showTable('MaskDetails1');
showTable('BasicMaskTable2');
}
function showSpinner(spinnerId) {
document.getElementById(spinnerId).style.display = 'block';
}
function hideSpinner(spinnerId) {
document.getElementById(spinnerId).style.display = 'none';
}
function hideAllSpinners() {
hideSpinner('spin1');
hideSpinner('spin3');
}
function showAllSpinners() {
showSpinner('spin1');
showSpinner('spin3');
}
function showTable(tableId) {
document.getElementById(tableId).style.display = 'table';
}
function createHorizontalTable(obj, tableId) {
const table = document.getElementById(tableId);
if (!table) {
console.error('Table element not found for ID:', tableId);
return;
}
const tbody = table.querySelector('tbody');
const keys = Object.keys(obj);
const numKeys = keys.length;
// Create a table header with the object keys
const headerRow = document.createElement('tr');
for (const key in obj) {
const th = document.createElement('th');
th.textContent = key.replace(/-/g, ' ');
headerRow.appendChild(th);
}
tbody.appendChild(headerRow);
// Add the row with the object values
const valueRow = document.createElement('tr');
for (const value of Object.values(obj)) {
const td = document.createElement('td');
td.textContent = value;
valueRow.appendChild(td);
}
// Append the value row to the table body
tbody.appendChild(valueRow);
}
function createHeaderTable(objList, tableId) {
const table = document.getElementById(tableId);
table.classList.add('slit-table-details');
if (!table) {
console.error('Table element not found for ID:', tableId);
return;
}
const tbody = table.querySelector('tbody');
if (!tbody) {
console.error('Tbody element not found for table:', table);
return;
}
// Clear existing tbody content
tbody.innerHTML = '';
// Create header row from the keys of the first object in the list
const headerRow = document.createElement('tr');
Object.keys(objList[0]).forEach(key => {
const th = document.createElement('th');
th.textContent = key.replace(/-/g, ' ');
headerRow.appendChild(th);
});
tbody.appendChild(headerRow);
// Create data rows from each object in the list
objList.forEach(obj => {
const dataRow = document.createElement('tr');
Object.values(obj).forEach(value => {
const td = document.createElement('td');
td.textContent = value;
dataRow.appendChild(td);
});
tbody.appendChild(dataRow);
});
}
function getDesignID() {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get('design-id');
}
// Call getResults when the page loads
window.onload = getResults();
</script>
</body>