all files / bpmn-js/lib/features/modeling/behavior/ DropOnFlowBehavior.js

100% Statements 72/72
98.15% Branches 53/54
100% Functions 12/12
100% Lines 72/72
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207                                  1189×               17×               17× 17×   17×   17× 15× 15×     15×     14×     14× 12×       14× 12×       16× 16×   16×   13×   13×     16×   14×       11×           16×                 16×       1189×   414× 414× 414× 414×   414× 195×         219×     219× 219×             219× 1532×   1532×     219×         1189×   414× 414× 414×   414×         1189×   420× 420×   420×       1189×   420× 420× 420×   420×                             28× 28×   28×             24×         219×            
import inherits from 'inherits';
 
import {
  assign,
  find,
  filter
} from 'min-dash';
 
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
 
import {
  getApproxIntersection
} from 'diagram-js/lib/util/LineIntersection';
 
 
export default function DropOnFlowBehavior(eventBus, bpmnRules, modeling) {
 
  CommandInterceptor.call(this, eventBus);
 
  /**
   * Reconnect start / end of a connection after
   * dropping an element on a flow.
   */
 
  function insertShape(shape, targetFlow, position) {
    var waypoints = targetFlow.waypoints,
        waypointsBefore,
        waypointsAfter,
        dockingPoint,
        source,
        target,
        incomingConnection,
        outgoingConnection,
        oldOutgoing = shape.outgoing.slice(),
        oldIncoming = shape.incoming.slice();
 
    var intersection = getApproxIntersection(waypoints, position);
 
    if (intersection) {
      waypointsBefore = waypoints.slice(0, intersection.index);
      waypointsAfter = waypoints.slice(intersection.index + (intersection.bendpoint ? 1 : 0));
 
      // due to inaccuracy intersection might have been found
      if (!waypointsBefore.length || !waypointsAfter.length) {
        return;
      }
 
      dockingPoint = intersection.bendpoint ? waypoints[intersection.index] : position;
 
      // if last waypointBefore is inside shape's bounds, ignore docking point
      if (!isPointInsideBBox(shape, waypointsBefore[waypointsBefore.length-1])) {
        waypointsBefore.push(copy(dockingPoint));
      }
 
      // if first waypointAfter is inside shape's bounds, ignore docking point
      if (!isPointInsideBBox(shape, waypointsAfter[0])) {
        waypointsAfter.unshift(copy(dockingPoint));
      }
    }
 
    source = targetFlow.source;
    target = targetFlow.target;
 
    if (bpmnRules.canConnect(source, shape, targetFlow)) {
      // reconnect source -> inserted shape
      modeling.reconnectEnd(targetFlow, shape, waypointsBefore || position);
 
      incomingConnection = targetFlow;
    }
 
    if (bpmnRules.canConnect(shape, target, targetFlow)) {
 
      if (!incomingConnection) {
        // reconnect inserted shape -> end
        modeling.reconnectStart(targetFlow, shape, waypointsAfter || position);
 
        outgoingConnection = targetFlow;
      } else {
        outgoingConnection = modeling.connect(
          shape, target, { type: targetFlow.type, waypoints: waypointsAfter }
        );
      }
    }
 
    var duplicateConnections = [].concat(
 
      incomingConnection && filter(oldIncoming, function(connection) {
        return connection.source === incomingConnection.source;
      }) || [],
 
      outgoingConnection && filter(oldOutgoing, function(connection) {
        return connection.source === outgoingConnection.source;
      }) || []
    );
 
    if (duplicateConnections.length) {
      modeling.removeElements(duplicateConnections);
    }
  }
 
  this.preExecute('elements.move', function(context) {
 
    var newParent = context.newParent,
        shapes = context.shapes,
        delta = context.delta,
        shape = shapes[0];
 
    if (!shape || !newParent) {
      return;
    }
 
    // if the new parent is a connection,
    // change it to the new parent's parent
    if (newParent && newParent.waypoints) {
      context.newParent = newParent = newParent.parent;
    }
 
    var shapeMid = getMid(shape);
    var newShapeMid = {
      x: shapeMid.x + delta.x,
      y: shapeMid.y + delta.y
    };
 
    // find a connection which intersects with the
    // element's mid point
    var connection = find(newParent.children, function(element) {
      var canInsert = bpmnRules.canInsert(shapes, element);
 
      return canInsert && getApproxIntersection(element.waypoints, newShapeMid);
    });
 
    if (connection) {
      context.targetFlow = connection;
      context.position = newShapeMid;
    }
 
  }, true);
 
  this.postExecuted('elements.move', function(context) {
 
    var shapes = context.shapes,
        targetFlow = context.targetFlow,
        position = context.position;
 
    if (targetFlow) {
      insertShape(shapes[0], targetFlow, position);
    }
 
  }, true);
 
  this.preExecute('shape.create', function(context) {
 
    var parent = context.parent,
        shape = context.shape;
 
    if (bpmnRules.canInsert(shape, parent)) {
      context.targetFlow = parent;
      context.parent = parent.parent;
    }
  }, true);
 
  this.postExecuted('shape.create', function(context) {
 
    var shape = context.shape,
        targetFlow = context.targetFlow,
        position = context.position;
 
    if (targetFlow) {
      insertShape(shape, targetFlow, position);
    }
  }, true);
}
 
inherits(DropOnFlowBehavior, CommandInterceptor);
 
DropOnFlowBehavior.$inject = [
  'eventBus',
  'bpmnRules',
  'modeling'
];
 
 
// helpers /////////////////////
 
function isPointInsideBBox(bbox, point) {
  var x = point.x,
      y = point.y;
 
  return x >= bbox.x &&
    x <= bbox.x + bbox.width &&
    y >= bbox.y &&
    y <= bbox.y + bbox.height;
}
 
function copy(obj) {
  return assign({}, obj);
}
 
function getMid(bounds) {
 
  return {
    x: Math.round(bounds.x + bounds.width / 2),
    y: Math.round(bounds.y + bounds.height / 2)
  };
}