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 option to control layer insertion position in addLayer action #1198

Merged
merged 1 commit into from
Oct 24, 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
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