-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (132 loc) · 3.82 KB
/
index.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
import React from 'react';
import load from 'little-loader'
import EventEmitter from 'events';
const emitter = new EventEmitter();
let scriptLoaded = false;
let scriptLoading = false;
class Apparatus extends React.Component {
constructor(props) {
super(props);
this.state = {
viewer: null
}
this._hasInitialized = false;
this._injectingIdyllVars = false;
this.handleViewerRender = this.handleViewerRender.bind(this);
this.handleRef = this.handleRef.bind(this);
}
componentWillReceiveProps(nextProps) {
if (!this.state.viewer) {
return;
}
Object.keys(this.props).filter((d) => {
return d.indexOf('_') !== 0 && (this.props._writeOnly || []).indexOf(d) === -1;
}).filter((d) => {
return ['error', 'children', 'idyll', 'hasError', 'updateProps'].indexOf(d) === -1;
}).forEach((d) => {
const currentValue = this.props[d];
const newValue = nextProps[d];
if (currentValue !== newValue) {
const attribute = this.state.viewer.getAttributeByLabel(d);
attribute.setExpression(newValue);
}
});
}
injectIdyllVars() {
if (this._injectingIdyllVars) {
return;
}
this._injectingIdyllVars = true;
Object.keys(this.props).filter((d) => {
return d.indexOf('_') !== 0 && (this.props._writeOnly || []).indexOf(d) === -1;;
}).filter((d) => {
return ['error', 'children', 'idyll', 'hasError', 'updateProps'].indexOf(d) === -1;
}).forEach((d) => {
const val = this.props[d];
try {
const attribute = this.state.viewer.getAttributeByLabel(d);
attribute.setExpression(val);
} catch(e) {
console.warn(e);
}
});
this._hasInitialized = true;
this._injectingIdyllVars = false;
}
handleViewerRender() {
if (!this._hasInitialized) {
this.injectIdyllVars();
return;
}
Object.keys(this.props).filter((d) => {
return d.indexOf('_') !== 0;
}).filter((d) => {
return ['error', 'children', 'idyll', 'hasError', 'updateProps'].indexOf(d) === -1;
}).forEach((d) => {
const currentValue = this.props[d];
try {
const attribute = this.state.viewer.getAttributeByLabel(d);
const apparatusValue = attribute.value();
if (currentValue !== apparatusValue) {
this.props.updateProps({
[d]: apparatusValue
})
}
} catch(e) {
console.warn(e);
}
})
}
initializeViewer() {
if (!this._ref || this.state.viewer) {
return;
}
const viewer = new ApparatusViewer({
url: this.props._url,
element: this._ref,
regionOfInterest: this.props._regionOfInterest,
onRender: this.handleViewerRender
});
this.setState({
viewer: viewer
});
}
componentDidMount() {
if (!scriptLoaded && !scriptLoading) {
scriptLoading = true;
load("https://rawgit.com/cdglabs/apparatus-site/gh-pages/editor/dist/apparatus-viewer.js", (err) => {
if (!err) {
scriptLoaded = true;
scriptLoading = false;
this.initializeViewer();
emitter.emit('scriptloaded');
} else {
console.warn(err);
}
});
} else if (scriptLoaded) {
this.initializeViewer();
} else {
emitter.on('scriptloaded', () => {
this.initializeViewer();
})
}
}
shouldComponentUpdate() {
return false;
}
handleRef(el) {
// console.log()
this._ref = el;
if (scriptLoaded) {
this.initializeViewer();
}
}
render() {
const { className, _width, _height, style } = this.props;
return (
<div ref={(el) => this.handleRef(el)} className={className} style={Object.assign({ margin: '30px auto', width: _width ? _width: '100%', height: _height ? _height : 'auto'}, style)} />
);
}
}
module.exports = Apparatus;