Files
iD/js/id/svg/midpoints.js
T
2013-12-10 10:17:25 -05:00

65 lines
2.2 KiB
JavaScript

iD.svg.Midpoints = function(projection, context) {
return function drawMidpoints(surface, graph, entities, filter, extent) {
var midpoints = {};
for (var i = 0; i < entities.length; i++) {
var entity = entities[i];
if (entity.type !== 'way') continue;
if (context.selectedIDs().indexOf(entity.id) < 0) continue;
var nodes = graph.childNodes(entity);
// skip the last node because it is always repeated
for (var j = 0; j < nodes.length - 1; j++) {
var a = nodes[j],
b = nodes[j + 1],
id = [a.id, b.id].sort().join('-');
// Redraw midpoints in two cases:
// 1. One of the two endpoint nodes changed (e.g. was moved).
// 2. A node was deleted. The midpoint between the two new
// endpoints needs to be redrawn. In this case only the
// way will be in the diff.
if (!midpoints[id] && (filter(a) || filter(b) || filter(entity))) {
var loc = iD.geo.interp(a.loc, b.loc, 0.5);
if (extent.intersects(loc) && iD.geo.euclideanDistance(projection(a.loc), projection(b.loc)) > 40) {
midpoints[id] = {
type: 'midpoint',
id: id,
loc: loc,
edge: [a.id, b.id]
};
}
}
}
}
var groups = surface.select('.layer-hit').selectAll('g.midpoint')
.filter(filter)
.data(_.values(midpoints), function(d) { return d.id; });
var group = groups.enter()
.insert('g', ':first-child')
.attr('class', 'midpoint');
group.append('circle')
.attr('r', 7)
.attr('class', 'shadow');
group.append('circle')
.attr('r', 3)
.attr('class', 'fill');
groups.attr('transform', iD.svg.PointTransform(projection));
// Propagate data bindings.
groups.select('circle.shadow');
groups.select('circle.fill');
groups.exit()
.remove();
};
};