5
5
* This source code is licensed under the license found in the LICENSE file in
6
6
* the root directory of this source tree.
7
7
*/
8
- import * as Filters from 'lib/Filters' ;
9
- import Button from 'components/Button/Button.react' ;
10
- import Filter from 'components/Filter/Filter.react' ;
11
- import FilterRow from 'components/BrowserFilter/FilterRow.react' ;
12
- import Icon from 'components/Icon/Icon.react' ;
13
- import Popover from 'components/Popover/Popover.react' ;
14
- import Position from 'lib/Position' ;
15
- import React from 'react' ;
16
- import styles from 'components/BrowserFilter/BrowserFilter.scss' ;
8
+ import * as Filters from 'lib/Filters' ;
9
+ import Button from 'components/Button/Button.react' ;
10
+ import Filter from 'components/Filter/Filter.react' ;
11
+ import FilterRow from 'components/BrowserFilter/FilterRow.react' ;
12
+ import Icon from 'components/Icon/Icon.react' ;
13
+ import Popover from 'components/Popover/Popover.react' ;
14
+ import Field from 'components/Field/Field.react' ;
15
+ import TextInput from 'components/TextInput/TextInput.react' ;
16
+ import Label from 'components/Label/Label.react' ;
17
+ import Position from 'lib/Position' ;
18
+ import React from 'react' ;
19
+ import styles from 'components/BrowserFilter/BrowserFilter.scss' ;
17
20
import { List , Map } from 'immutable' ;
18
21
19
22
const POPOVER_CONTENT_ID = 'browserFilterPopover' ;
@@ -26,7 +29,9 @@ export default class BrowserFilter extends React.Component {
26
29
open : false ,
27
30
editMode : true ,
28
31
filters : new List ( ) ,
29
- blacklistedFilters : Filters . BLACKLISTED_FILTERS . concat ( props . blacklistedFilters )
32
+ confirmName : false ,
33
+ name : '' ,
34
+ blacklistedFilters : Filters . BLACKLISTED_FILTERS . concat ( props . blacklistedFilters ) ,
30
35
} ;
31
36
this . toggle = this . toggle . bind ( this ) ;
32
37
this . wrapRef = React . createRef ( ) ;
@@ -43,13 +48,13 @@ export default class BrowserFilter extends React.Component {
43
48
if ( this . props . filters . size === 0 ) {
44
49
let available = Filters . availableFilters ( this . props . schema , null , this . state . blacklistedFilters ) ;
45
50
let field = Object . keys ( available ) [ 0 ] ;
46
- filters = new List ( [
47
- new Map ( { field : field , constraint : available [ field ] [ 0 ] } )
48
- ] ) ;
51
+ filters = new List ( [ new Map ( { field : field , constraint : available [ field ] [ 0 ] } ) ] ) ;
49
52
}
50
- this . setState ( prevState => ( {
53
+ this . setState ( ( prevState ) => ( {
51
54
open : ! prevState . open ,
52
55
filters : filters ,
56
+ name : '' ,
57
+ confirmName : false ,
53
58
editMode : this . props . filters . size === 0
54
59
} ) ) ;
55
60
this . props . setCurrent ( null ) ;
@@ -71,7 +76,7 @@ export default class BrowserFilter extends React.Component {
71
76
}
72
77
73
78
apply ( ) {
74
- let formatted = this . state . filters . map ( filter => {
79
+ let formatted = this . state . filters . map ( ( filter ) => {
75
80
// TODO: type is unused?
76
81
/*let type = this.props.schema[filter.get('field')].type;
77
82
if (Filters.Constraints[filter.get('constraint')].hasOwnProperty('field')) {
@@ -82,13 +87,25 @@ export default class BrowserFilter extends React.Component {
82
87
// remove compareTo for constraints which are not comparable
83
88
let isComparable = Filters . Constraints [ filter . get ( 'constraint' ) ] . comparable ;
84
89
if ( ! isComparable ) {
85
- return filter . delete ( 'compareTo' )
90
+ return filter . delete ( 'compareTo' ) ;
86
91
}
87
92
return filter ;
88
93
} ) ;
89
94
this . props . onChange ( formatted ) ;
90
95
}
91
96
97
+ save ( ) {
98
+ let formatted = this . state . filters . map ( ( filter ) => {
99
+ let isComparable = Filters . Constraints [ filter . get ( 'constraint' ) ] . comparable ;
100
+ if ( ! isComparable ) {
101
+ return filter . delete ( 'compareTo' ) ;
102
+ }
103
+ return filter ;
104
+ } ) ;
105
+ this . props . onSaveFilter ( formatted , this . state . name ) ;
106
+ this . toggle ( ) ;
107
+ }
108
+
92
109
render ( ) {
93
110
let popover = null ;
94
111
let buttonStyle = [ styles . entry ] ;
@@ -102,49 +119,45 @@ export default class BrowserFilter extends React.Component {
102
119
if ( this . props . filters . size ) {
103
120
popoverStyle . push ( styles . active ) ;
104
121
}
105
- let available = Filters . availableFilters (
106
- this . props . schema ,
107
- this . state . filters
108
- ) ;
122
+ let available = Filters . availableFilters ( this . props . schema , this . state . filters ) ;
109
123
popover = (
110
124
< Popover fixed = { true } position = { position } onExternalClick = { this . toggle } contentId = { POPOVER_CONTENT_ID } >
111
125
< div className = { popoverStyle . join ( ' ' ) } onClick = { ( ) => this . props . setCurrent ( null ) } id = { POPOVER_CONTENT_ID } >
112
- < div onClick = { this . toggle } style = { { cursor : 'pointer' , width : node . clientWidth , height : node . clientHeight } } > </ div >
126
+ < div
127
+ onClick = { this . toggle }
128
+ style = { {
129
+ cursor : 'pointer' ,
130
+ width : node . clientWidth ,
131
+ height : node . clientHeight ,
132
+ } }
133
+ > </ div >
113
134
< div className = { styles . body } >
114
135
< Filter
115
136
className = { this . props . className }
116
137
blacklist = { this . state . blacklistedFilters }
117
138
schema = { this . props . schema }
118
139
filters = { this . state . filters }
119
- onChange = { filters => this . setState ( { filters : filters } ) }
140
+ onChange = { ( filters ) => this . setState ( { filters : filters } ) }
120
141
onSearch = { this . apply . bind ( this ) }
121
142
renderRow = { props => (
122
143
< FilterRow { ...props } active = { this . props . filters . size > 0 } editMode = { this . state . editMode } parentContentId = { POPOVER_CONTENT_ID } />
123
144
) }
124
145
/>
125
- < div className = { styles . footer } >
126
- < Button
127
- color = "white"
128
- value = "Clear all"
129
- disabled = { this . state . filters . size === 0 }
130
- width = "120px"
131
- onClick = { this . clear . bind ( this ) }
132
- />
133
- < Button
134
- color = "white"
135
- value = "Add filter"
136
- disabled = { Object . keys ( available ) . length === 0 }
137
- width = "120px"
138
- onClick = { this . addRow . bind ( this ) }
139
- />
140
- < Button
141
- color = "white"
142
- primary = { true }
143
- value = "Apply these filters"
144
- width = "245px"
145
- onClick = { this . apply . bind ( this ) }
146
- />
147
- </ div >
146
+ { this . state . confirmName && < Field label = { < Label text = "Filter view name" /> } input = { < TextInput placeholder = "Give it a good name..." value = { this . state . name } onChange = { ( name ) => this . setState ( { name } ) } /> } /> }
147
+ { this . state . confirmName && (
148
+ < div className = { styles . footer } >
149
+ < Button color = "white" value = "Back" width = "120px" onClick = { ( ) => this . setState ( { confirmName : false } ) } />
150
+ < Button color = "white" value = "Confirm" primary = { true } width = "120px" onClick = { ( ) => this . save ( ) } />
151
+ </ div >
152
+ ) }
153
+ { ! this . state . confirmName && (
154
+ < div className = { styles . footer } >
155
+ < Button color = "white" value = "Save" width = "120px" onClick = { ( ) => this . setState ( { confirmName : true } ) } />
156
+ < Button color = "white" value = "Clear" disabled = { this . state . filters . size === 0 } width = "120px" onClick = { ( ) => this . clear ( ) } />
157
+ < Button color = "white" value = "Add" disabled = { Object . keys ( available ) . length === 0 } width = "120px" onClick = { ( ) => this . addRow ( ) } />
158
+ < Button color = "white" primary = { true } value = "Apply" width = "120px" onClick = { ( ) => this . apply ( ) } />
159
+ </ div >
160
+ ) }
148
161
</ div >
149
162
</ div >
150
163
</ Popover >
0 commit comments