diff --git a/docs/plugins/overlays.md b/docs/plugins/overlays.md index ac86b9c1a7d771c858cc472bc0ed7492dfc3a244..b03b1ea17d69ad8e2981987d9e5f62e5521f41e9 100644 --- a/docs/plugins/overlays.md +++ b/docs/plugins/overlays.md @@ -22,12 +22,17 @@ window.minerva.overlays.data.getVisibleDataOverlays(); #### Show an overlay -To show an overlay, plugins can use the `showDataOverlay` method defined in `window.minerva.overlays`. This method takes one argument: the ID of the overlay that the plugin wants to show. +To show an overlay, plugins can use the `showDataOverlay` method defined in `window.minerva.overlays`. This method takes following arguments: + +- overlayId - the ID of the overlay that the plugin wants to show. +- setBackgroundEmpty (optional) - whether `showDataOverlay` should set the background to empty if available when it shows overlay. Its value should be a boolean type. ##### Example of showDataOverlay usage: ```javascript window.minerva.overlays.showDataOverlay(109); + +window.minerva.overlays.showDataOverlay(112, true); ``` #### Hide an overlay diff --git a/src/components/Map/Drawer/OverlaysDrawer/hooks/useEmptyBackground.ts b/src/components/Map/Drawer/OverlaysDrawer/hooks/useEmptyBackground.ts index fb2eae6c68e0e26cd2c240a3b2d17adaf75c266f..2536fa84e049dd7dc659040a96933d3287639436 100644 --- a/src/components/Map/Drawer/OverlaysDrawer/hooks/useEmptyBackground.ts +++ b/src/components/Map/Drawer/OverlaysDrawer/hooks/useEmptyBackground.ts @@ -3,7 +3,6 @@ import { emptyBackgroundIdSelector } from '@/redux/backgrounds/background.select import { useAppDispatch } from '@/redux/hooks/useAppDispatch'; import { useAppSelector } from '@/redux/hooks/useAppSelector'; import { setMapBackground } from '@/redux/map/map.slice'; -import { PluginsEventBus } from '@/services/pluginsManager/pluginsEventBus'; type UseEmptyBackgroundReturn = { setBackgroundtoEmptyIfAvailable: () => void; @@ -16,8 +15,6 @@ export const useEmptyBackground = (): UseEmptyBackgroundReturn => { const setBackgroundtoEmptyIfAvailable = useCallback(() => { if (emptyBackgroundId) { dispatch(setMapBackground(emptyBackgroundId)); - - PluginsEventBus.dispatchEvent('onBackgroundOverlayChange', emptyBackgroundId); } }, [dispatch, emptyBackgroundId]); diff --git a/src/components/Map/MapAdditionalOptions/BackgroundsSelector/BackgroundsSelector.component.tsx b/src/components/Map/MapAdditionalOptions/BackgroundsSelector/BackgroundsSelector.component.tsx index 7b31ff35d5928f6e36482d9e7dbc92a7badc1f89..ca0dd73392f972a5285211dba89fb71d1a87a102 100644 --- a/src/components/Map/MapAdditionalOptions/BackgroundsSelector/BackgroundsSelector.component.tsx +++ b/src/components/Map/MapAdditionalOptions/BackgroundsSelector/BackgroundsSelector.component.tsx @@ -10,7 +10,6 @@ import { Icon } from '@/shared/Icon'; import { MapBackground } from '@/types/models'; import { useAppDispatch } from '@/redux/hooks/useAppDispatch'; import { setMapBackground } from '@/redux/map/map.slice'; -import { PluginsEventBus } from '@/services/pluginsManager/pluginsEventBus'; const DEFAULT_TOGGLE_BUTTON_TEXT = 'Background'; @@ -23,7 +22,6 @@ export const BackgroundSelector = (): JSX.Element => { const onItemSelect = (background: MapBackground | undefined | null): void => { if (background) { dispatch(setMapBackground(background.id)); - PluginsEventBus.dispatchEvent('onBackgroundOverlayChange', background.id); } }; diff --git a/src/redux/map/map.reducers.ts b/src/redux/map/map.reducers.ts index b2fbff2d89f78e038e0f995d85b1641031d1907d..d1562cadbbc7f31647c4baae6d9cd6d3836f17b3 100644 --- a/src/redux/map/map.reducers.ts +++ b/src/redux/map/map.reducers.ts @@ -144,6 +144,10 @@ export const closeMapAndSetMainMapActiveReducer = ( }; export const setMapBackgroundReducer = (state: MapState, action: SetBackgroundAction): void => { + if (action.payload !== state.data.backgroundId) { + PluginsEventBus.dispatchEvent('onBackgroundOverlayChange', action.payload); + } + state.data.backgroundId = action.payload; }; diff --git a/src/redux/overlayBioEntity/overlayBioEntity.thunk.ts b/src/redux/overlayBioEntity/overlayBioEntity.thunk.ts index 30222f300c8a31f6d6104e23634c9d9b0d6d33ac..956b6751b59b785cf082e89b5630f65d8acaf08f 100644 --- a/src/redux/overlayBioEntity/overlayBioEntity.thunk.ts +++ b/src/redux/overlayBioEntity/overlayBioEntity.thunk.ts @@ -100,7 +100,6 @@ export const getInitOverlays = createAsyncThunk< const emptyBackgroundId = emptyBackgroundIdSelector(state); if (emptyBackgroundId) { dispatch(setMapBackground(emptyBackgroundId)); - PluginsEventBus.dispatchEvent('onBackgroundOverlayChange', emptyBackgroundId); } overlaysId.forEach(id => { diff --git a/src/services/pluginsManager/map/overlays/addDataOverlay/addDataOverlay.test.ts b/src/services/pluginsManager/map/overlays/addDataOverlay/addDataOverlay.test.ts index a99220a5ba5f573645b14dcd3ed792d2ca71409d..729d53cb97d470de7a70414541689cd034f20165 100644 --- a/src/services/pluginsManager/map/overlays/addDataOverlay/addDataOverlay.test.ts +++ b/src/services/pluginsManager/map/overlays/addDataOverlay/addDataOverlay.test.ts @@ -9,6 +9,10 @@ import { RootState, store } from '@/redux/store'; import { mockNetworkResponse } from '@/utils/mockNetworkResponse'; import { HttpStatusCode } from 'axios'; import { addOverlay } from '@/redux/overlays/overlays.thunks'; +import { + ERROR_OVERLAY_NAME_NOT_PROVIDED, + ERROR_PROJECT_ID_NOT_FOUND, +} from '@/services/pluginsManager/errorMessages'; import { addDataOverlay } from './addDataOverlay'; import { DEFAULT_FILE_NAME, DEFAULT_TYPE } from './addDataOverlay.constants'; @@ -82,7 +86,7 @@ describe('addDataOverlay', () => { }) as RootState, ); - await expect(() => addDataOverlay(overlay)).rejects.toThrow('Project id does not exist'); + await expect(() => addDataOverlay(overlay)).rejects.toThrow(ERROR_PROJECT_ID_NOT_FOUND); }); it('should throw error when overlay name is not provided', async () => { @@ -94,7 +98,7 @@ describe('addDataOverlay', () => { }; await expect(addDataOverlay(overlayWithoutName)).rejects.toThrow( - 'Overlay name is not provided', + ERROR_OVERLAY_NAME_NOT_PROVIDED, ); }); diff --git a/src/services/pluginsManager/map/overlays/showDataOverlay/setBackgroundtoEmptyIfAvailable.test.ts b/src/services/pluginsManager/map/overlays/showDataOverlay/setBackgroundtoEmptyIfAvailable.test.ts index f8c875ee1ea0e264f58df92b5072415e5bc37d9f..15b32c94cf7c96ce2acb9972717a7e3d412ee20b 100644 --- a/src/services/pluginsManager/map/overlays/showDataOverlay/setBackgroundtoEmptyIfAvailable.test.ts +++ b/src/services/pluginsManager/map/overlays/showDataOverlay/setBackgroundtoEmptyIfAvailable.test.ts @@ -4,7 +4,6 @@ import { } from '@/redux/backgrounds/background.mock'; import { initialMapStateFixture } from '@/redux/map/map.fixtures'; import { RootState, store } from '@/redux/store'; -import { PluginsEventBus } from '@/services/pluginsManager/pluginsEventBus'; import { setBackgroundtoEmptyIfAvailable } from './setBackgroundtoEmptyIfAvailable'; const DEFAULT_BACKGROUND_ID = 0; @@ -50,21 +49,4 @@ describe('setEmptyBackground', () => { type: 'map/setMapBackground', }); }); - it('should dispatch plugin event with empty background id', () => { - const pluginDispatchEvent = jest.spyOn(PluginsEventBus, 'dispatchEvent'); - getStateSpy.mockImplementation( - () => - ({ - map: initialMapStateFixture, - backgrounds: { ...BACKGROUND_INITIAL_STATE_MOCK, data: BACKGROUNDS_MOCK }, - }) as RootState, - ); - - setBackgroundtoEmptyIfAvailable(); - - expect(pluginDispatchEvent).toHaveBeenCalledWith( - 'onBackgroundOverlayChange', - EMPTY_BACKGROUND_ID, - ); - }); }); diff --git a/src/services/pluginsManager/map/overlays/showDataOverlay/setBackgroundtoEmptyIfAvailable.ts b/src/services/pluginsManager/map/overlays/showDataOverlay/setBackgroundtoEmptyIfAvailable.ts index 2d23f45e1ae0c5db004ef81a3c5b9e4ae5875c33..59c6c06103cdaff3be28cfaaa149e1337f8e39f1 100644 --- a/src/services/pluginsManager/map/overlays/showDataOverlay/setBackgroundtoEmptyIfAvailable.ts +++ b/src/services/pluginsManager/map/overlays/showDataOverlay/setBackgroundtoEmptyIfAvailable.ts @@ -1,7 +1,6 @@ import { emptyBackgroundIdSelector } from '@/redux/backgrounds/background.selectors'; import { setMapBackground } from '@/redux/map/map.slice'; import { store } from '@/redux/store'; -import { PluginsEventBus } from '@/services/pluginsManager/pluginsEventBus'; export const setBackgroundtoEmptyIfAvailable = (): void => { const { dispatch, getState } = store; @@ -9,7 +8,5 @@ export const setBackgroundtoEmptyIfAvailable = (): void => { if (emptyBackgroundId) { dispatch(setMapBackground(emptyBackgroundId)); - - PluginsEventBus.dispatchEvent('onBackgroundOverlayChange', emptyBackgroundId); } }; diff --git a/src/services/pluginsManager/map/overlays/showDataOverlay/showDataOverlay.test.ts b/src/services/pluginsManager/map/overlays/showDataOverlay/showDataOverlay.test.ts index 3276622cd5ff7a67104289099bea18fdd9476366..3ccebefa0b896ad8aeaac85e2c6ebd3e7b32ec19 100644 --- a/src/services/pluginsManager/map/overlays/showDataOverlay/showDataOverlay.test.ts +++ b/src/services/pluginsManager/map/overlays/showDataOverlay/showDataOverlay.test.ts @@ -108,7 +108,7 @@ describe('showDataOverlay function', () => { expect(pluginDispatchEvent).toHaveBeenCalledWith('onShowOverlay', overlaysFixture[0]); }); - it('should set empty background when it show overlay', () => { + it('should not set empty background when it show overlay', () => { getStateSpy.mockImplementation( () => ({ @@ -126,6 +126,26 @@ describe('showDataOverlay function', () => { showDataOverlay(OVERLAY_ID); + expect(setBackgroundtoEmptyIfAvailable).not.toHaveBeenCalled(); + }); + it('should set empty background when it show overlay if setBackgroundEmpty is true', () => { + getStateSpy.mockImplementation( + () => + ({ + overlays: { + ...OVERLAYS_INITIAL_STATE_MOCK, + userOverlays: { + data: overlaysFixture, + loading: 'idle', + error: DEFAULT_ERROR, + }, + }, + overlayBioEntity: { ...OVERLAY_BIO_ENTITY_INITIAL_STATE_MOCK }, + }) as RootState, + ); + + showDataOverlay(OVERLAY_ID, true); + expect(setBackgroundtoEmptyIfAvailable).toHaveBeenCalled(); }); }); diff --git a/src/services/pluginsManager/map/overlays/showDataOverlay/showDataOverlay.ts b/src/services/pluginsManager/map/overlays/showDataOverlay/showDataOverlay.ts index b299debf582629db6a07abcf016ee39691c4cca5..8304beed8c8ddf35a0ec97f6f9d4ce90cb76efce 100644 --- a/src/services/pluginsManager/map/overlays/showDataOverlay/showDataOverlay.ts +++ b/src/services/pluginsManager/map/overlays/showDataOverlay/showDataOverlay.ts @@ -9,7 +9,7 @@ import { } from '@/services/pluginsManager/errorMessages'; import { setBackgroundtoEmptyIfAvailable } from './setBackgroundtoEmptyIfAvailable'; -export const showDataOverlay = (overlayId: number): void => { +export const showDataOverlay = (overlayId: number, setBackgroundEmpty?: boolean): void => { const { dispatch, getState } = store; const state = getState(); const isOverlayActive = isOverlayActiveSelector(state, overlayId); @@ -24,7 +24,9 @@ export const showDataOverlay = (overlayId: number): void => { throw new Error(ERROR_OVERLAY_ID_ALREADY_ACTIVE); } - setBackgroundtoEmptyIfAvailable(); + if (setBackgroundEmpty) { + setBackgroundtoEmptyIfAvailable(); + } dispatch(getOverlayBioEntityForAllModels({ overlayId })); PluginsEventBus.dispatchEvent('onShowOverlay', matchingOverlay);