# all files / Github/diagram-js/lib/ Diagram.js

87.88% Statements 29/33
75% Branches 12/16
100% Functions 10/10
87.88% Lines 29/33

    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
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203

    1295×
    1295×

    85717×

    36659×

    49058×
    12399×

    36659×

    36659×

    36659×

    36659×
    65166×

    1295×

    1295×

    1295×

    65166×

    65166×

    1295×

    1295×

    1295×

    1295×

    1295×

    1295×

    1295×

    1295×

    1295×

    1×
    1195×

    1×
    1272×

     import { Injector } from 'didi';

    import CoreModule from './core';

    /**
     * Bootstrap an injector from a list of modules, instantiating a number of default components
     *
     * @ignore
     * @param {Array<didi.Module>} bootstrapModules
     *
     * @return {didi.Injector} a injector to use to access the components
     */
    function bootstrap(bootstrapModules) {

      var modules = [],
          components = [];

      function hasModule(m) {
        return modules.indexOf(m) >= 0;
      }

      function addModule(m) {
        modules.push(m);
      }

      function visit(m) {
        if (hasModule(m)) {
          return;
        }

        (m.__depends__ || []).forEach(visit);

        Iif (hasModule(m)) {
          return;
        }

        addModule(m);

        (m.__init__ || []).forEach(function(c) {
          components.push(c);
        });
      }

      bootstrapModules.forEach(visit);

      var injector = new Injector(modules);

      components.forEach(function(c) {

        try {
          // eagerly resolve component (fn or string)
          injector[typeof c === 'string' ? 'get' : 'invoke'](c);
        } catch (e) {
          console.error('Failed to instantiate component');
          console.error(e.stack);

          throw e;
        }
      });

      return injector;
    }

    /**
     * Creates an injector from passed options.
     *
     * @ignore
     * @param  {Object} options
     * @return {didi.Injector}
     */
    function createInjector(options) {

      options = options || {};

      var configModule = {
        'config': ['value', options]
      };

      var modules = [ configModule, CoreModule ].concat(options.modules || []);

      return bootstrap(modules);
    }

    /**
     * The main diagram-js entry point that bootstraps the diagram with the given
     * configuration.
     *
     * To register extensions with the diagram, pass them as Array<didi.Module> to the constructor.
     *
     * @class djs.Diagram
     * @memberOf djs
     * @constructor
     *
     * @example
     *
     * <caption>Creating a plug-in that logs whenever a shape is added to the canvas.</caption>
     *
     * // plug-in implemenentation
     * function MyLoggingPlugin(eventBus) {
     *   eventBus.on('shape.added', function(event) {
     *     console.log('shape ', event.shape, ' was added to the diagram');
     *   });
     * }
     *
     * // export as module
     * export default {
     *   __init__: [ 'myLoggingPlugin' ],
     *     myLoggingPlugin: [ 'type', MyLoggingPlugin ]
     * };
     *
     *
     * // instantiate the diagram with the new plug-in
     *
     * import MyLoggingModule from 'path-to-my-logging-plugin';
     *
     * var diagram = new Diagram({
     *   modules: [
     *     MyLoggingModule
     *   ]
     * });
     *
     * diagram.invoke([ 'canvas', function(canvas) {
     *   // add shape to drawing canvas
     *   canvas.addShape({ x: 10, y: 10 });
     * });
     *
     * // 'shape ... was added to the diagram' logged to console
     *
     * @param {Object} options
     * @param {Array<didi.Module>} [options.modules] external modules to instantiate with the diagram
     * @param {didi.Injector} [injector] an (optional) injector to bootstrap the diagram with
     */
    export default function Diagram(options, injector) {

      // create injector unless explicitly specified
      this.injector = injector = injector || createInjector(options);

      // API

      /**
       * Resolves a diagram service
       *
       * @method Diagram#get
       *
       * @param {String} name the name of the diagram service to be retrieved
       * @param {Boolean} [strict=true] if false, resolve missing services to null
       */
      this.get = injector.get;

      /**
       * Executes a function into which diagram services are injected
       *
       * @method Diagram#invoke
       *
       * @param {Function|Object[]} fn the function to resolve
       * @param {Object} locals a number of locals to use to resolve certain dependencies
       */
      this.invoke = injector.invoke;

      // init

      // indicate via event

      /**
       * An event indicating that all plug-ins are loaded.
       *
       * Use this event to fire other events to interested plug-ins
       *
       * @memberOf Diagram
       *
       * @event diagram.init
       *
       * @example
       *
       * eventBus.on('diagram.init', function() {
       *   eventBus.fire('my-custom-event', { foo: 'BAR' });
       * });
       *
       * @type {Object}
       */
      this.get('eventBus').fire('diagram.init');
    }

    /**
     * Destroys the diagram
     *
     * @method  Diagram#destroy
     */
    Diagram.prototype.destroy = function() {
      this.get('eventBus').fire('diagram.destroy');
    };

    /**
     * Clear the diagram, removing all contents.
     */
    Diagram.prototype.clear = function() {
      this.get('eventBus').fire('diagram.clear');
    };