Skip to content

Commit 9ec6ff7

Browse files
anup39allyoucanmap
andauthored
Fix #2169 Background layers are now saved in the mapConfig. (#2300)
--------- Co-authored-by: allyoucanmap <[email protected]>
1 parent beed70a commit 9ec6ff7

File tree

6 files changed

+81
-45
lines changed

6 files changed

+81
-45
lines changed

geonode_mapstore_client/client/js/epics/__tests__/gnresource-test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
closeDatasetCatalogPanel,
1717
gnZoomToFitBounds,
1818
closeResourceDetailsOnMapInfoOpen,
19-
gnUpdateResourceExtent
19+
gnUpdateResourceExtent,
20+
gnUpdateBackgroundEditEpic
2021
} from '@js/epics/gnresource';
2122
import { SAVE_SUCCESS } from '@mapstore/framework/actions/featuregrid';
2223
import {
@@ -33,6 +34,7 @@ import {
3334
} from '@mapstore/framework/actions/notifications';
3435
import { newMapInfoRequest } from '@mapstore/framework/actions/mapInfo';
3536
import { SET_SHOW_DETAILS } from '@mapstore/framework/plugins/ResourcesCatalog/actions/resources';
37+
import { CREATE_BACKGROUNDS_LIST } from '@mapstore/framework/actions/backgroundselector';
3638

3739
let mockAxios;
3840

@@ -345,4 +347,34 @@ describe('gnresource epics', () => {
345347
testState
346348
);
347349
});
350+
351+
it('should set canEdit true for MAP resource with change_resourcebase permission', (done) => {
352+
const NUM_ACTIONS = 1;
353+
const testState = {
354+
gnresource: {
355+
type: "map",
356+
data: {
357+
pk: 1,
358+
title: 'Test Map',
359+
perms: ['view_resourcebase', 'change_resourcebase']
360+
}
361+
}
362+
};
363+
364+
testEpic(
365+
gnUpdateBackgroundEditEpic,
366+
NUM_ACTIONS,
367+
{ type: CREATE_BACKGROUNDS_LIST },
368+
(actions) => {
369+
try {
370+
expect(actions.length).toBe(1);
371+
expect(actions[0].resource.canEdit).toBe(true);
372+
} catch (e) {
373+
done(e);
374+
}
375+
done();
376+
},
377+
testState
378+
);
379+
});
348380
});

geonode_mapstore_client/client/js/epics/gnresource.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ import {
129129
loadFinished,
130130
setCreationStep
131131
} from '@mapstore/framework/actions/contextcreator';
132-
import { setContext } from '@mapstore/framework/actions/context';
132+
import { setContext, setResource as setResourceContext } from '@mapstore/framework/actions/context';
133133
import { REDUCERS_LOADED } from '@mapstore/framework/actions/storemanager';
134134
import { wrapStartStop } from '@mapstore/framework/observables/epics';
135135
import { parseDevHostname } from '@js/utils/APIUtils';
@@ -139,6 +139,7 @@ import { VisualizationModes } from '@mapstore/framework/utils/MapTypeUtils';
139139
import { forceUpdateMapLayout } from '@mapstore/framework/actions/maplayout';
140140
import { getShowDetails } from '@mapstore/framework/plugins/ResourcesCatalog/selectors/resources';
141141
import { searchSelector } from '@mapstore/framework/selectors/router';
142+
import { CREATE_BACKGROUNDS_LIST, allowBackgroundsDeletion } from '@mapstore/framework/actions/backgroundselector';
142143

143144
const FIT_BOUNDS_CONTROL = 'fitBounds';
144145

@@ -840,10 +841,24 @@ export const gnUpdateResourceExtent = (action$, store) =>
840841
);
841842
});
842843

844+
export const gnUpdateBackgroundEditEpic = (action$, store) =>
845+
action$.ofType(CREATE_BACKGROUNDS_LIST)
846+
.switchMap(() => {
847+
const state = store.getState();
848+
const resource = state.gnresource?.data || {};
849+
const resourceType = state.gnresource?.type;
850+
const canEdit = resourceType === ResourceTypes.MAP && resource?.perms?.includes('change_resourcebase') ? true : false;
851+
return Observable.of(
852+
setResourceContext({ canEdit }),
853+
...(canEdit ? [allowBackgroundsDeletion(true)] : [])
854+
);
855+
});
856+
843857
export default {
844858
gnViewerRequestNewResourceConfig,
845859
gnViewerRequestResourceConfig,
846860
gnViewerSetNewResourceThumbnail,
861+
gnUpdateBackgroundEditEpic,
847862
closeInfoPanelOnMapClick,
848863
closeOpenPanels,
849864
closeDatasetCatalogPanel,

geonode_mapstore_client/client/js/plugins/index.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import TOCPlugin from '@mapstore/framework/plugins/TOC';
2121
import Isochrone from "@mapstore/framework/plugins/Isochrone";
2222
import Itinerary from "@mapstore/framework/plugins/Itinerary";
2323
import SecurityPopup from "@mapstore/framework/plugins/SecurityPopup";
24+
import BackgroundSelector from '@mapstore/framework/plugins/BackgroundSelector';
25+
import MetadataExplorer from '@mapstore/framework/plugins/MetadataExplorer';
2426

2527
import OperationPlugin from '@js/plugins/Operation';
2628
import MetadataEditorPlugin from '@js/plugins/MetadataEditor';
@@ -87,6 +89,8 @@ export const plugins = {
8789
IsochronePlugin: Isochrone,
8890
ItineraryPlugin: Itinerary,
8991
SecurityPopupPlugin: SecurityPopup,
92+
BackgroundSelectorPlugin: BackgroundSelector,
93+
MetadataExplorerPlugin: MetadataExplorer,
9094
LayerDownloadPlugin: toModulePlugin(
9195
'LayerDownload',
9296
() => import(/* webpackChunkName: 'plugins/layer-download' */ '@mapstore/framework/plugins/LayerDownload'),
@@ -131,10 +135,6 @@ export const plugins = {
131135
'StyleEditor',
132136
() => import(/* webpackChunkName: 'plugins/style-editor' */ '@mapstore/framework/plugins/StyleEditor')
133137
),
134-
MetadataExplorerPlugin: toModulePlugin(
135-
'MetadataExplorer',
136-
() => import(/* webpackChunkName: 'plugins/metadata-explorer' */ '@mapstore/framework/plugins/MetadataExplorer')
137-
),
138138
QueryPanelPlugin: toModulePlugin(
139139
'QueryPanel',
140140
() => import(/* webpackChunkName: 'plugins/query-panel' */ '@mapstore/framework/plugins/QueryPanel')
@@ -258,10 +258,6 @@ export const plugins = {
258258
'MapLoading',
259259
() => import(/* webpackChunkName: 'plugins/map-loading-plugin' */ '@mapstore/framework/plugins/MapLoading')
260260
),
261-
BackgroundSelectorPlugin: toModulePlugin(
262-
'BackgroundSelector',
263-
() => import(/* webpackChunkName: 'plugins/background-selector-plugin' */ '@mapstore/framework/plugins/BackgroundSelector')
264-
),
265261
ZoomInPlugin: toModulePlugin(
266262
'ZoomIn',
267263
() => import(/* webpackChunkName: 'plugins/zoom-in-plugin' */ '@mapstore/framework/plugins/ZoomIn')

geonode_mapstore_client/client/js/utils/ResourceUtils.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -620,28 +620,9 @@ export function toGeoNodeMapConfig(data) {
620620
};
621621
}
622622

623-
export function compareBackgroundLayers(aLayer, bLayer) {
624-
return aLayer.type === bLayer.type
625-
&& aLayer.name === bLayer.name
626-
&& aLayer.source === bLayer.source
627-
&& aLayer.provider === bLayer.provider
628-
&& aLayer.url === bLayer.url;
629-
}
630-
631623
export function toMapStoreMapConfig(resource, baseConfig) {
632624
const { maplayers = [], data } = resource || {};
633-
const baseMapBackgroundLayers = (baseConfig?.map?.layers || []).filter(layer => layer.group === 'background');
634-
const currentBackgroundLayer = (data?.map?.layers || [])
635-
.filter(layer => layer.group === 'background')
636-
.find(layer => layer.visibility && baseMapBackgroundLayers.find(bLayer => compareBackgroundLayers(layer, bLayer)));
637-
638-
const backgroundLayers = !currentBackgroundLayer
639-
? baseMapBackgroundLayers
640-
: baseMapBackgroundLayers.map((layer) => ({
641-
...layer,
642-
visibility: compareBackgroundLayers(layer, currentBackgroundLayer)
643-
}));
644-
625+
const backgroundLayers = (data?.map?.layers || []).filter(layer => layer.group === 'background');
645626
const layers = (data?.map?.layers || [])
646627
.filter(layer => layer.group !== 'background')
647628
.map((layer) => {

geonode_mapstore_client/client/js/utils/__tests__/ResourceUtils-test.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
setAvailableResourceTypes,
1818
getGeoNodeMapLayers,
1919
toGeoNodeMapConfig,
20-
compareBackgroundLayers,
2120
toMapStoreMapConfig,
2221
parseStyleName,
2322
canCopyResource,
@@ -198,9 +197,6 @@ describe('Test Resource Utils', () => {
198197
const geoNodeMapConfig = toGeoNodeMapConfig(data, mapState);
199198
expect(geoNodeMapConfig.maplayers.length).toBe(1);
200199
});
201-
it('should be able to compare background layers with different ids', () => {
202-
expect(compareBackgroundLayers({ type: 'osm', source: 'osm', id: '11' }, { type: 'osm', source: 'osm' })).toBe(true);
203-
});
204200
it('should transform a resource to a mapstore map config', () => {
205201
const resource = {
206202
maplayers: [
@@ -249,7 +245,7 @@ describe('Test Resource Utils', () => {
249245
map: {
250246
sources: {},
251247
layers: [
252-
{ type: 'osm', source: 'osm', group: 'background', visibility: true },
248+
{ id: '01', type: 'osm', source: 'osm', group: 'background', visibility: true },
253249
{ id: '02', type: 'vector', features: [] },
254250
{
255251
id: '03',
@@ -327,7 +323,7 @@ describe('Test Resource Utils', () => {
327323
map: {
328324
sources: {},
329325
layers: [
330-
{ type: 'osm', source: 'osm', group: 'background', visibility: true },
326+
{ id: '01', type: 'osm', source: 'osm', group: 'background', visibility: true },
331327
{ id: '02', type: 'vector', features: [] },
332328
{
333329
id: '03',
@@ -354,7 +350,7 @@ describe('Test Resource Utils', () => {
354350
}
355351
);
356352
});
357-
it('should transform a resource to a mapstore map config and update backgrounds', () => {
353+
it('should transform a resource to a mapstore map config and to not update backgrounds', () => {
358354
const resource = {
359355
maplayers: [
360356
{
@@ -410,12 +406,11 @@ describe('Test Resource Utils', () => {
410406
sources: {},
411407
layers: [
412408
{
413-
name: 'OpenTopoMap',
414-
provider: 'OpenTopoMap',
415-
source: 'OpenTopoMap',
416-
type: 'tileprovider',
417-
visibility: true,
418-
group: 'background'
409+
id: '01',
410+
type: 'osm',
411+
source: 'osm',
412+
group: 'background',
413+
visibility: true
419414
},
420415
{ id: '02', type: 'vector', features: [] },
421416
{
@@ -492,8 +487,8 @@ describe('Test Resource Utils', () => {
492487
const mapStoreMapConfig = toMapStoreMapConfig(resource, baseConfig);
493488
expect(mapStoreMapConfig).toBeTruthy();
494489
const layers = mapStoreMapConfig.map.layers;
495-
expect(layers.length).toBe(2);
496-
expect(layers[1].featureInfo).toEqual({ template, format: FEATURE_INFO_FORMAT });
490+
expect(layers.length).toBe(1);
491+
expect(layers[0].featureInfo).toEqual({ template, format: FEATURE_INFO_FORMAT });
497492
});
498493

499494
it('should parse style name into accepted format', () => {

geonode_mapstore_client/templates/geonode-mapstore-client/_geonode_config.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,23 @@
185185
endpointV1: siteUrl + 'api',
186186
endpointV2: siteUrl + 'api/v2/'
187187
},
188+
initialState: {
189+
defaultState: {
190+
catalog: {
191+
default: {
192+
staticServices: {
193+
default_map_backgrounds: {
194+
type: 'backgrounds',
195+
title: 'Default Backgrounds',
196+
titleMsgId: 'defaultMapBackgroundsServiceTitle',
197+
autoload: true,
198+
backgrounds: baseLayers
199+
}
200+
}
201+
}
202+
}
203+
}
204+
},
188205
projectionDefs: projectionDefs,
189206
geoNodeCustomFilters: customFilters,
190207
geoNodeCardsMenuItems: cardsMenuItems,

0 commit comments

Comments
 (0)