forked from geosolutions-it/MapStore2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CSWFilters.jsx
129 lines (121 loc) · 3.87 KB
/
CSWFilters.jsx
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
import React, { useState, useEffect } from "react";
import {
Col,
ControlLabel,
FormGroup,
Glyphicon,
Tooltip,
HelpBlock
} from "react-bootstrap";
import { Controlled as CodeMirror } from "react-codemirror2";
import "codemirror/lib/codemirror.css";
import "codemirror/theme/material.css";
import "codemirror/mode/xml/xml";
import "codemirror/addon/lint/lint";
import "codemirror/addon/display/autorefresh";
import template from "lodash/template";
import isEqual from "lodash/isEqual";
import { cswGetRecordsXml } from "../../../../api/CSW";
import OverlayTrigger from "../../../misc/OverlayTrigger";
import Message from "../../../I18N/Message";
const options = {
mode: "xml",
theme: "material",
lineNumbers: true,
lineWrapping: true,
autoRefresh: true,
indentUnit: 2,
tabSize: 2
};
const FilterInfo = ({ glyph = "info-sign", className = "", tooltip }) => (
<OverlayTrigger placement={"bottom"} overlay={tooltip}>
<Glyphicon
style={{ marginLeft: 4 }}
glyph={glyph}
className={className}
/>
</OverlayTrigger>
);
const tooltip = _type => {
let msgId = `catalog.filter.${_type}.info`;
if (_type === "error") msgId = `catalog.filter.error`;
return (
<Tooltip id={"filter"}>
<Message msgId={msgId} />
</Tooltip>
);
};
const renderError = (
<FilterInfo
tooltip={tooltip("error")}
glyph={"exclamation-mark"}
className={"text-danger"}
/>
);
const renderHelpText = (
<HelpBlock style={{ fontSize: 12 }}>
<Message msgId={`catalog.filter.dynamic.helpText`} />
</HelpBlock>
);
const FilterCode = ({ type, code, setCode, error }) => {
const filterProp = `${type}Filter`;
return (
<FormGroup>
<Col xs={4}>
<ControlLabel>
<Message msgId={`catalog.filter.${type}.label`} />
</ControlLabel>
<FilterInfo tooltip={tooltip(type)} />
{error[type] && renderError}
</Col>
<Col xs={8} style={{ marginBottom: 5 }}>
<CodeMirror
value={code[filterProp]}
options={options}
onBeforeChange={(_, __, value) => {
setCode({ ...code, [filterProp]: value });
}}
/>
{type === 'dynamic' && renderHelpText}
</Col>
</FormGroup>
);
};
export default ({
onChangeServiceProperty = () => {},
filter: { staticFilter, dynamicFilter } = {}
} = {}) => {
const [error, setError] = useState({});
const [code, setCode] = useState({ staticFilter, dynamicFilter });
const cmProps = { code, setCode, error };
const isValid = value => {
const _filter = template(cswGetRecordsXml)({
filterXml: value,
startPosition: 1,
maxRecords: 4
});
return !new DOMParser()
.parseFromString(_filter, "application/xml")
?.getElementsByTagName("parsererror")?.length;
};
useEffect(() => {
const validFt = isValid(code.staticFilter);
validFt &&
!isEqual(code.staticFilter, staticFilter) &&
onChangeServiceProperty("filter.staticFilter", code.staticFilter);
setError({ ...error, "static": !validFt });
}, [code.staticFilter]);
useEffect(() => {
const validFt = isValid(code.dynamicFilter);
validFt &&
!isEqual(code.dynamicFilter, dynamicFilter) &&
onChangeServiceProperty("filter.dynamicFilter", code.dynamicFilter);
setError({ ...error, dynamic: !validFt });
}, [code.dynamicFilter]);
return (
<div className={"catalog-csw-filters"}>
<FilterCode type={"static"} {...cmProps} />
<FilterCode type={"dynamic"} {...cmProps} />
</div>
);
};