Update operationStraighten.available() function

- operationStraighten now allows for two vertices to be selected and straightened between
 - Added test cases
This commit is contained in:
J Guthrie
2019-01-21 17:39:46 +00:00
committed by Bryan Housel
parent f2722fe95f
commit 16513b1194
2 changed files with 26 additions and 10 deletions
+13 -2
View File
@@ -18,13 +18,19 @@ export function operationStraighten(selectedIDs, context) {
operation.available = function() {
var nodes = [],
startNodes = [],
endNodes = [];
endNodes = [],
selectedNodes = [];
for (var i = 0; i < selectedIDs.length; i++) {
if (!context.hasEntity(selectedIDs[i])) return false;
var entity = context.entity(selectedIDs[i]);
if (entity.type === 'node') {
selectedNodes.push(entity.id);
continue;
}
if (entity.type !== 'way' ||
entity.isClosed()) {
return false;
@@ -35,12 +41,17 @@ export function operationStraighten(selectedIDs, context) {
endNodes.push(entity.nodes[entity.nodes.length-1]);
}
if (_uniq(nodes).length <= 2) return false;
if (_uniq(nodes).length <= 2 || selectedNodes.length > 2) return false;
// Ensure all ways are connected (i.e. only one unique start point and one unique end point)
if (_difference(startNodes, endNodes).length !== 1 ||
_difference(endNodes, startNodes).length !== 1) return false;
// Ensure both selected nodes lie on the selected path
if (!selectedNodes.every(function(n) {
return nodes.includes(n);
})) return false;
return true;
};
+13 -8
View File
@@ -10,12 +10,15 @@ describe('iD.operationStraighten', function () {
describe('#available', function () {
beforeEach(function () {
// w1 - way with 2 nodes
// w1-2 - way with 2 nodes connected to w1
// w2 - way with 3 nodes connected to w1
// w3 - way with 3 nodes connected to w2
// w4 - way with 3 nodes not connected to other ways
// w4 - way with 3 nodes connected to w3
// w5 - way with 4 nodes not connected to any other nodes
graph = iD.coreGraph([
iD.osmNode({ id: 'n1', type: 'node' }),
iD.osmNode({ id: 'n2', type: 'node' }),
iD.osmNode({ id: 'n2-1', type: 'node' }),
iD.osmNode({ id: 'n3', type: 'node' }),
iD.osmNode({ id: 'n4', type: 'node' }),
iD.osmNode({ id: 'n5', type: 'node' }),
@@ -25,11 +28,13 @@ describe('iD.operationStraighten', function () {
iD.osmNode({ id: 'n9', type: 'node' }),
iD.osmNode({ id: 'n10', type: 'node' }),
iD.osmNode({ id: 'n11', type: 'node' }),
iD.osmNode({ id: 'n12', type: 'node' }),
iD.osmWay({ id: 'w1', nodes: ['n1', 'n2'] }),
iD.osmWay({ id: 'w1-2', nodes: ['n2', 'n2-1'] }),
iD.osmWay({ id: 'w2', nodes: ['n2', 'n3', 'n4'] }),
iD.osmWay({ id: 'w3', nodes: ['n4', 'n5', 'n6'] }),
iD.osmWay({ id: 'w4', nodes: ['n7', 'n8', 'n9', 'n10'] }),
iD.osmWay({ id: 'w5', nodes: ['n2', 'n11'] })
iD.osmWay({ id: 'w4', nodes: ['n6', 'n7', 'n8'] }),
iD.osmWay({ id: 'w5', nodes: ['n9', 'n10', 'n11', 'n12'] }),
]);
});
@@ -44,7 +49,7 @@ describe('iD.operationStraighten', function () {
});
it('is available for way with only 2 nodes connected to another 2-node way', function () {
var result = iD.operationStraighten(['w1', 'w5'], fakeContext.graph()).available();
var result = iD.operationStraighten(['w1', 'w1-2'], fakeContext.graph()).available();
expect(result).to.be.ok;
});
@@ -54,7 +59,7 @@ describe('iD.operationStraighten', function () {
});
it('is not available for non-continuous ways', function () {
var result = iD.operationStraighten(['w3', 'w4'], fakeContext.graph()).available();
var result = iD.operationStraighten(['w2', 'w4'], fakeContext.graph()).available();
expect(result).to.be.not.ok;
});
@@ -74,17 +79,17 @@ describe('iD.operationStraighten', function () {
});
it('is available for 2 selected nodes in the same way, more than one node apart', function () {
var result = iD.operationStraighten(['n8', 'n10'], fakeContext.graph()).available();
var result = iD.operationStraighten(['w5', 'n9', 'n11'], fakeContext.graph()).available();
expect(result).to.be.ok;
});
it('is available for 2 selected nodes in adjacent ways, more than one node apart', function () {
var result = iD.operationStraighten(['n5', 'n3'], fakeContext.graph()).available();
var result = iD.operationStraighten(['w2', 'w3', 'n5', 'n3'], fakeContext.graph()).available();
expect(result).to.be.ok;
});
it('is available for 2 selected nodes in non-adjacent ways, providing inbetween ways are selected', function () {
var result = iD.operationStraighten(['n9', 'n1', 'w3', 'w2'], fakeContext.graph()).available();
var result = iD.operationStraighten(['n2', 'n7', 'w4', 'w1', 'w3', 'w2'], fakeContext.graph()).available();
expect(result).to.be.ok;
});
});