From 60941f701b1c68cfb5f3e8e71a7f78e3b8aefc43 Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Mon, 3 Dec 2012 18:24:47 -0500 Subject: [PATCH] Support way dragging. Fixes #45 --- js/id/renderer/map.js | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/js/id/renderer/map.js b/js/id/renderer/map.js index 08fe2e49b..a6219db8a 100644 --- a/js/id/renderer/map.js +++ b/js/id/renderer/map.js @@ -49,6 +49,38 @@ iD.Map = function() { redraw(); } }), + waydragbehavior = d3.behavior.drag() + .origin(function(entity) { + var p = projection(ll2a(entity.nodes[0])); + return { x: p[0], y: p[1] }; + }) + .on('drag', function(entity) { + d3.event.sourceEvent.stopPropagation(); + + if (!dragging) { + dragging = true; + only = iD.Util.trueObj([entity.id].concat( + _.pluck(map.history.graph().parents(entity.id), 'id'))); + map.history.perform(iD.actions.noop()); + } + + entity.nodes.forEach(function(node) { + var start = projection(ll2a(node)); + var end = projection.invert([start[0] + d3.event.dx, start[1] + d3.event.dy]); + node.lon = end[0]; + node.lat = end[1]; + map.history.replace(iD.actions.move(node, end)); + }); + + redraw(only); + }) + .on('dragend', function () { + if (dragging) { + dragging = false; + map.update(); + redraw(); + } + }), nodeline = function(d) { return 'M' + d.nodes.map(ll2a).map(projection).map(roundCoords).join('L'); }, @@ -394,11 +426,16 @@ iD.Map = function() { function deselectClick() { var hadSelection = !!selection; - selection = null; if (hadSelection) { + if (selection.type === 'way') { + d3.select(d3.event.target) + .on('mousedown.drag', null) + .on('touchstart.drag', null); + } redraw(); hideInspector(); } + selection = null; } function selectEntity(entity) { @@ -413,6 +450,7 @@ iD.Map = function() { var entity = d3.select(d3.event.target).data(); if (entity) entity = entity[0]; if (!entity || selection === entity.id || (entity.tags && entity.tags.elastic)) return; + if (entity.type === 'way') d3.select(d3.event.target).call(waydragbehavior); selection = entity.id; d3.select('.inspector-wrap') .style('display', 'block')