Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add removeLayer action #1157

Merged
merged 2 commits into from
Oct 18, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions web/client/actions/__tests__/layers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var {
LAYER_LOAD,
LAYER_ERROR,
ADD_LAYER,
REMOVE_LAYER,
SHOW_SETTINGS,
HIDE_SETTINGS,
UPDATE_SETTINGS,
Expand All @@ -29,6 +30,7 @@ var {
layerLoad,
layerError,
addLayer,
removeLayer,
showSettings,
hideSettings,
updateSettings
Expand Down Expand Up @@ -127,6 +129,15 @@ describe('Test correctness of the layers actions', () => {
expect(retval.layer).toBe(testVal);
});

it('remove layer', () => {
const testVal = 'layerid1';
const retval = removeLayer(testVal);

expect(retval).toExist();
expect(retval.type).toBe(REMOVE_LAYER);
expect(retval.layerId).toBe(testVal);
});

it('show settings', () => {
const action = showSettings("node1", "layers", {opacity: 0.5});
expect(action).toExist();
Expand Down
12 changes: 10 additions & 2 deletions web/client/actions/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const LAYER_LOAD = 'LAYER_LOAD';
const LAYER_ERROR = 'LAYER_ERROR';
const INVALID_LAYER = 'INVALID_LAYER';
const ADD_LAYER = 'ADD_LAYER';
const REMOVE_LAYER = 'REMOVE_LAYER';
const SHOW_SETTINGS = 'SHOW_SETTINGS';
const HIDE_SETTINGS = 'HIDE_SETTINGS';
const UPDATE_SETTINGS = 'UPDATE_SETTINGS';
Expand Down Expand Up @@ -132,6 +133,13 @@ function addLayer(layer) {
};
}

function removeLayer(layerId) {
return {
type: REMOVE_LAYER,
layerId: layerId
};
}

function invalidLayer(layerType, options) {
return {
type: INVALID_LAYER,
Expand Down Expand Up @@ -218,9 +226,9 @@ function getLayerCapabilities(layer, options) {


module.exports = {changeLayerProperties, changeGroupProperties, toggleNode, sortNode, removeNode, invalidLayer,
updateNode, layerLoading, layerLoad, layerError, addLayer, showSettings, hideSettings, updateSettings,
updateNode, layerLoading, layerLoad, layerError, addLayer, removeLayer, showSettings, hideSettings, updateSettings,
getDescribeLayer, getLayerCapabilities,
CHANGE_LAYER_PROPERTIES, CHANGE_GROUP_PROPERTIES, TOGGLE_NODE, SORT_NODE,
REMOVE_NODE, UPDATE_NODE, LAYER_LOADING, LAYER_LOAD, LAYER_ERROR, ADD_LAYER,
REMOVE_NODE, UPDATE_NODE, LAYER_LOADING, LAYER_LOAD, LAYER_ERROR, ADD_LAYER, REMOVE_LAYER,
SHOW_SETTINGS, HIDE_SETTINGS, UPDATE_SETTINGS, INVALID_LAYER
};
44 changes: 44 additions & 0 deletions web/client/reducers/__tests__/layers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,50 @@ describe('Test the layers reducer', () => {
expect(state.groups[0].nodes[0]).toBe("test_id");
});

it('remove layer', () => {
let addAction = {
type: "ADD_LAYER",
layer: {group: "group1", id: "test_id1"}
};
let state = layers({}, addAction);

addAction = {
type: "ADD_LAYER",
layer: {group: "group1", id: "test_id2"}
};
state = layers(state, addAction);
addAction = {
type: "ADD_LAYER",
layer: {group: "group2", id: "test_id3"}
};
state = layers(state, addAction);

let removeAction = {
type: "REMOVE_LAYER",
layerId: "test_id1"
};
state = layers(state, removeAction);

/* bogous on purpose */
removeAction = {
type: "REMOVE_LAYER",
layerId: "test_id4"
};
state = layers(state, removeAction);

expect(state).toExist();
expect(state.flat).toExist();
expect(state.flat).toExclude({group: "group1", id: "test_id1"});
expect(state.flat).toInclude({group: "group1", id: "test_id2"});
expect(state.flat).toInclude({group: "group2", id: "test_id3"});
expect(state.groups).toExist();
expect(state.groups[1].nodes).toExclude('test_id1');
expect(state.groups[1].nodes).toInclude('test_id2');
expect(state.groups[1].name).toBe('group1');
expect(state.groups[0].nodes).toInclude('test_id3');
expect(state.groups[0].name).toBe('group2');
});

it('show settings', () => {
const action = {
type: "SHOW_SETTINGS",
Expand Down
19 changes: 18 additions & 1 deletion web/client/reducers/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

var {LAYER_LOADING, LAYER_LOAD, LAYER_ERROR, CHANGE_LAYER_PROPERTIES, CHANGE_GROUP_PROPERTIES,
TOGGLE_NODE, SORT_NODE, REMOVE_NODE, UPDATE_NODE, ADD_LAYER,
TOGGLE_NODE, SORT_NODE, REMOVE_NODE, UPDATE_NODE, ADD_LAYER, REMOVE_LAYER,
SHOW_SETTINGS, HIDE_SETTINGS, UPDATE_SETTINGS, INVALID_LAYER
} = require('../actions/layers');

Expand Down Expand Up @@ -264,6 +264,23 @@ function layers(state = [], action) {
groups: newGroups
};
}
case REMOVE_LAYER: {
let layer = head((state.flat || []).filter(obj => obj.id === action.layerId));
let groupName = layer && layer.group ? layer.group : 'Default';
let newLayers = (state.flat || []).filter(lyr => lyr.id !== action.layerId);
let newGroups = (state.groups || []).concat();
let gidx = newGroups.findIndex(entry => entry.id === groupName);
if (gidx >= 0) {
let newGroup = assign({}, newGroups[gidx]);
let nidx = newGroup.nodes.indexOf(action.layerId);
newGroup.nodes = [...newGroup.nodes.slice(0, nidx), ...newGroup.nodes.slice(nidx + 1)];
newGroups[gidx] = newGroup;
}
return {
flat: newLayers,
groups: newGroups
};
}
case SHOW_SETTINGS: {
let settings = assign({}, state.settings, {
expanded: true,
Expand Down