-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfhir-response-formatter.html
442 lines (391 loc) · 15.8 KB
/
fhir-response-formatter.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>FHIR Response Formatter</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">
<style>
body {
background: #f2f2f2;
padding: 20px;
font-family: Arial, sans-serif;
}
.header {
display: flex;
align-items: center;
margin-bottom: 20px;
}
.header i {
color: #20c997;
font-size: 1.5rem;
margin-right: 10px;
}
.header h2 {
margin: 0;
font-weight: 600;
}
label {
font-weight: 600;
}
.form-control {
font-family: monospace;
font-size: 0.9rem;
}
#formatBtn:disabled {
opacity: 0.7;
cursor: not-allowed;
}
#output {
white-space: pre-wrap;
word-wrap: break-word;
background-color: #ffffff;
padding: 15px;
border-radius: 5px;
overflow-x: auto;
overflow-y: auto;
font-family: monospace;
max-height: 400px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.output-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 20px;
}
.output-buttons {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.btn {
transition: background-color 0.2s ease, box-shadow 0.2s ease;
}
.btn:hover {
box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}
/* JSON Syntax Highlighting */
.string { color: #0284c7; }
.number { color: #d97706; }
.boolean { color: #059669; }
.null { color: #6b7280; font-style: italic; }
.key { color: #6d28d9; }
.error { color: #dc2626; font-weight: bold; }
.warning { color: #d97706; font-weight: bold; }
.information { color: #0ea5e9; font-weight: bold; }
.resourceTypeValue { color: #9333ea; font-weight: bold; }
/* Spinner for loading feedback */
.spinner-border {
width: 1rem;
height: 1rem;
border-width: 2px;
margin-left: 5px;
}
/* Smooth scroll highlight */
@keyframes highlight {
0% { background-color: yellow; }
100% { background-color: transparent; }
}
/* Legend styling */
.legend {
margin-top: 20px;
background: #ffffff;
padding: 15px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.legend h5 {
margin-bottom: 10px;
}
.legend-item {
margin-bottom: 5px;
font-family: Arial, sans-serif;
font-size: 0.9rem;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<i class="fas fa-stethoscope"></i>
<h2>FHIR Response Formatter</h2>
</div>
<div class="form-group">
<label for="inputData">Paste your FHIR response JSON here:</label>
<textarea class="form-control" id="inputData" rows="10" placeholder="Paste FHIR JSON response..."></textarea>
</div>
<button id="formatBtn" class="btn btn-primary">
<i class="fas fa-cogs"></i> Format JSON
</button>
<span id="loadingIndicator" class="ml-2"></span>
<div class="output-header">
<label for="output" class="mt-4">Formatted Output:</label>
<div class="output-buttons" id="buttonsContainer">
<button class="btn btn-secondary" id="copyBtn">
<i class="fas fa-copy"></i> Copy
</button>
</div>
</div>
<div id="output" class="mt-2"></div>
<!-- Legend & Guide -->
<div class="legend mt-4">
<h5>Legend & Guide</h5>
<div class="legend-item"><strong>Keys:</strong> <span class="key">"key"</span></div>
<div class="legend-item"><strong>Strings:</strong> <span class="string">"value"</span></div>
<div class="legend-item"><strong>Numbers:</strong> <span class="number">123</span></div>
<div class="legend-item"><strong>Booleans:</strong> <span class="boolean">true</span> / <span class="boolean">false</span></div>
<div class="legend-item"><strong>null:</strong> <span class="null">null</span></div>
<div class="legend-item"><strong>Resource Types:</strong> <span class="resourceTypeValue">"Patient"</span></div>
<div class="legend-item"><strong>Errors:</strong> <span class="error">"error"</span></div>
<div class="legend-item"><strong>Warnings:</strong> <span class="warning">"warning"</span></div>
<div class="legend-item"><strong>Information:</strong> <span class="information">"information"</span></div>
</div>
</div>
<!-- Bootstrap JS and dependencies -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
// Variables to keep track of elements
let errorElements = [];
let warningElements = [];
let resourceTypeElements = [];
let errorIndex = 0;
let warningIndex = 0;
let resourceIndex = 0;
let resourceTypeInstances = {};
let resourceTypeIndices = {};
const formatBtn = document.getElementById('formatBtn');
const loadingIndicator = document.getElementById('loadingIndicator');
formatBtn.addEventListener('click', function() {
const input = document.getElementById('inputData').value;
const outputDiv = document.getElementById('output');
const buttonsContainer = document.getElementById('buttonsContainer');
outputDiv.innerHTML = ''; // Clear previous output
// Remove previous buttons except copy button
const buttonsToRemove = buttonsContainer.querySelectorAll('.dynamic-btn');
buttonsToRemove.forEach(btn => btn.remove());
// Reset elements arrays and indices
errorElements = [];
warningElements = [];
resourceTypeElements = [];
errorIndex = 0;
warningIndex = 0;
resourceIndex = 0;
resourceTypeInstances = {};
resourceTypeIndices = {};
// Show loading spinner
formatBtn.disabled = true;
loadingIndicator.innerHTML = '<div class="spinner-border text-primary" role="status"><span class="sr-only">Loading...</span></div>';
setTimeout(() => {
try {
let cleanedInput = cleanInput(input);
let jsonData = JSON.parse(cleanedInput);
let prettyJson = jsonToHtml(jsonData);
outputDiv.innerHTML = prettyJson;
// Collect error and warning elements
errorElements = outputDiv.querySelectorAll('.error');
warningElements = outputDiv.querySelectorAll('.warning');
// Collect all resourceType elements
let resourceTypeSpans = outputDiv.querySelectorAll('.resourceTypeValue');
resourceTypeSpans.forEach(span => {
let resourceType = span.getAttribute('data-resource-type');
resourceTypeElements.push(span);
// For individual resource type buttons
if (resourceType) {
if (!resourceTypeInstances[resourceType]) {
resourceTypeInstances[resourceType] = [];
resourceTypeIndices[resourceType] = 0;
}
resourceTypeInstances[resourceType].push(span);
}
});
// Create Error button if errors are present
if (errorElements.length > 0) {
let errorBtn = document.createElement('button');
errorBtn.className = 'btn btn-danger dynamic-btn';
errorBtn.id = 'errorBtn';
errorBtn.innerHTML = '<i class="fas fa-exclamation-triangle"></i> Error';
errorBtn.addEventListener('click', navigateErrors);
buttonsContainer.insertBefore(errorBtn, document.getElementById('copyBtn'));
}
// Create Warning button if warnings are present
if (warningElements.length > 0) {
let warningBtn = document.createElement('button');
warningBtn.className = 'btn btn-warning dynamic-btn';
warningBtn.id = 'warningBtn';
warningBtn.innerHTML = '<i class="fas fa-exclamation-circle"></i> Warning';
warningBtn.addEventListener('click', navigateWarnings);
buttonsContainer.insertBefore(warningBtn, document.getElementById('copyBtn'));
}
// Create Resource button
if (resourceTypeElements.length > 0) {
let resourceBtn = document.createElement('button');
resourceBtn.className = 'btn btn-info dynamic-btn';
resourceBtn.id = 'resourceBtn';
resourceBtn.innerHTML = '<i class="fas fa-database"></i> Resource';
resourceBtn.addEventListener('click', navigateResources);
buttonsContainer.insertBefore(resourceBtn, document.getElementById('copyBtn'));
}
// Create resource type buttons
for (let resourceType in resourceTypeInstances) {
let btn = document.createElement('button');
btn.className = 'btn btn-secondary resource-btn dynamic-btn';
btn.innerHTML = `<i class="fas fa-tag"></i> ${resourceType}`;
btn.style.backgroundColor = getResourceColor(resourceType);
btn.style.color = '#fff';
btn.addEventListener('click', function() {
navigateResourceType(resourceType);
});
buttonsContainer.insertBefore(btn, document.getElementById('copyBtn'));
}
} catch (e) {
outputDiv.innerHTML = '<span style="color: red; font-weight: bold;">Error parsing JSON: ' + e.message + '</span>';
} finally {
formatBtn.disabled = false;
loadingIndicator.innerHTML = '';
}
}, 300);
});
// Function to clean the input
function cleanInput(input) {
// Remove any REST tags or non-JSON content
let jsonStart = input.indexOf('{');
let jsonEnd = input.lastIndexOf('}');
if (jsonStart !== -1 && jsonEnd !== -1) {
return input.substring(jsonStart, jsonEnd + 1);
} else {
throw new Error('No valid JSON object found.');
}
}
// Function to convert JSON object to HTML with syntax highlighting
function jsonToHtml(obj, indent = 0, keyName = '') {
let html = '';
const indentStr = ' '.repeat(indent);
if (Array.isArray(obj)) {
html += '[<br>';
obj.forEach((item, index) => {
html += indentStr + ' ' + jsonToHtml(item, indent + 1);
if (index < obj.length - 1) html += ',';
html += '<br>';
});
html += indentStr + ']';
} else if (typeof obj === 'object' && obj !== null) {
html += '{<br>';
const keys = Object.keys(obj);
keys.forEach((key, index) => {
const value = obj[key];
const keyClass = 'key';
const keyHtml = indentStr + ' ' + `<span class="${keyClass}">"${key}"</span>: `;
let valueHtml = jsonToHtml(value, indent + 1, key);
html += keyHtml + valueHtml;
if (index < keys.length - 1) html += ',';
html += '<br>';
});
html += indentStr + '}';
} else {
let valueClass = typeof obj;
let valueStr = obj;
if (obj === null) {
valueClass = 'null';
valueStr = 'null';
} else if (typeof obj === 'string') {
valueStr = `"${obj}"`;
// Apply custom styles based on key and value
if (keyName === 'severity') {
if (obj === 'error') {
valueClass = 'error';
} else if (obj === 'warning') {
valueClass = 'warning';
} else if (obj === 'information') {
valueClass = 'information';
} else {
valueClass = 'string';
}
} else if (keyName === 'resourceType') {
valueClass = 'resourceTypeValue';
valueStr = `<span class="${valueClass}" data-resource-type="${obj}">${valueStr}</span>`;
} else {
valueClass = 'string';
}
}
html = `<span class="${valueClass}">${valueStr}</span>`;
}
return html;
}
// Function to get a color for a resource type
function getResourceColor(resourceType) {
const colors = {
Patient: '#6f42c1',
Condition: '#d63384',
Observation: '#fd7e14',
Medication: '#20c997',
Encounter: '#0dcaf0',
Practitioner: '#6610f2',
};
return colors[resourceType] || '#6c757d';
}
// Function to navigate through resourceType elements
function navigateResources() {
if (resourceTypeElements.length > 0) {
let element = resourceTypeElements[resourceIndex];
element.scrollIntoView({behavior: 'smooth', block: 'center'});
highlightElement(element);
resourceIndex = (resourceIndex + 1) % resourceTypeElements.length;
}
}
// Function to navigate through specific resource types
function navigateResourceType(resourceType) {
let elements = resourceTypeInstances[resourceType];
if (elements && elements.length > 0) {
let index = resourceTypeIndices[resourceType];
let element = elements[index];
element.scrollIntoView({behavior: 'smooth', block: 'center'});
highlightElement(element);
resourceTypeIndices[resourceType] = (index + 1) % elements.length;
}
}
// Copy to clipboard functionality
document.getElementById('copyBtn').addEventListener('click', function() {
let outputDiv = document.getElementById('output');
let range = document.createRange();
range.selectNode(outputDiv);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
try {
document.execCommand('copy');
alert('Output copied to clipboard!');
} catch(err) {
alert('Failed to copy text.');
}
window.getSelection().removeAllRanges();
});
// Function to highlight an element temporarily
function highlightElement(element) {
element.style.animation = 'highlight 2s ease-in-out';
setTimeout(function() {
element.style.animation = '';
}, 2000);
}
// Error button click handler
function navigateErrors() {
if (errorElements.length > 0) {
let element = errorElements[errorIndex];
element.scrollIntoView({behavior: 'smooth', block: 'center'});
highlightElement(element);
errorIndex = (errorIndex + 1) % errorElements.length;
}
}
// Warning button click handler
function navigateWarnings() {
if (warningElements.length > 0) {
let element = warningElements[warningIndex];
element.scrollIntoView({behavior: 'smooth', block: 'center'});
highlightElement(element);
warningIndex = (warningIndex + 1) % warningElements.length;
}
}
</script>
</body>
</html>