Skip to content

Commit 10a82ea

Browse files
committed
fix(JSONReader): Add basic JSON reader as vtkAlgo
1 parent df20084 commit 10a82ea

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import macro from 'vtk.js/Sources/macro';
2+
3+
// ----------------------------------------------------------------------------
4+
// vtkJSONReader methods
5+
// ----------------------------------------------------------------------------
6+
7+
function vtkJSONReader(publicAPI, model) {
8+
// Set our className
9+
model.classHierarchy.push('vtkJSONReader');
10+
11+
// Internal method to fetch Array
12+
function fetchData(url, option = {}) {
13+
return model.dataAccessHelper.fetchText(publicAPI, url, option);
14+
}
15+
16+
// Set DataSet url
17+
publicAPI.setUrl = (url, option = {}) => {
18+
model.url = url;
19+
20+
// Fetch metadata
21+
return publicAPI.loadData(option);
22+
};
23+
24+
// Fetch the actual data arrays
25+
publicAPI.loadData = (option = {}) =>
26+
fetchData(model.url, option).then(publicAPI.parseAsText);
27+
28+
publicAPI.parseAsText = (content) => {
29+
if (!content) {
30+
return false;
31+
}
32+
model.data = JSON.parse(content);
33+
publicAPI.modified();
34+
return true;
35+
};
36+
37+
publicAPI.requestData = (inData, outData) => {
38+
outData[0] = model.data;
39+
};
40+
41+
// return Busy state
42+
publicAPI.isBusy = () => false;
43+
44+
publicAPI.getNumberOfOutputPorts = () => model.numberOfOutputs;
45+
}
46+
47+
// ----------------------------------------------------------------------------
48+
// Object factory
49+
// ----------------------------------------------------------------------------
50+
51+
const DEFAULT_VALUES = {
52+
// url: null,
53+
};
54+
55+
// ----------------------------------------------------------------------------
56+
57+
export function extend(publicAPI, model, initialValues = {}) {
58+
Object.assign(model, DEFAULT_VALUES, initialValues);
59+
60+
// Build VTK API
61+
macro.obj(publicAPI, model);
62+
macro.get(publicAPI, model, ['url']);
63+
macro.algo(publicAPI, model, 0, 1);
64+
macro.event(publicAPI, model, 'busy');
65+
66+
// Object methods
67+
vtkJSONReader(publicAPI, model);
68+
}
69+
70+
// ----------------------------------------------------------------------------
71+
72+
export const newInstance = macro.newInstance(extend, 'vtkJSONReader');
73+
74+
// ----------------------------------------------------------------------------
75+
76+
export default { newInstance, extend };

Sources/IO/Misc/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import vtkElevationReader from './ElevationReader';
2+
import vtkJSONReader from './JSONReader';
23
import vtkMTLReader from './MTLReader';
34
import vtkOBJReader from './OBJReader';
45
import vtkPDBReader from './PDBReader';
56

67
export default {
78
vtkElevationReader,
9+
vtkJSONReader,
810
vtkMTLReader,
911
vtkOBJReader,
1012
vtkPDBReader,

0 commit comments

Comments
 (0)