-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathscript.js
92 lines (79 loc) · 2.52 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
/* global document, FileReader, fetch, window, caches */
import 'babel-polyfill';
import nifti from 'nifti-js';
import pako from 'pako';
import { get as idbGet, set as idbSet } from 'idb-keyval';
import MRISlice from './mri-volume-slice';
const mRISlice = new MRISlice();
const LAST_NIFTI_KEY = 'Last-nifti-file';
const xDiv = document.getElementById('z-view-wrapper');
const yDiv = document.getElementById('y-view-wrapper');
const zDiv = document.getElementById('x-view-wrapper');
const isIncognito = !(window.RequestFileSystem || window.webkitRequestFileSystem);
loadDefaultData();
appendCanvasesToHTML();
function appendCanvasesToHTML() {
xDiv.appendChild(mRISlice.canvases.z);
yDiv.appendChild(mRISlice.canvases.y);
zDiv.appendChild(mRISlice.canvases.x);
}
document.getElementById('file-input').onchange = (event) => {
const fr = new FileReader();
fr.onload = fileLoadEvent => setupNifti(fileLoadEvent.target.result);
fr.readAsArrayBuffer(event.target.files[0]);
};
document.getElementById('toggle-cross-hairs').onchange = (event) => {
if (event.target.checked) {
mRISlice.showCrosshairs();
} else {
mRISlice.hideCrossHairs();
}
};
function setupNifti(file) {
idbSet(LAST_NIFTI_KEY, file);
mRISlice.loadNewNifti(nifti.parse(file));
mRISlice.mouseNavigationEnabled('enable please');
hideLoader();
}
function hideLoader() {
const loader = document.getElementById('principal-loader');
loader.style.display = 'none';
}
function showError() {
hideLoader();
const loader = document.getElementById('error');
loader.style.display = 'block';
}
async function loadDefaultData() {
let lastFile;
try {
lastFile = await idbGet(LAST_NIFTI_KEY);
} catch (e) {
console.error(e);
}
if (lastFile) return setupNifti(lastFile);
const url = 'https://openneuro.org/crn/datasets/ds001417/files/sub-study002:ses-after:anat:sub-study002_ses-after_T1w.nii.gz';
// load from the cache API or fetch if not found
let response;
if ('caches' in window && !isIncognito) {
try {
response = await caches.match(url);
if (!response) {
response = await fetch(url);
const cache = await caches.open(url);
await cache.put(url, response.clone());
}
} catch (e) {
console.error(e);
}
} else {
// browser does not support the cache APi
response = await fetch(url);
}
if (!response) showError();
const blob = await response.arrayBuffer();
const compressed = new Uint8Array(blob);
const file = pako.inflate(compressed);
hideLoader();
setupNifti(file);
}