all files / lib/features/create/ CreatePreview.js

100% Statements 28/28
83.33% Branches 5/6
100% Functions 5/5
100% Lines 28/28
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                                              48×   48×   48×   48×   48×   48×         68×     63×   63×   63×       68×     48×     139×   108× 108× 108×     108× 48×     108×     139× 48× 48×   48× 48×                    
import {
  translate
} from '../../util/SvgTransformUtil';
 
import { getVisual } from '../../util/GraphicsUtil';
 
import {
  append as svgAppend,
  attr as svgAttr,
  create as svgCreate,
  remove as svgRemove
} from 'tiny-svg';
 
var LOW_PRIORITY = 750;
 
 
export default function CreatePreview(
    canvas,
    eventBus,
    graphicsFactory,
    previewSupport,
    styles
) {
  function createDragGroup(elements) {
    var dragGroup = svgCreate('g');
 
    svgAttr(dragGroup, styles.cls('djs-drag-group', [ 'no-events' ]));
 
    var defaultLayer = canvas.getDefaultLayer();
 
    svgAppend(defaultLayer, dragGroup);
 
    var childrenGfx = svgCreate('g');
 
    elements.forEach(function(element) {
 
      // create graphics
      var gfx;
 
      if (element.waypoints) {
        gfx = graphicsFactory._createContainer('connection', childrenGfx);
 
        graphicsFactory.drawConnection(getVisual(gfx), element);
      } else {
        gfx = graphicsFactory._createContainer('shape', childrenGfx);
 
        graphicsFactory.drawShape(getVisual(gfx), element);
 
        translate(gfx, element.x, element.y);
      }
 
      // add preview
      previewSupport.addDragger(element, dragGroup, gfx);
    });
 
    return dragGroup;
  }
 
  eventBus.on('create.move', LOW_PRIORITY, function(event) {
 
    var context = event.context,
        elements = context.elements,
        dragGroup = context.dragGroup;
 
    // lazily create previews
    if (!dragGroup) {
      dragGroup = context.dragGroup = createDragGroup(elements);
    }
 
    translate(dragGroup, event.x, event.y);
  });
 
  eventBus.on('create.cleanup', function(event) {
    var context = event.context,
        dragGroup = context.dragGroup;
 
    Eif (dragGroup) {
      svgRemove(dragGroup);
    }
  });
}
 
CreatePreview.$inject = [
  'canvas',
  'eventBus',
  'graphicsFactory',
  'previewSupport',
  'styles'
];