Skip to content

Commit 9680099

Browse files
authored
File uploader for json (#9)
Signed-off-by: MeastroZI <[email protected]> Signed-off-by: Vinit Pandit <[email protected]>
1 parent 80a86ee commit 9680099

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed

www/app.js

+50-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'codemirror/mode/javascript/javascript'
33
import { Chart } from 'chart.js/dist/chart'
44
import analyze from '../js/analyze'
55
import taxonomy from '..'
6+
/* global FileReader */
67

78
// From https://arxiv.org/abs/2201.02089
89
const EXAMPLE_JSON = {
@@ -178,8 +179,55 @@ function onAnalyze (value) {
178179
}
179180
}
180181

181-
document.getElementById('analyze').addEventListener('click', () => {
182-
return onAnalyze(code.getValue())
182+
const LoaderElement = document.createElement('div')
183+
LoaderElement.classList.add('loader')
184+
const fileInputElement = document.getElementById('fileInput')
185+
const removeBtn = document.getElementById('remove')
186+
187+
function showLoader (bool) {
188+
if (bool) {
189+
document.querySelector('main').appendChild(LoaderElement)
190+
document.getElementById('overlay').style.display = 'block'
191+
} else {
192+
document.querySelector('main').removeChild(LoaderElement)
193+
document.getElementById('overlay').style.display = 'none'
194+
}
195+
}
196+
197+
function analyzer () {
198+
const file = fileInputElement.files[0]
199+
showLoader(true)
200+
if (file) {
201+
const reader = new FileReader()
202+
reader.onload = function (event) {
203+
const contents = event.target.result
204+
onAnalyze(contents)
205+
showLoader(false)
206+
}
207+
reader.readAsText(file)
208+
} else {
209+
onAnalyze(code.getValue())
210+
showLoader(false)
211+
}
212+
}
213+
214+
removeBtn.addEventListener('click', () => {
215+
fileInputElement.value = ''
216+
code.setValue('')
217+
code.setOption('readOnly', false)
218+
removeBtn.classList.add('uk-disabled')
219+
})
220+
221+
fileInputElement.addEventListener('change', (e) => {
222+
if (e.target.files[0]) {
223+
code.setValue('Reading from input file')
224+
code.setOption('readOnly', true)
225+
removeBtn.classList.remove('uk-disabled')
226+
} else {
227+
console.log('else is clicked')
228+
removeBtn.classList.add('uk-disabled')
229+
}
183230
})
184231

232+
document.getElementById('analyze').addEventListener('click', analyzer)
185233
onAnalyze(code.getValue())

www/index.html

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@
1313
<script defer data-domain="sourcemeta.github.io/json-taxonomy" src="https://plausible.io/js/plausible.js"></script>
1414
</head>
1515
<body>
16+
<!-- element to make the page unresponsive in loading time -->
17+
<div id="overlay"></div>
18+
1619
<main class="uk-child-width-expand uk-grid-collapse uk-grid uk-grid-match" uk-grid>
1720
<div id="editor" class="uk-width-1-2@m"></div>
1821
<div id="results" class="uk-padding-small uk-width-1-2@m">
19-
<button id="analyze" class="uk-button uk-button-primary uk-width-1-1">Analyze JSON</button>
22+
23+
<div>
24+
<input type="file" id="fileInput" accept="application/json" class="uk-input uk-form-file">
25+
<button id="remove" class="uk-button uk-button-danger uk-margin-small uk-width-1-1 uk-disabled">Remove File</button>
26+
<button id="analyze" class="uk-button uk-button-primary uk-width-1-1">Analyze JSON</button>
27+
</div>
2028

2129
<div id="json-error" class="uk-alert-danger uk-margin uk-padding-small">
2230
<span>The JSON document you entered is invalid. Please enter a new one and click the <b>Analyze JSON</b> button again.</span>

www/style.css

+33
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,36 @@
99
.CodeMirror {
1010
height: 100% !important;
1111
}
12+
13+
.loader {
14+
width: 50px;
15+
height: 50px;
16+
position: fixed;
17+
top: 50%;
18+
left: 50%;
19+
transform: translate(-50%, -50%);
20+
border: 6px solid #666;
21+
border-top: 6px solid #2c3e50;
22+
border-radius: 50%;
23+
animation: spin 1s linear infinite;
24+
}
25+
26+
#overlay {
27+
position: fixed;
28+
top: 0;
29+
left: 0;
30+
width: 100%;
31+
height: 100%;
32+
background-color: rgba(0, 0, 0, 0.5);
33+
z-index: 9999;
34+
display: none;
35+
}
36+
37+
@keyframes spin {
38+
from {
39+
transform: rotate(0deg);
40+
}
41+
to {
42+
transform: rotate(360deg);
43+
}
44+
}

0 commit comments

Comments
 (0)