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

Align OL measure number format of tooltip to measure panel's one #3673

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2019, 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 ReactDOM = require('react-dom');
const {createSink} = require('recompose');
const expect = require('expect');
const addI18NProps = require('../addI18NProps');
var Localized = require('../../Localized');


describe('addI18NProps enhancer', () => {
beforeEach((done) => {
document.body.innerHTML = '<div id="container"></div>';
setTimeout(done);
});
afterEach((done) => {
ReactDOM.unmountComponentAtNode(document.getElementById("container"));
document.body.innerHTML = '';
setTimeout(done);
});
it('addI18NProps format numbers', () => {
const Sink = addI18NProps(['formatNumber'])(createSink( props => {
expect(props).toExist();
expect(props.formatNumber).toExist();
expect(typeof props.formatNumber(1)).toBe('string');
expect(props.formatNumber(1.1)).toBe("1.1");
}));
ReactDOM.render(<Localized locale="en-EN" messages={{}}>
<Sink />
</Localized>, document.getElementById("container"));
});
});
25 changes: 25 additions & 0 deletions web/client/components/I18N/enhancers/addI18NProps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2019, 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 {injectIntl} = require('react-intl');
const {omit} = require('lodash');
const { compose, toClass, mapProps, withProps } = require('recompose');

const omitProps = keys => mapProps(props => omit(props, keys));

/**
* @name addI18NFormat
* @param {string[]} propsToAdd add the props to format as props. Choose in theese of these https://github.com/yahoo/react-intl/wiki/API#intlshape
*/
module.exports = (propsToAdd = []) => compose(
Copy link
Contributor

Choose a reason for hiding this comment

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

Not much readable for me.

Copy link
Member Author

Choose a reason for hiding this comment

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

I had to refactor it a bit to support existing tests and manage errors. Please take a look at it now, It could be more readable and better documented.

C => injectIntl(C, { intlPropName: '__intl__'} ),
withProps(({__intl__ = {}}) => propsToAdd.reduce((acc = {}, k) => ({
...acc,
[k]: __intl__[k]
}), {})),
omitProps(['__intl__'])
);
12 changes: 9 additions & 3 deletions web/client/components/map/openlayers/MeasurementSupport.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const PropTypes = require('prop-types');
const {round, isEqual, dropRight, pick} = require('lodash');
const assign = require('object-assign');
const ol = require('openlayers');
const addI18NProps = require('../../I18N/enhancers/addI18NProps');

const wgs84Sphere = new ol.Sphere(6378137);
const {reprojectGeoJson, reproject, calculateAzimuth, calculateDistance, transformLineToArcs} = require('../../../utils/CoordinatesUtils');
const {convertUom, getFormattedBearingValue} = require('../../../utils/MeasureUtils');
Expand All @@ -30,6 +32,7 @@ class MeasurementSupport extends React.Component {
measurement: PropTypes.object,
enabled: PropTypes.bool,
uom: PropTypes.object,
formatNumber: PropTypes.func,
changeMeasurementState: PropTypes.func,
updateMeasures: PropTypes.func,
resetGeometry: PropTypes.func,
Expand All @@ -46,6 +49,7 @@ class MeasurementSupport extends React.Component {
resetGeometry: () => {},
updateMeasures: () => {},
changeGeometry: () => {},
formatNumber: () => {},
startEndPoint: {
startPointOptions: {
radius: 3,
Expand Down Expand Up @@ -451,7 +455,7 @@ class MeasurementSupport extends React.Component {
const length = calculateDistance(reprojectedCoords, props.measurement.lengthFormula);
const {label, unit} = props.uom && props.uom.length;
const output = round(convertUom(length, "m", unit), 2);
return output + " " + (label);
return this.props.formatNumber(output) + " " + (label);
};

/**
Expand All @@ -464,7 +468,7 @@ class MeasurementSupport extends React.Component {
const {label, unit} = props.uom && props.uom.area;
const output = round(convertUom(area, "sqm", unit), 2);

return output + " " + label;
return this.props.formatNumber(output) + " " + label;
};

removeHelpTooltip = () => {
Expand All @@ -486,4 +490,6 @@ class MeasurementSupport extends React.Component {
}
}

module.exports = MeasurementSupport;
module.exports = addI18NProps(
['formatNumber']
)(MeasurementSupport);