55 * This source code is licensed under the license found in the LICENSE file in
66 * the root directory of this source tree.
77 */
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' ;
1720import { List , Map } from 'immutable' ;
1821
1922const POPOVER_CONTENT_ID = 'browserFilterPopover' ;
@@ -26,7 +29,9 @@ export default class BrowserFilter extends React.Component {
2629 open : false ,
2730 editMode : true ,
2831 filters : new List ( ) ,
29- blacklistedFilters : Filters . BLACKLISTED_FILTERS . concat ( props . blacklistedFilters )
32+ confirmName : false ,
33+ name : '' ,
34+ blacklistedFilters : Filters . BLACKLISTED_FILTERS . concat ( props . blacklistedFilters ) ,
3035 } ;
3136 this . toggle = this . toggle . bind ( this ) ;
3237 this . wrapRef = React . createRef ( ) ;
@@ -43,13 +48,13 @@ export default class BrowserFilter extends React.Component {
4348 if ( this . props . filters . size === 0 ) {
4449 let available = Filters . availableFilters ( this . props . schema , null , this . state . blacklistedFilters ) ;
4550 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 ] } ) ] ) ;
4952 }
50- this . setState ( prevState => ( {
53+ this . setState ( ( prevState ) => ( {
5154 open : ! prevState . open ,
5255 filters : filters ,
56+ name : '' ,
57+ confirmName : false ,
5358 editMode : this . props . filters . size === 0
5459 } ) ) ;
5560 this . props . setCurrent ( null ) ;
@@ -71,7 +76,7 @@ export default class BrowserFilter extends React.Component {
7176 }
7277
7378 apply ( ) {
74- let formatted = this . state . filters . map ( filter => {
79+ let formatted = this . state . filters . map ( ( filter ) => {
7580 // TODO: type is unused?
7681 /*let type = this.props.schema[filter.get('field')].type;
7782 if (Filters.Constraints[filter.get('constraint')].hasOwnProperty('field')) {
@@ -82,13 +87,25 @@ export default class BrowserFilter extends React.Component {
8287 // remove compareTo for constraints which are not comparable
8388 let isComparable = Filters . Constraints [ filter . get ( 'constraint' ) ] . comparable ;
8489 if ( ! isComparable ) {
85- return filter . delete ( 'compareTo' )
90+ return filter . delete ( 'compareTo' ) ;
8691 }
8792 return filter ;
8893 } ) ;
8994 this . props . onChange ( formatted ) ;
9095 }
9196
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+
92109 render ( ) {
93110 let popover = null ;
94111 let buttonStyle = [ styles . entry ] ;
@@ -102,49 +119,45 @@ export default class BrowserFilter extends React.Component {
102119 if ( this . props . filters . size ) {
103120 popoverStyle . push ( styles . active ) ;
104121 }
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 ) ;
109123 popover = (
110124 < Popover fixed = { true } position = { position } onExternalClick = { this . toggle } contentId = { POPOVER_CONTENT_ID } >
111125 < 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 >
113134 < div className = { styles . body } >
114135 < Filter
115136 className = { this . props . className }
116137 blacklist = { this . state . blacklistedFilters }
117138 schema = { this . props . schema }
118139 filters = { this . state . filters }
119- onChange = { filters => this . setState ( { filters : filters } ) }
140+ onChange = { ( filters ) => this . setState ( { filters : filters } ) }
120141 onSearch = { this . apply . bind ( this ) }
121142 renderRow = { props => (
122143 < FilterRow { ...props } active = { this . props . filters . size > 0 } editMode = { this . state . editMode } parentContentId = { POPOVER_CONTENT_ID } />
123144 ) }
124145 />
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+ ) }
148161 </ div >
149162 </ div >
150163 </ Popover >
0 commit comments