Skip to content

Commit

Permalink
Fix circular dependencies on c125_annotations branch (#3636)
Browse files Browse the repository at this point in the history
* Fixed circular dependencies
* moved forceselection to a utils file
* fix travis build
  • Loading branch information
MV88 authored Mar 22, 2019
1 parent 323c2e3 commit 843a500
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const React = require('react');
const PropTypes = require('prop-types');
const AttributeEditor = require('./AttributeEditor');
const ControlledCombobox = require('../../../misc/combobox/ControlledCombobox');
const {forceSelection} = require('../../../../utils/FeatureGridEditorUtils');
const {head} = require('lodash');
const assign = require('object-assign');

Expand Down Expand Up @@ -47,8 +48,6 @@ class DropDownEditor extends AttributeEditor {
};
this.getValue = () => {
const updated = super.getValue();
const {forceSelection} = require('../../../../utils/featuregrid/EditorRegistry');

if (this.props.forceSelection) {
return {[this.props.column.key]: forceSelection({
oldValue: this.props.defaultOption,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

const React = require('react');
const {template} = require('lodash');
const MapInfoUtils = require('../../../../utils/MapInfoUtils');
const TemplateUtils = require('../../../../utils/TemplateUtils');
const HtmlRenderer = require('../../../misc/HtmlRenderer');
const {Row, Col, Grid} = require('react-bootstrap');

Expand All @@ -17,7 +17,7 @@ module.exports = ({layer = {}, response}) => (
{response.features.map((feature, i) =>
<Row key={i}>
<Col xs={12}>
<HtmlRenderer html={template(MapInfoUtils.getCleanTemplate(layer.featureInfo && layer.featureInfo.template || '', feature, /\$\{.*?\}/g, 2, 1))(feature)}/>
<HtmlRenderer html={template(TemplateUtils.getCleanTemplate(layer.featureInfo && layer.featureInfo.template || '', feature, /\$\{.*?\}/g, 2, 1))(feature)}/>
</Col>
<Col xs={12}>
<hr/>
Expand Down
3 changes: 2 additions & 1 deletion web/client/components/map/openlayers/DrawSupport.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const {isSimpleGeomType, getSimpleGeomType} = require('../../../utils/MapUtils')
const {reprojectGeoJson, calculateDistance, reproject} = require('../../../utils/CoordinatesUtils');
const {createStylesAsync} = require('../../../utils/VectorStyleUtils');
const wgs84Sphere = new ol.Sphere(6378137);
const {transformPolygonToCircle, isCompletePolygon} = require('../../../utils/DrawSupportUtils');
const {transformPolygonToCircle} = require('../../../utils/DrawSupportUtils');
const {isCompletePolygon} = require('../../../utils/AnnotationsUtils');
const VectorStyle = require('./VectorStyle');
const {parseStyles} = require('./VectorStyle');
const geojsonFormat = new ol.format.GeoJSON();
Expand Down
13 changes: 11 additions & 2 deletions web/client/utils/AnnotationsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const uuidv1 = require('uuid/v1');
const LocaleUtils = require('./LocaleUtils');
const {extraMarkers} = require('./MarkerUtils');
const {geometryFunctions, fetchStyle, hashAndStringify} = require('./VectorStyleUtils');
const {isCompletePolygon} = require('./DrawSupportUtils');
const {set} = require('./ImmutableUtils');
const {values, isNil, slice, head, castArray, last, isArray, findIndex} = require('lodash');
const uuid = require('uuid');
Expand Down Expand Up @@ -473,7 +472,7 @@ const AnnotationsUtils = {
getComponents: ({type, coordinates}) => {
switch (type) {
case "Polygon": {
return isCompletePolygon(coordinates) ? AnnotationsUtils.formatCoordinates(slice(coordinates[0], 0, coordinates[0].length - 1)) : AnnotationsUtils.formatCoordinates(coordinates[0]);
return AnnotationsUtils.isCompletePolygon(coordinates) ? AnnotationsUtils.formatCoordinates(slice(coordinates[0], 0, coordinates[0].length - 1)) : AnnotationsUtils.formatCoordinates(coordinates[0]);
}
case "LineString": case "MultiPoint": {
return AnnotationsUtils.formatCoordinates(coordinates);
Expand Down Expand Up @@ -556,6 +555,16 @@ const AnnotationsUtils = {
},
isAMissingSymbol: (style) => {
return style.symbolUrlCustomized === require('../product/assets/symbols/symbolMissing.svg');
},
/**
* it tells if the filtered list of the coordinates is a geojson polygon,
* with the first point = to the last
* @param {number[[[]]]} coords the coordinates of the polygon
* @return {boolean} true if it is a valid polygon, false otherwise
*/
isCompletePolygon: (coords = [[[]]]) => {
const validCoords = coords[0].filter(AnnotationsUtils.validateCoordsArray);
return validCoords.length > 3 && head(validCoords)[0] === last(validCoords)[0] && head(validCoords)[1] === last(validCoords)[1];
}
};

Expand Down
16 changes: 1 addition & 15 deletions web/client/utils/DrawSupportUtils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
*/

const ol = require('openlayers');
const {isArray, head, last} = require('lodash');
const {isArray} = require('lodash');
const {reproject} = require('./CoordinatesUtils');


/**
* Transforms a leaflet bounds object into an array.
* @prop {object} the bounds
Expand Down Expand Up @@ -73,21 +72,8 @@ const transformPolygonToCircle = (feature, mapCrs) => {
return feature;
};

/**
* it tells if the filtered list of the coordinates is a geojson polygon,
* with the first point = to the last
* @param {number[[[]]]} coords the coordinates of the polygon
* @return {boolean} true if it is a valid polygon, false otherwise
*/
const isCompletePolygon = (coords = [[[]]]) => {
const {validateCoordsArray} = require('./AnnotationsUtils');
const validCoords = coords[0].filter(validateCoordsArray);
return validCoords.length > 3 && head(validCoords)[0] === last(validCoords)[0] && head(validCoords)[1] === last(validCoords)[1];
};

module.exports = {
transformPolygonToCircle,
boundsToOLExtent,
isCompletePolygon,
fromLeafletFeatureToQueryform
};
18 changes: 18 additions & 0 deletions web/client/utils/FeatureGridEditorUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* 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 FeatureGridEditorUtils = {
forceSelection: ( {oldValue, changedValue, data, allowEmpty}) => {
if (allowEmpty && changedValue === "") {
return "";
}
return data.indexOf(changedValue) !== -1 ? changedValue : oldValue;
}
};

module.exports = FeatureGridEditorUtils;
28 changes: 1 addition & 27 deletions web/client/utils/MapInfoUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const FeatureInfoUtils = require("./FeatureInfoUtils");
const INFO_FORMATS = FeatureInfoUtils.INFO_FORMATS;
const INFO_FORMATS_BY_MIME_TYPE = FeatureInfoUtils.INFO_FORMATS_BY_MIME_TYPE;
const pointOnSurface = require('turf-point-on-surface');
const {findIndex, has, trim, isString} = require('lodash');
const {findIndex} = require('lodash');

const MapInfoUtils = {
/**
Expand Down Expand Up @@ -233,32 +233,6 @@ const MapInfoUtils = {
return op;
}, {});
return options;
},
/**
* check if a string attribute is inside of a given object
* @param feature {object}
* @param attribute {string} name of attribue with dot notations
* @param start {array} substring start
* @param end {array} substring end
* @return {bool} true if feature contains the attribute
*/
validateStringAttribute: (feature, attribute, start = 0, end = 0) => {
const path = isString(attribute) && trim(attribute.substring(start, attribute.length - end)) || '';
return has(feature, path);
},
/**
* returns a valid template
* @param template {string} text with attribute to validate
* @param feature {object} object to match attributes
* @param regex {regex}
* @param start {array} substring start
* @param end {array} substring end
* @return {string} templete without invalid attribute and html tag inside attribute, e.g. ${ <p>properties.id</p> } -> ${ properties.id }
*/
getCleanTemplate: (template, feature, regex, start = 0, end = 0) => {
const matchVariables = isString(template) && template.match(regex);
const replacedTag = matchVariables && matchVariables.map(temp => ({ previous: temp, next: MapInfoUtils.validateStringAttribute(feature, temp.replace(/(<([^>]+)>)/ig, ''), start, end) && temp.replace(/(<([^>]+)>)/ig, '') || ''})) || null;
return replacedTag && replacedTag.reduce((temp, variable) => temp.replace(variable.previous, variable.next), template) || template || '';
}
};

Expand Down
32 changes: 30 additions & 2 deletions web/client/utils/TemplateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* LICENSE file in the root directory of this source tree.
*/

const {isString} = require('lodash');
const {isString, has, trim} = require('lodash');

module.exports = {
const TemplateUtils = {
/**
* generates a template string to use for static replacements.
* It's useful for using a similar syntax for static configured strings to
Expand Down Expand Up @@ -56,5 +56,33 @@ module.exports = {
callback(null, e);
}
});
},
/**
* check if a string attribute is inside of a given object
* @param feature {object}
* @param attribute {string} name of attribue with dot notations
* @param start {array} substring start
* @param end {array} substring end
* @return {bool} true if feature contains the attribute
*/
validateStringAttribute: (feature, attribute, start = 0, end = 0) => {
const path = isString(attribute) && trim(attribute.substring(start, attribute.length - end)) || '';
return has(feature, path);
},
/**
* returns a valid template
* @param template {string} text with attribute to validate
* @param feature {object} object to match attributes
* @param regex {regex}
* @param start {array} substring start
* @param end {array} substring end
* @return {string} templete without invalid attribute and html tag inside attribute, e.g. ${ <p>properties.id</p> } -> ${ properties.id }
*/
getCleanTemplate: (template, feature, regex, start = 0, end = 0) => {
const matchVariables = isString(template) && template.match(regex);
const replacedTag = matchVariables && matchVariables.map(temp => ({ previous: temp, next: TemplateUtils.validateStringAttribute(feature, temp.replace(/(<([^>]+)>)/ig, ''), start, end) && temp.replace(/(<([^>]+)>)/ig, '') || ''})) || null;
return replacedTag && replacedTag.reduce((temp, variable) => temp.replace(variable.previous, variable.next), template) || template || '';
}
};

module.exports = TemplateUtils;
14 changes: 13 additions & 1 deletion web/client/utils/__tests__/AnnotationsUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const {getAvailableStyler, getRelativeStyler, convertGeoJSONToInternalModel,
getStartEndPointsForLinestring,
createGeometryFromGeomFunction,
updateAllStyles,
fromLineStringToGeodesicLineString
fromLineStringToGeodesicLineString, isCompletePolygon
} = require('../AnnotationsUtils');

const featureCollection = {
Expand Down Expand Up @@ -727,4 +727,16 @@ describe('Test the AnnotationsUtils', () => {
expect(f.properties.id).toEqual("VR46");
expect(f.properties.ms_style).toExist();
});

it('test isCompletePolygon defaults', () => {
const polygonCoords1 = [[[1, 1], [2, 2]]];
const polygonCoords2 = [[[1, 1], [2, 2], [1, 1]]];
const polygonCoords3 = [[[1, 1], [2, 2], [3, 3], [1, 1]]];
const polygonCoords4 = [[[1, 1], [2, undefined], [3, 3], [1, 1]]];
expect(isCompletePolygon()).toBe(false);
expect(isCompletePolygon(polygonCoords1)).toBe(false);
expect(isCompletePolygon(polygonCoords2)).toBe(false);
expect(isCompletePolygon(polygonCoords3)).toBe(true);
expect(isCompletePolygon(polygonCoords4)).toBe(false);
});
});
14 changes: 1 addition & 13 deletions web/client/utils/__tests__/DrawSupportUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

const expect = require('expect');
const {boundsToOLExtent, fromLeafletFeatureToQueryform, isCompletePolygon} = require('../DrawSupportUtils');
const {boundsToOLExtent, fromLeafletFeatureToQueryform} = require('../DrawSupportUtils');
const L = require('leaflet');

describe('LocaleUtils', () => {
Expand Down Expand Up @@ -43,16 +43,4 @@ describe('LocaleUtils', () => {
const convertedBounds = boundsToOLExtent(bounds);
expect(convertedBounds.length).toBe(4);
});

it('test isCompletePolygon defaults', () => {
const polygonCoords1 = [[[1, 1], [2, 2]]];
const polygonCoords2 = [[[1, 1], [2, 2], [1, 1]]];
const polygonCoords3 = [[[1, 1], [2, 2], [3, 3], [1, 1]]];
const polygonCoords4 = [[[1, 1], [2, undefined], [3, 3], [1, 1]]];
expect(isCompletePolygon()).toBe(false);
expect(isCompletePolygon(polygonCoords1)).toBe(false);
expect(isCompletePolygon(polygonCoords2)).toBe(false);
expect(isCompletePolygon(polygonCoords3)).toBe(true);
expect(isCompletePolygon(polygonCoords4)).toBe(false);
});
});
47 changes: 47 additions & 0 deletions web/client/utils/__tests__/FeatureGridEditorUtils-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 expect = require('expect');
const {
forceSelection
} = require('../FeatureGridEditorUtils');

describe('FeatureGridEditorUtils', () => {
it('forceSelection allowEmpty=true', () => {
const oldValue = "old";
const changedValue = "new";
const data = ["new", "old", "agile"];
const allowEmpty = true;
const newVal = forceSelection({oldValue, changedValue, data, allowEmpty});
expect(newVal).toBe("new");
});
it('forceSelection allowEmpty=true with "" value', () => {
const oldValue = "old";
const changedValue = "";
const data = ["new", "old", "agile"];
const allowEmpty = true;
const newVal = forceSelection({oldValue, changedValue, data, allowEmpty});
expect(newVal).toBe("");
});
it('forceSelection allowEmpty=false with "" value', () => {
const oldValue = "old";
const changedValue = "";
const data = ["new", "old", "agile"];
const allowEmpty = false;
const newVal = forceSelection({oldValue, changedValue, data, allowEmpty});
expect(newVal).toBe("old");
});
it('forceSelection allowEmpty=false with "agile" value', () => {
const oldValue = "old";
const changedValue = "agile";
const data = ["new", "old", "agile"];
const allowEmpty = false;
const newVal = forceSelection({oldValue, changedValue, data, allowEmpty});
expect(newVal).toBe("agile");
});
});
29 changes: 1 addition & 28 deletions web/client/utils/__tests__/MapInfoUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ var {
getLabelFromValue,
getDefaultInfoFormatValueFromLayer,
getLayerFeatureInfo,
filterRequestParams,
validateStringAttribute,
getCleanTemplate
filterRequestParams
} = require('../MapInfoUtils');

const CoordinatesUtils = require('../CoordinatesUtils');
Expand Down Expand Up @@ -389,30 +387,6 @@ describe('MapInfoUtils', () => {
}, includeOptions, excludeParams)).toEqual({ buffer: 'buffer', cql_filter: 'cql_filter', filter: 'filter', propertyName: 'propertyName' });
});

it('validateStringAttribute', () => {
const testObj = {
properties: {
name: 'object-name'
}
};
expect(validateStringAttribute(testObj, 'properties.name')).toBe(true);
expect(validateStringAttribute(testObj, ' properties.name ')).toBe(true);
expect(validateStringAttribute(testObj, '${properties.name}')).toBe(false);
expect(validateStringAttribute(testObj, '${properties.name}', 2, 1)).toBe(true);
expect(validateStringAttribute(testObj, '${ properties.name }', 2, 1)).toBe(true);
expect(validateStringAttribute(testObj, 'properties.desc')).toBe(false);
});

it('getCleanTemplate', () => {
const template = '<p>the name is ${ properties.name } and the description ${ properties.desc } and again the name is ${ <strong>properties.name</strong> }</p>';
const testObj = {
properties: {
name: 'object-name'
}
};
expect(getCleanTemplate(template, testObj, /\$\{.*?\}/g, 2, 1)).toBe('<p>the name is ${ properties.name } and the description and again the name is ${ properties.name }</p>');

});
it('buildIdentifyRequest cql_filter management for wms and wmts2', () => {
let props = {
map: {
Expand Down Expand Up @@ -486,4 +460,3 @@ describe('MapInfoUtils', () => {
});

});

Loading

0 comments on commit 843a500

Please sign in to comment.