2015-01-19 11:13:39 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
var forEach = require('lodash/collection/forEach'),
|
|
|
|
filter = require('lodash/collection/filter');
|
|
|
|
|
|
|
|
var REPLACE_OPTIONS = require ('./ReplaceOptions');
|
|
|
|
|
|
|
|
var startEventReplace = REPLACE_OPTIONS.START_EVENT,
|
|
|
|
intermediateEventReplace = REPLACE_OPTIONS.INTERMEDIATE_EVENT,
|
|
|
|
endEventReplace = REPLACE_OPTIONS.END_EVENT,
|
|
|
|
gatewayReplace = REPLACE_OPTIONS.GATEWAY,
|
|
|
|
taskReplace = REPLACE_OPTIONS.TASK;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A replace menu provider that gives users the controls to choose
|
|
|
|
* and replace BPMN elements with each other.
|
|
|
|
*
|
|
|
|
* @param {BpmnFactory} bpmnFactory
|
|
|
|
* @param {Moddle} moddle
|
|
|
|
* @param {PopupMenu} popupMenu
|
|
|
|
* @param {Replace} replace
|
|
|
|
*/
|
2015-03-11 15:31:42 +00:00
|
|
|
function BpmnReplace(bpmnFactory, moddle, popupMenu, replace, selection) {
|
2015-03-11 15:17:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares a new business object for the replacement element
|
|
|
|
* and triggers the replace operation.
|
|
|
|
*
|
|
|
|
* @param {djs.model.Base} element
|
|
|
|
* @param {Object} target
|
|
|
|
* @return {djs.model.Base} the newly created element
|
|
|
|
*/
|
|
|
|
function replaceElement(element, target) {
|
|
|
|
|
|
|
|
var type = target.type,
|
|
|
|
oldBusinessObject = element.businessObject,
|
|
|
|
businessObject = bpmnFactory.create(type);
|
|
|
|
|
|
|
|
var newElement = {
|
|
|
|
type: type,
|
|
|
|
businessObject: businessObject
|
|
|
|
};
|
|
|
|
|
|
|
|
// initialize custom BPMN extensions
|
|
|
|
|
|
|
|
if (target.eventDefinition) {
|
|
|
|
var eventDefinitions = businessObject.get('eventDefinitions'),
|
|
|
|
eventDefinition = moddle.create(target.eventDefinition);
|
|
|
|
|
|
|
|
eventDefinitions.push(eventDefinition);
|
|
|
|
}
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
if (target.instantiate !== undefined) {
|
|
|
|
businessObject.instantiate = target.instantiate;
|
|
|
|
}
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
if (target.eventGatewayType !== undefined) {
|
|
|
|
businessObject.eventGatewayType = target.eventGatewayType;
|
|
|
|
}
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
// copy size (for activities only)
|
|
|
|
if (oldBusinessObject.$instanceOf('bpmn:Activity')) {
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
// TODO: need also to respect min/max Size
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
newElement.width = element.width;
|
|
|
|
newElement.height = element.height;
|
|
|
|
}
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
// TODO: copy other elligable properties from old business object
|
|
|
|
businessObject.name = oldBusinessObject.name;
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-06-23 15:24:13 +00:00
|
|
|
businessObject.loopCharacteristics = oldBusinessObject.loopCharacteristics;
|
|
|
|
|
2015-03-11 15:31:42 +00:00
|
|
|
newElement = replace.replaceElement(element, newElement);
|
|
|
|
|
|
|
|
selection.select(newElement);
|
|
|
|
|
|
|
|
return newElement;
|
2015-03-11 15:17:19 +00:00
|
|
|
}
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-02-26 15:19:34 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
function getReplaceOptions(element) {
|
2015-02-26 15:19:34 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
var menuEntries = [];
|
|
|
|
var businessObject = element.businessObject;
|
2015-02-26 15:19:34 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
if (businessObject.$instanceOf('bpmn:StartEvent')) {
|
|
|
|
addEntries(startEventReplace, filterEvents);
|
|
|
|
} else
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
if (businessObject.$instanceOf('bpmn:IntermediateCatchEvent') ||
|
|
|
|
businessObject.$instanceOf('bpmn:IntermediateThrowEvent')) {
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
addEntries(intermediateEventReplace, filterEvents);
|
|
|
|
} else
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
if (businessObject.$instanceOf('bpmn:EndEvent')) {
|
2015-02-26 15:19:34 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
addEntries(endEventReplace, filterEvents);
|
|
|
|
} else
|
2015-02-26 15:19:34 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
if (businessObject.$instanceOf('bpmn:Gateway')) {
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
addEntries(gatewayReplace, function(entry) {
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
return entry.target.type !== businessObject.$type;
|
|
|
|
});
|
|
|
|
} else
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
if (businessObject.$instanceOf('bpmn:FlowNode')) {
|
|
|
|
addEntries(taskReplace, function(entry) {
|
|
|
|
return entry.target.type !== businessObject.$type;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function filterEvents(entry) {
|
|
|
|
|
|
|
|
var target = entry.target;
|
|
|
|
|
|
|
|
var eventDefinition = businessObject.eventDefinitions && businessObject.eventDefinitions[0].$type;
|
|
|
|
var isEventDefinitionEqual = target.eventDefinition == eventDefinition;
|
|
|
|
var isEventTypeEqual = businessObject.$type == target.type;
|
|
|
|
|
|
|
|
return ((!isEventDefinitionEqual && isEventTypeEqual) ||
|
|
|
|
!isEventTypeEqual) ||
|
|
|
|
!(isEventDefinitionEqual && isEventTypeEqual);
|
|
|
|
}
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
function addEntries(entries, filterFun) {
|
|
|
|
// Filter selected type from the array
|
|
|
|
var filteredEntries = filter(entries, filterFun);
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
// Add entries to replace menu
|
|
|
|
forEach(filteredEntries, function(definition) {
|
|
|
|
|
|
|
|
var entry = addMenuEntry(definition);
|
|
|
|
menuEntries.push(entry);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addMenuEntry(definition) {
|
|
|
|
|
|
|
|
return {
|
|
|
|
label: definition.label,
|
|
|
|
className: definition.className,
|
2015-06-11 13:21:21 +00:00
|
|
|
id: definition.actionName,
|
|
|
|
action: function() {
|
|
|
|
replaceElement(element, definition.target);
|
2015-03-11 15:17:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return menuEntries;
|
2015-01-19 11:13:39 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
|
|
|
|
// API
|
|
|
|
|
|
|
|
this.openChooser = function(position, element) {
|
|
|
|
var entries = this.getReplaceOptions(element);
|
|
|
|
|
2015-06-11 13:21:21 +00:00
|
|
|
popupMenu.open(
|
|
|
|
{
|
|
|
|
className: 'replace-menu',
|
|
|
|
position: position,
|
|
|
|
entries: entries
|
|
|
|
}
|
|
|
|
);
|
2015-03-11 15:17:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.getReplaceOptions = getReplaceOptions;
|
|
|
|
|
|
|
|
this.replaceElement = replaceElement;
|
|
|
|
}
|
|
|
|
|
2015-03-11 15:31:42 +00:00
|
|
|
BpmnReplace.$inject = [ 'bpmnFactory', 'moddle', 'popupMenu', 'replace', 'selection' ];
|
2015-03-11 15:17:19 +00:00
|
|
|
|
|
|
|
module.exports = BpmnReplace;
|