all files / Github/bpmn-js/lib/features/modeling/cmd/ UpdateFlowNodeRefsHandler.js

100% Statements 70/70
100% Branches 18/18
100% Functions 21/21
100% Lines 70/70
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                                                1128×               1143×   1143×   1143×   1143×       743×   743×         743×             1153× 1051× 1051×           852×   852× 632×     852×       1051× 199×     852×   852× 743×   303×       1143× 171×   171× 75×     96× 344×     96×   96×     1143×     1143×   1051×   1051× 1051× 1051×   1051×     1143×   171×     171× 30× 25×         1143×       1261×   1261× 1143×       1261×   1196× 1196×     1196× 279× 279×       1196× 345× 345×                   218×   218×   194× 194×     194× 45× 45×       194× 59× 59×            
import {
  collectLanes,
  getLanesRoot
} from '../util/LaneUtil';
 
import {
  is
} from '../../../util/ModelUtil';
 
import {
  add as collectionAdd,
  remove as collectionRemove
} from 'diagram-js/lib/util/Collections';
 
import {
  asTRBL
} from 'diagram-js/lib/layout/LayoutUtil';
 
var FLOW_NODE_REFS_ATTR = 'flowNodeRef',
    LANES_ATTR = 'lanes';
 
 
/**
 * A handler that updates lane refs on changed elements
 */
export default function UpdateFlowNodeRefsHandler(elementRegistry) {
  this._elementRegistry = elementRegistry;
}
 
UpdateFlowNodeRefsHandler.$inject = [
  'elementRegistry'
];
 
 
UpdateFlowNodeRefsHandler.prototype.computeUpdates = function(flowNodeShapes, laneShapes) {
 
  var handledNodes = {};
 
  var updates = [];
 
  var participantCache = {};
 
  var allFlowNodeShapes = [];
 
  function isInLaneShape(element, laneShape) {
 
    var laneTrbl = asTRBL(laneShape);
 
    var elementMid = {
      x: element.x + element.width / 2,
      y: element.y + element.height / 2
    };
 
    return elementMid.x > laneTrbl.left &&
           elementMid.x < laneTrbl.right &&
           elementMid.y > laneTrbl.top &&
           elementMid.y < laneTrbl.bottom;
  }
 
  function addFlowNodeShape(flowNodeShape) {
    if (!handledNodes[flowNodeShape.id]) {
      allFlowNodeShapes.push(flowNodeShape);
      handledNodes[flowNodeShape.id] = flowNodeShape;
    }
  }
 
  function getAllLaneShapes(flowNodeShape) {
 
    var root = getLanesRoot(flowNodeShape);
 
    if (!participantCache[root.id]) {
      participantCache[root.id] = collectLanes(root);
    }
 
    return participantCache[root.id];
  }
 
  function getNewLanes(flowNodeShape) {
    if (!flowNodeShape.parent) {
      return [];
    }
 
    var allLaneShapes = getAllLaneShapes(flowNodeShape);
 
    return allLaneShapes.filter(function(l) {
      return isInLaneShape(flowNodeShape, l);
    }).map(function(shape) {
      return shape.businessObject;
    });
  }
 
  laneShapes.forEach(function(laneShape) {
    var root = getLanesRoot(laneShape);
 
    if (!root || handledNodes[root.id]) {
      return;
    }
 
    var children = root.children.filter(function(c) {
      return is(c, 'bpmn:FlowNode');
    });
 
    children.forEach(addFlowNodeShape);
 
    handledNodes[root.id] = root;
  });
 
  flowNodeShapes.forEach(addFlowNodeShape);
 
 
  allFlowNodeShapes.forEach(function(flowNodeShape) {
 
    var flowNode = flowNodeShape.businessObject;
 
    var lanes = flowNode.get(LANES_ATTR),
        remove = lanes.slice(),
        add = getNewLanes(flowNodeShape);
 
    updates.push({ flowNode: flowNode, remove: remove, add: add });
  });
 
  laneShapes.forEach(function(laneShape) {
 
    var lane = laneShape.businessObject;
 
    // lane got removed XX-)
    if (!laneShape.parent) {
      lane.get(FLOW_NODE_REFS_ATTR).forEach(function(flowNode) {
        updates.push({ flowNode: flowNode, remove: [ lane ], add: [] });
      });
    }
  });
 
  return updates;
};
 
UpdateFlowNodeRefsHandler.prototype.execute = function(context) {
 
  var updates = context.updates;
 
  if (!updates) {
    updates = context.updates = this.computeUpdates(context.flowNodeShapes, context.laneShapes);
  }
 
 
  updates.forEach(function(update) {
 
    var flowNode = update.flowNode,
        lanes = flowNode.get(LANES_ATTR);
 
    // unwire old
    update.remove.forEach(function(oldLane) {
      collectionRemove(lanes, oldLane);
      collectionRemove(oldLane.get(FLOW_NODE_REFS_ATTR), flowNode);
    });
 
    // wire new
    update.add.forEach(function(newLane) {
      collectionAdd(lanes, newLane);
      collectionAdd(newLane.get(FLOW_NODE_REFS_ATTR), flowNode);
    });
  });
 
  // TODO(nikku): return changed elements
  // return [ ... ];
};
 
 
UpdateFlowNodeRefsHandler.prototype.revert = function(context) {
 
  var updates = context.updates;
 
  updates.forEach(function(update) {
 
    var flowNode = update.flowNode,
        lanes = flowNode.get(LANES_ATTR);
 
    // unwire new
    update.add.forEach(function(newLane) {
      collectionRemove(lanes, newLane);
      collectionRemove(newLane.get(FLOW_NODE_REFS_ATTR), flowNode);
    });
 
    // wire old
    update.remove.forEach(function(oldLane) {
      collectionAdd(lanes, oldLane);
      collectionAdd(oldLane.get(FLOW_NODE_REFS_ATTR), flowNode);
    });
  });
 
  // TODO(nikku): return changed elements
  // return [ ... ];
};