diff --git a/index.html b/index.html index 26b7f2673..57ccc56f4 100644 --- a/index.html +++ b/index.html @@ -64,9 +64,11 @@ + + + - diff --git a/js/id/behavior/drag_accuracy_handle.js b/js/id/behavior/drag_accuracy_handle.js new file mode 100644 index 000000000..2804739d9 --- /dev/null +++ b/js/id/behavior/drag_accuracy_handle.js @@ -0,0 +1,26 @@ +iD.behavior.DragAccuracyHandle = function(mode) { + var history = mode.history, + projection = mode.map.projection; + + return iD.behavior.drag() + .delegate(".accuracy-handle") + .origin(function(d) { + return projection(d.loc); + }) + .on('start', function(d) { + d.node = iD.Node({loc: d.loc}); + history.perform( + iD.actions.AddNode(d.node), + iD.actions.AddWayNode(d.way, d.node.id, d.index)); + }) + .on('move', function(d) { + d3.event.sourceEvent.stopPropagation(); + history.replace( + iD.actions.MoveNode(d.node.id, projection.invert(d3.event.loc))); + }) + .on('end', function() { + history.replace( + iD.actions.Noop(), + 'added a node to a way'); + }); +}; diff --git a/js/id/behavior/drag_node.js b/js/id/behavior/drag_node.js new file mode 100644 index 000000000..e39717be1 --- /dev/null +++ b/js/id/behavior/drag_node.js @@ -0,0 +1,24 @@ +iD.behavior.DragNode = function(mode) { + var history = mode.history, + projection = mode.map.projection; + + return iD.behavior.drag() + .delegate(".handle, .marker") + .origin(function(entity) { + return projection(entity.loc); + }) + .on('start', function() { + history.perform( + iD.actions.Noop()); + }) + .on('move', function(entity) { + d3.event.sourceEvent.stopPropagation(); + history.replace( + iD.actions.MoveNode(entity.id, projection.invert(d3.event.loc))); + }) + .on('end', function() { + history.replace( + iD.actions.Noop(), + 'moved a node'); + }); +}; diff --git a/js/id/behavior/drag_way.js b/js/id/behavior/drag_way.js new file mode 100644 index 000000000..a8669cbe1 --- /dev/null +++ b/js/id/behavior/drag_way.js @@ -0,0 +1,24 @@ +iD.behavior.DragWay = function(mode) { + var history = mode.history, + projection = mode.map.projection; + + return iD.behavior.drag() + .delegate('.casing, .stroke, .area') + .origin(function(entity) { + return projection(entity.nodes[0].loc); + }) + .on('start', function() { + history.perform( + iD.actions.Noop()); + }) + .on('move', function(entity) { + d3.event.sourceEvent.stopPropagation(); + history.replace( + iD.actions.MoveWay(entity.id, d3.event.dxdy, projection)); + }) + .on('end', function() { + history.replace( + iD.actions.Noop(), + 'moved a way'); + }); +}; diff --git a/js/id/modes/browse.js b/js/id/modes/browse.js index f21f3878f..90d1ee16b 100644 --- a/js/id/modes/browse.js +++ b/js/id/modes/browse.js @@ -6,11 +6,22 @@ iD.modes.Browse = function() { description: 'Pan and zoom the map' }; + var behaviors; + mode.enter = function() { d3.select('#map').attr('class', function() { return mode.id; }); - iD.modes._dragFeatures(mode); - mode.map.surface.on('click.browse', function () { + var surface = mode.map.surface; + + behaviors = [ + iD.behavior.DragNode(mode), + iD.behavior.DragAccuracyHandle(mode)]; + + behaviors.forEach(function(behavior) { + behavior(surface); + }); + + surface.on('click.browse', function () { var datum = d3.select(d3.event.target).datum(); if (datum instanceof iD.Entity) { mode.controller.enter(iD.modes.Select(datum)); @@ -19,8 +30,14 @@ iD.modes.Browse = function() { }; mode.exit = function() { - mode.map.surface.on('mousedown.latedrag', null); - mode.map.surface.on('click.browse', null); + var surface = mode.map.surface; + + behaviors.forEach(function(behavior) { + behavior.off(surface); + }); + + surface.on('mousedown.latedrag', null); + surface.on('click.browse', null); d3.select('#map').attr('class', null); }; diff --git a/js/id/modes/drag_features.js b/js/id/modes/drag_features.js deleted file mode 100644 index 2ed6caa44..000000000 --- a/js/id/modes/drag_features.js +++ /dev/null @@ -1,50 +0,0 @@ -iD.modes._dragFeatures = function(mode) { - var history = mode.history, - projection = mode.map.projection; - - var dragNode = iD.behavior.drag() - .delegate(".handle, .marker") - .origin(function(entity) { - return projection(entity.loc); - }) - .on('start', function() { - history.perform( - iD.actions.Noop()); - }) - .on('move', function(entity) { - d3.event.sourceEvent.stopPropagation(); - history.replace( - iD.actions.MoveNode(entity.id, projection.invert(d3.event.loc))); - }) - .on('end', function() { - history.replace( - iD.actions.Noop(), - 'moved a node'); - }); - - var dragAccuracy = iD.behavior.drag() - .delegate(".accuracy-handle") - .origin(function(d) { - return projection(d.loc); - }) - .on('start', function(d) { - d.node = iD.Node({loc: d.loc}); - history.perform( - iD.actions.AddNode(d.node), - iD.actions.AddWayNode(d.way, d.node.id, d.index)); - }) - .on('move', function(d) { - d3.event.sourceEvent.stopPropagation(); - history.replace( - iD.actions.MoveNode(d.node.id, projection.invert(d3.event.loc))); - }) - .on('end', function() { - history.replace( - iD.actions.Noop(), - 'added a node to a way'); - }); - - mode.map.surface - .call(dragNode) - .call(dragAccuracy); -}; diff --git a/js/id/modes/select.js b/js/id/modes/select.js index 46d6cd404..b03482777 100644 --- a/js/id/modes/select.js +++ b/js/id/modes/select.js @@ -6,7 +6,7 @@ iD.modes.Select = function (entity) { }; var inspector = iD.Inspector(), - target; + behaviors; function remove() { if (entity.type === 'way') { @@ -27,10 +27,16 @@ iD.modes.Select = function (entity) { } mode.enter = function () { - target = mode.map.surface.selectAll('*') - .filter(function (d) { return d === entity; }); + var surface = mode.map.surface; - iD.modes._dragFeatures(mode); + behaviors = [ + iD.behavior.DragNode(mode), + iD.behavior.DragWay(mode), + iD.behavior.DragAccuracyHandle(mode)]; + + behaviors.forEach(function(behavior) { + behavior(surface); + }); d3.select('.inspector-wrap') .style('display', 'block') @@ -60,30 +66,7 @@ iD.modes.Select = function (entity) { mode.controller.exit(); }); - if (entity.type === 'way') { - var history = mode.history, - projection = mode.map.projection; - - target.call(iD.behavior.drag() - .origin(function(entity) { - return projection(entity.nodes[0].loc); - }) - .on('start', function() { - history.perform(iD.actions.Noop()); - }) - .on('move', function(entity) { - d3.event.sourceEvent.stopPropagation(); - history.replace( - iD.actions.MoveWay(entity.id, d3.event.dxdy, projection)); - }) - .on('end', function() { - history.replace( - iD.actions.Noop(), - 'moved a way'); - })); - } - - mode.map.surface.on('click.browse', function () { + surface.on('click.browse', function () { var datum = d3.select(d3.event.target).datum(); if (datum instanceof iD.Entity) { mode.controller.enter(iD.modes.Select(datum)); @@ -101,18 +84,18 @@ iD.modes.Select = function (entity) { }; mode.exit = function () { + var surface = mode.map.surface; + d3.select('.inspector-wrap') .style('display', 'none'); - if (entity.type === 'way') { - target.on('mousedown.drag', null) - .on('touchstart.drag', null); - } + behaviors.forEach(function(behavior) { + behavior.off(surface); + }); - mode.map.surface.on("click.browse", null); - mode.map.surface.on('mousedown.latedrag', null); + surface.on("click.browse", null); + surface.on('mousedown.latedrag', null); mode.map.keybinding().on('⌫.browse', null); - mode.map.selection(null); }; diff --git a/test/index.html b/test/index.html index 2ff91d1c7..d08ea2158 100644 --- a/test/index.html +++ b/test/index.html @@ -63,13 +63,15 @@ + + + -