forked from parse-community/parse-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowserFilter.react.js
More file actions
169 lines (156 loc) · 5.51 KB
/
BrowserFilter.react.js
File metadata and controls
169 lines (156 loc) · 5.51 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
169
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import * as Filters from 'lib/Filters';
import Button from 'components/Button/Button.react';
import Filter from 'components/Filter/Filter.react';
import FilterRow from 'components/BrowserFilter/FilterRow.react';
import Icon from 'components/Icon/Icon.react';
import Popover from 'components/Popover/Popover.react';
import Position from 'lib/Position';
import React from 'react';
import ReactDOM from 'react-dom';
import styles from 'components/BrowserFilter/BrowserFilter.scss';
import { List, Map } from 'immutable';
const POPOVER_CONTENT_ID = 'browserFilterPopover';
export default class BrowserFilter extends React.Component {
constructor(props) {
super(props);
this.state = {
open: false,
filters: new List(),
blacklistedFilters: Filters.BLACKLISTED_FILTERS.concat(props.blacklistedFilters)
};
this.toggle = this.toggle.bind(this);
}
componentDidMount() {
this.node = ReactDOM.findDOMNode(this);
}
componentWillReceiveProps(props) {
if (props.className !== this.props.className) {
this.setState({ open: false });
}
}
toggle() {
let filters = this.props.filters;
if (this.props.filters.size === 0) {
let available = Filters.availableFilters(this.props.schema, null, this.state.blacklistedFilters);
let field = Object.keys(available)[0];
filters = new List([
new Map({ field: field, constraint: available[field][0] })
]);
}
this.setState(prevState => ({
open: !prevState.open,
filters: filters
}));
this.props.setCurrent(null);
}
addRow() {
let available = Filters.availableFilters(this.props.schema, this.state.filters, this.state.blacklistedFilters);
let field = Object.keys(available)[0];
this.setState(({ filters }) => ({
filters: filters.push(
new Map({ field: field, constraint: available[field][0] })
)
}));
}
clear() {
this.props.onChange(new Map());
}
apply() {
let formatted = this.state.filters.map(filter => {
// TODO: type is unused?
/*let type = this.props.schema[filter.get('field')].type;
if (Filters.Constraints[filter.get('constraint')].hasOwnProperty('field')) {
type = Filters.Constraints[filter.get('constraint')].field;
}*/
// since we are preserving previous compareTo value
// remove compareTo for constraints which are not comparable
let isComparable = Filters.Constraints[filter.get('constraint')].comparable;
if (!isComparable) {
return filter.delete('compareTo')
}
return filter;
});
this.props.onChange(formatted);
}
render() {
let popover = null;
let buttonStyle = [styles.entry];
if (this.state.open) {
let position = Position.inDocument(this.node);
let popoverStyle = [styles.popover];
buttonStyle.push(styles.title);
if (this.props.filters.size) {
popoverStyle.push(styles.active);
}
let available = Filters.availableFilters(
this.props.schema,
this.state.filters
);
popover = (
<Popover fixed={true} position={position} onExternalClick={this.toggle} contentId={POPOVER_CONTENT_ID}>
<div className={popoverStyle.join(' ')} onClick={() => this.props.setCurrent(null)} id={POPOVER_CONTENT_ID}>
<div onClick={this.toggle} style={{ cursor: 'pointer', width: this.node.clientWidth, height: this.node.clientHeight }}></div>
<div className={styles.body}>
<Filter
className={this.props.className}
blacklist={this.state.blacklistedFilters}
schema={this.props.schema}
filters={this.state.filters}
onChange={filters => this.setState({ filters: filters })}
onSearch={this.apply.bind(this)}
renderRow={props => (
<FilterRow {...props} active={this.props.filters.size > 0} parentContentId={POPOVER_CONTENT_ID} />
)}
/>
<div className={styles.footer}>
<Button
color="white"
value="Clear all"
disabled={this.state.filters.size === 0}
width="120px"
onClick={this.clear.bind(this)}
/>
<Button
color="white"
value="Add filter"
disabled={Object.keys(available).length === 0}
width="120px"
onClick={this.addRow.bind(this)}
/>
<Button
color="white"
primary={true}
value="Apply these filters"
width="245px"
onClick={this.apply.bind(this)}
/>
</div>
</div>
</div>
</Popover>
);
}
if (this.props.filters.size) {
buttonStyle.push(styles.active);
}
if (this.props.disabled) {
buttonStyle.push(styles.disabled);
}
return (
<div className={styles.wrap}>
<div className={buttonStyle.join(' ')} onClick={this.toggle}>
<Icon name="filter-solid" width={14} height={14} />
<span>{this.props.filters.size ? 'Filtered' : 'Filter'}</span>
</div>
{popover}
</div>
);
}
}