all files / lib/features/attach-support/ AttachSupport.js

100% Statements 113/113
96.67% Branches 29/30
100% Functions 33/33
100% Lines 113/113
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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327                                                                              132×   132×         132×   42× 42× 42×   42×   42×       132×   41× 41× 41×   41× 27×   27×             132× 59× 59× 59× 59×   59× 31×         132×   59× 59× 59×       59× 18×     41×   13×     28× 13×       41× 20×         132×   59×   59×   81×     31×                   31×                       132× 53× 53× 53×   53× 31×         132×                                       132× 34× 34× 34× 34× 34×   34× 13×   13×   13×           132×   43×   43× 20×     43× 33×             132×   28× 28× 28×   28× 14×     14×     14× 25×   25×       14×         14×                                     142× 213×                       42×   42×                           42×   42× 56×     124×     122×     54×      
import {
  flatten,
  filter,
  forEach,
  groupBy,
  map,
  unionBy
} from 'min-dash';
 
import { saveClear } from '../../util/Removal';
 
import {
  remove as collectionRemove
} from '../../util/Collections';
 
import { getNewAttachShapeDelta } from '../../util/AttachUtil';
 
import inherits from 'inherits';
 
var LOW_PRIORITY = 251,
    HIGH_PRIORITY = 1401;
 
import CommandInterceptor from '../../command/CommandInterceptor';
 
 
/**
 * Adds the notion of attached elements to the modeler.
 *
 * Optionally depends on `diagram-js/lib/features/move` to render
 * the attached elements during move preview.
 *
 * Optionally depends on `diagram-js/lib/features/label-support`
 * to render attached labels during move preview.
 *
 * @param {didi.Injector} injector
 * @param {EventBus} eventBus
 * @param {Rules} rules
 * @param {Modeling} modeling
 */
export default function AttachSupport(injector, eventBus, rules, modeling) {
 
  CommandInterceptor.call(this, eventBus);
 
  var movePreview = injector.get('movePreview', false);
 
 
  // remove all the attached elements from the shapes to be validated
  // add all the attached shapes to the overall list of moved shapes
  eventBus.on('shape.move.start', HIGH_PRIORITY, function(e) {
 
    var context = e.context,
        shapes = context.shapes,
        validatedShapes = context.validatedShapes;
 
    context.shapes = addAttached(shapes);
 
    context.validatedShapes = removeAttached(validatedShapes);
  });
 
  // add attachers to the visual's group
  movePreview && eventBus.on('shape.move.start', LOW_PRIORITY, function(e) {
 
    var context = e.context,
        shapes = context.shapes,
        attachers = getAttachers(shapes);
 
    forEach(attachers, function(attacher) {
      movePreview.makeDraggable(context, attacher, true);
 
      forEach(attacher.labels, function(label) {
        movePreview.makeDraggable(context, label, true);
      });
    });
  });
 
 
  // add all attachers to move closure
  this.preExecuted('elements.move', HIGH_PRIORITY, function(e) {
    var context = e.context,
        closure = context.closure,
        shapes = context.shapes,
        attachers = getAttachers(shapes);
 
    forEach(attachers, function(attacher) {
      closure.add(attacher, closure.topLevel[attacher.host.id]);
    });
  });
 
  // perform the attaching after shapes are done moving
  this.postExecuted('elements.move', function(e) {
 
    var context = e.context,
        shapes = context.shapes,
        newHost = context.newHost,
        attachers;
 
    // we only support attachment / detachment of one element
    if (shapes.length > 1) {
      return;
    }
 
    if (newHost) {
 
      attachers = shapes;
    } else {
 
      attachers = filter(shapes, function(s) {
        return !!s.host;
      });
    }
 
    forEach(attachers, function(attacher) {
      modeling.updateAttachment(attacher, newHost);
    });
  });
 
  // ensure invalid attachment connections are removed
  this.postExecuted('elements.move', function(e) {
 
    var shapes = e.context.shapes;
 
    forEach(shapes, function(shape) {
 
      forEach(shape.attachers, function(attacher) {
 
        // remove invalid outgoing connections
        forEach(attacher.outgoing.slice(), function(connection) {
          var allowed = rules.allowed('connection.reconnectStart', {
            connection: connection,
            source: connection.source,
            target: connection.target
          });
 
          if (!allowed) {
            modeling.removeConnection(connection);
          }
        });
 
        // remove invalid incoming connections
        forEach(attacher.incoming.slice(), function(connection) {
          var allowed = rules.allowed('connection.reconnectEnd', {
            connection: connection,
            source: connection.source,
            target: connection.target
          });
 
          if (!allowed) {
            modeling.removeConnection(connection);
          }
        });
      });
    });
  });
 
  this.postExecute('shape.create', function(e) {
    var context = e.context,
        shape = context.shape,
        host = context.host;
 
    if (host) {
      modeling.updateAttachment(shape, host);
    }
  });
 
  // update attachments if the host is replaced
  this.postExecute('shape.replace', function(e) {
 
    var context = e.context,
        oldShape = context.oldShape,
        newShape = context.newShape;
 
    // move the attachers to the new host
    saveClear(oldShape.attachers, function(attacher) {
      var allowed = rules.allowed('elements.move', {
        target: newShape,
        shapes: [attacher]
      });
 
      if (allowed === 'attach') {
        modeling.updateAttachment(attacher, newShape);
      } else {
        modeling.removeShape(attacher);
      }
    });
 
    // move attachers if new host has different size
    if (newShape.attachers.length) {
 
      forEach(newShape.attachers, function(attacher) {
        var delta = getNewAttachShapeDelta(attacher, oldShape, newShape);
        modeling.moveShape(attacher, delta, attacher.parent);
      });
    }
 
  });
 
  // move shape on host resize
  this.postExecute('shape.resize', function(event) {
    var context = event.context,
        shape = context.shape,
        oldBounds = context.oldBounds,
        newBounds = context.newBounds,
        attachers = shape.attachers;
 
    forEach(attachers, function(attacher) {
      var delta = getNewAttachShapeDelta(attacher, oldBounds, newBounds);
 
      modeling.moveShape(attacher, delta, attacher.parent);
 
      forEach(attacher.labels, function(label) {
        modeling.moveShape(label, delta, label.parent);
      });
    });
  });
 
  // remove attachments
  this.preExecute('shape.delete', function(event) {
 
    var shape = event.context.shape;
 
    saveClear(shape.attachers, function(attacher) {
      modeling.removeShape(attacher);
    });
 
    if (shape.host) {
      modeling.updateAttachment(shape, null);
    }
  });
 
 
  // Prevent attachers and their labels from moving, when the space tool is performed.
  // Otherwise the attachers and their labels would be moved twice.
  eventBus.on('spaceTool.move', function(event) {
 
    var context = event.context,
        initialized = context.initialized,
        attachSupportInitialized = context.attachSupportInitialized;
 
    if (!initialized || attachSupportInitialized) {
      return;
    }
 
    var movingShapes = context.movingShapes;
 
    // collect attachers whose host is not being moved using the space tool
    var staticAttachers = filter(movingShapes, function(shape) {
      var host = shape.host;
 
      return host && movingShapes.indexOf(host) === -1;
    });
 
    // remove attachers that are not going to be moved from moving shapes
    forEach(staticAttachers, function(shape) {
      collectionRemove(movingShapes, shape);
 
      forEach(shape.labels, function(label) {
        collectionRemove(movingShapes, shape.label);
      });
    });
 
    context.attachSupportInitialized = true;
  });
}
 
inherits(AttachSupport, CommandInterceptor);
 
AttachSupport.$inject = [
  'injector',
  'eventBus',
  'rules',
  'modeling'
];
 
 
/**
 * Return attachers of the given shapes
 *
 * @param {Array<djs.model.Base>} shapes
 * @return {Array<djs.model.Base>}
 */
function getAttachers(shapes) {
  return flatten(map(shapes, function(s) {
    return s.attachers || [];
  }));
}
 
/**
 * Return a combined list of elements and
 * attachers.
 *
 * @param {Array<djs.model.Base>} elements
 * @return {Array<djs.model.Base>} filtered
 */
function addAttached(elements) {
  var attachers = getAttachers(elements);
 
  return unionBy('id', elements, attachers);
}
 
/**
 * Return a filtered list of elements that do not
 * contain attached elements with hosts being part
 * of the selection.
 *
 * @param  {Array<djs.model.Base>} elements
 *
 * @return {Array<djs.model.Base>} filtered
 */
function removeAttached(elements) {
 
  var ids = groupBy(elements, 'id');
 
  return filter(elements, function(element) {
    while (element) {
 
      // host in selection
      if (element.host && ids[element.host.id]) {
        return false;
      }
 
      element = element.parent;
    }
 
    return true;
  });
}