tooltip.js 18 KB
Newer Older
fat's avatar
fat committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(function (global, factory) {
  if (typeof define === 'function' && define.amd) {
    define(['exports', 'module', './util'], factory);
  } else if (typeof exports !== 'undefined' && typeof module !== 'undefined') {
    factory(exports, module, require('./util'));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, mod, global.Util);
    global.tooltip = mod.exports;
  }
})(this, function (exports, module, _util) {
  'use strict';

  var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

18
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
fat's avatar
fat committed
19
20
21

  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }

22
  var _Util = _interopRequireDefault(_util);
fat's avatar
fat committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

  /**
   * --------------------------------------------------------------------------
   * Bootstrap (v4.0.0): tooltip.js
   * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
   * --------------------------------------------------------------------------
   */

  var Tooltip = (function ($) {

    /**
     * ------------------------------------------------------------------------
     * Constants
     * ------------------------------------------------------------------------
     */

    var NAME = 'tooltip';
    var VERSION = '4.0.0';
    var DATA_KEY = 'bs.tooltip';
fat's avatar
fat committed
42
    var EVENT_KEY = '.' + DATA_KEY;
fat's avatar
fat committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    var JQUERY_NO_CONFLICT = $.fn[NAME];
    var TRANSITION_DURATION = 150;
    var CLASS_PREFIX = 'bs-tether';

    var Default = {
      animation: true,
      template: '<div class="tooltip" role="tooltip">' + '<div class="tooltip-arrow"></div>' + '<div class="tooltip-inner"></div></div>',
      trigger: 'hover focus',
      title: '',
      delay: 0,
      html: false,
      selector: false,
      placement: 'top',
      offset: '0 0',
57
58
59
60
61
62
63
64
65
66
67
68
69
70
      constraints: []
    };

    var DefaultType = {
      animation: 'boolean',
      template: 'string',
      title: '(string|function)',
      trigger: 'string',
      delay: '(number|object)',
      html: 'boolean',
      selector: '(string|boolean)',
      placement: '(string|function)',
      offset: 'string',
      constraints: 'array'
fat's avatar
fat committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    };

    var AttachmentMap = {
      TOP: 'bottom center',
      RIGHT: 'middle left',
      BOTTOM: 'top center',
      LEFT: 'middle right'
    };

    var HoverState = {
      IN: 'in',
      OUT: 'out'
    };

    var Event = {
fat's avatar
fat committed
86
87
88
89
90
91
92
93
94
95
      HIDE: 'hide' + EVENT_KEY,
      HIDDEN: 'hidden' + EVENT_KEY,
      SHOW: 'show' + EVENT_KEY,
      SHOWN: 'shown' + EVENT_KEY,
      INSERTED: 'inserted' + EVENT_KEY,
      CLICK: 'click' + EVENT_KEY,
      FOCUSIN: 'focusin' + EVENT_KEY,
      FOCUSOUT: 'focusout' + EVENT_KEY,
      MOUSEENTER: 'mouseenter' + EVENT_KEY,
      MOUSELEAVE: 'mouseleave' + EVENT_KEY
fat's avatar
fat committed
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
    };

    var ClassName = {
      FADE: 'fade',
      IN: 'in'
    };

    var Selector = {
      TOOLTIP: '.tooltip',
      TOOLTIP_INNER: '.tooltip-inner'
    };

    var TetherClass = {
      element: false,
      enabled: false
    };

    var Trigger = {
      HOVER: 'hover',
      FOCUS: 'focus',
      CLICK: 'click',
      MANUAL: 'manual'
    };

    /**
     * ------------------------------------------------------------------------
     * Class Definition
     * ------------------------------------------------------------------------
     */

    var Tooltip = (function () {
      function Tooltip(element, config) {
        _classCallCheck(this, Tooltip);

        // private
        this._isEnabled = true;
        this._timeout = 0;
        this._hoverState = '';
        this._activeTrigger = {};
        this._tether = null;

        // protected
        this.element = element;
        this.config = this._getConfig(config);
        this.tip = null;

        this._setListeners();
      }

Jacob Thornton's avatar
Jacob Thornton committed
145
146
147
148
149
150
151
152
      /**
       * ------------------------------------------------------------------------
       * jQuery
       * ------------------------------------------------------------------------
       */

      // getters

fat's avatar
fat committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
      _createClass(Tooltip, [{
        key: 'enable',

        // public

        value: function enable() {
          this._isEnabled = true;
        }
      }, {
        key: 'disable',
        value: function disable() {
          this._isEnabled = false;
        }
      }, {
        key: 'toggleEnabled',
        value: function toggleEnabled() {
          this._isEnabled = !this._isEnabled;
        }
      }, {
        key: 'toggle',
        value: function toggle(event) {
          if (event) {
Jacob Thornton's avatar
Jacob Thornton committed
175
176
            var dataKey = this.constructor.DATA_KEY;
            var context = $(event.currentTarget).data(dataKey);
fat's avatar
fat committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190

            if (!context) {
              context = new this.constructor(event.currentTarget, this._getDelegateConfig());
              $(event.currentTarget).data(dataKey, context);
            }

            context._activeTrigger.click = !context._activeTrigger.click;

            if (context._isWithActiveTrigger()) {
              context._enter(null, context);
            } else {
              context._leave(null, context);
            }
          } else {
Jacob Thornton's avatar
Jacob Thornton committed
191
192
193
194
195
196
197

            if ($(this.getTipElement()).hasClass(ClassName.IN)) {
              this._leave(null, this);
              return;
            }

            this._enter(null, this);
fat's avatar
fat committed
198
199
200
          }
        }
      }, {
fat's avatar
fat committed
201
202
        key: 'dispose',
        value: function dispose() {
fat's avatar
fat committed
203
204
          clearTimeout(this._timeout);

fat's avatar
fat committed
205
          this.cleanupTether();
fat's avatar
fat committed
206

fat's avatar
fat committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
          $.removeData(this.element, this.constructor.DATA_KEY);

          $(this.element).off(this.constructor.EVENT_KEY);

          if (this.tip) {
            $(this.tip).remove();
          }

          this._isEnabled = null;
          this._timeout = null;
          this._hoverState = null;
          this._activeTrigger = null;
          this._tether = null;

          this.element = null;
          this.config = null;
          this.tip = null;
fat's avatar
fat committed
224
225
226
227
        }
      }, {
        key: 'show',
        value: function show() {
fat's avatar
fat committed
228
          var _this = this;
fat's avatar
fat committed
229
230
231
232
233
234
235
236
237
238
239
240
241

          var showEvent = $.Event(this.constructor.Event.SHOW);

          if (this.isWithContent() && this._isEnabled) {
            $(this.element).trigger(showEvent);

            var isInTheDom = $.contains(this.element.ownerDocument.documentElement, this.element);

            if (showEvent.isDefaultPrevented() || !isInTheDom) {
              return;
            }

            var tip = this.getTipElement();
242
            var tipId = _Util['default'].getUID(this.constructor.NAME);
fat's avatar
fat committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261

            tip.setAttribute('id', tipId);
            this.element.setAttribute('aria-describedby', tipId);

            this.setContent();

            if (this.config.animation) {
              $(tip).addClass(ClassName.FADE);
            }

            var placement = typeof this.config.placement === 'function' ? this.config.placement.call(this, tip, this.element) : this.config.placement;

            var attachment = this._getAttachment(placement);

            $(tip).data(this.constructor.DATA_KEY, this).appendTo(document.body);

            $(this.element).trigger(this.constructor.Event.INSERTED);

            this._tether = new Tether({
Jacob Thornton's avatar
Jacob Thornton committed
262
              attachment: attachment,
fat's avatar
fat committed
263
264
265
266
267
268
269
270
              element: tip,
              target: this.element,
              classes: TetherClass,
              classPrefix: CLASS_PREFIX,
              offset: this.config.offset,
              constraints: this.config.constraints
            });

271
            _Util['default'].reflow(tip);
fat's avatar
fat committed
272
273
274
275
276
            this._tether.position();

            $(tip).addClass(ClassName.IN);

            var complete = function complete() {
fat's avatar
fat committed
277
278
              var prevHoverState = _this._hoverState;
              _this._hoverState = null;
fat's avatar
fat committed
279

fat's avatar
fat committed
280
              $(_this.element).trigger(_this.constructor.Event.SHOWN);
fat's avatar
fat committed
281
282

              if (prevHoverState === HoverState.OUT) {
fat's avatar
fat committed
283
                _this._leave(null, _this);
fat's avatar
fat committed
284
285
286
              }
            };

Jacob Thornton's avatar
Jacob Thornton committed
287
288
289
290
291
292
            if (_Util['default'].supportsTransitionEnd() && $(this.tip).hasClass(ClassName.FADE)) {
              $(this.tip).one(_Util['default'].TRANSITION_END, complete).emulateTransitionEnd(Tooltip._TRANSITION_DURATION);
              return;
            }

            complete();
fat's avatar
fat committed
293
294
295
296
297
          }
        }
      }, {
        key: 'hide',
        value: function hide(callback) {
fat's avatar
fat committed
298
          var _this2 = this;
fat's avatar
fat committed
299
300
301
302

          var tip = this.getTipElement();
          var hideEvent = $.Event(this.constructor.Event.HIDE);
          var complete = function complete() {
fat's avatar
fat committed
303
            if (_this2._hoverState !== HoverState.IN && tip.parentNode) {
fat's avatar
fat committed
304
305
306
              tip.parentNode.removeChild(tip);
            }

fat's avatar
fat committed
307
308
309
            _this2.element.removeAttribute('aria-describedby');
            $(_this2.element).trigger(_this2.constructor.Event.HIDDEN);
            _this2.cleanupTether();
fat's avatar
fat committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323

            if (callback) {
              callback();
            }
          };

          $(this.element).trigger(hideEvent);

          if (hideEvent.isDefaultPrevented()) {
            return;
          }

          $(tip).removeClass(ClassName.IN);

324
          if (_Util['default'].supportsTransitionEnd() && $(this.tip).hasClass(ClassName.FADE)) {
fat's avatar
fat committed
325

326
            $(tip).one(_Util['default'].TRANSITION_END, complete).emulateTransitionEnd(TRANSITION_DURATION);
fat's avatar
fat committed
327
328
329
330
331
332
333
334
335
          } else {
            complete();
          }

          this._hoverState = '';
        }

        // protected

Jacob Thornton's avatar
Jacob Thornton committed
336
337
      }, {
        key: 'isWithContent',
fat's avatar
fat committed
338
        value: function isWithContent() {
Jacob Thornton's avatar
Jacob Thornton committed
339
          return Boolean(this.getTitle());
fat's avatar
fat committed
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
        }
      }, {
        key: 'getTipElement',
        value: function getTipElement() {
          return this.tip = this.tip || $(this.config.template)[0];
        }
      }, {
        key: 'setContent',
        value: function setContent() {
          var tip = this.getTipElement();
          var title = this.getTitle();
          var method = this.config.html ? 'innerHTML' : 'innerText';

          $(tip).find(Selector.TOOLTIP_INNER)[0][method] = title;

          $(tip).removeClass(ClassName.FADE).removeClass(ClassName.IN);

          this.cleanupTether();
        }
      }, {
        key: 'getTitle',
        value: function getTitle() {
          var title = this.element.getAttribute('data-original-title');

          if (!title) {
            title = typeof this.config.title === 'function' ? this.config.title.call(this.element) : this.config.title;
          }

          return title;
        }
      }, {
        key: 'cleanupTether',
        value: function cleanupTether() {
          if (this._tether) {
            this._tether.destroy();

            // clean up after tether's junk classes
            // remove after they fix issue
            // (https://github.com/HubSpot/tether/issues/36)
            $(this.element).removeClass(this._removeTetherClasses);
            $(this.tip).removeClass(this._removeTetherClasses);
          }
        }

        // private

Jacob Thornton's avatar
Jacob Thornton committed
386
387
      }, {
        key: '_getAttachment',
fat's avatar
fat committed
388
389
390
391
392
393
        value: function _getAttachment(placement) {
          return AttachmentMap[placement.toUpperCase()];
        }
      }, {
        key: '_setListeners',
        value: function _setListeners() {
fat's avatar
fat committed
394
          var _this3 = this;
fat's avatar
fat committed
395
396
397
398
399

          var triggers = this.config.trigger.split(' ');

          triggers.forEach(function (trigger) {
            if (trigger === 'click') {
fat's avatar
fat committed
400
              $(_this3.element).on(_this3.constructor.Event.CLICK, _this3.config.selector, $.proxy(_this3.toggle, _this3));
fat's avatar
fat committed
401
            } else if (trigger !== Trigger.MANUAL) {
Jacob Thornton's avatar
Jacob Thornton committed
402
403
              var eventIn = trigger === Trigger.HOVER ? _this3.constructor.Event.MOUSEENTER : _this3.constructor.Event.FOCUSIN;
              var eventOut = trigger === Trigger.HOVER ? _this3.constructor.Event.MOUSELEAVE : _this3.constructor.Event.FOCUSOUT;
fat's avatar
fat committed
404

fat's avatar
fat committed
405
              $(_this3.element).on(eventIn, _this3.config.selector, $.proxy(_this3._enter, _this3)).on(eventOut, _this3.config.selector, $.proxy(_this3._leave, _this3));
fat's avatar
fat committed
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
            }
          });

          if (this.config.selector) {
            this.config = $.extend({}, this.config, {
              trigger: 'manual',
              selector: ''
            });
          } else {
            this._fixTitle();
          }
        }
      }, {
        key: '_removeTetherClasses',
        value: function _removeTetherClasses(i, css) {
          return ((css.baseVal || css).match(new RegExp('(^|\\s)' + CLASS_PREFIX + '-\\S+', 'g')) || []).join(' ');
        }
      }, {
        key: '_fixTitle',
        value: function _fixTitle() {
          var titleType = typeof this.element.getAttribute('data-original-title');
          if (this.element.getAttribute('title') || titleType !== 'string') {
            this.element.setAttribute('data-original-title', this.element.getAttribute('title') || '');
            this.element.setAttribute('title', '');
          }
        }
      }, {
        key: '_enter',
        value: function _enter(event, context) {
          var dataKey = this.constructor.DATA_KEY;

          context = context || $(event.currentTarget).data(dataKey);

          if (!context) {
            context = new this.constructor(event.currentTarget, this._getDelegateConfig());
            $(event.currentTarget).data(dataKey, context);
          }

          if (event) {
Jacob Thornton's avatar
Jacob Thornton committed
445
            context._activeTrigger[event.type === 'focusin' ? Trigger.FOCUS : Trigger.HOVER] = true;
fat's avatar
fat committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
          }

          if ($(context.getTipElement()).hasClass(ClassName.IN) || context._hoverState === HoverState.IN) {
            context._hoverState = HoverState.IN;
            return;
          }

          clearTimeout(context._timeout);

          context._hoverState = HoverState.IN;

          if (!context.config.delay || !context.config.delay.show) {
            context.show();
            return;
          }

          context._timeout = setTimeout(function () {
            if (context._hoverState === HoverState.IN) {
              context.show();
            }
          }, context.config.delay.show);
        }
      }, {
        key: '_leave',
        value: function _leave(event, context) {
          var dataKey = this.constructor.DATA_KEY;

          context = context || $(event.currentTarget).data(dataKey);

          if (!context) {
            context = new this.constructor(event.currentTarget, this._getDelegateConfig());
            $(event.currentTarget).data(dataKey, context);
          }

          if (event) {
Jacob Thornton's avatar
Jacob Thornton committed
481
            context._activeTrigger[event.type === 'focusout' ? Trigger.FOCUS : Trigger.HOVER] = false;
fat's avatar
fat committed
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
          }

          if (context._isWithActiveTrigger()) {
            return;
          }

          clearTimeout(context._timeout);

          context._hoverState = HoverState.OUT;

          if (!context.config.delay || !context.config.delay.hide) {
            context.hide();
            return;
          }

          context._timeout = setTimeout(function () {
            if (context._hoverState === HoverState.OUT) {
              context.hide();
            }
          }, context.config.delay.hide);
        }
      }, {
        key: '_isWithActiveTrigger',
        value: function _isWithActiveTrigger() {
          for (var trigger in this._activeTrigger) {
            if (this._activeTrigger[trigger]) {
              return true;
            }
          }

          return false;
        }
      }, {
        key: '_getConfig',
        value: function _getConfig(config) {
          config = $.extend({}, this.constructor.Default, $(this.element).data(), config);

          if (config.delay && typeof config.delay === 'number') {
            config.delay = {
              show: config.delay,
              hide: config.delay
            };
          }

526
          _Util['default'].typeCheckConfig(NAME, config, this.constructor.DefaultType);
527

fat's avatar
fat committed
528
529
530
531
532
533
534
535
536
          return config;
        }
      }, {
        key: '_getDelegateConfig',
        value: function _getDelegateConfig() {
          var config = {};

          if (this.config) {
            for (var key in this.config) {
Jacob Thornton's avatar
Jacob Thornton committed
537
538
              if (this.constructor.Default[key] !== this.config[key]) {
                config[key] = this.config[key];
fat's avatar
fat committed
539
540
541
542
543
544
              }
            }
          }

          return config;
        }
Mark Otto's avatar
grunt    
Mark Otto committed
545
546
547

        // static

Jacob Thornton's avatar
Jacob Thornton committed
548
549
      }], [{
        key: '_jQueryInterface',
Mark Otto's avatar
grunt    
Mark Otto committed
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
        value: function _jQueryInterface(config) {
          return this.each(function () {
            var data = $(this).data(DATA_KEY);
            var _config = typeof config === 'object' ? config : null;

            if (!data && /destroy|hide/.test(config)) {
              return;
            }

            if (!data) {
              data = new Tooltip(this, _config);
              $(this).data(DATA_KEY, data);
            }

            if (typeof config === 'string') {
              data[config]();
            }
          });
        }
      }, {
fat's avatar
fat committed
570
        key: 'VERSION',
Jacob Thornton's avatar
Jacob Thornton committed
571
        get: function get() {
fat's avatar
fat committed
572
573
574
575
          return VERSION;
        }
      }, {
        key: 'Default',
Jacob Thornton's avatar
Jacob Thornton committed
576
        get: function get() {
fat's avatar
fat committed
577
578
579
580
          return Default;
        }
      }, {
        key: 'NAME',
Jacob Thornton's avatar
Jacob Thornton committed
581
        get: function get() {
fat's avatar
fat committed
582
583
584
585
          return NAME;
        }
      }, {
        key: 'DATA_KEY',
Jacob Thornton's avatar
Jacob Thornton committed
586
        get: function get() {
fat's avatar
fat committed
587
588
589
590
          return DATA_KEY;
        }
      }, {
        key: 'Event',
Jacob Thornton's avatar
Jacob Thornton committed
591
        get: function get() {
fat's avatar
fat committed
592
593
          return Event;
        }
fat's avatar
fat committed
594
595
      }, {
        key: 'EVENT_KEY',
Jacob Thornton's avatar
Jacob Thornton committed
596
        get: function get() {
fat's avatar
fat committed
597
598
          return EVENT_KEY;
        }
599
600
      }, {
        key: 'DefaultType',
Jacob Thornton's avatar
Jacob Thornton committed
601
        get: function get() {
602
603
          return DefaultType;
        }
fat's avatar
fat committed
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
      }]);

      return Tooltip;
    })();

    $.fn[NAME] = Tooltip._jQueryInterface;
    $.fn[NAME].Constructor = Tooltip;
    $.fn[NAME].noConflict = function () {
      $.fn[NAME] = JQUERY_NO_CONFLICT;
      return Tooltip._jQueryInterface;
    };

    return Tooltip;
  })(jQuery);

  module.exports = Tooltip;
Mark Otto's avatar
grunt    
Mark Otto committed
620
});