all files / bpmn-js/lib/features/snapping/ BpmnCreateMoveSnapping.js

100% Statements 77/77
84.31% Branches 43/51
100% Functions 18/18
100% Lines 77/77
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228                                                                        93×     93×     25×       93×           56× 56× 56×   56×                     20×   20×     20×               20×               20×         20×     19×   19×   19×           47× 13× 13×         19×               19×               19×     19×     38×       94×                                 36× 38×         47×     43×         20× 20×   20× 18×                    
import inherits from 'inherits';
 
import CreateMoveSnapping from 'diagram-js/lib/features/snapping/CreateMoveSnapping';
 
import {
  isSnapped,
  setSnapped,
  topLeft,
  bottomRight
} from 'diagram-js/lib/features/snapping/SnapUtil';
 
import {
  isExpanded
} from '../../util/DiUtil';
 
import { is } from '../../util/ModelUtil';
 
import {
  asTRBL,
  getMid
} from 'diagram-js/lib/layout/LayoutUtil';
 
import { getBoundaryAttachment } from './BpmnSnappingUtil';
 
import { forEach } from 'min-dash';
 
var HIGH_PRIORITY = 1500;
 
 
/**
 * Snap during create and move.
 *
 * @param {BpmnRules} bpmnRules
 * @param {EventBus} eventBus
 * @param {Injector} injector
 */
export default function BpmnCreateMoveSnapping(bpmnRules, eventBus, injector) {
  injector.invoke(CreateMoveSnapping, this);
 
  // creating first participant
  eventBus.on([ 'create.move', 'create.end' ], HIGH_PRIORITY, setSnappedIfConstrained);
 
  function canAttach(shape, target, position) {
    return bpmnRules.canAttach([ shape ], target, null, position) === 'attach';
  }
 
  // snap boundary events
  eventBus.on([
    'create.move',
    'create.end',
    'shape.move.move',
    'shape.move.end'
  ], HIGH_PRIORITY, function(event) {
    var context = event.context,
        target = context.target,
        shape = context.shape;
 
    if (target && canAttach(shape, target, event) && !isSnapped(event)) {
      snapBoundaryEvent(event, target);
    }
  });
}
 
inherits(BpmnCreateMoveSnapping, CreateMoveSnapping);
 
BpmnCreateMoveSnapping.$inject = [
  'bpmnRules',
  'eventBus',
  'injector'
];
 
BpmnCreateMoveSnapping.prototype.initSnap = function(event) {
  var snapContext = CreateMoveSnapping.prototype.initSnap.call(this, event);
 
  var shape = event.shape;
 
  // snap to docking points
  forEach(shape.outgoing, function(connection) {
    var docking = connection.waypoints[0];
 
    docking = docking.original || docking;
 
    snapContext.setSnapOrigin(connection.id + '-docking', {
      x: docking.x - event.x,
      y: docking.y - event.y
    });
  });
 
  forEach(shape.incoming, function(connection) {
    var docking = connection.waypoints[connection.waypoints.length - 1];
 
    docking = docking.original || docking;
 
    snapContext.setSnapOrigin(connection.id + '-docking', {
      x: docking.x - event.x,
      y: docking.y - event.y
    });
  });
 
  if (is(shape, 'bpmn:Participant')) {
 
    // snap to borders with higher priority
    snapContext.setSnapLocations([ 'top-left', 'bottom-right', 'mid' ]);
  }
 
  return snapContext;
};
 
BpmnCreateMoveSnapping.prototype.addSnapTargetPoints = function(snapPoints, shape, target) {
  CreateMoveSnapping.prototype.addSnapTargetPoints.call(this, snapPoints, shape, target);
 
  var snapTargets = this.getSnapTargets(shape, target);
 
  forEach(snapTargets, function(snapTarget) {
 
    // handle TRBL alignment
    //
    // * with container elements
    // * with text annotations
    if (isContainer(snapTarget) || areAll([ shape, snapTarget ], 'bpmn:TextAnnotation')) {
      snapPoints.add('top-left', topLeft(snapTarget));
      snapPoints.add('bottom-right', bottomRight(snapTarget));
    }
  });
 
  // snap to docking points
  forEach(shape.incoming, function(connection) {
 
    Eif (!includes(snapTargets, connection.source)) {
      snapPoints.add('mid', getMid(connection.source));
    }
 
    var docking = connection.waypoints[0];
 
    snapPoints.add(connection.id + '-docking', docking.original || docking);
  });
 
 
  forEach(shape.outgoing, function(connection) {
 
    Eif (!includes(snapTargets, connection.target)) {
      snapPoints.add('mid', getMid(connection.target));
    }
 
    var docking = connection.waypoints[ connection.waypoints.length - 1 ];
 
    snapPoints.add(connection.id + '-docking', docking.original || docking);
  });
 
  // add sequence flow parents as snap targets
  if (is(target, 'bpmn:SequenceFlow')) {
    snapPoints = this.addSnapTargetPoints(snapPoints, shape, target.parent);
  }
 
  return snapPoints;
};
 
BpmnCreateMoveSnapping.prototype.getSnapTargets = function(shape, target) {
  return CreateMoveSnapping.prototype.getSnapTargets.call(this, shape, target)
    .filter(function(snapTarget) {
 
      // do not snap to lanes
      return !is(snapTarget, 'bpmn:Lane');
    });
};
 
// helpers //////////
 
function snapBoundaryEvent(event, target) {
  var targetTRBL = asTRBL(target);
 
  var direction = getBoundaryAttachment(event, target);
 
  if (/top/.test(direction)) {
    setSnapped(event, 'y', targetTRBL.top);
  } else
  if (/bottom/.test(direction)) {
    setSnapped(event, 'y', targetTRBL.bottom);
  }
 
  if (/left/.test(direction)) {
    setSnapped(event, 'x', targetTRBL.left);
  } else
  if (/right/.test(direction)) {
    setSnapped(event, 'x', targetTRBL.right);
  }
}
 
function areAll(elements, type) {
  return elements.every(function(el) {
    return is(el, type);
  });
}
 
function isContainer(element) {
  if (is(element, 'bpmn:SubProcess') && isExpanded(element)) {
    return true;
  }
 
  return is(element, 'bpmn:Participant');
}
 
 
function setSnappedIfConstrained(event) {
  var context = event.context,
      createConstraints = context.createConstraints;
 
  if (!createConstraints) {
    return;
  }
 
  var top = createConstraints.top,
      right = createConstraints.right,
      bottom = createConstraints.bottom,
      left = createConstraints.left;
 
  Eif ((left && left >= event.x) || (right && right <= event.x)) {
    setSnapped(event, 'x', event.x);
  }
 
  Eif ((top && top >= event.y) || (bottom && bottom <= event.y)) {
    setSnapped(event, 'y', event.y);
  }
}
 
function includes(array, value) {
  return array.indexOf(value) !== -1;
}