# all files / diagram-js/lib/features/context-pad/ ContextPad.js

87.04% Statements 94/108
70% Branches 35/50
77.27% Functions 17/22
87.04% Lines 94/108

    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
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322

    1×

    79×

    79×
    79×

    79×

    79×

    79×

    79×

    1×

    1×

    79×

    79×

    79×

    100×

    100×
    8×

    92×

    79×

    79×
    55×
    55×

    55×

    1×
    79×

    1×
    32×

    32×
    32×

    32×
    195×

    32×

    1×

    6×
    6×

    6×

    6×

    6×
    6×

    6×

    6×

    6×
    6×

    1×
    29×

    29×
    29×

    1×

    29×
    29×
    29×

    29×
    176×
    176×

    176×

    176×
    176×
    82×
    82×

    176×

    176×
    176×

    176×
    176×

    176×

    29×

    29×

    29×

    1×
    40×
    11×

    29×

    29×

    29×

    29×

    29×

    29×

    29×

    29×

    29×

    29×

    29×

    1×
    121×
    117×

    4×

    4×

    4×

    4×

    1×
    183×

    176×

    176×
    176×
    176×

     import {
      assign,
      isFunction,
      isArray,
      forEach,
      isDefined
    } from 'min-dash';

    import {
      delegate as domDelegate,
      event as domEvent,
      attr as domAttr,
      query as domQuery,
      classes as domClasses,
      domify as domify
    } from 'min-dom';

    var entrySelector = '.entry';

    /**
     * A context pad that displays element specific, contextual actions next
     * to a diagram element.
     *
     * @param {Object} config
     * @param {Boolean|Object} [config.scale={ min: 1.0, max: 1.5 }]
     * @param {Number} [config.scale.min]
     * @param {Number} [config.scale.max]
     * @param {EventBus} eventBus
     * @param {Overlays} overlays
     */
    export default function ContextPad(config, eventBus, overlays) {

      this._providers = [];

      this._eventBus = eventBus;
      this._overlays = overlays;

      var scale = isDefined(config && config.scale) ? config.scale : {
        min: 1,
        max: 1.5
      };

      this._overlaysConfig = {
        position: {
          right: -9,
          top: -6
        },
        scale: scale
      };

      this._current = null;

      this._init();
    }

    ContextPad.$inject = [
      'config.contextPad',
      'eventBus',
      'overlays'
    ];

    /**
     * Registers events needed for interaction with other components
     */
    ContextPad.prototype._init = function() {

      var eventBus = this._eventBus;

      var self = this;

      eventBus.on('selection.changed', function(e) {

        var selection = e.newSelection;

        if (selection.length === 1) {
          self.open(selection[0]);
        } else {
          self.close();
        }
      });

      eventBus.on('elements.delete', function(event) {
        var elements = event.elements;

        forEach(elements, function(e) {
          if (self.isOpen(e)) {
            self.close();
          }
        });
      });

      eventBus.on('element.changed', function(event) {
        var element = event.element,
            current = self._current;

        // force reopen if element for which we are currently opened changed
        Iif (current && current.element === element) {
          self.open(element, true);
        }
      });
    };

    /**
     * Register a provider with the context pad
     *
     * @param  {ContextPadProvider} provider
     */
    ContextPad.prototype.registerProvider = function(provider) {
      this._providers.push(provider);
    };

    /**
     * Returns the context pad entries for a given element
     *
     * @param {djs.element.Base} element
     *
     * @return {Array<ContextPadEntryDescriptor>} list of entries
     */
    ContextPad.prototype.getEntries = function(element) {
      var entries = {};

      // loop through all providers and their entries.
      // group entries by id so that overriding an entry is possible
      forEach(this._providers, function(provider) {
        var e = provider.getContextPadEntries(element);

        forEach(e, function(entry, id) {
          entries[id] = entry;
        });
      });

      return entries;
    };

    /**
     * Trigger an action available on the opened context pad
     *
     * @param  {String} action
     * @param  {Event} event
     * @param  {Boolean} [autoActivate=false]
     */
    ContextPad.prototype.trigger = function(action, event, autoActivate) {

      var element = this._current.element,
          entries = this._current.entries,
          entry,
          handler,
          originalEvent,
          button = event.delegateTarget || event.target;

      Iif (!button) {
        return event.preventDefault();
      }

      entry = entries[domAttr(button, 'data-action')];
      handler = entry.action;

      originalEvent = event.originalEvent || event;

      // simple action (via callback function)
      Iif (isFunction(handler)) {
        if (action === 'click') {
          return handler(originalEvent, element, autoActivate);
        }
      } else {
        Eif (handler[action]) {
          return handler[action](originalEvent, element, autoActivate);
        }
      }

      // silence other actions
      event.preventDefault();
    };

    /**
     * Open the context pad for the given element
     *
     * @param {djs.model.Base} element
     * @param {Boolean} force if true, force reopening the context pad
     */
    ContextPad.prototype.open = function(element, force) {
      Iif (!force && this.isOpen(element)) {
        return;
      }

      this.close();
      this._updateAndOpen(element);
    };

    ContextPad.prototype._updateAndOpen = function(element) {

      var entries = this.getEntries(element),
          pad = this.getPad(element),
          html = pad.html;

      forEach(entries, function(entry, id) {
        var grouping = entry.group || 'default',
            control = domify(entry.html || '<div class="entry" draggable="true"></div>'),
            container;

        domAttr(control, 'data-action', id);

        container = domQuery('[data-group=' + grouping + ']', html);
        if (!container) {
          container = domify('<div class="group" data-group="' + grouping + '"></div>');
          html.appendChild(container);
        }

        container.appendChild(control);

        Eif (entry.className) {
          addClasses(control, entry.className);
        }

        Eif (entry.title) {
          domAttr(control, 'title', entry.title);
        }

        Iif (entry.imageUrl) {
          control.appendChild(domify('<img src="' + entry.imageUrl + '">'));
        }
      });

      domClasses(html).add('open');

      this._current = {
        element: element,
        pad: pad,
        entries: entries
      };

      this._eventBus.fire('contextPad.open', { current: this._current });
    };

    ContextPad.prototype.getPad = function(element) {
      if (this.isOpen()) {
        return this._current.pad;
      }

      var self = this;

      var overlays = this._overlays;

      var html = domify('<div class="djs-context-pad"></div>');

      var overlaysConfig = assign({
        html: html
      }, this._overlaysConfig);

      domDelegate.bind(html, entrySelector, 'click', function(event) {
        self.trigger('click', event);
      });

      domDelegate.bind(html, entrySelector, 'dragstart', function(event) {
        self.trigger('dragstart', event);
      });

      // stop propagation of mouse events
      domEvent.bind(html, 'mousedown', function(event) {
        event.stopPropagation();
      });

      this._overlayId = overlays.add(element, 'context-pad', overlaysConfig);

      var pad = overlays.get(this._overlayId);

      this._eventBus.fire('contextPad.create', { element: element, pad: pad });

      return pad;
    };

    /**
     * Close the context pad
     */
    ContextPad.prototype.close = function() {
      if (!this.isOpen()) {
        return;
      }

      this._overlays.remove(this._overlayId);

      this._overlayId = null;

      this._eventBus.fire('contextPad.close', { current: this._current });

      this._current = null;
    };

    /**
     * Check if pad is open. If element is given, will check
     * if pad is opened with given element.
     *
     * @param {Element} element
     * @return {Boolean}
     */
    ContextPad.prototype.isOpen = function(element) {
      return !!this._current && (!element ? true : this._current.element === element);
    };

    // helpers //////////////////////

    function addClasses(element, classNames) {

      var classes = domClasses(element);

      var actualClassNames = isArray(classNames) ? classNames : classNames.split(/\s+/g);
      actualClassNames.forEach(function(cls) {
        classes.add(cls);
      });
    }