Importer.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import BpmnTreeWalker from './BpmnTreeWalker';
  2. import {
  3. isFunction
  4. } from 'min-dash';
  5. /**
  6. * Import the definitions into a diagram.
  7. *
  8. * Errors and warnings are reported through the specified callback.
  9. *
  10. * @param {djs.Diagram} diagram
  11. * @param {ModdleElement<Definitions>} definitions
  12. * @param {ModdleElement<BPMNDiagram>} [bpmnDiagram] the diagram to be rendered
  13. * (if not provided, the first one will be rendered)
  14. * @param {Function} done the callback, invoked with (err, [ warning ]) once the import is done
  15. */
  16. export function importBpmnDiagram(diagram, definitions, bpmnDiagram, done) {
  17. if (isFunction(bpmnDiagram)) {
  18. done = bpmnDiagram;
  19. bpmnDiagram = null;
  20. }
  21. var importer,
  22. eventBus,
  23. translate;
  24. var error,
  25. warnings = [];
  26. /**
  27. * Walk the diagram semantically, importing (=drawing)
  28. * all elements you encounter.
  29. *
  30. * @param {ModdleElement<Definitions>} definitions
  31. * @param {ModdleElement<BPMNDiagram>} bpmnDiagram
  32. */
  33. function render(definitions, bpmnDiagram) {
  34. var visitor = {
  35. root: function(element) {
  36. return importer.add(element);
  37. },
  38. element: function(element, parentShape) {
  39. return importer.add(element, parentShape);
  40. },
  41. error: function(message, context) {
  42. warnings.push({ message: message, context: context });
  43. }
  44. };
  45. var walker = new BpmnTreeWalker(visitor, translate);
  46. // traverse BPMN 2.0 document model,
  47. // starting at definitions
  48. walker.handleDefinitions(definitions, bpmnDiagram);
  49. }
  50. try {
  51. importer = diagram.get('bpmnImporter');
  52. eventBus = diagram.get('eventBus');
  53. translate = diagram.get('translate');
  54. eventBus.fire('import.render.start', { definitions: definitions });
  55. render(definitions, bpmnDiagram);
  56. eventBus.fire('import.render.complete', {
  57. error: error,
  58. warnings: warnings
  59. });
  60. } catch (e) {
  61. error = e;
  62. }
  63. done(error, warnings);
  64. }