Repository navigation
Expand file tree
/
Copy pathdocument-handler.js
More file actions
84 lines (71 loc) · 2.68 KB
/
Copy pathdocument-handler.js
File metadata and controls
84 lines (71 loc) · 2.68 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
/**
* Document Handler Module
* Handles embedded document display and extraction
*/
class DocumentHandler {
constructor() {
this.documentListElement = document.getElementById('documentList');
this.documentsTabElement = document.getElementById('documents-tab');
this.currentDocuments = [];
// Initialize event listeners
this.initEventListeners();
}
/**
* Set up event delegation for document list
*/
initEventListeners() {
// Use event delegation for document actions
this.documentListElement.addEventListener('click', (e) => {
if (e.target.classList.contains('download-doc-button')) {
const docIndex = parseInt(e.target.getAttribute('data-index'));
if (this.currentDocuments[docIndex]) {
this.downloadDocument(this.currentDocuments[docIndex]);
}
}
});
}
/**
* Displays the list of embedded documents
* @param {Array} documents - Array of document objects
*/
displayDocuments(documents) {
this.currentDocuments = documents || [];
if (!this.currentDocuments.length) {
this.documentListElement.innerHTML = '';
this.documentsTabElement.innerHTML = '<p>No embedded documents found in this DICOM file.</p>';
return;
}
let html = '';
for (let i = 0; i < this.currentDocuments.length; i++) {
const doc = this.currentDocuments[i];
const docType = DocumentTypeDetector.detectType(doc.buffer);
html += `<li class="document-item">
<div class="document-info">
<strong>Document ${i+1}</strong> (${doc.length} bytes, ${docType.mimeType})
</div>
<div class="document-actions">
<button class="download-doc-button" data-index="${i}">Download</button>
</div>
</li>`;
}
this.documentListElement.innerHTML = html;
}
/**
* Download an embedded document
* @param {Object} docData - Document data object
*/
downloadDocument(docData) {
const docType = DocumentTypeDetector.detectType(docData.buffer);
const blob = new Blob([docData.buffer], { type: docType.mimeType });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `document.${docType.extension}`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
// Clean up
setTimeout(() => URL.revokeObjectURL(url), 100);
}
}
window.DocumentHandler = DocumentHandler;