2014-03-26 16:46:02 +00:00
|
|
|
(function(BpmnJS, $) {
|
2014-03-18 16:01:24 +00:00
|
|
|
|
|
|
|
var container = $('#js-drop-zone');
|
|
|
|
|
|
|
|
var canvas = $('#js-canvas');
|
|
|
|
|
2014-04-04 16:48:37 +00:00
|
|
|
var renderer = new BpmnJS({ container: canvas });
|
2014-03-18 16:01:24 +00:00
|
|
|
|
|
|
|
var newDiagramXML =
|
|
|
|
'<?xml version="1.0" encoding="UTF-8"?>' +
|
2014-03-21 15:43:15 +00:00
|
|
|
'<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd" id="sample-diagram" targetNamespace="http://bpmn.io/schema/bpmn">' +
|
2014-03-18 16:01:24 +00:00
|
|
|
' <bpmn2:process id="Process_1" isExecutable="false">' +
|
|
|
|
' <bpmn2:startEvent id="StartEvent_1"/>' +
|
|
|
|
' </bpmn2:process>' +
|
|
|
|
' <bpmndi:BPMNDiagram id="BPMNDiagram_1">' +
|
|
|
|
' <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">' +
|
|
|
|
' <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">' +
|
|
|
|
' <dc:Bounds height="36.0" width="36.0" x="412.0" y="240.0"/>' +
|
|
|
|
' </bpmndi:BPMNShape>' +
|
|
|
|
' </bpmndi:BPMNPlane>' +
|
|
|
|
' </bpmndi:BPMNDiagram>' +
|
|
|
|
'</bpmn2:definitions>';
|
|
|
|
|
|
|
|
function createNewDiagram() {
|
|
|
|
openDiagram(newDiagramXML);
|
|
|
|
}
|
|
|
|
|
|
|
|
function openDiagram(xml) {
|
|
|
|
|
2014-03-21 20:10:01 +00:00
|
|
|
renderer.importXML(xml, function(err) {
|
|
|
|
|
2014-03-18 16:01:24 +00:00
|
|
|
if (err) {
|
2014-03-21 20:10:01 +00:00
|
|
|
container
|
|
|
|
.removeClass('with-diagram')
|
|
|
|
.addClass('with-error');
|
|
|
|
|
|
|
|
container.find('.error pre').text(err.message);
|
|
|
|
|
2014-03-25 13:08:11 +00:00
|
|
|
console.error(err);
|
2014-03-18 16:01:24 +00:00
|
|
|
} else {
|
2014-03-21 20:10:01 +00:00
|
|
|
container
|
|
|
|
.removeClass('with-error')
|
|
|
|
.addClass('with-diagram');
|
2014-03-18 16:01:24 +00:00
|
|
|
}
|
2014-03-26 16:46:02 +00:00
|
|
|
|
|
|
|
|
2014-03-18 16:01:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-25 13:08:11 +00:00
|
|
|
function saveSVG(done) {
|
|
|
|
renderer.saveSVG(done);
|
|
|
|
}
|
2014-03-18 16:01:24 +00:00
|
|
|
|
2014-03-25 13:08:11 +00:00
|
|
|
function saveDiagram(done) {
|
2014-03-18 16:01:24 +00:00
|
|
|
|
2014-03-25 13:08:11 +00:00
|
|
|
renderer.saveXML({ format: true }, function(err, xml) {
|
|
|
|
done(err, xml);
|
|
|
|
});
|
2014-03-18 16:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////// file drag / drop ///////////////////////
|
|
|
|
|
|
|
|
// check file api availability
|
|
|
|
if (!window.FileList || !window.FileReader) {
|
|
|
|
window.alert('Looks like you use an older browser that does not support drag and drop. Try using Chrome, Firefox or the Internet Explorer > 10.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(function onFileDrop(container, callback) {
|
|
|
|
|
|
|
|
function handleFileSelect(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
var files = e.dataTransfer.files;
|
|
|
|
|
|
|
|
var file = files[0];
|
|
|
|
|
|
|
|
var reader = new FileReader();
|
|
|
|
|
|
|
|
reader.onload = function(e) {
|
|
|
|
|
|
|
|
var xml = e.target.result;
|
|
|
|
|
|
|
|
callback(xml);
|
|
|
|
};
|
|
|
|
|
|
|
|
reader.readAsText(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleDragOver(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
e.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
|
|
|
|
}
|
|
|
|
|
|
|
|
container.get(0).addEventListener('dragover', handleDragOver, false);
|
|
|
|
container.get(0).addEventListener('drop', handleFileSelect, false);
|
|
|
|
|
|
|
|
})(container, openDiagram);
|
|
|
|
|
|
|
|
|
|
|
|
$(document).on('ready', function() {
|
|
|
|
|
|
|
|
$('#js-create-diagram').click(function(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
createNewDiagram();
|
|
|
|
});
|
|
|
|
|
|
|
|
var downloadLink = $('#js-download-diagram');
|
2014-03-25 13:08:11 +00:00
|
|
|
var downloadSvgLink = $('#js-download-svg');
|
2014-03-18 16:01:24 +00:00
|
|
|
|
2014-03-25 13:08:11 +00:00
|
|
|
$('.buttons a').click(function(e) {
|
|
|
|
if (!$(this).is('.active')) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function setEncoded(link, name, data) {
|
|
|
|
var encodedData = encodeURIComponent(data);
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
link.addClass('active').attr({
|
|
|
|
'href': 'data:application/bpmn20-xml;charset=UTF-8,' + encodedData,
|
|
|
|
'download': name
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
link.removeClass('active');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-26 16:46:02 +00:00
|
|
|
var _ = require('lodash');
|
|
|
|
|
|
|
|
var exportArtifacts = _.debounce(function() {
|
2014-03-18 16:01:24 +00:00
|
|
|
|
2014-03-25 13:08:11 +00:00
|
|
|
saveSVG(function(err, svg) {
|
|
|
|
setEncoded(downloadSvgLink, 'diagram.svg', err ? null : svg);
|
|
|
|
});
|
2014-03-18 16:01:24 +00:00
|
|
|
|
2014-03-25 13:08:11 +00:00
|
|
|
saveDiagram(function(err, xml) {
|
|
|
|
setEncoded(downloadLink, 'diagram.bpmn', err ? null : xml);
|
2014-03-18 16:01:24 +00:00
|
|
|
});
|
2014-03-26 16:46:02 +00:00
|
|
|
}, 500);
|
2014-03-18 16:01:24 +00:00
|
|
|
|
2014-03-26 16:46:02 +00:00
|
|
|
renderer.on('commandStack.changed', exportArtifacts);
|
2014-03-18 16:01:24 +00:00
|
|
|
});
|
|
|
|
|
2014-04-04 16:48:37 +00:00
|
|
|
})(window.BpmnJS, require('jquery'));
|