-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathiSamples_rangeFacet.js
More file actions
168 lines (146 loc) · 5.21 KB
/
iSamples_rangeFacet.js
File metadata and controls
168 lines (146 loc) · 5.21 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
162
163
164
165
166
167
168
import PropTypes from 'prop-types';
import React from "react";
import cx from "classnames";
import {
defaultComponentPack
} from "solr-faceted-search-react";
import { MIN_YEAR, MAX_YEAR } from "fields.js";
class iSamples_RangeFacet extends React.Component {
constructor(props) {
super(props);
this.state = {
value: props.value,
// New state values for min and max range for slider UI.
minValue: props.minValue,
maxValue: props.maxValue,
};
}
componentWillReceiveProps(nextProps) {
this.setState({ value: nextProps.value });
}
// This function converts the faceted data to range values for the slider. Since we have
// changed the functionality to no longer consult the facets when rendering, it is unneeded.
// facetsToRange() {
// const {facets} = this.props;
// if (facets == null || facets.length === 0) {
// return this.state;
// }
// return facets
// .filter((facet, i) => i % 2 === 0)
// .map((v) => parseInt(v))
// .sort((a, b) => a > b ? 1 : -1)
// .filter((a, i, me) => i === 0 || i === me.length - 1);
// }
onRangeChange(range) {
// Original line consulted the faceted data to figure out the range value from selection.
// const bounds = this.facetsToRange();
const bounds = [this.state.minValue, this.state.maxValue];
const lowerBound = bounds[0];
const upperBound = bounds[1];
const realRange = upperBound - lowerBound;
const newState = {
value: [
Math.floor(range.lowerLimit * realRange) + lowerBound,
Math.ceil(range.upperLimit * realRange) + lowerBound
]
};
if (range.refresh) {
this.props.onChange(this.props.field, newState.value);
} else {
this.setState(newState);
}
}
getPercentage(range, value) {
let lowerBound = range[0];
let upperBound = range[1];
let realRange = upperBound - lowerBound;
let atRange = value - lowerBound;
return atRange / realRange;
}
toggleExpand(ev) {
if (ev.target.className.indexOf("clear-button") < 0) {
this.props.onSetCollapse(this.props.field, !(this.props.collapse || false));
}
}
getCount(rangeValues, rangeCounts){
const newCounts = {};
for (let i = MIN_YEAR; i <= MAX_YEAR; i++){
let idx = rangeValues.indexOf(i);
if ( idx !== -1) {
newCounts[i] = rangeCounts[idx];
}
else {
newCounts[i] = 0;
}
}
return newCounts;
}
getBarData(rangeValues, rangeCounts){
const data = this.getCount(rangeValues, rangeCounts);
const newBarData = [];
if (rangeCounts.length > 0) {
const ranges = Object.keys(data); // year (keys) of objects
ranges.forEach((range) => newBarData.push(data[range]));
}
return newBarData;
}
render() {
const { label, facets, field, bootstrapCss, collapse } = this.props;
const { value } = this.state;
// Original line was:
// const range = this.facetsToRange();
// Instead, make our UI range always conform to min/max values rather than the returned facets.
const range = [this.state.minValue, this.state.maxValue];
const filterRange = value.length > 0 ? value : range;
const rangeCounts = facets.filter((facet, i) => i % 2 === 1);
const rangeValues = facets.filter((facet, i) => i % 2 === 0).map(range => parseInt(range.substring(0,4)));
return (
<li className={cx("range-facet", { "list-group-item": bootstrapCss })} id={`solr-range-facet-${field}`}>
<header onClick={this.toggleExpand.bind(this)}>
<button style={{ display: this.state.expanded ? "block" : "none" }}
className={cx("clear-button", {
"btn": bootstrapCss,
"btn-default": bootstrapCss,
"btn-xs": bootstrapCss,
"pull-right": bootstrapCss
}
)}
onClick={() => this.props.onChange(field, [])}>
clear
</button>
<h5>
{bootstrapCss ? (<span>
<span className={cx("glyphicon", {
"glyphicon-collapse-down": !collapse,
"glyphicon-collapse-up": collapse
})} />{" "}
</span>) : null}
{label}
</h5>
</header>
<div style={{ display: collapse ? "none" : "block" }}>
<defaultComponentPack.searchFields.barChart data = {this.getBarData(rangeValues, rangeCounts)} minYear = {MIN_YEAR} />
<defaultComponentPack.searchFields.rangeSlider lowerLimit={this.getPercentage(range, filterRange[0])} onChange={this.onRangeChange.bind(this)}upperLimit={this.getPercentage(range, filterRange[1])} />
<label>{filterRange[0]}</label>
<label className={cx({ "pull-right": bootstrapCss })}>{filterRange[1]}</label>
</div>
</li>
);
}
}
iSamples_RangeFacet.defaultProps = {
value: []
};
iSamples_RangeFacet.propTypes = {
bootstrapCss: PropTypes.bool,
collapse: PropTypes.bool,
facets: PropTypes.array,
field: PropTypes.string.isRequired,
label: PropTypes.string,
onChange: PropTypes.func,
onSetCollapse: PropTypes.func,
value: PropTypes.array,
minValue: PropTypes.number.isRequired,
maxValue: PropTypes.number.isRequired
};
export default iSamples_RangeFacet;