Skip to content

Commit

Permalink
Exclude google background from widgets (#2817)
Browse files Browse the repository at this point in the history
  • Loading branch information
offtherailz authored Apr 11, 2018
1 parent ecccca0 commit d6f6194
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require('rxjs');
const GeoStoreDAO = require('../../../../../api/GeoStoreDAO');
const axios = require('../../../../../libs/ajax');
const ConfigUtils = require('../../../../../utils/ConfigUtils');

const { excludeGoogleBackground } = require('../../../../../utils/LayersUtils');
const BorderLayout = require('../../../../layout/BorderLayout');

const Toolbar = require('../../../../misc/toolbar/Toolbar');
Expand All @@ -35,12 +35,12 @@ module.exports = compose(
let mapState = (!config.version && typeof map.id !== 'string') ? ConfigUtils.convertFromLegacy(config) : ConfigUtils.normalizeConfig(config.map);
return {
...(mapState && mapState.map || {}),
layers: mapState.layers.map(l => {
layers: excludeGoogleBackground(mapState.layers.map(l => {
if (l.group === "background" && (l.type === "ol" || l.type === "OpenLayers.Layer")) {
l.type = "empty";
}
return l;
})
}))
};
}))
.then(res => onMapSelected({
Expand Down
32 changes: 31 additions & 1 deletion web/client/utils/LayersUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

const assign = require('object-assign');
const toBbox = require('turf-bbox');
const {isString, isObject, isArray, head, isEmpty} = require('lodash');
const { isString, isObject, isArray, head, isEmpty, findIndex} = require('lodash');
const REG_GEOSERVER_RULE = /\/[\w- ]*geoserver[\w- ]*\//;
const findGeoServerName = ({url, regex = REG_GEOSERVER_RULE}) => {
return regex.test(url) && url.match(regex)[0] || null;
Expand Down Expand Up @@ -468,6 +468,36 @@ const LayersUtils = {
SecurityUtils.addAuthenticationParameter(url, authenticationParam, options.securityToken);
});
return authenticationParam;
},
/**
* Removes google backgrounds and select an alternative one as visible
* returns a new list of layers modified accordingly
*/
excludeGoogleBackground: ll => {
const hasVisibleGoogleBackground = ll.filter(({ type, group, visibility } = {}) => group === 'background' && type === 'google' && visibility).length > 0;
const layers = ll.filter(({ type } = {}) => type !== 'google');
const backgrounds = layers.filter(({ group } = {}) => group === 'background');

// check if the selection of a new background is required
if (hasVisibleGoogleBackground && backgrounds.filter(({ visibility } = {}) => visibility).length === 0) {
// select the first available
if (backgrounds.length > 0) {
const candidate = findIndex(layers, {group: 'background'});
return layers.map((l, i) => i === candidate ? {...l, visibility: true} : l);
}
// add osm if any other background is missing
return [{
"type": "osm",
"title": "Open Street Map",
"name": "mapnik",
"source": "osm",
"group": "background",
"visibility": true
}, ...layers];


}
return layers;
}
};

Expand Down
38 changes: 38 additions & 0 deletions web/client/utils/__tests__/LayersUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const typeV1 = "empty";
const emptyBackground = {
type: typeV1
};

const bingLayerWithApikey = {
type: 'bing',
apiKey: "SOME_APIKEY_VALUE"
Expand Down Expand Up @@ -419,5 +420,42 @@ describe('LayersUtils', () => {
expect(LayersUtils.getURLs(['http://url/?delete=param'])).toEqual(['http://url/']);
expect(LayersUtils.getURLs(['http://url/?delete=param'], '?custom=param')).toEqual(['http://url/?custom=param']);
});
it('excludeGoogleBackground', () => {
const GOOGLE_BG = {
type: 'google',
group: 'background',
visibility: true
};

const LAYERS_1 = [GOOGLE_BG, wmsLayer];
const LAYERS_2 = [GOOGLE_BG, bingLayerWithApikey, wmsLayer];
const LAYERS_3 = [GOOGLE_BG, {group: 'background', ...bingLayerWithApikey}, wmsLayer];
const LAYERS_4 = [{visibility: false, ...GOOGLE_BG}, { group: 'background', visibility: true, ...bingLayerWithApikey }, wmsLayer];

// check adds a osm as default background
const RES_1 = LayersUtils.excludeGoogleBackground(LAYERS_1);
expect(RES_1.length).toBe(2);
expect(RES_1[0].type).toBe('osm');
expect(RES_1[0].visibility).toBe(true);

// check adds anyway osm as default background
const RES_2 = LayersUtils.excludeGoogleBackground(LAYERS_2);
expect(RES_2.length).toBe(3);
expect(RES_2[0].type).toBe('osm');
expect(RES_2[0].visibility).toBe(true);

// check select as visible the first background available
const RES_3 = LayersUtils.excludeGoogleBackground(LAYERS_3);
expect(RES_3.length).toBe(2);
expect(RES_3[0].type).toBe('bing');
expect(RES_3[0].visibility).toBe(true);

// check select as visible the first background available
const RES_4 = LayersUtils.excludeGoogleBackground(LAYERS_4);
expect(RES_4.length).toBe(2);
expect(RES_4[0].type).toBe('bing');
expect(RES_4[0].visibility).toBe(true);

});

});

0 comments on commit d6f6194

Please sign in to comment.