all files / diagram-js/lib/features/preview-support/ PreviewSupport.js

96.55% Statements 56/58
78.57% Branches 11/14
100% Functions 13/13
96.55% Lines 56/58
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                                                                        1189× 1189× 1189×   1189×   1189×   1189× 145×     145×                                   136×                       167×   167× 167×   167×   167×         167×   167×                       13×               13×   13×               567×   567×     567×     400×       567× 314×     253× 759× 24×   24×                       24×   24×   24×                               24×   24×                             24×   24×                       24×                     24×                     567×  
import {
  forEach
} from 'min-dash';
 
import {
  append as svgAppend,
  attr as svgAttr,
  classes as svgClasses,
  clone as svgClone,
  create as svgCreate,
  remove as svgRemove
} from 'tiny-svg';
 
import { query as domQuery } from 'min-dom';
 
import { getVisual } from '../../util/GraphicsUtil';
 
var MARKER_TYPES = [
  'marker-start',
  'marker-mid',
  'marker-end'
];
 
var NODES_CAN_HAVE_MARKER = [
  'circle',
  'ellipse',
  'line',
  'path',
  'polygon',
  'polyline',
  'rect'
];
 
 
/**
 * Adds support for previews of moving/resizing elements.
 */
export default function PreviewSupport(elementRegistry, eventBus, canvas, styles) {
  this._elementRegistry = elementRegistry;
  this._canvas = canvas;
  this._styles = styles;
 
  this._clonedMarkers = {};
 
  var self = this;
 
  eventBus.on('drag.cleanup', function() {
    forEach(self._clonedMarkers, function(clonedMarker) {
      svgRemove(clonedMarker);
    });
 
    self._clonedMarkers = {};
  });
}
 
PreviewSupport.$inject = [
  'elementRegistry',
  'eventBus',
  'canvas',
  'styles'
];
 
 
/**
 * Returns graphics of an element.
 *
 * @param {djs.model.Base} element
 *
 * @return {SVGElement}
 */
PreviewSupport.prototype.getGfx = function(element) {
  return this._elementRegistry.getGraphics(element);
};
 
/**
 * Adds a move preview of a given shape to a given svg group.
 *
 * @param {djs.model.Base} element
 * @param {SVGElement} group
 * @param {SVGElement} [gfx]
 *
 * @return {SVGElement} dragger
 */
PreviewSupport.prototype.addDragger = function(element, group, gfx) {
  gfx = gfx || this.getGfx(element);
 
  var dragger = svgClone(gfx);
  var bbox = gfx.getBoundingClientRect();
 
  this._cloneMarkers(getVisual(dragger));
 
  svgAttr(dragger, this._styles.cls('djs-dragger', [], {
    x: bbox.top,
    y: bbox.left
  }));
 
  svgAppend(group, dragger);
 
  return dragger;
};
 
/**
 * Adds a resize preview of a given shape to a given svg group.
 *
 * @param {djs.model.Base} element
 * @param {SVGElement} group
 *
 * @return {SVGElement} frame
 */
PreviewSupport.prototype.addFrame = function(shape, group) {
 
  var frame = svgCreate('rect', {
    class: 'djs-resize-overlay',
    width:  shape.width,
    height: shape.height,
    x: shape.x,
    y: shape.y
  });
 
  svgAppend(group, frame);
 
  return frame;
};
 
/**
 * Clone all markers referenced by a node and its child nodes.
 *
 * @param {SVGElement} gfx
 */
PreviewSupport.prototype._cloneMarkers = function(gfx) {
  var self = this;
 
  Eif (gfx.childNodes) {
 
    // TODO: use forEach once we drop PhantomJS
    for (var i = 0; i < gfx.childNodes.length; i++) {
 
      // recursively clone markers of child nodes
      self._cloneMarkers(gfx.childNodes[ i ]);
    }
  }
 
  if (!canHaveMarker(gfx)) {
    return;
  }
 
  MARKER_TYPES.forEach(function(markerType) {
    if (svgAttr(gfx, markerType)) {
      var marker = getMarker(gfx, markerType, self._canvas.getContainer());
 
      self._cloneMarker(gfx, marker, markerType);
    }
  });
};
 
/**
 * Clone marker referenced by an element.
 *
 * @param {SVGElement} gfx
 * @param {SVGElement} marker
 * @param {string} markerType
 */
PreviewSupport.prototype._cloneMarker = function(gfx, marker, markerType) {
  var markerId = marker.id;
 
  var clonedMarker = this._clonedMarkers[ markerId ];
 
  if (!clonedMarker) {
    clonedMarker = svgClone(marker);
 
    var clonedMarkerId = markerId + '-clone';
 
    clonedMarker.id = clonedMarkerId;
 
    svgClasses(clonedMarker)
      .add('djs-dragger')
      .add('djs-dragger-marker');
 
    this._clonedMarkers[ markerId ] = clonedMarker;
 
    var defs = domQuery('defs', this._canvas._svg);
 
    Iif (!defs) {
      defs = svgCreate('defs');
 
      svgAppend(this._canvas._svg, defs);
    }
 
    svgAppend(defs, clonedMarker);
  }
 
  var reference = idToReference(this._clonedMarkers[ markerId ].id);
 
  svgAttr(gfx, markerType, reference);
};
 
// helpers //////////
 
/**
 * Get marker of given type referenced by node.
 *
 * @param {Node} node
 * @param {string} markerType
 * @param {Node} [parentNode]
 *
 * @param {Node}
 */
function getMarker(node, markerType, parentNode) {
  var id = referenceToId(svgAttr(node, markerType));
 
  return domQuery('marker#' + id, parentNode || document);
}
 
/**
 * Get ID of fragment within current document from its functional IRI reference.
 * References may use single or double quotes.
 *
 * @param {string} reference
 *
 * @returns {string}
 */
function referenceToId(reference) {
  return reference.match(/url\(['"]?#([^'"]*)['"]?\)/)[1];
}
 
/**
 * Get functional IRI reference for given ID of fragment within current document.
 *
 * @param {string} id
 *
 * @returns {string}
 */
function idToReference(id) {
  return 'url(#' + id + ')';
}
 
/**
 * Check wether node type can have marker attributes.
 *
 * @param {Node} node
 *
 * @returns {boolean}
 */
function canHaveMarker(node) {
  return NODES_CAN_HAVE_MARKER.indexOf(node.nodeName) !== -1;
}