forked from allyoucanmap/geonode-mapstore-client
-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathresource.js
More file actions
475 lines (421 loc) · 18.3 KB
/
resource.js
File metadata and controls
475 lines (421 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
* Copyright 2021, 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.
*/
import { mapSelector } from '@mapstore/framework/selectors/map';
import { mapSaveSelector } from '@js/selectors/mapsave';
import { compareMapChanges } from '@mapstore/framework/utils/MapUtils';
import { currentStorySelector } from '@mapstore/framework/selectors/geostory';
import { originalDataSelector } from '@mapstore/framework/selectors/dashboard';
import { widgetsConfig } from '@mapstore/framework/selectors/widgets';
import { ResourceTypes, RESOURCE_PUBLISHING_PROPERTIES, RESOURCE_OPTIONS_PROPERTIES, resourceToLayerConfig } from '@js/utils/ResourceUtils';
import {
getCurrentResourceDeleteLoading,
getCurrentResourceCopyLoading
} from '@js/selectors/resourceservice';
import omit from 'lodash/omit';
import isArray from 'lodash/isArray';
import isObject from 'lodash/isObject';
import isEqual from 'lodash/isEqual';
import pick from 'lodash/pick';
import isEmpty from 'lodash/isEmpty';
import get from 'lodash/get';
import omitBy from 'lodash/omitBy';
import isNil from 'lodash/isNil';
import { generateContextResource } from '@mapstore/framework/selectors/contextcreator';
import { layerSettingSelector, getSelectedLayer as getSelectedNode } from '@mapstore/framework/selectors/layers';
import { saveLayer } from '@mapstore/framework/utils/LayersUtils';
const RESOURCE_MANAGEMENT_PROPERTIES_KEYS = Object.keys({...RESOURCE_PUBLISHING_PROPERTIES, ...RESOURCE_OPTIONS_PROPERTIES});
/**
* @module selectors/resource
*/
export const getResourceId = (state) => {
const resourceId = state?.gnresource?.id;
const resourcePk = state?.gnresource?.data?.pk;
return resourceId || resourcePk;
};
export const getResourcePerms = (state) => {
return state?.gnresource?.data?.perms || [];
};
export const getResourceName = (state) => {
return state?.gnresource?.data?.title || false;
};
export const getResourceDescription = (state) => {
return state?.gnresource?.data?.abstract || false;
};
export const getResourceThumbnail = (state) => {
return state?.gnresource?.data?.thumbnail_url || false;
};
export const updatingThumbnailResource = (state) => {
return state?.gnresource?.data?.updatingThumbnail || false;
};
export const isThumbnailChanged = (state) => {
return state?.gnresource?.data?.thumbnailChanged || false;
};
export const getViewedResourceType = (state) => {
const viewedResourceType = state?.gnresource?.type || false;
return viewedResourceType;
};
export const canEditResource = (state) => {
const canEdit = state?.gnresource?.permissions?.canEdit;
const perms = state?.gnresource?.data?.perms || [];
return canEdit || perms.includes('change_resourcebase');
};
export const canViewResource = (state) => {
const canView = state?.gnresource?.permissions?.canView;
const perms = state?.gnresource?.data?.perms || [];
return canView || perms.includes('view_resourcebase');
};
export const canAddResource = (state) => {
return state?.security?.user?.perms?.includes("add_resource");
};
export const isNewResource = (state) => {
return !!state?.gnresource?.isNew;
};
export const getResourceData = (state) => {
return state?.gnresource?.data;
};
export const getLayerResourceData = (state) => {
return state?.gnresource?.layerDataset;
};
export const getSelectedLayer = (state) => {
return state?.gnresource?.selectedLayer;
};
export const getSelectedLayerDataset = (state) => {
return state?.gnresource?.selectedLayerDataset;
};
export const isResourceDetail = (state) => {
return get(state, 'gnresource.data["@ms-detail"]', false);
};
export const getCompactPermissions = (state) => {
const compactPermissions = state?.gnresource?.compactPermissions || {};
return compactPermissions;
};
export const getPermissionsPayload = (state) => {
const compactPermissions = state?.gnresource?.compactPermissions;
const isCompactPermissionsChanged = state?.gnresource?.isCompactPermissionsChanged;
const geoLimits = state?.gnresource?.geoLimits;
return {
compactPermissions: (isCompactPermissionsChanged || geoLimits?.length > 0) && compactPermissions ? compactPermissions : null,
geoLimits: geoLimits?.length > 0 ? geoLimits : null
};
};
const inheritsPerms = (user = null, groups = []) => {
return user && groups.some(group => user.info.groups.some(userGroup => userGroup === group.name) && group.permissions === 'manage') || false;
};
export const canEditPermissions = (state) => {
const compactPermissions = getCompactPermissions(state);
const users = compactPermissions.users || [];
const groups = compactPermissions.groups || [];
const organizations = compactPermissions.organizations || [];
const user = state?.security?.user;
const { permissions } = user && users.find(({ id }) => id === user.pk) || {};
return ['owner', 'manage'].includes(permissions) || inheritsPerms(user, groups) || inheritsPerms(user, organizations);
};
export const canManageResourcePermissions = (state) => {
const perms = getResourcePerms(state);
return perms.includes('change_resourcebase_permissions');
};
export const getSelectedLayerPermissions = (state) => {
const selectedLayerPermissions = state?.gnresource?.selectedLayerPermissions;
return selectedLayerPermissions;
};
export const getResourceExtent = (state) => {
return state?.gnresource?.data?.extent || {};
};
export const getDataPayload = (state, resourceType) => {
const type = resourceType || state?.gnresource?.type;
switch (type) {
case ResourceTypes.MAP: {
const isMapAvailable = !!mapSelector(state);
return isMapAvailable ? mapSaveSelector(state) : null;
}
case ResourceTypes.GEOSTORY: {
return currentStorySelector(state);
}
case ResourceTypes.DASHBOARD: {
return widgetsConfig(state);
}
case ResourceTypes.VIEWER: {
const { data } = generateContextResource(state) || {};
const { mapConfig, ...mapViewerConfig } = data || {};
return mapViewerConfig || {};
}
case ResourceTypes.DATASET: {
let currentLayerSettings = layerSettingSelector(state)?.options ?? {};
currentLayerSettings = omitBy(currentLayerSettings,
(value, key) => key === "opacity" && value === 1); // skip default value
const selectedLayer = getSelectedNode(state);
const omitKeys = ['extendedParams', 'availableStyles', 'infoFormats', 'style'];
const data = saveLayer(selectedLayer ?? {});
return omit({
...data,
...currentLayerSettings,
...(selectedLayer && {fields: selectedLayer?.fields ?? {}})
}, omitKeys);
}
default:
return null;
}
};
export const getExtentPayload = (state, resourceType) => {
const type = resourceType || state?.gnresource?.type;
if (![ResourceTypes.DATASET, ResourceTypes.MAP].includes(type)) {
return getResourceExtent(state);
}
return null;
};
const compareObjects = (obj1, obj2) => {
if (!isEmpty(obj1) && !isEmpty(obj2)) {
return Object.keys(obj1).every((key) => {
const val1 = obj1[key];
const val2 = obj2?.[key];
if (isNil(val2)) return true;
if (typeof val1 === 'boolean') return val1 === (val2 ?? false);
if (typeof val1 === 'number') return val1 === (val2 ?? 0);
if (isEmpty(val1) && isEmpty(val2)) return true;
return isEqual(obj2?.[key], obj1[key]);
});
}
return false;
};
function removeProperty(value, paths) {
if (isArray(value)) {
return value.map(val => removeProperty(val, paths));
}
if (isObject(value)) {
return Object.keys(omit(value, paths))
.reduce((acc, key) => ({
...acc,
[key]: removeProperty(value[key], paths)
}), {});
}
return value;
}
function isMapCenterEqual(initialCenter = {}, currentCenter = {}) {
const CENTER_EPS = 1e-12;
return initialCenter.crs === currentCenter.crs && Math.abs(initialCenter.x - currentCenter.x) < CENTER_EPS && Math.abs(initialCenter.y - currentCenter.y) < CENTER_EPS;
}
export const getInitialDatasetResource = (state) => {
const initialResource = state?.gnresource?.initialResource;
return initialResource && initialResource.resource_type === ResourceTypes.DATASET ? initialResource : null;
};
export const getInitialDatasetLayer = (state) => {
const initialResource = getInitialDatasetResource(state);
return initialResource && resourceToLayerConfig(omit(initialResource, ['default_style']));
};
export const getInitialDatasetLayerStyle = (state) => {
const initialResource = getInitialDatasetResource(state);
return initialResource ? resourceToLayerConfig(initialResource)?.style : null;
};
function isResourceDataEqual(state, initialData = {}, currentData = {}) {
const resourceType = state?.gnresource?.type;
if (isEmpty(initialData) || isEmpty(currentData)) {
return true;
}
switch (resourceType) {
case ResourceTypes.MAP: {
return compareMapChanges(
removeProperty(initialData, ['extraParams', 'getFeatureInfo', 'store', 'capability', 'extendedParams', 'availableStyles', 'center', 'zoom', 'bbox']),
removeProperty(currentData, ['extraParams', 'getFeatureInfo', 'store', 'capability', 'extendedParams', 'availableStyles', 'center', 'zoom', 'bbox'])
);
}
case ResourceTypes.GEOSTORY: {
return isEqual(
removeProperty(initialData, ['fontFamilies']),
removeProperty(currentData, ['fontFamilies'])
);
}
case ResourceTypes.DASHBOARD: {
const initialWidgets = (initialData?.widgets || []);
// check if the current dashboard was built after mapstore update
// the mapstore submodule update introduces breaking changes which must be catered for
// If the dashboard was created with the new features from mapstore, it supports multiple map widgets
const supportsMultipleWidgets = initialWidgets.every((widget) => {
if (widget.widgetType === 'map') {
return !!widget.maps;
}
return true;
});
const isWidgetMapCenterChanged = !!(currentData?.widgets || [])
.find((widget) => {
if (widget.widgetType === 'map') {
const initialWidget = initialWidgets.find(({ id }) => id === widget.id);
const currentWidgets = [...widget.maps];
const allCentresEqual = supportsMultipleWidgets ? currentWidgets.every((mapWidget) => {
const relatedWidget = initialWidget?.maps?.find(w => w.mapId === mapWidget.mapId) || {};
return isMapCenterEqual(relatedWidget?.center, mapWidget?.center);
}) : isMapCenterEqual(initialWidget?.map?.center, currentWidgets[0].center);
return initialWidget ? !allCentresEqual : true;
}
return false;
});
const newCurrentData = supportsMultipleWidgets ? currentData : {
...currentData,
widgets: currentData.widgets.map(widget => {
if (!!widget.maps) {
const mapList = widget.maps;
delete widget.maps;
widget.map = mapList[0];
}
return widget;
})
};
const initialListItemsToRemove = ['bbox', 'size', 'center', 'layouts'];
const currentListItemsToRemove = !supportsMultipleWidgets ? ['bbox', 'size', 'center', 'layouts', 'mapId', 'dependenciesMap', 'selectedMapId'] : initialListItemsToRemove;
return isEqual(
removeProperty(initialData, initialListItemsToRemove),
removeProperty(newCurrentData, currentListItemsToRemove)
) && !isWidgetMapCenterChanged;
}
case ResourceTypes.VIEWER: {
return isEqual(
removeProperty(initialData, ['mapConfig']),
removeProperty(currentData, ['mapConfig'])
);
}
case ResourceTypes.DATASET: {
const selectedLayer = getSelectedNode(state);
const selectedLayerInitial = getSelectedLayer(state);
const initialLayerData = {...selectedLayerInitial, ...initialData};
const initialStyle = getInitialDatasetLayerStyle(state);
const isSettingsEqual = compareObjects(omit(currentData, ['style', 'fields']),
omit(initialLayerData, ['style', 'fields', 'extendedParams', 'pk', '_v_', 'isDataset', 'perms']));
const isStyleEqual = isEmpty(initialStyle) || isEmpty(selectedLayer?.style) ? true
: selectedLayer?.style === initialStyle;
const isAttributesEqual = isEmpty(selectedLayer) ? true
: !isEmpty(initialLayerData) && isEqual(
isEmpty(initialLayerData?.fields) ? {} : initialLayerData?.fields,
isEmpty(selectedLayer?.fields) ? {} : selectedLayer?.fields
);
return isSettingsEqual && isAttributesEqual && isStyleEqual;
}
default:
return true;
}
}
export const isNewResourcePk = (state) => {
return state?.gnresource?.params?.pk === "new";
};
export const isNewMapDirty = (state) => {
const mapConfigRawData = state?.mapConfigRawData;
if (!mapConfigRawData) {
return false;
}
const currentMapData = mapSaveSelector(state);
return !compareMapChanges(mapConfigRawData, currentMapData);
};
export const isNewDashboardDirty = (state) => {
const currentData = getDataPayload(state, ResourceTypes.DASHBOARD);
return (currentData?.widgets || []).length > 0;
};
export const isNewGeoStoryDirty = (state) => {
const currentData = getDataPayload(state, ResourceTypes.GEOSTORY);
if (!currentData) return false;
const defaultConfig = currentStorySelector(state)?.defaultGeoStoryConfig ?? {};
return (
currentData.sections?.length > 1 || // More than the default title section
currentData.sections?.[0]?.contents?.[0]?.html?.trim() || // Title section has content
currentData.sections?.[0]?.title !== defaultConfig.sections?.[0]?.title || // Title changed from default
currentData.resources?.length > 0 || // Has resources
!isEqual( // Settings changed from default
omitBy(currentData.settings || {}, isNil),
omitBy(defaultConfig.settings || {}, isNil)
)
);
};
const isNewResourceDirty = (state) => {
const resourceType = state?.gnresource?.type;
switch (resourceType) {
case ResourceTypes.MAP:
return isNewMapDirty(state);
case ResourceTypes.VIEWER:
return true;
case ResourceTypes.DASHBOARD:
return isNewDashboardDirty(state);
case ResourceTypes.GEOSTORY:
return isNewGeoStoryDirty(state);
default:
return false;
}
};
export const getResourceDirtyState = (state) => {
if (isNewResourcePk(state)) {
return isNewResourceDirty(state);
}
const canEdit = canEditPermissions(state);
const isDeleting = getCurrentResourceDeleteLoading(state);
const isCopying = getCurrentResourceCopyLoading(state);
if (!canEdit || isDeleting || isCopying) {
return null;
}
const resourceType = state?.gnresource?.type;
let metadataKeys = ['title', 'abstract', 'data', 'extent', 'group', ...RESOURCE_MANAGEMENT_PROPERTIES_KEYS];
if (resourceType === ResourceTypes.DATASET) {
metadataKeys = metadataKeys.concat('timeseries');
}
let { data: initialData = {}, ...resource } = pick(state?.gnresource?.initialResource || {}, metadataKeys);
if (isResourceDetail(state)) initialData = {}; // detail page allows only metadata editing. Data is not editable.
const { compactPermissions, geoLimits } = getPermissionsPayload(state);
const currentData = JSON.parse(JSON.stringify(getDataPayload(state) || {})); // JSON stringify is needed to remove undefined values
// omitting data on thumbnail
const thumbnailData = ['thumbnail_url', 'thumbnailChanged', 'updatingThumbnail'];
const newMetadata = omit(state?.gnresource?.data, thumbnailData) || {};
const newResource = pick(newMetadata, metadataKeys);
const isDataChanged = !isResourceDataEqual(state, initialData, currentData);
const isMetadataChanged = !!(!isEmpty(newResource) && !isEmpty(resource) && !isEqual(newResource, resource));
return (!!compactPermissions || !!geoLimits || isDataChanged || isMetadataChanged)
? {
compactPermissions,
geoLimits,
resource: isMetadataChanged ? newMetadata : undefined,
data: isDataChanged ? currentData : undefined,
resourceType,
pk: newMetadata.pk
}
: null;
};
/**
* Get geonode resources from within a Geostory
* @param {Object} state App state
* @returns {Array} Array of geonode resources
*/
export const getGeoNodeResourceDataFromGeoStory = (state) => get(currentStorySelector(state), 'resources', [])
.filter(res => res?.data?.sourceId === 'geonode')
.reduce((acc, resource) => {
if (['image', 'video'].includes(resource.type) && resource.id) {
return { ...acc, documents: [...acc.documents, resource.id] };
}
if (resource.type === 'map' && resource.id) {
return { ...acc, maps: [...acc.maps, resource.id] };
}
return acc;
}, { maps: [], documents: [] });
export const getGeoNodeResourceFromDashboard = (state) => get(originalDataSelector(state), 'widgets', [])
.reduce((acc, { maps, ...widget }) => {
if (maps && widget.widgetType === 'map') {
return {
...acc,
maps: [
...acc.maps,
...maps
.filter((map) => !!map?.extraParams?.pk)
.map((map) => map.extraParams.pk)
]
};
}
if (widget.widgetType === 'map' && widget.map?.extraParams?.pk) {
return {
...acc,
maps: [ ...acc.maps, widget.map?.extraParams?.pk ]
};
}
return acc;
}, { maps: [] });
export const defaultViewerPluginsSelector = (state) => {
return state?.gnresource?.defaultViewerPlugins ?? [];
};
export const getResourceLoading = (state) => state?.gnresource?.loading;