# all files / Github/diagram-js/lib/features/tooltips/ Tooltips.js

77.69% Statements 94/121
34% Branches 17/50
80% Functions 20/25
77.69% Lines 94/121

    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
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368

    1×

    1128×

    1128×

    1128×

    2×

    24×

    1×
    1×

    1128×
    1128×

    1128×

    1128×

    1128×

    1128×

    1128×

    1128×

    1128×

    1128×

    1128×

    1×

    1×

    2×

    2×

    2×

    2×

    2×

    2×
    2×

    2×

    1×

    1×

    6×
    6×

    6×

    1×

    2×

    2×

    2×

    2×

    1×

    2×

    2×

    2×

    2×

    2×
    2×

    1×

    2×

    2×
    2×
    2×

    2×

    2×

    1×
    12×

    1×
    12×

    1×
    12×
    12×

    12×

    12×
    12×

    1×

    2×
    2×

    2×

    2×

    2×
    2×

    2×

    2×

    2×
    2×

    2×

    2×

    2×

    2×

    2×

    1×

    2×
    2×

    2×

    1×

    12×

    1×

    1128×

    12×
    12×

    12×

    1128×
    12×

    1128×
    12×

     import {
      isString,
      assign,
      forEach
    } from 'min-dash';

    import {
      domify,
      attr as domAttr,
      classes as domClasses,
      remove as domRemove,
      delegate as domDelegate
    } from 'min-dom';

    import Ids from '../../util/IdGenerator';

    // document wide unique tooltip ids
    var ids = new Ids('tt');

    function createRoot(parentNode) {
      var root = domify(
        '<div class="djs-tooltip-container" style="position: absolute; width: 0; height: 0;" />'
      );

      parentNode.insertBefore(root, parentNode.firstChild);

      return root;
    }

    function setPosition(el, x, y) {
      assign(el.style, { left: x + 'px', top: y + 'px' });
    }

    function setVisible(el, visible) {
      el.style.display = visible === false ? 'none' : '';
    }

    var tooltipClass = 'djs-tooltip',
        tooltipSelector = '.' + tooltipClass;

    /**
     * A service that allows users to render tool tips on the diagram.
     *
     * The tooltip service will take care of updating the tooltip positioning
     * during navigation + zooming.
     *
     * @example
     *
     * ```javascript
     *
     * // add a pink badge on the top left of the shape
     * tooltips.add({
     *   position: {
     *     x: 50,
     *     y: 100
     *   },
     *   html: '<div style="width: 10px; background: fuchsia; color: white;">0</div>'
     * });
     *
     * // or with optional life span
     * tooltips.add({
     *   position: {
     *     top: -5,
     *     left: -5
     *   },
     *   html: '<div style="width: 10px; background: fuchsia; color: white;">0</div>',
     *   ttl: 2000
     * });
     *
     * // remove a tool tip
     * var id = tooltips.add(...);
     * tooltips.remove(id);
     * ```
     *
     * @param {EventBus} eventBus
     * @param {Canvas} canvas
     */
    export default function Tooltips(eventBus, canvas) {

      this._eventBus = eventBus;
      this._canvas = canvas;

      this._ids = ids;

      this._tooltipDefaults = {
        show: {
          minZoom: 0.7,
          maxZoom: 5.0
        }
      };

      /**
       * Mapping tooltipId -> tooltip
       */
      this._tooltips = {};

      // root html element for all tooltips
      this._tooltipRoot = createRoot(canvas.getContainer());

      var self = this;

      domDelegate.bind(this._tooltipRoot, tooltipSelector, 'mousedown', function(event) {
        event.stopPropagation();
      });

      domDelegate.bind(this._tooltipRoot, tooltipSelector, 'mouseover', function(event) {
        self.trigger('mouseover', event);
      });

      domDelegate.bind(this._tooltipRoot, tooltipSelector, 'mouseout', function(event) {
        self.trigger('mouseout', event);
      });

      this._init();
    }

    Tooltips.$inject = [ 'eventBus', 'canvas' ];

    /**
     * Adds a HTML tooltip to the diagram
     *
     * @param {Object}               tooltip   the tooltip configuration
     *
     * @param {String|DOMElement}    tooltip.html                 html element to use as an tooltip
     * @param {Object}               [tooltip.show]               show configuration
     * @param {Number}               [tooltip.show.minZoom]       minimal zoom level to show the tooltip
     * @param {Number}               [tooltip.show.maxZoom]       maximum zoom level to show the tooltip
     * @param {Object}               tooltip.position             where to attach the tooltip
     * @param {Number}               [tooltip.position.left]      relative to element bbox left attachment
     * @param {Number}               [tooltip.position.top]       relative to element bbox top attachment
     * @param {Number}               [tooltip.position.bottom]    relative to element bbox bottom attachment
     * @param {Number}               [tooltip.position.right]     relative to element bbox right attachment
     * @param {Number}               [tooltip.timeout=-1]
     *
     * @return {String}              id that may be used to reference the tooltip for update or removal
     */
    Tooltips.prototype.add = function(tooltip) {

      Iif (!tooltip.position) {
        throw new Error('must specifiy tooltip position');
      }

      Iif (!tooltip.html) {
        throw new Error('must specifiy tooltip html');
      }

      var id = this._ids.next();

      tooltip = assign({}, this._tooltipDefaults, tooltip, {
        id: id
      });

      this._addTooltip(tooltip);

      Eif (tooltip.timeout) {
        this.setTimeout(tooltip);
      }

      return id;
    };

    Tooltips.prototype.trigger = function(action, event) {

      var node = event.delegateTarget || event.target;

      var tooltip = this.get(domAttr(node, 'data-tooltip-id'));

      if (!tooltip) {
        return;
      }

      if (action === 'mouseover' && tooltip.timeout) {
        this.clearTimeout(tooltip);
      }

      if (action === 'mouseout' && tooltip.timeout) {
        // cut timeout after mouse out
        tooltip.timeout = 1000;

        this.setTimeout(tooltip);
      }
    };

    /**
     * Get a tooltip with the given id
     *
     * @param {String} id
     */
    Tooltips.prototype.get = function(id) {

      Eif (typeof id !== 'string') {
        id = id.id;
      }

      return this._tooltips[id];
    };

    Tooltips.prototype.clearTimeout = function(tooltip) {

      tooltip = this.get(tooltip);

      Iif (!tooltip) {
        return;
      }

      var removeTimer = tooltip.removeTimer;

      Iif (removeTimer) {
        clearTimeout(removeTimer);
        tooltip.removeTimer = null;
      }
    };

    Tooltips.prototype.setTimeout = function(tooltip) {

      tooltip = this.get(tooltip);

      Iif (!tooltip) {
        return;
      }

      this.clearTimeout(tooltip);

      var self = this;

      tooltip.removeTimer = setTimeout(function() {
        self.remove(tooltip);
      }, tooltip.timeout);
    };

    /**
     * Remove an tooltip with the given id
     *
     * @param {String} id
     */
    Tooltips.prototype.remove = function(id) {

      var tooltip = this.get(id);

      Eif (tooltip) {
        domRemove(tooltip.html);
        domRemove(tooltip.htmlContainer);

        delete tooltip.htmlContainer;

        delete this._tooltips[tooltip.id];
      }
    };

    Tooltips.prototype.show = function() {
      setVisible(this._tooltipRoot);
    };

    Tooltips.prototype.hide = function() {
      setVisible(this._tooltipRoot, false);
    };

    Tooltips.prototype._updateRoot = function(viewbox) {
      var a = viewbox.scale || 1;
      var d = viewbox.scale || 1;

      var matrix = 'matrix(' + a + ',0,0,' + d + ',' + (-1 * viewbox.x * a) + ',' + (-1 * viewbox.y * d) + ')';

      this._tooltipRoot.style.transform = matrix;
      this._tooltipRoot.style['-ms-transform'] = matrix;
    };

    Tooltips.prototype._addTooltip = function(tooltip) {

      var id = tooltip.id,
          html = tooltip.html,
          htmlContainer,
          tooltipRoot = this._tooltipRoot;

      // unwrap jquery (for those who need it)
      Iif (html.get && html.constructor.prototype.jquery) {
        html = html.get(0);
      }

      // create proper html elements from
      // tooltip HTML strings
      Eif (isString(html)) {
        html = domify(html);
      }

      htmlContainer = domify('<div data-tooltip-id="' + id + '" class="' + tooltipClass + '" style="position: absolute">');

      htmlContainer.appendChild(html);

      Eif (tooltip.type) {
        domClasses(htmlContainer).add('djs-tooltip-' + tooltip.type);
      }

      Iif (tooltip.className) {
        domClasses(htmlContainer).add(tooltip.className);
      }

      tooltip.htmlContainer = htmlContainer;

      tooltipRoot.appendChild(htmlContainer);

      this._tooltips[id] = tooltip;

      this._updateTooltip(tooltip);
    };

    Tooltips.prototype._updateTooltip = function(tooltip) {

      var position = tooltip.position,
          htmlContainer = tooltip.htmlContainer;

      // update overlay html based on tooltip x, y

      setPosition(htmlContainer, position.x, position.y);
    };

    Tooltips.prototype._updateTooltipVisibilty = function(viewbox) {

      forEach(this._tooltips, function(tooltip) {
        var show = tooltip.show,
            htmlContainer = tooltip.htmlContainer,
            visible = true;

        if (show) {
          if (show.minZoom > viewbox.scale ||
              show.maxZoom < viewbox.scale) {
            visible = false;
          }

          setVisible(htmlContainer, visible);
        }
      });
    };

    Tooltips.prototype._init = function() {

      var self = this;

      // scroll/zoom integration

      function updateViewbox(viewbox) {
        self._updateRoot(viewbox);
        self._updateTooltipVisibilty(viewbox);

        self.show();
      }

      this._eventBus.on('canvas.viewbox.changing', function(event) {
        self.hide();
      });

      this._eventBus.on('canvas.viewbox.changed', function(event) {
        updateViewbox(event.viewbox);
      });
    };