2015-08-17 14:43:15 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var CommandInterceptor = require('diagram-js/lib/command/CommandInterceptor');
|
|
|
|
|
|
|
|
var inherits = require('inherits');
|
|
|
|
|
|
|
|
var assign = require('lodash/object/assign'),
|
2015-09-24 14:50:34 +00:00
|
|
|
forEach = require('lodash/collection/forEach');
|
2015-08-17 14:43:15 +00:00
|
|
|
|
2016-09-14 06:18:56 +00:00
|
|
|
var domQuery = require('min-dom/lib/query');
|
|
|
|
|
|
|
|
var svgAttr = require('tiny-svg/lib/attr');
|
|
|
|
|
2015-08-17 14:43:15 +00:00
|
|
|
var LOW_PRIORITY = 250;
|
|
|
|
|
2016-08-08 09:23:39 +00:00
|
|
|
function BpmnReplacePreview(eventBus, elementRegistry, elementFactory, canvas, previewSupport) {
|
2015-08-17 14:43:15 +00:00
|
|
|
|
|
|
|
CommandInterceptor.call(this, eventBus);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the visuals of all elements in the context which can be replaced
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
*/
|
|
|
|
function replaceVisual(context) {
|
|
|
|
|
2015-09-01 15:57:32 +00:00
|
|
|
var replacements = context.canExecute.replacements;
|
2015-08-17 14:43:15 +00:00
|
|
|
|
2015-08-20 15:52:49 +00:00
|
|
|
forEach(replacements, function(replacement) {
|
2015-08-17 14:43:15 +00:00
|
|
|
|
2015-08-20 15:52:49 +00:00
|
|
|
var id = replacement.oldElementId;
|
2015-08-17 14:43:15 +00:00
|
|
|
|
|
|
|
var newElement = {
|
2015-08-20 15:52:49 +00:00
|
|
|
type: replacement.newElementType
|
2015-08-17 14:43:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// if the visual of the element is already replaced
|
2015-09-24 14:50:34 +00:00
|
|
|
if (context.visualReplacements[id]) {
|
2015-08-17 14:43:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var element = elementRegistry.get(id);
|
|
|
|
|
|
|
|
assign(newElement, { x: element.x, y: element.y });
|
|
|
|
|
|
|
|
// create a temporary shape
|
|
|
|
var tempShape = elementFactory.createShape(newElement);
|
|
|
|
|
|
|
|
canvas.addShape(tempShape, element.parent);
|
|
|
|
|
|
|
|
// select the original SVG element related to the element and hide it
|
2016-09-14 06:18:56 +00:00
|
|
|
var gfx = domQuery('[data-element-id=' + element.id + ']', context.dragGroup);
|
2015-08-17 14:43:15 +00:00
|
|
|
|
|
|
|
if (gfx) {
|
2016-09-14 06:18:56 +00:00
|
|
|
svgAttr(gfx, { display: 'none' });
|
2015-08-17 14:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// clone the gfx of the temporary shape and add it to the drag group
|
2016-08-08 09:23:39 +00:00
|
|
|
var dragger = previewSupport.addDragger(tempShape, context.dragGroup);
|
2015-08-17 14:43:15 +00:00
|
|
|
|
2015-09-24 14:50:34 +00:00
|
|
|
context.visualReplacements[id] = dragger;
|
2015-08-17 14:43:15 +00:00
|
|
|
|
|
|
|
canvas.removeShape(tempShape);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore the original visuals of the previously replaced elements
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
*/
|
|
|
|
function restoreVisual(context) {
|
|
|
|
|
|
|
|
var visualReplacements = context.visualReplacements;
|
|
|
|
|
2015-09-24 14:50:34 +00:00
|
|
|
forEach(visualReplacements, function(dragger, id) {
|
2015-08-17 14:43:15 +00:00
|
|
|
|
2016-09-14 06:18:56 +00:00
|
|
|
var originalGfx = domQuery('[data-element-id=' + id + ']', context.dragGroup);
|
2015-08-17 14:43:15 +00:00
|
|
|
|
|
|
|
if (originalGfx) {
|
2016-09-14 06:18:56 +00:00
|
|
|
svgAttr(originalGfx, { display: 'inline' });
|
2015-08-17 14:43:15 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 14:50:34 +00:00
|
|
|
dragger.remove();
|
2015-08-17 14:43:15 +00:00
|
|
|
|
2015-09-24 14:50:34 +00:00
|
|
|
if (visualReplacements[id]) {
|
|
|
|
delete visualReplacements[id];
|
2015-08-17 14:43:15 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
eventBus.on('shape.move.move', LOW_PRIORITY, function(event) {
|
|
|
|
|
|
|
|
var context = event.context,
|
|
|
|
canExecute = context.canExecute;
|
|
|
|
|
|
|
|
if (!context.visualReplacements) {
|
2015-09-24 14:50:34 +00:00
|
|
|
context.visualReplacements = {};
|
2015-08-17 14:43:15 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 15:57:32 +00:00
|
|
|
if (canExecute.replacements) {
|
2015-08-17 14:43:15 +00:00
|
|
|
replaceVisual(context);
|
|
|
|
} else {
|
|
|
|
restoreVisual(context);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-08 09:23:39 +00:00
|
|
|
BpmnReplacePreview.$inject = [ 'eventBus', 'elementRegistry', 'elementFactory', 'canvas', 'previewSupport' ];
|
2015-08-17 14:43:15 +00:00
|
|
|
|
|
|
|
inherits(BpmnReplacePreview, CommandInterceptor);
|
|
|
|
|
|
|
|
module.exports = BpmnReplacePreview;
|