/
app.js
179 lines (156 loc) · 5.02 KB
/
app.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
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
import { structuredReportToText } from '../itk-dicom.js'
// promise-file-reader
function readAsArrayBuffer (file) {
if (!(file instanceof Blob)) {
throw new TypeError('Must be a File or Blob')
}
return new Promise(function(resolve, reject) {
var reader = new FileReader()
reader.onload = function(e) { resolve(e.target.result) }
reader.onerror = function(e) { reject(new Error('Error reading' + file.name + ': ' + e.target.result)) }
reader['readAsArrayBuffer'](file)
})
}
const structuredReportToTextOptions = new Map([
["unknownRelationship", "Accept unknown relationship type"],
["invalidItemValue", "Accept invalid content item value"],
["ignoreConstraints", "Ignore relationship constraints"],
["ignoreItemErrors", "Ignore content item errors"],
["skipInvalidItems", "Skip invalid content items"],
["noDocumentHeader", "Print no document header"],
["numberNestedItems", "Number nested items"],
["shortenLongValues", "Shorten long item values"],
["printInstanceUid", "Print SOP Instance UID"],
["printSopclassShort", "Print short SOP class name"],
["printSopclassLong", "Print SOP class name"],
["printSopclassUid", "Print long SOP class name"],
["printAllCodes", "Print all codes"],
["printInvalidCodes", "Print invalid codes"],
["printTemplateId", "Print template identification"],
["indicateEnhanced", "Indicate enhanced encoding mode"],
["printColor", "Use ANSI escape codes"],
])
function createOptionsElements(context, event) {
context.options = {}
const optionsChildren = []
structuredReportToTextOptions.forEach((description, name) =>{
context.options[name] = false
const entryDiv = document.createElement("div")
entryDiv.innerHTML = `<sp-checkbox name=${name} id=${name} ><label for="${name}">${description}</label></sp-checkbox>`
entryDiv.addEventListener('click', (e) => {
context.options[name] = !entryDiv.children[0].checked
context.service.send({ type: 'PROCESS' })
})
optionsChildren.push(entryDiv)
})
const optionsDiv = document.getElementById("options")
optionsDiv.replaceChildren(...optionsChildren)
}
async function loadData(context, event) {
const arrayBuffer = await readAsArrayBuffer(event.data)
const dicomData = new Uint8Array(arrayBuffer)
return dicomData
}
let appWebWorker = null
async function processData(context, event) {
if (!context.dicomData) {
return
}
const outputTextArea = document.getElementById('outputTextArea')
outputTextArea.innerText = "Processing..."
const { webWorker, outputText } = await structuredReportToText(appWebWorker, context.dicomData.slice(), context.options)
appWebWorker = webWorker
return outputText
}
function renderResults(context) {
const outputTextArea = document.getElementById('outputTextArea')
if (context.processResult) {
outputTextArea.innerText = context.processResult
}
}
const context = {
options: {},
dicomData: null,
processResult: null,
}
const demoAppMachine = XState.createMachine({
id: 'demoApp',
initial: 'creatingOptions',
context,
states: {
idle: {
on: {
UPLOAD_DATA: 'loadingData',
PROCESS: 'processing',
},
},
creatingOptions: {
entry: createOptionsElements,
always: { target: 'idle' }
},
loadingData: {
invoke: {
id: 'loadData',
src: loadData,
onDone: {
actions: [
XState.assign({
dicomData: (context, { data }) => { return data }
}),
],
target: "processing",
},
onError: {
actions: [
(c, event) => {
const message = `Could not load data: ${event.data.toString()}`
console.error(message)
alert(message)
}
],
target: 'idle',
}
}
},
processing: {
invoke: {
id: 'processData',
src: processData,
onDone: {
actions: [
XState.assign({
processResult: (context, { data }) => { return data }
}),
renderResults,
],
target: "idle",
},
onError: {
actions: [
(c, event) => {
console.log(c, event)
const message = `Could not process file: ${event.data.toString()}`
console.error(message)
alert(message)
}
],
target: 'idle',
}
}
}
}
})
const demoAppService = XState.interpret(demoAppMachine)
context.service = demoAppService
demoAppService.start()
const fileInput = document.getElementById('inputFile')
fileInput.addEventListener('change', (event) => {
const dataTransfer = event.dataTransfer
const files = event.target.files || dataTransfer.files
demoAppService.send({ type: 'UPLOAD_DATA', data: files[0] })
})
const dropzoneInput = document.getElementById('fileDropzone')
dropzoneInput.addEventListener('drop', (event) => {
const files = event.dataTransfer.files
demoAppService.send({ type: 'UPLOAD_DATA', data: files[0] })
})