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 | 1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
11×
22×
11×
1×
1×
7×
7×
7×
7×
7×
7×
7×
7×
7×
1×
7×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
6×
6×
6×
4×
2×
4×
6×
2×
1×
1×
1×
2×
6×
6×
1×
2×
1×
1×
1×
1×
1×
37×
74×
37×
1×
1×
| import React from 'react';
import PropTypes from 'prop-types';
import { Button, Row, Col } from 'react-bootstrap';
import Filter from './Filter';
import { t } from '../../../locales';
const $ = window.$ = require('jquery');
const propTypes = {
name: PropTypes.string,
onChange: PropTypes.func,
value: PropTypes.array,
datasource: PropTypes.object,
};
const defaultProps = {
onChange: () => {},
value: [],
};
export default class FilterControl extends React.Component {
constructor(props) {
super(props);
const initialFilters = props.value.map(() => ({
valuesLoading: false,
valueChoices: [],
}));
this.state = {
filters: initialFilters,
activeRequest: null,
};
}
componentDidMount() {
this.state.filters.forEach((filter, index) => this.fetchFilterValues(index));
}
fetchFilterValues(index, column) {
const datasource = this.props.datasource;
const col = column || this.props.value[index].col;
const having = this.props.name === 'having_filters';
if (Ecol && this.props.datasource && this.props.datasource.filter_select && !having) {
this.setState((prevState) => {
const newStateFilters = Object.assign([], prevState.filters);
newStateFilters[index].valuesLoading = true;
return { filters: newStateFilters };
});
// if there is an outstanding request to fetch values, cancel it.
if (this.state.activeRequest) {
this.state.activeRequest.abort();
}
this.setState({
activeRequest: $.ajax({
type: 'GET',
url: `/superset/filter/${datasource.type}/${datasource.id}/${col}/`,
success: (data) => {
this.setState((prevState) => {
const newStateFilters = Object.assign([], prevState.filters);
newStateFilters[index] = { valuesLoading: false, valueChoices: data };
return { filters: newStateFilters, activeRequest: null };
});
},
}),
});
}
}
addFilter() {
const newFilters = Object.assign([], this.props.value);
const col = this.props.datasource && this.props.datasource.filterable_cols.length > 0 ?
this.props.datasource.filterable_cols[0][0] :
null;
newFilters.push({
col,
op: 'in',
val: this.props.datasource.filter_select ? [] : '',
});
this.props.onChange(newFilters);
const nextIndex = this.state.filters.length;
this.setState((prevState) => {
const newStateFilters = Object.assign([], prevState.filters);
newStateFilters.push({ valuesLoading: false, valueChoices: [] });
return { filters: newStateFilters };
});
this.fetchFilterValues(nextIndex, col);
}
changeFilter(index, control, value) {
const newFilters = Object.assign([], this.props.value);
const modifiedFilter = Object.assign({}, newFilters[index]);
if (typeof control === 'string') {
modifiedFilter[control] = value;
} else {
control.forEach((c, i) => {
modifiedFilter[c] = value[i];
});
}
// Clear selected values and refresh upon column change
if (control === 'col') {
if (modifiedFilter.val.constructor === Array) {
modifiedFilter.val = [];
} else Eif (typeof modifiedFilter.val === 'string') {
modifiedFilter.val = '';
}
this.fetchFilterValues(index, value);
}
newFilters.splice(index, 1, modifiedFilter);
this.props.onChange(newFilters);
}
removeFilter(index) {
this.props.onChange(this.props.value.filter((f, i) => i !== index));
this.setState((prevState) => {
const newStateFilters = Object.assign([], prevState.filters);
newStateFilters.splice(index, 1);
return { filters: newStateFilters };
});
}
render() {
const filters = this.props.value.map((filter, i) => (
<div key={i}>
<Filter
having={this.props.name === 'having_filters'}
filter={filter}
datasource={this.props.datasource}
removeFilter={this.removeFilter.bind(this, i)}
changeFilter={this.changeFilter.bind(this, i)}
valuesLoading={this.state.filters[i].valuesLoading}
valueChoices={this.state.filters[i].valueChoices}
/>
</div>
));
return (
<div>
{filters}
<Row className="space-2">
<Col md={2}>
<Button
id="add-button"
bsSize="sm"
onClick={this.addFilter.bind(this)}
>
<i className="fa fa-plus" /> {t('Add Filter')}
</Button>
</Col>
</Row>
</div>
);
}
}
FilterControl.propTypes = propTypes;
FilterControl.defaultProps = defaultProps;
|