-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathAnatomogramDemo.js
More file actions
161 lines (135 loc) · 5.53 KB
/
AnatomogramDemo.js
File metadata and controls
161 lines (135 loc) · 5.53 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
import React from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types'
import AnatomogramContainer from '../src/index'
import svgsMetadata from '../src/json/svgsMetadata.json'
import {xor} from 'lodash'
const unique = (value, index, self) => self.indexOf(value) === index
const capitalizeFirstLetter = (str) => str.charAt(0).toUpperCase() + str.slice(1)
const allSpecies =
svgsMetadata
.map((svgMetadata) => svgMetadata.species)
.filter(unique)
.sort()
const getAllIds = (species) =>
svgsMetadata
.filter((svgMetadata) => svgMetadata.species === species)
.reduce((acc, svgMetadata) => acc.concat(svgMetadata.ids), [])
.filter(unique)
.sort()
class AnatomogramDemo extends React.Component {
constructor(props) {
super(props)
const selectedSpecies = allSpecies.includes(`homo_sapiens`) ? `homo_sapiens` : allSpecies[0]
this.state = {
selectedSpecies: selectedSpecies,
allIds: getAllIds(selectedSpecies),
showIds: [],
highlightIds: [],
selectIds: []
}
this._handleSelectOnChange = this._handleSelectOnChange.bind(this)
this._handleCheckboxOnChange = this._handleCheckboxOnChange.bind(this)
this._handleOnClick = this._handleOnClick.bind(this)
this._addRemoveFromSelectIds = this._addRemoveFromSelectIds.bind(this)
}
_handleSelectOnChange(event) {
this.setState({
selectedSpecies: event.target.value,
allIds: getAllIds(event.target.value)
})
}
_handleCheckboxOnChange(event, stateField) {
const newShowIds =
event.target.checked ?
this.state[stateField].concat(event.target.value) :
this.state[stateField].filter((id) => event.target.value !== id)
this.setState({
[stateField]: newShowIds
})
}
_handleOnClick(allOrNone, stateField) {
this.setState({
[stateField]: allOrNone ? Array.from(this.state.allIds) : []
})
}
_addRemoveFromSelectIds(ids) {
this.setState({
selectIds: xor(this.state.selectIds, ids)
})
}
render() {
return (
<div className={`row`}>
<div className={`row`}>
<div className={`small-3 small-centered columns`}>
<select value={this.state.selectedSpecies} onChange={this._handleSelectOnChange}>
{[...allSpecies, `foobar`].map((species) => <option key={species} value={species}>{capitalizeFirstLetter(species.replace(`_`, ` `))}</option>)}
</select>
</div>
</div>
<div className={`row`}>
<div className={`small-4 columns`} id={`anatomogramContainer`}>
<AnatomogramContainer
{...this.props}
species={this.state.selectedSpecies}
showIds={this.state.showIds}
highlightIds={this.state.highlightIds}
selectIds={this.state.selectIds}
onClick={this._addRemoveFromSelectIds} />
</div>
<div className={`small-8 columns`}>
<div className={`row`}>
<div className={`small-4 columns`}>
<button className={`button`} onClick={() => {this._handleOnClick(true, `showIds`)}}>Show all</button>
<button className={`button`} onClick={() => {this._handleOnClick(false, `showIds`)}}>Hide all</button>
</div>
<div className={`small-4 columns`}>
<button className={`button`} onClick={() => {this._handleOnClick(true, `highlightIds`)}}>Highlight all</button>
<button className={`button`} onClick={() => {this._handleOnClick(false, `highlightIds`)}}>Unhighlight all</button>
</div>
<div className={`small-4 columns`}>
<button className={`button`} onClick={() => {this._handleOnClick(true, `selectIds`)}}>Select all</button>
<button className={`button`} onClick={() => {this._handleOnClick(false, `selectIds`)}}>Unselect all</button>
</div>
</div>
<div className={`row column`}>
<p>
Use the first checkbox to show the tissue, the second to highlight it, and the third to select it.
Hover over a tissue to display its name. Click on it to select it.
</p>
</div>
<div className={`row column`}>
{Array.from(this.state.allIds).sort().map((id) =>
<div key={id} style={{display: `inline-block`}}>
<input
type={`checkbox`}
name={`showIds`} value={id}
onChange={(e) => {this._handleCheckboxOnChange(e, `showIds`)}}
checked={this.state.showIds.includes(id)}/>
<input
type={`checkbox`}
name={`highlightIds`} value={id}
onChange={(e) => {this._handleCheckboxOnChange(e, `highlightIds`)}}
checked={this.state.highlightIds.includes(id)}/>
<input type={`checkbox`}
name={`selectIds`} value={id}
onChange={(e) => {this._handleCheckboxOnChange(e, `selectIds`)}}
checked={this.state.selectIds.includes(id)}/>
<label>{id}</label>
</div>)}
</div>
</div>
</div>
</div>
)
}
}
AnatomogramDemo.propTypes = {
atlasUrl: PropTypes.string,
pathToResources: PropTypes.string
}
const render = function (options, target) {
ReactDOM.render(<AnatomogramDemo {...options}/>, document.getElementById(target))
}
export {AnatomogramDemo as default, render}