Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved radius with unit of measure #3033

Merged
merged 1 commit into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
257 changes: 130 additions & 127 deletions web/client/components/mapcontrols/annotations/AnnotationsEditor.jsx

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ const draggableContainer = require('../../misc/enhancers/draggableContainer');
const Message = require('../../I18N/Message');
const {validateCoords, coordToArray} = require('../../../utils/AnnotationsUtils');
const CoordinatesRow = require('./CoordinatesRow');
const MeasureEditor = require('./MeasureEditor');

/**
* Geometry editor for annotation Features.
* @memberof components.annotations
* @class
* @prop {object[]} components the data used as value in the CoordinatesRow
* @prop {object} measureOptions options for the measure input
* @prop {function} onSetInvalidSelected if the form becomes invalid, this will be triggered
* @prop {function} onChange triggered on every coordinate change
* @prop {function} onChangeText triggered every text change
Expand All @@ -30,6 +32,7 @@ const CoordinatesRow = require('./CoordinatesRow');
class CoordinateEditor extends React.Component {
static propTypes = {
components: PropTypes.array,
measureOptions: PropTypes.object,
onSetInvalidSelected: PropTypes.func,
onChange: PropTypes.func,
onChangeRadius: PropTypes.func,
Expand All @@ -46,6 +49,7 @@ class CoordinateEditor extends React.Component {

static defaultProps = {
components: [],
measureOptions: {},
onChange: () => {},
onChangeRadius: () => {},
onHighlightPoint: () => {},
Expand Down Expand Up @@ -86,12 +90,12 @@ class CoordinateEditor extends React.Component {
<Col xs={12}>
<FormGroup validationState={this.getValidationStateRadius(this.props.properties.radius)}>
<ControlLabel><Message msgId="annotations.editor.radius"/></ControlLabel>
<FormControl
value={this.props.properties.radius}
<MeasureEditor
placeholder="radius"
{...this.props.measureOptions}
value={this.props.properties.radius}
name="radius"
onChange={e => {
const radius = e.target.value;
onChange={radius => {
if (this.isValid(this.props.components, radius )) {
this.props.onChangeRadius(parseFloat(radius), this.props.components.map(coordToArray));
} else if (radius !== "") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {getComponents} = require('../../../utils/AnnotationsUtils');
class GeometryEditor extends React.Component {
static propTypes = {
components: PropTypes.array,
options: PropTypes.object,
onRemove: PropTypes.func,
onChange: PropTypes.func,
transitionProps: PropTypes.object,
Expand All @@ -22,6 +23,7 @@ class GeometryEditor extends React.Component {

static defaultProps = {
selected: {},
options: {},
components: [],
onRemove: null,
onChange: () => {},
Expand All @@ -40,6 +42,7 @@ class GeometryEditor extends React.Component {

render() {
return (<CoordinatesEditor
{...this.props.options}
items={[]}
isDraggable
type={this.props.featureType}
Expand Down
93 changes: 93 additions & 0 deletions web/client/components/mapcontrols/annotations/MeasureEditor.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2018, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
const React = require('react');
const { FormControl, FormGroup } = require('react-bootstrap');
const { isNumber } = require('lodash');
const { convertUom } = require('../../../utils/MeasureUtils');

// convert to valueUom if it is a valid number
const toValue = (value, uom, valueUom) => (isNumber(parseFloat(value)) && !isNaN(parseFloat(value)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these functions be moved into some utils (measureUtils)?

Copy link
Member Author

@offtherailz offtherailz Jun 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is specific of this component,if the value is not a number, you should show the raw value, otherwise convert.
This doesn't concern any kind of common behiviour.

? convertUom(parseFloat(value), uom, valueUom)
: value;
// convert to local uom if it is a valid number
const toLocalValue = (value, uom, valueUom) =>
isNumber(parseFloat(value)) && !isNaN(parseFloat(value))
? parseFloat(convertUom(value, valueUom, uom).toFixed(4))
: value;

const { compose, withHandlers, withPropsOnChange, withState, withStateHandlers, defaultProps} = require('recompose');


module.exports = compose(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not a separate enhancer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If needed in other contexts (for instance spatial filter) this can be moved in a common folder and we may have to separate logic from the component (only if we need to append a different logic, or for testability reasons).
Now is not needed, so I prefer to keep the logic near the component.

defaultProps({
valueUom: 'm',
displayUom: 'm',
units: [
{ value: "ft", label: "ft" },
{ value: "m", label: "m" },
{ value: "km", label: "km" },
{ value: "mi", label: "mi" },
{ value: "nm", label: "nm" }
]
}),
withStateHandlers(
({ displayUom = "nm"}) => ({
uom: displayUom
}), {
setUom: () => (uom) => ({
uom
})
}
),
/**
* The component keeps locally the current edited value, and update it only when the value prop is different
* from the latest localValue (conversion apart)
* This avoid the conversion errors to be propagated in
*/
withState('localValue', 'setLocalValue'),
withPropsOnChange(
['value', 'localValue', 'uom', 'valueUom'],
({ value, localValue, uom, valueUom}) => ({
value: value === toValue(localValue, uom, valueUom)
? localValue
: toLocalValue(value, uom, valueUom)
})),
withHandlers({
onChange: ({ uom, valueUom, onChange = () => { }, setLocalValue = () => {} }) => (value) => {
setLocalValue(value);
onChange(
toValue(value, uom, valueUom)
);
}

})

)(({
value,
units,
uom,
style = {display: "inline-flex", width: "100%"},
setUom = () => {},
onChange = () => {}
}) => (
<FormGroup style={style}>
<FormControl
value={value}
placeholder="radius"
name="radius"
onChange={e => onChange(e.target.value)}
step={1}
type="number" />
<FormControl
componentClass="select" placeholder="select"
value={uom}
onChange={e => setUom(e.target.value)}
style={{ width: 85 }}>
{units.map((u) => <option key={u.value} value={u.value}>{u.label}</option>)}
</FormControl>
</FormGroup>));
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const React = require('react');
const ReactDOM = require('react-dom');
const ReactTestUtils = require('react-dom/test-utils');
const expect = require('expect');
const MeasureEditor = require('../MeasureEditor');

describe('MeasureEditor component', () => {
beforeEach((done) => {
document.body.innerHTML = '<div id="container"></div>';
setTimeout(done);
});
afterEach((done) => {
ReactDOM.unmountComponentAtNode(document.getElementById("container"));
document.body.innerHTML = '';
setTimeout(done);
});
it('MeasureEditor rendering with defaults', () => {
ReactDOM.render(<MeasureEditor />, document.getElementById("container"));
const container = document.getElementById('container');
const el = container.querySelector('input');
expect(el).toExist();
});
it('MeasureEditor rendering value', () => {
ReactDOM.render(<MeasureEditor value={10}/>, document.getElementById("container"));
const container = document.getElementById('container');
const el = container.querySelector('input');
expect(el).toExist();
expect(el.value).toBe('10');
});
it('MeasureEditor rendering value different UOM', () => {
ReactDOM.render(<MeasureEditor value={10000} displayUom="km" />, document.getElementById("container"));
const container = document.getElementById('container');
const el = container.querySelector('input');
expect(el).toExist();
expect(el.value).toBe('10');
});
it('MeasureEditor rendering value changes', () => {
ReactDOM.render(<MeasureEditor value={10000} displayUom="km" />, document.getElementById("container"));
const container = document.getElementById('container');
const el = container.querySelector('input');
expect(el).toExist();
expect(el.value).toBe('10');
ReactDOM.render(<MeasureEditor value={1000} displayUom="km" />, document.getElementById("container"));
expect(el.value).toBe('1');
});
it('Test MeasureEditor onChange callback, and change after re-render', () => {
const actions = {
onChange: () => {}
};
const spyonChange = expect.spyOn(actions, 'onChange');
ReactDOM.render(<MeasureEditor onChange={actions.onChange} displayUom="km" value={10} />, document.getElementById("container"));
const container = document.getElementById('container');

const el = container.querySelector('input');
el.value = "1";
ReactTestUtils.Simulate.change(el);
ReactDOM.render(<MeasureEditor onChange={actions.onChange} displayUom="km" value={1000} />, document.getElementById("container"));
expect(spyonChange).toHaveBeenCalled();
expect(spyonChange.calls[0].arguments[0]).toBe(1000);
expect(el.value).toBe('1');
ReactDOM.render(<MeasureEditor onChange={actions.onChange} displayUom="km" value={10000} />, document.getElementById("container"));
expect(el.value).toBe('10');
});
});
4 changes: 2 additions & 2 deletions web/client/translations/data.de-DE
Original file line number Diff line number Diff line change
Expand Up @@ -804,14 +804,14 @@
},
"add": "Fügen Sie neue Koordinaten hinzu",
"valid": "Geometrie ist gültig",
"radius": "Radius (m)",
"radius": "Radius",
"text": "Text",
"lat": "Breite",
"lon": "Längengrad",
"notValidMarker": "Fügen Sie eine gültige Koordinate ein (+|- 90° lat, +|-180° lon)",
"notValidPolyline": "Alle Koordinaten müssen gültig sein (+|- 90° lat, +|-180° lon)",
"notValidText": "Fügen Sie einen Textwert und eine gültige Koordinate ein (+|- 90° lat, +|-180° lon)",
"notValidCircle": "Geben Sie einen Radius (m) und eine gültige Koordinate ein (+|- 90° lat, +|-180° lon)"
"notValidCircle": "Geben Sie einen Radius und eine gültige Koordinate ein (+|- 90° lat, +|-180° lon)"
}
},
"user":{
Expand Down
4 changes: 2 additions & 2 deletions web/client/translations/data.en-US
Original file line number Diff line number Diff line change
Expand Up @@ -805,14 +805,14 @@
},
"add": "Add new coordinates",
"valid": "Geometry is valid",
"radius": "Radius (m)",
"radius": "Radius",
"text": "Text",
"lat": "Latitude",
"lon": "Longitude",
"notValidMarker": "Insert a valid coordinate (+|- 90° lat, +|-180° lon)",
"notValidPolyline": "All coordinate must be valid (+|- 90° lat, +|-180° lon)",
"notValidText": "Insert a text value and a valid coordinate (+|- 90° lat, +|-180° lon)",
"notValidCircle": "Insert a radius value (m) and a valid coordinate (+|- 90° lat, +|-180° lon)"
"notValidCircle": "Insert a radius value and a valid coordinate (+|- 90° lat, +|-180° lon)"
}
},
"user":{
Expand Down
4 changes: 2 additions & 2 deletions web/client/translations/data.es-ES
Original file line number Diff line number Diff line change
Expand Up @@ -804,14 +804,14 @@
},
"add": "Agregar nuevas coordenadas",
"valid": "La geometría es válida",
"radius": "Radio (m)",
"radius": "Radio",
"text": "Texto",
"lat": "Latitud",
"lon": "Longitud",
"notValidMarker": "Insertar una coordenada válida (+|- 90° lat, +|-180° lon)",
"notValidPolyline": "Todas las coordenadas deben ser válidas (+|- 90° lat, +|-180° lon)",
"notValidText": "Insertar un valor de texto y una coordenada válida (+|- 90° lat, +|-180° lon)",
"notValidCircle": "Ingrese un radio (m) y una coordenada válida (+|- 90° lat, +|-180° lon)"
"notValidCircle": "Ingrese un radio y una coordenada válida (+|- 90° lat, +|-180° lon)"
}
},
"user":{
Expand Down
4 changes: 2 additions & 2 deletions web/client/translations/data.fr-FR
Original file line number Diff line number Diff line change
Expand Up @@ -805,14 +805,14 @@
},
"add": "Ajouter de nouvelles coordonnées",
"valid": "La géométrie est valide",
"radius": "Rayon (m)",
"radius": "Rayon",
"text": "Texte",
"lat": "Latitude",
"lon": "Longitude",
"notValidMarker": "Insérer une coordonnée valide (+|- 90° lat, +|-180° lon)",
"notValidPolyline": "Toutes les coordonnées doivent être valides (+|- 90° lat, +|-180° lon)",
"notValidText": "Insérer une valeur de texte et une coordonnée valide (+|- 90° lat, +|-180° lon)",
"notValidCircle": "Entrez un rayon (m) et une coordonnée valide (+|- 90° lat, +|-180° lon)"
"notValidCircle": "Entrez un rayon et une coordonnée valide (+|- 90° lat, +|-180° lon)"
}
},
"user":{
Expand Down
4 changes: 2 additions & 2 deletions web/client/translations/data.hr-HR
Original file line number Diff line number Diff line change
Expand Up @@ -778,14 +778,14 @@
},
"add": "Add new coordinates",
"valid": "Geometry is valid",
"radius": "Radius (m)",
"radius": "Radius",
"text": "Text",
"lat": "Latitude",
"lon": "Longitude",
"notValidMarker": "Insert a valid coordinate (+|- 90° lat, +|-180° lon)",
"notValidPolyline": "All coordinate must be valid (+|- 90° lat, +|-180° lon)",
"notValidText": "Insert a text value and a valid coordinate (+|- 90° lat, +|-180° lon)",
"notValidCircle": "Insert a radius value (m) and a valid coordinate (+|- 90° lat, +|-180° lon)"
"notValidCircle": "Insert a radius value and a valid coordinate (+|- 90° lat, +|-180° lon)"
}
},
"users": {
Expand Down
4 changes: 2 additions & 2 deletions web/client/translations/data.it-IT
Original file line number Diff line number Diff line change
Expand Up @@ -804,14 +804,14 @@
},
"add": "Aggiungi una nuova coordinata",
"valid": "La geometria è valida",
"radius": "Raggio (m)",
"radius": "Raggio",
"text": "Testo",
"lat": "Latitudine",
"lon": "Longitudine",
"notValidMarker": "Inserisci una coordinata valida (+|- 90° lat, +|-180° lon)",
"notValidPolyline": "Tutte le coordinate devono essere valide (+|- 90° lat, +|-180° lon)",
"notValidText": "Inserisci un testo ed una coordinata valida (+|- 90° lat, +|-180° lon)",
"notValidCircle": "Inserisci un raggio (m) ed una coordinata valida (+|- 90° lat, +|-180° lon)"
"notValidCircle": "Inserisci un raggio ed una coordinata valida (+|- 90° lat, +|-180° lon)"
}
},
"user":{
Expand Down
4 changes: 2 additions & 2 deletions web/client/translations/data.nl-NL
Original file line number Diff line number Diff line change
Expand Up @@ -655,14 +655,14 @@
},
"add": "Add new coordinates",
"valid": "Geometry is valid",
"radius": "Radius (m)",
"radius": "Radius",
"text": "Text",
"lat": "Latitude",
"lon": "Longitude",
"notValidMarker": "Insert a valid coordinate (+|- 90° lat, +|-180° lon)",
"notValidPolyline": "All coordinate must be valid (+|- 90° lat, +|-180° lon)",
"notValidText": "Insert a text value and a valid coordinate (+|- 90° lat, +|-180° lon)",
"notValidCircle": "Insert a radius value (m) and a valid coordinate (+|- 90° lat, +|-180° lon)"
"notValidCircle": "Insert a radius value and a valid coordinate (+|- 90° lat, +|-180° lon)"
}
},
"user":{
Expand Down
4 changes: 2 additions & 2 deletions web/client/translations/data.zh-ZH
Original file line number Diff line number Diff line change
Expand Up @@ -668,14 +668,14 @@
},
"add": "Add new coordinates",
"valid": "Geometry is valid",
"radius": "Radius (m)",
"radius": "Radius",
"text": "Text",
"lat": "Latitude",
"lon": "Longitude",
"notValidMarker": "Insert a valid coordinate (+|- 90° lat, +|-180° lon)",
"notValidPolyline": "All coordinate must be valid (+|- 90° lat, +|-180° lon)",
"notValidText": "Insert a text value and a valid coordinate (+|- 90° lat, +|-180° lon)",
"notValidCircle": "Insert a radius value (m) and a valid coordinate (+|- 90° lat, +|-180° lon)"
"notValidCircle": "Insert a radius value and a valid coordinate (+|- 90° lat, +|-180° lon)"
}
},
"user":{
Expand Down