Added infobox for old style filters to allow you to switch to the filter editor.

This commit is contained in:
orangemug 2020-02-22 11:54:59 +00:00
parent 52e8b21b3d
commit a693f6db4e
2 changed files with 84 additions and 39 deletions

View file

@ -13,6 +13,19 @@ import SpecDoc from '../inputs/SpecDoc'
import ExpressionProperty from '../fields/_ExpressionProperty'; import ExpressionProperty from '../fields/_ExpressionProperty';
function combiningFilter (props) {
let filter = props.filter || ['all'];
let combiningOp = filter[0];
let filters = filter.slice(1);
if(combiningFilterOps.indexOf(combiningOp) < 0) {
combiningOp = 'all';
filters = [filter.slice(0)];
}
return [combiningOp, ...filters];
}
function migrateFilter (filter) { function migrateFilter (filter) {
return migrate(createStyleFromFilter(filter)).layers[0].filter; return migrate(createStyleFromFilter(filter)).layers[0].filter;
@ -92,39 +105,25 @@ export default class CombiningFilterEditor extends React.Component {
super(); super();
this.state = { this.state = {
showDoc: false, showDoc: false,
isSimpleFilter: checkIfSimpleFilter(this.combiningFilter(props)), displaySimpleFilter: checkIfSimpleFilter(combiningFilter(props)),
}; };
} }
// Convert filter to combining filter // Convert filter to combining filter
combiningFilter(props=this.props) {
let filter = props.filter || ['all']
let combiningOp = filter[0]
let filters = filter.slice(1)
if(combiningFilterOps.indexOf(combiningOp) < 0) {
combiningOp = 'all'
filters = [filter.slice(0)]
}
return [combiningOp, ...filters]
}
onFilterPartChanged(filterIdx, newPart) { onFilterPartChanged(filterIdx, newPart) {
const newFilter = this.combiningFilter().slice(0) const newFilter = combiningFilter(this.props).slice(0)
newFilter[filterIdx] = newPart newFilter[filterIdx] = newPart
this.props.onChange(newFilter) this.props.onChange(newFilter)
} }
deleteFilterItem(filterIdx) { deleteFilterItem(filterIdx) {
const newFilter = this.combiningFilter().slice(0) const newFilter = combiningFilter(this.props).slice(0)
newFilter.splice(filterIdx + 1, 1) newFilter.splice(filterIdx + 1, 1)
this.props.onChange(newFilter) this.props.onChange(newFilter)
} }
addFilterItem = () => { addFilterItem = () => {
const newFilterItem = this.combiningFilter().slice(0) const newFilterItem = combiningFilter(this.props).slice(0)
newFilterItem.push(['==', 'name', '']) newFilterItem.push(['==', 'name', ''])
this.props.onChange(newFilterItem) this.props.onChange(newFilterItem)
} }
@ -135,38 +134,52 @@ export default class CombiningFilterEditor extends React.Component {
}); });
} }
makeFilter = () => {
this.setState({
displaySimpleFilter: true,
})
}
makeExpression = () => { makeExpression = () => {
let filter = this.combiningFilter(); let filter = combiningFilter(this.props);
this.props.onChange(migrateFilter(filter)); this.props.onChange(migrateFilter(filter));
this.setState({ this.setState({
isSimpleFilter: false, displaySimpleFilter: false,
}) })
} }
static getDerivedStateFromProps (props, currentState) { static getDerivedStateFromProps (props, currentState) {
const {filter} = props; const {filter} = props;
const isSimpleFilter = checkIfSimpleFilter(props.filter); const displaySimpleFilter = checkIfSimpleFilter(combiningFilter(props));
// Upgrade but never downgrade // Upgrade but never downgrade
if (!isSimpleFilter && currentState.isSimpleFilter === true) { if (!displaySimpleFilter && currentState.displaySimpleFilter === true) {
return { return {
isSimpleFilter: false, displaySimpleFilter: false,
valueIsSimpleFilter: false,
}; };
} }
else if (displaySimpleFilter && currentState.displaySimpleFilter === false) {
return {
valueIsSimpleFilter: true,
}
}
else { else {
return {}; return {
valueIsSimpleFilter: false,
};
} }
} }
render() { render() {
const {errors} = this.props; const {errors} = this.props;
const {isSimpleFilter} = this.state; const {displaySimpleFilter} = this.state;
const fieldSpec={ const fieldSpec={
doc: latest.layer.filter.doc + " Combine multiple filters together by using a compound filter." doc: latest.layer.filter.doc + " Combine multiple filters together by using a compound filter."
}; };
const defaultFilter = ["all"]; const defaultFilter = ["all"];
const isNestedCombiningFilter = isSimpleFilter && hasNestedCombiningFilter(this.combiningFilter()); const isNestedCombiningFilter = displaySimpleFilter && hasNestedCombiningFilter(combiningFilter(this.props));
if (isNestedCombiningFilter) { if (isNestedCombiningFilter) {
return <div className="maputnik-filter-editor-unsupported"> return <div className="maputnik-filter-editor-unsupported">
@ -183,8 +196,8 @@ export default class CombiningFilterEditor extends React.Component {
</Button> </Button>
</div> </div>
} }
else if (isSimpleFilter) { else if (displaySimpleFilter) {
const filter = this.combiningFilter(); const filter = combiningFilter(this.props);
let combiningOp = filter[0]; let combiningOp = filter[0];
let filters = filter.slice(1) let filters = filter.slice(1)
@ -269,9 +282,10 @@ export default class CombiningFilterEditor extends React.Component {
const error = errorMessage ? {message: errorMessage} : null; const error = errorMessage ? {message: errorMessage} : null;
return ( return (
<>
<ExpressionProperty <ExpressionProperty
onDelete={() => { onDelete={() => {
this.setState({isSimpleFilter: true}); this.setState({displaySimpleFilter: true});
this.props.onChange(defaultFilter); this.props.onChange(defaultFilter);
}} }}
fieldName="filter-compound-filter" fieldName="filter-compound-filter"
@ -280,6 +294,18 @@ export default class CombiningFilterEditor extends React.Component {
error={error} error={error}
onChange={this.props.onChange} onChange={this.props.onChange}
/> />
{this.state.valueIsSimpleFilter &&
<div className="maputnik-expr-infobox">
You've entered a old style filter,{' '}
<button
onClick={this.makeFilter}
className="maputnik-expr-infobox__button"
>
switch to filter editor
</button>
</div>
}
</>
); );
} }
} }

View file

@ -63,3 +63,22 @@
text-align: right; text-align: right;
} }
} }
.maputnik-expr-infobox {
font-size: $font-size-6;
background: $color-midgray;
padding: $margin-2;
border-radius: 2px;
border-top-right-radius: 0px;
border-top-left-radius: 0px;
color: $color-white;
}
.maputnik-expr-infobox__button {
unset: all;
background: none;
border: none;
padding: 0;
text-decoration: underline;
color: currentColor;
cursor: pointer;
}