(function() { var Model = require('bpmn/Model'), Viewer = require('bpmn/Viewer'), Modeler = require('bpmn/Modeler'); var container = $('#js-drop-zone'); var canvas = $('#js-canvas'); var BpmnJS = canvas.is('.modeler') ? Modeler : Viewer; var renderer = new BpmnJS(canvas.get(0)); var newDiagramXML = '' + '' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ''; function createNewDiagram() { var bpmn = Model.instance(); openDiagram(newDiagramXML); } function openDiagram(xml) { Model.fromXML(xml, 'bpmn:Definitions', function(err, definitions) { if (err) { console.error(err); } else { renderer.importDefinitions(definitions, function() { container.addClass('with-diagram'); }); } }); } function saveDiagram(done) { var definitions = renderer.definitions; if (definitions) { Model.toXML(definitions, { format: true }, function(err, xml) { done(err, xml); }); } } ////// 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'); setInterval(function() { saveDiagram(function(err, xml) { if (!xml || err) { return; } var encoded = encodeURIComponent(xml); downloadLink.attr({ 'href': 'data:application/bpmn20-xml;charset=UTF-8,' + encoded, 'download': 'diagram.bpmn' }); }); }, 5000); }); })();