2022-10-12 14:21:19 +00:00
|
|
|
import {
|
|
|
|
HeaderButton,
|
|
|
|
TextAreaEntry,
|
|
|
|
isTextFieldEntryEdited,
|
|
|
|
ListGroup,
|
|
|
|
} from '@bpmn-io/properties-panel';
|
|
|
|
import { useService } from 'bpmn-js-properties-panel';
|
|
|
|
import { ScriptUnitTestArray } from './ScriptUnitTestArray';
|
|
|
|
|
|
|
|
export const SCRIPT_TYPE = {
|
|
|
|
bpmn: 'bpmn:script',
|
|
|
|
pre: 'spiffworkflow:preScript',
|
|
|
|
post: 'spiffworkflow:postScript',
|
|
|
|
};
|
|
|
|
|
|
|
|
function PythonScript(props) {
|
|
|
|
const { element, id } = props;
|
|
|
|
const { type } = props;
|
2022-10-31 21:02:34 +00:00
|
|
|
const { moddle, commandStack } = props;
|
2022-10-12 14:21:19 +00:00
|
|
|
const { label } = props;
|
|
|
|
const { description } = props;
|
|
|
|
|
|
|
|
const translate = useService('translate');
|
|
|
|
const debounce = useService('debounceInput');
|
|
|
|
|
|
|
|
const getValue = () => {
|
2022-10-31 21:02:34 +00:00
|
|
|
return getScriptString(element, type);
|
2022-10-12 14:21:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const setValue = (value) => {
|
2022-10-31 21:02:34 +00:00
|
|
|
updateScript(commandStack, moddle, element, type, value);
|
2022-10-12 14:21:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return TextAreaEntry({
|
|
|
|
id,
|
|
|
|
element,
|
|
|
|
description: translate(description),
|
|
|
|
label: translate(label),
|
|
|
|
getValue,
|
|
|
|
setValue,
|
|
|
|
debounce,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function LaunchEditorButton(props) {
|
2022-10-31 21:02:34 +00:00
|
|
|
const { element, type, moddle, commandStack } = props;
|
2022-10-12 14:21:19 +00:00
|
|
|
const eventBus = useService('eventBus');
|
|
|
|
return HeaderButton({
|
|
|
|
className: 'spiffworkflow-properties-panel-button',
|
|
|
|
onClick: () => {
|
2022-10-31 21:02:34 +00:00
|
|
|
const script = getScriptString(element, type);
|
2022-11-07 19:35:53 +00:00
|
|
|
eventBus.fire('spiff.script.edit', {
|
2022-10-31 21:02:34 +00:00
|
|
|
element,
|
|
|
|
scriptType: type,
|
|
|
|
script,
|
|
|
|
eventBus,
|
|
|
|
});
|
|
|
|
// Listen for a response, to update the script.
|
2022-11-07 19:35:53 +00:00
|
|
|
eventBus.once('spiff.script.update', (event) => {
|
2022-10-31 21:02:34 +00:00
|
|
|
updateScript(
|
|
|
|
commandStack,
|
|
|
|
moddle,
|
|
|
|
element,
|
|
|
|
event.scriptType,
|
|
|
|
event.script
|
|
|
|
);
|
|
|
|
});
|
2022-10-12 14:21:19 +00:00
|
|
|
},
|
|
|
|
children: 'Launch Editor',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-31 21:02:34 +00:00
|
|
|
* Finds the value of the given type within the extensionElements
|
|
|
|
* given a type of "spiff:preScript", would find it in this, and return
|
|
|
|
* the object.
|
|
|
|
*
|
|
|
|
* <bpmn:
|
|
|
|
<bpmn:userTask id="123" name="My User Task!">
|
|
|
|
<bpmn:extensionElements>
|
|
|
|
<spiff:preScript>
|
|
|
|
me = "100% awesome"
|
|
|
|
</spiff:preScript>
|
|
|
|
</bpmn:extensionElements>
|
|
|
|
...
|
|
|
|
</bpmn:userTask>
|
|
|
|
*
|
|
|
|
* @returns {string|null|*}
|
|
|
|
*/
|
|
|
|
function getScriptObject(element, scriptType) {
|
|
|
|
const bizObj = element.businessObject;
|
|
|
|
if (scriptType === SCRIPT_TYPE.bpmn) {
|
|
|
|
return bizObj;
|
|
|
|
}
|
|
|
|
if (!bizObj.extensionElements) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return bizObj.extensionElements
|
|
|
|
.get('values')
|
|
|
|
.filter(function getInstanceOfType(e) {
|
|
|
|
return e.$instanceOf(scriptType);
|
|
|
|
})[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateScript(commandStack, moddle, element, scriptType, newValue) {
|
|
|
|
const { businessObject } = element;
|
|
|
|
let scriptObj = getScriptObject(element, scriptType);
|
|
|
|
// Create the script object if needed.
|
|
|
|
if (!scriptObj) {
|
|
|
|
scriptObj = moddle.create(scriptType);
|
|
|
|
if (scriptType !== SCRIPT_TYPE.bpmn) {
|
|
|
|
let { extensionElements } = businessObject;
|
|
|
|
if (!extensionElements) {
|
|
|
|
extensionElements = moddle.create('bpmn:ExtensionElements');
|
|
|
|
}
|
2022-11-07 19:35:53 +00:00
|
|
|
scriptObj.value = newValue;
|
2022-10-31 21:02:34 +00:00
|
|
|
extensionElements.get('values').push(scriptObj);
|
|
|
|
commandStack.execute('element.updateModdleProperties', {
|
|
|
|
element,
|
|
|
|
moddleElement: businessObject,
|
|
|
|
properties: {
|
|
|
|
extensionElements,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-07 19:35:53 +00:00
|
|
|
let newProps = { value: newValue };
|
|
|
|
if (scriptType === SCRIPT_TYPE.bpmn) {
|
|
|
|
newProps = { script: newValue };
|
|
|
|
}
|
2022-10-31 21:02:34 +00:00
|
|
|
commandStack.execute('element.updateModdleProperties', {
|
|
|
|
element,
|
|
|
|
moddleElement: scriptObj,
|
2022-11-07 19:35:53 +00:00
|
|
|
properties: newProps,
|
2022-10-31 21:02:34 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getScriptString(element, scriptType) {
|
|
|
|
const scriptObj = getScriptObject(element, scriptType);
|
2022-11-07 19:35:53 +00:00
|
|
|
if (scriptObj && scriptObj.value) {
|
|
|
|
return scriptObj.value;
|
|
|
|
}
|
2022-10-31 21:02:34 +00:00
|
|
|
if (scriptObj && scriptObj.script) {
|
|
|
|
return scriptObj.script;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a text box and button for editing a script.
|
2022-10-12 14:21:19 +00:00
|
|
|
* @param element The elemment that should get the script task.
|
|
|
|
* @param scriptType The type of script -- can be a preScript, postScript or a BPMN:Script for script tags
|
|
|
|
* @param moddle For updating the underlying xml document when needed.
|
|
|
|
* @returns {[{component: (function(*)), isEdited: *, id: string, element},{component: (function(*)), isEdited: *, id: string, element}]}
|
|
|
|
*/
|
|
|
|
export default function getEntries(props) {
|
|
|
|
const {
|
|
|
|
element,
|
|
|
|
moddle,
|
|
|
|
scriptType,
|
|
|
|
label,
|
|
|
|
description,
|
|
|
|
translate,
|
|
|
|
commandStack,
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const entries = [
|
|
|
|
{
|
|
|
|
id: `pythonScript_${scriptType}`,
|
|
|
|
element,
|
|
|
|
type: scriptType,
|
|
|
|
component: PythonScript,
|
|
|
|
isEdited: isTextFieldEntryEdited,
|
|
|
|
moddle,
|
2022-10-31 21:02:34 +00:00
|
|
|
commandStack,
|
2022-10-12 14:21:19 +00:00
|
|
|
label,
|
|
|
|
description,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: `launchEditorButton${scriptType}`,
|
|
|
|
type: scriptType,
|
|
|
|
element,
|
|
|
|
component: LaunchEditorButton,
|
|
|
|
isEdited: isTextFieldEntryEdited,
|
|
|
|
moddle,
|
2022-10-31 21:02:34 +00:00
|
|
|
commandStack,
|
2022-10-12 14:21:19 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// do not support testing pre and post scripts at the moment
|
|
|
|
if (scriptType === SCRIPT_TYPE.bpmn) {
|
|
|
|
entries.push({
|
|
|
|
id: `scriptUnitTests${scriptType}`,
|
|
|
|
label: translate('Unit Tests'),
|
|
|
|
component: ListGroup,
|
|
|
|
...ScriptUnitTestArray({
|
|
|
|
element,
|
|
|
|
moddle,
|
|
|
|
translate,
|
|
|
|
commandStack,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
}
|