Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions x-pack/legacy/plugins/maps/public/actions/map_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const ADD_WAITING_FOR_MAP_READY_LAYER = 'ADD_WAITING_FOR_MAP_READY_LAYER'
export const CLEAR_WAITING_FOR_MAP_READY_LAYER_LIST = 'CLEAR_WAITING_FOR_MAP_READY_LAYER_LIST';
export const REMOVE_LAYER = 'REMOVE_LAYER';
export const TOGGLE_LAYER_VISIBLE = 'TOGGLE_LAYER_VISIBLE';
export const SET_LAYER_VISIBILITY = 'SET_LAYER_VISIBILITY';
export const MAP_EXTENT_CHANGED = 'MAP_EXTENT_CHANGED';
export const MAP_READY = 'MAP_READY';
export const MAP_DESTROYED = 'MAP_DESTROYED';
Expand Down Expand Up @@ -72,6 +73,7 @@ export const DISABLE_TOOLTIP_CONTROL = 'DISABLE_TOOLTIP_CONTROL';
export const HIDE_TOOLBAR_OVERLAY = 'HIDE_TOOLBAR_OVERLAY';
export const HIDE_LAYER_CONTROL = 'HIDE_LAYER_CONTROL';
export const HIDE_VIEW_CONTROL = 'HIDE_VIEW_CONTROL';
export const SET_WAITING_FOR_READY_HIDDEN_LAYERS = 'SET_WAITING_FOR_READY_HIDDEN_LAYERS';

function getLayerLoadingCallbacks(dispatch, layerId) {
return {
Expand Down Expand Up @@ -252,30 +254,44 @@ export function cleanTooltipStateForLayer(layerId, layerFeatures = []) {
};
}

export function toggleLayerVisible(layerId) {
export function setLayerVisibility(layerId, makeVisible) {
return async (dispatch, getState) => {
//if the current-state is invisible, we also want to sync data
//e.g. if a layer was invisible at start-up, it won't have any data loaded
const layer = getLayerById(layerId, getState());
if (!layer) {

// If the layer visibility is already what we want it to be, do nothing
if (!layer || layer.isVisible() === makeVisible) {
return;
}
const makeVisible = !layer.isVisible();

if (!makeVisible) {
dispatch(cleanTooltipStateForLayer(layerId));
}

await dispatch({
type: TOGGLE_LAYER_VISIBLE,
type: SET_LAYER_VISIBILITY,
layerId,
visibility: makeVisible,
});
if (makeVisible) {
dispatch(syncDataForLayer(layerId));
}
};
}

export function toggleLayerVisible(layerId) {
return async (dispatch, getState) => {
const layer = getLayerById(layerId, getState());
if (!layer) {
return;
}
const makeVisible = !layer.isVisible();

dispatch(setLayerVisibility(layerId, makeVisible));
};
}

export function setSelectedLayer(layerId) {
return async (dispatch, getState) => {
const oldSelectedLayer = getSelectedLayerId(getState());
Expand Down Expand Up @@ -840,3 +856,17 @@ export function hideLayerControl() {
export function hideViewControl() {
return { type: HIDE_VIEW_CONTROL, hideViewControl: true };
}

export function setHiddenLayers(hiddenLayerIds) {
return (dispatch, getState) => {
const isMapReady = getMapReady(getState());

if (!isMapReady) {
dispatch({ type: SET_WAITING_FOR_READY_HIDDEN_LAYERS, hiddenLayerIds });
} else {
getLayerListRaw(getState()).forEach(layer =>
dispatch(setLayerVisibility(layer.id, !hiddenLayerIds.includes(layer.id)))
);
}
};
}
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/maps/public/embeddable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- **hideToolbarOverlay:** (Boolean) Will disable toolbar, which can be used to navigate to coordinate by entering lat/long and zoom values.
- **hideLayerControl:** (Boolean) Will hide useful layer control, which can be used to hide/show a layer to get a refined view of the map.
- **hideViewControl:** (Boolean) Will hide view control at bottom right of the map, which shows lat/lon values based on mouse hover in the map, this is useful to get coordinate value from a particular point in map.
- **hiddenLayers:** (Array of Strings) Array of layer ids that should be hidden. Any other layers will be set to visible regardless of their value in the layerList used to initialize the embeddable

### Creating a Map embeddable from saved object
```
Expand Down
16 changes: 15 additions & 1 deletion x-pack/legacy/plugins/maps/public/embeddable/map_embeddable.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ import {
hideToolbarOverlay,
hideLayerControl,
hideViewControl,
setHiddenLayers,
} from '../actions/map_actions';
import { setReadOnly, setIsLayerTOCOpen, setOpenTOCDetails } from '../actions/ui_actions';
import { getIsLayerTOCOpen, getOpenTOCDetails } from '../selectors/ui_selectors';
import { getInspectorAdapters, setEventHandlers } from '../reducers/non_serializable_instances';
import { getMapCenter, getMapZoom } from '../selectors/map_selectors';
import { getMapCenter, getMapZoom, getHiddenLayers } from '../selectors/map_selectors';
import { MAP_SAVED_OBJECT_TYPE } from '../../common/constants';

export class MapEmbeddable extends Embeddable {
Expand Down Expand Up @@ -153,6 +154,9 @@ export class MapEmbeddable extends Embeddable {
}

this._store.dispatch(replaceLayerList(this._layerList));
if (this.input.hiddenLayers) {
this._store.dispatch(setHiddenLayers(this.input.hiddenLayers));
}
this._dispatchSetQuery(this.input);
this._dispatchSetRefreshConfig(this.input);

Expand Down Expand Up @@ -244,5 +248,15 @@ export class MapEmbeddable extends Embeddable {
openTOCDetails,
});
}

const hiddenLayerIds = getHiddenLayers(this._store.getState())
.map(layer => layer.id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move .map and .sort to the selector? The selector should return the data in the format needed here.

.sort();

if (!_.isEqual(this.input.hiddenLayers, hiddenLayerIds)) {
this.updateInput({
hiddenLayers: hiddenLayerIds,
});
}
}
}
12 changes: 12 additions & 0 deletions x-pack/legacy/plugins/maps/public/reducers/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
CLEAR_WAITING_FOR_MAP_READY_LAYER_LIST,
REMOVE_LAYER,
TOGGLE_LAYER_VISIBLE,
SET_LAYER_VISIBILITY,
MAP_EXTENT_CHANGED,
MAP_READY,
MAP_DESTROYED,
Expand Down Expand Up @@ -46,6 +47,7 @@ import {
HIDE_TOOLBAR_OVERLAY,
HIDE_LAYER_CONTROL,
HIDE_VIEW_CONTROL,
SET_WAITING_FOR_READY_HIDDEN_LAYERS,
} from '../actions/map_actions';

import { copyPersistentState, TRACKED_LAYER_DESCRIPTOR } from './util';
Expand Down Expand Up @@ -309,6 +311,8 @@ export function map(state = INITIAL_STATE, action) {
};
case TOGGLE_LAYER_VISIBLE:
return updateLayerInList(state, action.layerId, 'visible');
case SET_LAYER_VISIBILITY:
return updateLayerInList(state, action.layerId, 'visible', action.visibility);
case UPDATE_LAYER_STYLE:
const styleLayerId = action.layerId;
return updateLayerInList(state, styleLayerId, 'style', { ...action.style });
Expand Down Expand Up @@ -376,6 +380,14 @@ export function map(state = INITIAL_STATE, action) {
hideViewControl: action.hideViewControl,
},
};
case SET_WAITING_FOR_READY_HIDDEN_LAYERS:
return {
...state,
waitingForMapReadyLayerList: state.waitingForMapReadyLayerList.map(layer => ({
...layer,
visible: !action.hiddenLayerIds.includes(layer.id),
})),
};
default:
return state;
}
Expand Down
2 changes: 2 additions & 0 deletions x-pack/legacy/plugins/maps/public/selectors/map_selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ export const getLayerList = createSelector(
}
);

export const getHiddenLayers = state => getLayerListRaw(state).filter(layer => !layer.visible);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use reselect for this selector definition since it needs to derive its state from getLayerListRaw?

export const getHiddenLayers = createSelector(
  getLayerListRaw,
  (layerDescriptorList) => {
    return layerDescriptorList.filter(layer => !layer.visible);
  }
);


export const getSelectedLayer = createSelector(
getSelectedLayerId,
getLayerList,
Expand Down