Skip to content

Commit

Permalink
Add option to control layer insertion position in addLayer action
Browse files Browse the repository at this point in the history
  • Loading branch information
manisandro committed Oct 24, 2016
1 parent e2ef856 commit c024ad9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
16 changes: 12 additions & 4 deletions web/client/actions/__tests__/layers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,19 @@ describe('Test correctness of the layers actions', () => {

it('add layer', () => {
const testVal = 'layer1';
const retval = addLayer(testVal);
const retval1 = addLayer(testVal);

expect(retval).toExist();
expect(retval.type).toBe(ADD_LAYER);
expect(retval.layer).toBe(testVal);
expect(retval1).toExist();
expect(retval1.type).toBe(ADD_LAYER);
expect(retval1.layer).toBe(testVal);
expect(retval1.foreground).toBe(false);

const retval2 = addLayer(testVal, true);

expect(retval2).toExist();
expect(retval2.type).toBe(ADD_LAYER);
expect(retval2.layer).toBe(testVal);
expect(retval2.foreground).toBe(true);
});

it('remove layer', () => {
Expand Down
5 changes: 3 additions & 2 deletions web/client/actions/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ function layerError(layerId) {
};
}

function addLayer(layer) {
function addLayer(layer, foreground = false) {
return {
type: ADD_LAYER,
layer
layer,
foreground
};
}

Expand Down
44 changes: 39 additions & 5 deletions web/client/reducers/__tests__/layers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,20 +293,54 @@ describe('Test the layers reducer', () => {
});

it('add new layer', () => {
let testAction = {
let testAction1 = {
type: "ADD_LAYER",
layer: {group: "test", id: "test_id"}
layer: {group: "test", id: "test_id1"},
foreground: false
};

let state = layers({}, testAction);
let state = layers({}, testAction1);
expect(state).toExist();
expect(state.flat).toExist();
expect(state.flat[0].group).toExist();
expect(state.flat[0].id).toExist();
expect(state.flat[0].id).toBe("test_id");
expect(state.flat[0].id).toBe("test_id1");
expect(state.groups).toExist();
expect(state.groups[0].name).toBe("test");
expect(state.groups[0].nodes[0]).toBe("test_id1");

let testAction2 = {
type: "ADD_LAYER",
layer: {group: "test", id: "test_id2"},
foreground: false
};

state = layers(state, testAction2);
expect(state).toExist();
expect(state.flat).toExist();
expect(state.flat[0].group).toExist();
expect(state.flat[0].id).toExist();
expect(state.flat[0].id).toBe("test_id2");
expect(state.groups).toExist();
expect(state.groups[0].name).toBe("test");
expect(state.groups[0].nodes[1]).toBe("test_id2");

let testAction3 = {
type: "ADD_LAYER",
layer: {group: "test", id: "test_id3"},
foreground: true
};

state = layers(state, testAction3);
expect(state).toExist();
expect(state.flat).toExist();
expect(state.flat[2].group).toExist();
expect(state.flat[2].id).toExist();
expect(state.flat[2].id).toBe("test_id3");
expect(state.groups).toExist();
expect(state.groups[0].name).toBe("test");
expect(state.groups[0].nodes[0]).toBe("test_id");
expect(state.groups[0].nodes[0]).toBe("test_id3");
expect(state.groups[0].nodes[1]).toBe("test_id1");
});

it('remove layer', () => {
Expand Down
3 changes: 2 additions & 1 deletion web/client/reducers/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ function layers(state = [], action) {
if (groupName !== "background") {
let node = getNode(newGroups, groupName );
if (node) {
newGroups = deepChange(state.groups, groupName, 'nodes', node.nodes.concat(newLayer.id));
let newLayerIds = action.foreground ? [newLayer.id].concat(node.nodes) : node.nodes.concat([newLayer.id]);
newGroups = deepChange(state.groups, groupName, 'nodes', newLayerIds);
} else {
const newGroup = LayersUtils.getLayersByGroup([newLayer]);
newGroups = newGroup.concat(newGroups);
Expand Down

0 comments on commit c024ad9

Please sign in to comment.