2022-06-07 18:16:49 +00:00
|
|
|
import BpmnModeler from 'bpmn-js/lib/Modeler';
|
2022-08-05 18:57:30 +00:00
|
|
|
import {
|
|
|
|
BpmnPropertiesPanelModule,
|
|
|
|
BpmnPropertiesProviderModule,
|
|
|
|
} from 'bpmn-js-properties-panel';
|
2022-07-07 15:20:43 +00:00
|
|
|
import FileSaver from 'file-saver';
|
2022-08-05 18:57:30 +00:00
|
|
|
import diagramXML from '../test/spec/bpmn/collaboration.bpmn';
|
2022-07-08 14:30:21 +00:00
|
|
|
import spiffworkflow from './spiffworkflow';
|
2022-06-15 19:13:35 +00:00
|
|
|
|
2022-06-15 13:51:46 +00:00
|
|
|
const modelerEl = document.getElementById('modeler');
|
|
|
|
const panelEl = document.getElementById('panel');
|
2022-06-27 21:29:24 +00:00
|
|
|
const spiffModdleExtension = require('./spiffworkflow/moddle/spiffworkflow.json');
|
|
|
|
|
2022-08-04 20:38:40 +00:00
|
|
|
let bpmnModeler;
|
|
|
|
|
2022-06-07 18:16:49 +00:00
|
|
|
// create modeler
|
2022-08-04 19:57:15 +00:00
|
|
|
try {
|
2022-08-04 20:38:40 +00:00
|
|
|
bpmnModeler = new BpmnModeler({
|
2022-08-04 19:57:15 +00:00
|
|
|
container: modelerEl,
|
|
|
|
propertiesPanel: {
|
2022-08-05 18:57:30 +00:00
|
|
|
parent: panelEl,
|
2022-08-04 19:57:15 +00:00
|
|
|
},
|
|
|
|
additionalModules: [
|
|
|
|
spiffworkflow,
|
|
|
|
BpmnPropertiesPanelModule,
|
|
|
|
BpmnPropertiesProviderModule,
|
|
|
|
],
|
|
|
|
moddleExtensions: {
|
2022-08-05 18:57:30 +00:00
|
|
|
spiffworkflowModdle: spiffModdleExtension,
|
|
|
|
},
|
2022-08-04 19:57:15 +00:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
if (error.constructor.name === 'AggregateError') {
|
|
|
|
console.log(error.message);
|
|
|
|
console.log(error.name);
|
|
|
|
console.log(error.errors);
|
|
|
|
}
|
2022-08-04 20:38:40 +00:00
|
|
|
throw error;
|
2022-08-04 19:57:15 +00:00
|
|
|
}
|
2022-06-21 21:04:24 +00:00
|
|
|
|
2022-08-04 20:38:40 +00:00
|
|
|
// import XML
|
|
|
|
bpmnModeler.importXML(diagramXML).then(() => {});
|
|
|
|
|
2022-07-11 17:57:12 +00:00
|
|
|
/** ****************************************
|
|
|
|
* Below are a few helper methods so we can upload and download files
|
|
|
|
* easily from the editor for testing purposes.
|
|
|
|
* -----------------------------------------
|
|
|
|
*/
|
|
|
|
|
2022-06-21 21:04:24 +00:00
|
|
|
/**
|
|
|
|
* Just a quick bit of code so we can save the XML that is output.
|
|
|
|
* Helps for debugging against other libraries (like SpiffWorkflow)
|
|
|
|
*/
|
2022-08-05 18:57:30 +00:00
|
|
|
const btn = document.getElementById('downloadButton');
|
|
|
|
btn.addEventListener('click', (_event) => {
|
2022-06-21 21:04:24 +00:00
|
|
|
saveXML();
|
|
|
|
});
|
|
|
|
async function saveXML() {
|
|
|
|
const { xml } = await bpmnModeler.saveXML({ format: true });
|
2022-08-05 18:57:30 +00:00
|
|
|
const blob = new Blob([xml], { type: 'text/xml' });
|
2022-06-21 21:04:24 +00:00
|
|
|
FileSaver.saveAs(blob, 'diagram.bpmn');
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:57:12 +00:00
|
|
|
/**
|
|
|
|
* Just a quick bit of code so we can open a local XML file
|
|
|
|
* Helps for debugging against other libraries (like SpiffWorkflow)
|
|
|
|
*/
|
2022-08-05 18:57:30 +00:00
|
|
|
const uploadBtn = document.getElementById('uploadButton');
|
|
|
|
uploadBtn.addEventListener('click', (_event) => {
|
2022-07-11 17:57:12 +00:00
|
|
|
openFile(displayFile);
|
|
|
|
});
|
2022-06-21 21:04:24 +00:00
|
|
|
|
2022-07-11 17:57:12 +00:00
|
|
|
function clickElem(elem) {
|
2022-08-05 18:57:30 +00:00
|
|
|
const eventMouse = document.createEvent('MouseEvents');
|
|
|
|
eventMouse.initMouseEvent(
|
|
|
|
'click',
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
window,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
0,
|
|
|
|
null
|
|
|
|
);
|
2022-07-11 17:57:12 +00:00
|
|
|
elem.dispatchEvent(eventMouse);
|
|
|
|
}
|
2022-06-21 21:04:24 +00:00
|
|
|
|
2022-07-11 17:57:12 +00:00
|
|
|
function displayFile(contents) {
|
|
|
|
bpmnModeler.importXML(contents).then(() => {});
|
|
|
|
}
|
2022-06-21 21:04:24 +00:00
|
|
|
|
2022-08-05 18:57:30 +00:00
|
|
|
export default function openFile(func) {
|
|
|
|
const readFile = function readFileCallback(e) {
|
|
|
|
const file = e.target.files[0];
|
2022-07-11 17:57:12 +00:00
|
|
|
if (!file) {
|
|
|
|
return;
|
|
|
|
}
|
2022-08-05 18:57:30 +00:00
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = function onloadCallback(onloadEvent) {
|
|
|
|
const contents = onloadEvent.target.result;
|
2022-07-11 17:57:12 +00:00
|
|
|
fileInput.func(contents);
|
|
|
|
document.body.removeChild(fileInput);
|
|
|
|
};
|
|
|
|
reader.readAsText(file);
|
|
|
|
};
|
|
|
|
let fileInput = document.createElement('input');
|
|
|
|
fileInput.type = 'file';
|
|
|
|
fileInput.style.display = 'none';
|
|
|
|
fileInput.onchange = readFile;
|
|
|
|
fileInput.func = func;
|
|
|
|
document.body.appendChild(fileInput);
|
|
|
|
clickElem(fileInput);
|
|
|
|
}
|