-
Notifications
You must be signed in to change notification settings - Fork 3
/
App.jsx
108 lines (91 loc) · 2.9 KB
/
App.jsx
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
import React from "react";
import _ from "lodash";
import Vis from "./taxogenomic/Vis.jsx";
const Highlight = ({visData, selection, highlight}) => <div className="highlight" style={style(highlight)}>
Highlight</div>;
function style(highlight) {
return {
position: 'absolute',
top: highlight.y,
left: highlight.x
};
}
export default class App extends React.Component {
constructor(props) {
super(props);
this.numQueries = props.exampleQueries.length;
this.state = {qs: this.updateQueryState(), selectedTaxa: props.selectedTaxa};
}
updateQueryState() {
var currentIdx, newIdx, results, qName, taxonomy;
currentIdx = _.get(this.state, 'qs.queryIndex', -1);
newIdx = ((currentIdx + 1) % this.numQueries);
results = this.props.exampleResults[newIdx];
qName = this.props.exampleQueries[newIdx].name;
taxonomy = this.updateTaxonomy(results);
return {
queryIndex: newIdx,
results: results,
queryName: qName,
taxonomy: taxonomy
};
}
updateTaxonomy(results) {
this.props.taxonomy.setResults(results.fixed_1000__bin);
return this.props.taxonomy; // simulate immutability
}
changeQuery() {
this.setState({qs: this.updateQueryState()});
}
changeSpecies() {
const newSelectedTaxa = _.size(this.state.selectedTaxa) === 0 ? this.props.selectedTaxa : {};
this.setState({selectedTaxa: newSelectedTaxa});
}
logFactory(name) {
return () => console.log(`Event ${name}`, arguments);
}
render() {
return (
<div className="app">
<p>{this.state.qs.queryIndex} {this.state.qs.queryName}</p>
<button type="button" onClick={this.changeQuery.bind(this)}>Change Query</button>
<button type="button" onClick={this.changeSpecies.bind(this)}>Change Species</button>
<div style={{position: 'relative'}}>
<Vis taxonomy={this.state.qs.taxonomy}
selectedTaxa={this.state.selectedTaxa}
onSelection={(s)=>this.setState({selection: s})}
onHighlight={(h)=>this.setState({highlight: h})}
/>
{this.renderHighlight()}
</div>
{this.renderSelectedTaxa()}
</div>
);
}
renderHighlight() {
if (this.state.highlight) {
return <Highlight {...this.state} />
}
}
renderSelectedTaxa() {
if (this.state.highlight) {
const Selection = ({selection})=><li>{selection.name}</li>;
const state = [];
if (this.state.highlight) {
state.push(<Selection key="highlight"
selection={this.state.highlight}/>)
}
return (
<ul>
{state}
</ul>
)
}
}
}
App.propTypes = {
taxonomy: React.PropTypes.object.isRequired,
selectedTaxa: React.PropTypes.object.isRequired,
exampleQueries: React.PropTypes.array.isRequired,
exampleResults: React.PropTypes.array.isRequired
};