all files / Github/diagram-js/lib/features/space-tool/ SpaceTool.js

87.78% Statements 79/90
68.63% Branches 35/51
86.67% Functions 13/15
87.78% Lines 79/90
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                                                                    1128× 1128× 1128× 1128× 1128×   1128×   1128×         1128×                             1128×             1128×                                                                                                                                                         19×                                                                                               19× 19×   19×       19×   116× 116×     116×       115× 18×       97×       92×       84×       33×       19×                          
import {
  getDirection
} from './SpaceUtil';
 
import {
  set as cursorSet
} from '../../util/Cursor';
 
import {
  hasPrimaryModifier
} from '../../util/Mouse';
 
var abs = Math.abs,
    round = Math.round;
 
var HIGH_PRIORITY = 1500,
    SPACE_TOOL_CURSOR = 'crosshair';
 
var AXIS_TO_DIMENSION = { x: 'width', y: 'height' },
    AXIS_INVERTED = { x: 'y', y: 'x' };
 
import {
  selfAndAllChildren as getAllChildren
} from '../../util/Elements';
 
import {
  assign,
  forEach
} from 'min-dash';
 
 
/**
 * A tool that allows users to create and remove space in a diagram.
 *
 * The tool needs to be activated manually via {@link SpaceTool#activate(MouseEvent)}.
 */
export default function SpaceTool(
    eventBus, dragging, canvas,
    modeling, rules, toolManager) {
 
  this._canvas = canvas;
  this._dragging = dragging;
  this._modeling = modeling;
  this._rules = rules;
  this._toolManager = toolManager;
 
  var self = this;
 
  toolManager.registerTool('space', {
    tool: 'spaceTool.selection',
    dragging: 'spaceTool'
  });
 
  eventBus.on('spaceTool.selection.end', function(event) {
    var target = event.originalEvent.target;
 
    // only reactive on diagram click
    // on some occasions, event.hover is not set and we have to check if the target is an svg
    if (!event.hover && !(target instanceof SVGElement)) {
      return;
    }
 
    eventBus.once('spaceTool.selection.ended', function() {
      self.activateMakeSpace(event.originalEvent);
    });
  });
 
 
  eventBus.on('spaceTool.move', HIGH_PRIORITY , function(event) {
 
    var context = event.context;
 
    Eif (!context.initialized) {
      context.initialized = self.initializeMakeSpace(event, context);
    }
  });
 
 
  eventBus.on('spaceTool.end', function(event) {
 
    var context = event.context,
        axis = context.axis,
        direction = context.direction,
        movingShapes = context.movingShapes,
        resizingShapes = context.resizingShapes;
 
    // skip if create space has not been initialized yet
    Iif (!context.initialized) {
      return;
    }
 
    var delta = { x: round(event.dx), y: round(event.dy) };
    delta[ AXIS_INVERTED[ axis ] ] = 0;
 
    var insideBounds = true;
 
    // check if the space tool cursor is inside of bounds of
    // any of the shapes that would be resized.
    forEach(resizingShapes, function(shape) {
 
      Iif ((direction === 'w' && event.x > shape.x + shape.width) ||
          (direction === 'e' && event.x < shape.x) ||
          (direction === 'n' && event.y > shape.y + shape.height) ||
          (direction === 's' && event.y < shape.y)) {
 
        insideBounds = false;
        return;
      }
    });
 
    Eif (insideBounds) {
      // make space only if the cursor is inside bounds
      self.makeSpace(movingShapes, resizingShapes, delta, direction);
    }
 
    eventBus.once('spaceTool.ended', function(event) {
      // reactivate space tool after usage
      self.activateSelection(event.originalEvent, true, true);
    });
 
  });
}
 
SpaceTool.$inject = [
  'eventBus',
  'dragging',
  'canvas',
  'modeling',
  'rules',
  'toolManager'
];
 
 
/**
 * Activate space tool selection
 *
 * @param  {MouseEvent} event
 * @param  {Boolean} autoActivate
 */
SpaceTool.prototype.activateSelection = function(event, autoActivate, reactivate) {
  this._dragging.init(event, 'spaceTool.selection', {
    trapClick: false,
    cursor: SPACE_TOOL_CURSOR,
    autoActivate: autoActivate,
    data: {
      context: {
        reactivate: reactivate
      }
    }
  });
};
 
/**
 * Activate make space
 *
 * @param  {MouseEvent} event
 */
SpaceTool.prototype.activateMakeSpace = function(event) {
  this._dragging.init(event, 'spaceTool', {
    autoActivate: true,
    cursor: SPACE_TOOL_CURSOR,
    data: {
      context: {}
    }
  });
};
 
/**
 * Actually make space on the diagram
 *
 * @param  {Array<djs.model.Shape>} movingShapes
 * @param  {Array<djs.model.Shape>} resizingShapes
 * @param  {Point} delta
 * @param  {String} direction
 */
SpaceTool.prototype.makeSpace = function(movingShapes, resizingShapes, delta, direction) {
  return this._modeling.createSpace(movingShapes, resizingShapes, delta, direction);
};
 
/**
 * Initialize make space and return true if that was successful.
 *
 * @param {Event} event
 * @param {Object} context
 *
 * @return {Boolean} true, if successful
 */
SpaceTool.prototype.initializeMakeSpace = function(event, context) {
 
  var axis = abs(event.dx) > abs(event.dy) ? 'x' : 'y',
      offset = event['d' + axis],
      // start point of create space operation
      spacePos = event[axis] - offset;
 
  if (abs(offset) < 5) {
    return false;
  }
 
  // invert the offset in order to remove space when moving left
  Iif (offset < 0) {
    offset *= -1;
  }
 
  // inverts the offset to choose the shapes
  // on the opposite side of the resizer if
  // a key modifier is pressed
  Iif (hasPrimaryModifier(event)) {
    offset *= -1;
  }
 
  var rootShape = this._canvas.getRootElement();
 
  var allShapes = getAllChildren(rootShape, true);
 
  var adjustments = this.calculateAdjustments(allShapes, axis, offset, spacePos);
 
  // store data in context
  assign(context, adjustments, {
    axis: axis,
    direction: getDirection(axis, offset)
  });
 
  cursorSet('resize-' + (axis === 'x' ? 'ew' : 'ns'));
 
  return true;
};
 
/**
 * Calculate adjustments needed when making space
 *
 * @param  {Array<djs.model.Shape>} elements
 * @param  {String} axis
 * @param  {Number} offset
 * @param  {Number} spacePos
 *
 * @return {Object}
 */
SpaceTool.prototype.calculateAdjustments = function(elements, axis, offset, spacePos) {
 
  var movingShapes = [],
      resizingShapes = [];
 
  var rules = this._rules;
 
  // collect all elements that need to be moved _AND_
  // resized given on the initial create space position
  elements.forEach(function(shape) {
 
    var shapeStart = shape[axis],
        shapeEnd = shapeStart + shape[AXIS_TO_DIMENSION[axis]];
 
    // checking if it's root
    if (!shape.parent) {
      return;
    }
 
    // checking if it's a shape
    if (shape.waypoints) {
      return;
    }
 
    // shape after spacePos
    if (offset > 0 && shapeStart > spacePos) {
      return movingShapes.push(shape);
    }
 
    // shape before spacePos
    if (offset < 0 && shapeEnd < spacePos) {
      return movingShapes.push(shape);
    }
 
    // shape on top of spacePos, resize only if allowed
    if (shapeStart < spacePos &&
        shapeEnd > spacePos &&
        rules.allowed('shape.resize', { shape: shape })) {
 
      return resizingShapes.push(shape);
    }
  });
 
  return {
    movingShapes: movingShapes,
    resizingShapes: resizingShapes
  };
};
 
SpaceTool.prototype.toggle = function() {
  Iif (this.isActive()) {
    this._dragging.cancel();
  } else {
    this.activateSelection();
  }
};
 
SpaceTool.prototype.isActive = function() {
  var context = this._dragging.context();
 
  return context && /^spaceTool/.test(context.prefix);
};