mirror of
https://github.com/FoggedLens/iD.git
synced 2026-05-16 05:49:16 +02:00
Change the midpoint data a bit
Store the ways which share the segment and the index of the segment. This will be used in both DragWay and Draw behaviors.
This commit is contained in:
@@ -1,29 +1,26 @@
|
||||
iD.behavior.DragMidpoint = function(mode) {
|
||||
var history = mode.history,
|
||||
projection = mode.map.projection,
|
||||
behavior = iD.behavior.drag()
|
||||
projection = mode.map.projection;
|
||||
|
||||
var behavior = iD.behavior.drag()
|
||||
.delegate(".midpoint")
|
||||
.origin(function(d) {
|
||||
return projection(d.loc);
|
||||
})
|
||||
.on('start', function(d) {
|
||||
var w, nds;
|
||||
d.node = iD.Node({loc: d.loc});
|
||||
var args = [iD.actions.AddNode(d.node)];
|
||||
for (var i = 0; i < d.ways.length; i++) {
|
||||
w = d.ways[i], nds = w.nodes;
|
||||
for (var j = 0; j < nds.length; j++) {
|
||||
if ((nds[j] === d.nodes[0] && nds[j + 1] === d.nodes[1]) ||
|
||||
(nds[j] === d.nodes[1] && nds[j + 1] === d.nodes[0])) {
|
||||
args.push(iD.actions.AddWayNode(w.id, d.node.id, j + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
history.perform.apply(history, args);
|
||||
var node = d3.selectAll('.node.vertex')
|
||||
.filter(function(data) { return data.id === d.node.id; });
|
||||
behavior.target(node.node(), node.datum());
|
||||
var node = iD.Node({loc: d.loc});
|
||||
|
||||
var args = [iD.actions.AddNode(node)];
|
||||
for (var i = 0; i < d.ways.length; i++) {
|
||||
args.push(iD.actions.AddWayNode(d.ways[i].id, node.id, d.ways[i].index));
|
||||
}
|
||||
|
||||
history.perform.apply(history, args);
|
||||
|
||||
var vertex = d3.selectAll('.vertex')
|
||||
.filter(function(data) { return data.id === node.id; });
|
||||
|
||||
behavior.target(vertex.node(), vertex.datum());
|
||||
})
|
||||
.on('move', function(d) {
|
||||
d3.event.sourceEvent.stopPropagation();
|
||||
@@ -35,5 +32,6 @@ iD.behavior.DragMidpoint = function(mode) {
|
||||
iD.actions.Noop(),
|
||||
'added a node to a way');
|
||||
});
|
||||
|
||||
return behavior;
|
||||
};
|
||||
|
||||
+9
-11
@@ -15,19 +15,17 @@ iD.svg.Midpoints = function(projection) {
|
||||
b = nodes[j + 1],
|
||||
id = [a.id, b.id].sort().join('-');
|
||||
|
||||
if (!midpoints[id] &&
|
||||
iD.geo.dist(projection(a.loc), projection(b.loc)) > 40) {
|
||||
|
||||
var midpoint_loc = iD.geo.interp(a.loc, b.loc, 0.5),
|
||||
parents = _.intersection(graph.parentWays(a),
|
||||
graph.parentWays(b));
|
||||
if (midpoints[id]) {
|
||||
midpoints[id].ways.push({id: entity.id, index: j + 1});
|
||||
|
||||
} else if (iD.geo.dist(projection(a.loc), projection(b.loc)) > 40) {
|
||||
midpoints[id] = {
|
||||
loc: midpoint_loc,
|
||||
ways: parents,
|
||||
nodes: [a.id, b.id],
|
||||
midpoint: true,
|
||||
id: id,
|
||||
midpoint: true
|
||||
loc: iD.geo.interp(a.loc, b.loc, 0.5),
|
||||
a: a.id,
|
||||
b: b.id,
|
||||
ways: [{id: entity.id, index: j + 1}]
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -35,7 +33,7 @@ iD.svg.Midpoints = function(projection) {
|
||||
|
||||
var groups = surface.select('.layer-hit').selectAll('g.midpoint')
|
||||
.filter(filter)
|
||||
.data(_.values(midpoints), function (d) { return [d.parents, d.id].join(","); });
|
||||
.data(_.values(midpoints), function (d) { return d.id; });
|
||||
|
||||
var group = groups.enter()
|
||||
.insert('g', ':first-child')
|
||||
|
||||
@@ -170,6 +170,7 @@
|
||||
<script src="spec/svg/areas.js"></script>
|
||||
<script src="spec/svg/lines.js"></script>
|
||||
<script src="spec/svg/member_classes.js"></script>
|
||||
<script src="spec/svg/midpoints.js"></script>
|
||||
<script src="spec/svg/multipolygons.js"></script>
|
||||
<script src="spec/svg/points.js"></script>
|
||||
<script src="spec/svg/vertices.js"></script>
|
||||
|
||||
@@ -65,6 +65,7 @@
|
||||
<script src="spec/svg/areas.js"></script>
|
||||
<script src="spec/svg/lines.js"></script>
|
||||
<script src="spec/svg/member_classes.js"></script>
|
||||
<script src="spec/svg/midpoints.js"></script>
|
||||
<script src="spec/svg/multipolygons.js"></script>
|
||||
<script src="spec/svg/points.js"></script>
|
||||
<script src="spec/svg/vertices.js"></script>
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
describe("iD.svg.Midpoints", function () {
|
||||
var surface,
|
||||
projection = Object,
|
||||
filter = d3.functor(true);
|
||||
|
||||
beforeEach(function () {
|
||||
surface = d3.select(document.createElementNS('http://www.w3.org/2000/svg', 'svg'))
|
||||
.call(iD.svg.Surface());
|
||||
});
|
||||
|
||||
it("finds the location of the midpoints", function () {
|
||||
var a = iD.Node({loc: [0, 0]}),
|
||||
b = iD.Node({loc: [50, 0]}),
|
||||
line = iD.Way({nodes: [a.id, b.id]}),
|
||||
graph = iD.Graph([a, b, line]);
|
||||
|
||||
surface.call(iD.svg.Midpoints(projection), graph, [line], filter);
|
||||
|
||||
expect(surface.select('.midpoint').datum().loc).to.eql([25, 0]);
|
||||
});
|
||||
|
||||
it("doesn't create midpoints on segments with pixel length less than 40", function () {
|
||||
var a = iD.Node({loc: [0, 0]}),
|
||||
b = iD.Node({loc: [39, 0]}),
|
||||
line = iD.Way({nodes: [a.id, b.id]}),
|
||||
graph = iD.Graph([a, b, line]);
|
||||
|
||||
surface.call(iD.svg.Midpoints(projection), graph, [line], filter);
|
||||
|
||||
expect(surface.selectAll('.midpoint')[0]).to.have.length(0);
|
||||
});
|
||||
|
||||
it("binds a datum whose 'ways' property lists ways which include the segement", function () {
|
||||
var a = iD.Node({loc: [0, 0]}),
|
||||
b = iD.Node({loc: [50, 0]}),
|
||||
c = iD.Node({loc: [1, 1]}),
|
||||
d = iD.Node({loc: [2, 2]}),
|
||||
l1 = iD.Way({nodes: [a.id, b.id]}),
|
||||
l2 = iD.Way({nodes: [b.id, a.id]}),
|
||||
l3 = iD.Way({nodes: [c.id, a.id, b.id, d.id]}),
|
||||
l4 = iD.Way({nodes: [a.id, d.id, b.id]}),
|
||||
graph = iD.Graph([a, b, c, d, l1, l2, l3, l4]),
|
||||
ab = function (d) { return d.id === [a.id, b.id].sort().join("-"); };
|
||||
|
||||
surface.call(iD.svg.Midpoints(projection), graph, [l1, l2, l3, l4], filter);
|
||||
|
||||
expect(surface.selectAll('.midpoint').filter(ab).datum().ways).to.eql([
|
||||
{id: l1.id, index: 1},
|
||||
{id: l2.id, index: 1},
|
||||
{id: l3.id, index: 2}]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user