# all files / Github/bpmn-js/lib/import/ Importer.js

89.47% Statements 17/19
50% Branches 1/2
100% Functions 5/5
89.47% Lines 17/19

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81

    1292×

    1292×

    1291×

    1288×

    14080×

    7×

    1291×

    1291×

    1292×
    1292×
    1291×
    1291×

    1291×

    1291×

    1288×

    4×

    1292×
     import BpmnTreeWalker from './BpmnTreeWalker';

    import {
      isFunction
    } from 'min-dash';

    /**
     * Import the definitions into a diagram.
     *
     * Errors and warnings are reported through the specified callback.
     *
     * @param  {djs.Diagram} diagram
     * @param  {ModdleElement<Definitions>} definitions
     * @param  {ModdleElement<BPMNDiagram>} [bpmnDiagram] the diagram to be rendered
     * (if not provided, the first one will be rendered)
     * @param  {Function} done the callback, invoked with (err, [ warning ]) once the import is done
     */
    export function importBpmnDiagram(diagram, definitions, bpmnDiagram, done) {

      Iif (isFunction(bpmnDiagram)) {
        done = bpmnDiagram;
        bpmnDiagram = null;
      }

      var importer,
          eventBus,
          translate;

      var error,
          warnings = [];

      /**
       * Walk the diagram semantically, importing (=drawing)
       * all elements you encounter.
       *
       * @param {ModdleElement<Definitions>} definitions
       * @param {ModdleElement<BPMNDiagram>} bpmnDiagram
       */
      function render(definitions, bpmnDiagram) {

        var visitor = {

          root: function(element) {
            return importer.add(element);
          },

          element: function(element, parentShape) {
            return importer.add(element, parentShape);
          },

          error: function(message, context) {
            warnings.push({ message: message, context: context });
          }
        };

        var walker = new BpmnTreeWalker(visitor, translate);

        // traverse BPMN 2.0 document model,
        // starting at definitions
        walker.handleDefinitions(definitions, bpmnDiagram);
      }

      try {
        importer = diagram.get('bpmnImporter');
        eventBus = diagram.get('eventBus');
        translate = diagram.get('translate');

        eventBus.fire('import.render.start', { definitions: definitions });

        render(definitions, bpmnDiagram);

        eventBus.fire('import.render.complete', {
          error: error,
          warnings: warnings
        });
      } catch (e) {
        error = e;
      }

      done(error, warnings);
    }