Files
iD/js/id/actions/unjoin_node.js
John Firebaugh 292c916cb1 Converting some actions to entity methods
The guidelines here are:

Entity methods:
  return a modified entity
  don't necessarily maintain whole-graph consistency

Actions:
  return a modified graph
  always maintain whole-graph consistency
  call entity methods liberally
  generally don't call other actions
2013-01-23 12:40:27 -05:00

38 lines
1.2 KiB
JavaScript

// Unjoin the ways at the given node.
//
// For testing convenience, accepts an ID to assign to the (first) new node.
// Normally, this will be undefined and the way will automatically
// be assigned a new ID.
//
// Reference:
// https://github.com/openstreetmap/potlatch2/blob/master/net/systemeD/halcyon/connection/actions/UnjoinNodeAction.as
// https://github.com/openstreetmap/josm/blob/mirror/src/org/openstreetmap/josm/actions/UnGlueAction.java
//
iD.actions.UnjoinNode = function(nodeId, newNodeId) {
var action = function(graph) {
if (!action.permitted(graph))
return graph;
var node = graph.entity(nodeId);
graph.parentWays(node).forEach(function(parent, i) {
if (i === 0)
return;
var index = parent.nodes.indexOf(nodeId),
newNode = iD.Node({id: newNodeId, loc: node.loc, tags: node.tags});
graph = graph.replace(newNode);
graph = graph.replace(parent.updateNode(newNode.id, index));
});
return graph;
};
action.permitted = function(graph) {
return graph.parentWays(graph.entity(nodeId)).length >= 2;
};
return action;
};