tooltip.js 20 KB
Newer Older
1
2
/**
 * --------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
3
 * Bootstrap (v4.3.1): tooltip.js
4
5
6
7
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

8
9
10
11
12
13
14
15
16
17
18
19
import {
  jQuery as $,
  TRANSITION_END,
  emulateTransitionEnd,
  findShadowRoot,
  getTransitionDurationFromElement,
  getUID,
  isElement,
  makeArray,
  noop,
  typeCheckConfig
} from './util/index'
20
21
22
import {
  DefaultWhitelist,
  sanitizeHtml
23
} from './util/sanitizer'
24
25
import Data from './dom/data'
import EventHandler from './dom/eventHandler'
26
import Manipulator from './dom/manipulator'
27
import Popper from 'popper.js'
28
import SelectorEngine from './dom/selectorEngine'
29

Johann-S's avatar
Johann-S committed
30
31
32
33
34
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */
35

36
const NAME                  = 'tooltip'
XhmikosR's avatar
XhmikosR committed
37
const VERSION               = '4.3.1'
38
39
40
41
42
const DATA_KEY              = 'bs.tooltip'
const EVENT_KEY             = `.${DATA_KEY}`
const CLASS_PREFIX          = 'bs-tooltip'
const BSCLS_PREFIX_REGEX    = new RegExp(`(^|\\s)${CLASS_PREFIX}\\S+`, 'g')
const DISALLOWED_ATTRIBUTES = ['sanitize', 'whiteList', 'sanitizeFn']
Johann-S's avatar
Johann-S committed
43

44

Johann-S's avatar
Johann-S committed
45
const DefaultType = {
46
47
48
49
50
51
52
53
  animation         : 'boolean',
  template          : 'string',
  title             : '(string|element|function)',
  trigger           : 'string',
  delay             : '(number|object)',
  html              : 'boolean',
  selector          : '(string|boolean)',
  placement         : '(string|function)',
54
  offset            : '(number|string|function)',
55
56
  container         : '(string|element|boolean)',
  fallbackPlacement : '(string|array)',
57
58
59
60
  boundary          : '(string|element)',
  sanitize          : 'boolean',
  sanitizeFn        : '(null|function)',
  whiteList         : 'object'
Johann-S's avatar
Johann-S committed
61
62
63
64
65
66
67
68
69
70
71
}

const AttachmentMap = {
  AUTO   : 'auto',
  TOP    : 'top',
  RIGHT  : 'right',
  BOTTOM : 'bottom',
  LEFT   : 'left'
}

const Default = {
72
73
  animation         : true,
  template          : '<div class="tooltip" role="tooltip">' +
74
                    '<div class="tooltip-arrow"></div>' +
75
76
77
78
79
80
81
82
83
84
                    '<div class="tooltip-inner"></div></div>',
  trigger           : 'hover focus',
  title             : '',
  delay             : 0,
  html              : false,
  selector          : false,
  placement         : 'top',
  offset            : 0,
  container         : false,
  fallbackPlacement : 'flip',
85
86
87
88
  boundary          : 'scrollParent',
  sanitize          : true,
  sanitizeFn        : null,
  whiteList         : DefaultWhitelist
Johann-S's avatar
Johann-S committed
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
}

const HoverState = {
  SHOW : 'show',
  OUT  : 'out'
}

const Event = {
  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}`
}

const ClassName = {
  FADE : 'fade',
  SHOW : 'show'
}

const Selector = {
  TOOLTIP       : '.tooltip',
  TOOLTIP_INNER : '.tooltip-inner',
117
  TOOLTIP_ARROW : '.tooltip-arrow'
Johann-S's avatar
Johann-S committed
118
119
120
121
122
123
124
125
}

const Trigger = {
  HOVER  : 'hover',
  FOCUS  : 'focus',
  CLICK  : 'click',
  MANUAL : 'manual'
}
126
127


Johann-S's avatar
Johann-S committed
128
129
130
131
132
/**
 * ------------------------------------------------------------------------
 * Class Definition
 * ------------------------------------------------------------------------
 */
133

Johann-S's avatar
Johann-S committed
134
135
136
137
138
139
140
class Tooltip {
  constructor(element, config) {
    /**
     * Check for Popper dependency
     * Popper - https://popper.js.org
     */
    if (typeof Popper === 'undefined') {
141
      throw new TypeError('Bootstrap\'s tooltips require Popper.js (https://popper.js.org)')
142
143
    }

Johann-S's avatar
Johann-S committed
144
145
146
147
148
149
    // private
    this._isEnabled     = true
    this._timeout       = 0
    this._hoverState    = ''
    this._activeTrigger = {}
    this._popper        = null
150

Johann-S's avatar
Johann-S committed
151
152
153
154
    // Protected
    this.element = element
    this.config  = this._getConfig(config)
    this.tip     = null
fat's avatar
fat committed
155

Johann-S's avatar
Johann-S committed
156
    this._setListeners()
157
    Data.setData(element, this.constructor.DATA_KEY, this)
Johann-S's avatar
Johann-S committed
158
  }
fat's avatar
fat committed
159

Johann-S's avatar
Johann-S committed
160
  // Getters
fat's avatar
fat committed
161

Johann-S's avatar
Johann-S committed
162
163
164
  static get VERSION() {
    return VERSION
  }
fat's avatar
fat committed
165

Johann-S's avatar
Johann-S committed
166
167
168
  static get Default() {
    return Default
  }
fat's avatar
fat committed
169

Johann-S's avatar
Johann-S committed
170
171
172
  static get NAME() {
    return NAME
  }
173

Johann-S's avatar
Johann-S committed
174
175
176
  static get DATA_KEY() {
    return DATA_KEY
  }
177

Johann-S's avatar
Johann-S committed
178
179
180
  static get Event() {
    return Event
  }
181

Johann-S's avatar
Johann-S committed
182
183
184
  static get EVENT_KEY() {
    return EVENT_KEY
  }
185

Johann-S's avatar
Johann-S committed
186
187
188
  static get DefaultType() {
    return DefaultType
  }
189

Johann-S's avatar
Johann-S committed
190
  // Public
191

Johann-S's avatar
Johann-S committed
192
193
194
  enable() {
    this._isEnabled = true
  }
195

Johann-S's avatar
Johann-S committed
196
197
198
  disable() {
    this._isEnabled = false
  }
199

Johann-S's avatar
Johann-S committed
200
201
202
  toggleEnabled() {
    this._isEnabled = !this._isEnabled
  }
Jacob Thornton's avatar
Jacob Thornton committed
203

Johann-S's avatar
Johann-S committed
204
205
206
  toggle(event) {
    if (!this._isEnabled) {
      return
207
208
    }

Johann-S's avatar
Johann-S committed
209
210
    if (event) {
      const dataKey = this.constructor.DATA_KEY
211
      let context = Data.getData(event.delegateTarget, dataKey)
fat's avatar
fat committed
212

Johann-S's avatar
Johann-S committed
213
214
      if (!context) {
        context = new this.constructor(
215
          event.delegateTarget,
Johann-S's avatar
Johann-S committed
216
217
          this._getDelegateConfig()
        )
218
        Data.setData(event.delegateTarget, dataKey, context)
Johann-S's avatar
Johann-S committed
219
      }
fat's avatar
fat committed
220

Johann-S's avatar
Johann-S committed
221
      context._activeTrigger.click = !context._activeTrigger.click
fat's avatar
fat committed
222

Johann-S's avatar
Johann-S committed
223
224
225
226
      if (context._isWithActiveTrigger()) {
        context._enter(null, context)
      } else {
        context._leave(null, context)
fat's avatar
fat committed
227
      }
Johann-S's avatar
Johann-S committed
228
    } else {
229
      if (this.getTipElement().classList.contains(ClassName.SHOW)) {
Johann-S's avatar
Johann-S committed
230
231
        this._leave(null, this)
        return
232
      }
fat's avatar
fat committed
233

Johann-S's avatar
Johann-S committed
234
      this._enter(null, this)
235
    }
Johann-S's avatar
Johann-S committed
236
  }
237

Johann-S's avatar
Johann-S committed
238
239
  dispose() {
    clearTimeout(this._timeout)
240

241
    Data.removeData(this.element, this.constructor.DATA_KEY)
242

243
244
    EventHandler.off(this.element, this.constructor.EVENT_KEY)
    EventHandler.off(SelectorEngine.closest(this.element, '.modal'), 'hide.bs.modal')
245

Johann-S's avatar
Johann-S committed
246
    if (this.tip) {
247
      this.tip.parentNode.removeChild(this.tip)
Johann-S's avatar
Johann-S committed
248
249
250
251
252
253
254
255
256
    }

    this._isEnabled     = null
    this._timeout       = null
    this._hoverState    = null
    this._activeTrigger = null
    if (this._popper !== null) {
      this._popper.destroy()
    }
257

Johann-S's avatar
Johann-S committed
258
259
260
261
262
    this._popper = null
    this.element = null
    this.config  = null
    this.tip     = null
  }
263

Johann-S's avatar
Johann-S committed
264
  show() {
265
    if (this.element.style.display === 'none') {
Johann-S's avatar
Johann-S committed
266
267
      throw new Error('Please use show on visible elements')
    }
268

Johann-S's avatar
Johann-S committed
269
    if (this.isWithContent() && this._isEnabled) {
270
      const showEvent = EventHandler.trigger(this.element, this.constructor.Event.SHOW)
271
      const shadowRoot = findShadowRoot(this.element)
272
273
274
      const isInTheDom = shadowRoot !== null
        ? shadowRoot.contains(this.element)
        : this.element.ownerDocument.documentElement.contains(this.element)
275

276
      if (showEvent.defaultPrevented || !isInTheDom) {
Johann-S's avatar
Johann-S committed
277
278
        return
      }
279

Johann-S's avatar
Johann-S committed
280
      const tip   = this.getTipElement()
281
      const tipId = getUID(this.constructor.NAME)
282

Johann-S's avatar
Johann-S committed
283
284
      tip.setAttribute('id', tipId)
      this.element.setAttribute('aria-describedby', tipId)
285

Johann-S's avatar
Johann-S committed
286
      this.setContent()
287

Johann-S's avatar
Johann-S committed
288
      if (this.config.animation) {
289
        tip.classList.add(ClassName.FADE)
Johann-S's avatar
Johann-S committed
290
      }
291

Johann-S's avatar
Johann-S committed
292
293
294
      const placement  = typeof this.config.placement === 'function'
        ? this.config.placement.call(this, tip, this.element)
        : this.config.placement
295

Johann-S's avatar
Johann-S committed
296
297
      const attachment = this._getAttachment(placement)
      this.addAttachmentClass(attachment)
298

299
      const container = this._getContainer()
300
      Data.setData(tip, this.constructor.DATA_KEY, this)
Johann-S's avatar
Johann-S committed
301

302
303
      if (!this.element.ownerDocument.documentElement.contains(this.tip)) {
        container.appendChild(tip)
Johann-S's avatar
Johann-S committed
304
      }
305

306
      EventHandler.trigger(this.element, this.constructor.Event.INSERTED)
307

Johann-S's avatar
Johann-S committed
308
309
310
      this._popper = new Popper(this.element, tip, {
        placement: attachment,
        modifiers: {
311
          offset: this._getOffset(),
Johann-S's avatar
Johann-S committed
312
313
314
315
          flip: {
            behavior: this.config.fallbackPlacement
          },
          arrow: {
316
            element: Selector.TOOLTIP_ARROW
Johann-S's avatar
Johann-S committed
317
318
319
320
321
322
323
324
          },
          preventOverflow: {
            boundariesElement: this.config.boundary
          }
        },
        onCreate: (data) => {
          if (data.originalPlacement !== data.placement) {
            this._handlePopperPlacementChange(data)
325
          }
Johann-S's avatar
Johann-S committed
326
        },
327
        onUpdate: (data) => this._handlePopperPlacementChange(data)
Johann-S's avatar
Johann-S committed
328
      })
329

330
      tip.classList.add(ClassName.SHOW)
331

Johann-S's avatar
Johann-S committed
332
333
334
335
336
      // If this is a touch-enabled device we add extra
      // empty mouseover listeners to the body's immediate children;
      // only needed because of broken event delegation on iOS
      // https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html
      if ('ontouchstart' in document.documentElement) {
337
338
        makeArray(document.body.children).forEach((element) => {
          EventHandler.on(element, 'mouseover', noop())
339
        })
340
341
      }

XhmikosR's avatar
XhmikosR committed
342
      const complete = () => {
Johann-S's avatar
Johann-S committed
343
344
        if (this.config.animation) {
          this._fixTransition()
345
        }
Johann-S's avatar
Johann-S committed
346
347
        const prevHoverState = this._hoverState
        this._hoverState     = null
348

349
        EventHandler.trigger(this.element, this.constructor.Event.SHOWN)
350

Johann-S's avatar
Johann-S committed
351
352
        if (prevHoverState === HoverState.OUT) {
          this._leave(null, this)
353
354
355
        }
      }

356
      if (this.tip.classList.contains(ClassName.FADE)) {
357
358
359
        const transitionDuration = getTransitionDurationFromElement(this.tip)
        EventHandler.one(this.tip, TRANSITION_END, complete)
        emulateTransitionEnd(this.tip, transitionDuration)
360
361
362
363
      } else {
        complete()
      }
    }
Johann-S's avatar
Johann-S committed
364
  }
365

Johann-S's avatar
Johann-S committed
366
367
  hide(callback) {
    const tip       = this.getTipElement()
368
    const complete  = () => {
Johann-S's avatar
Johann-S committed
369
370
      if (this._hoverState !== HoverState.SHOW && tip.parentNode) {
        tip.parentNode.removeChild(tip)
371
      }
372

Johann-S's avatar
Johann-S committed
373
374
      this._cleanTipClass()
      this.element.removeAttribute('aria-describedby')
375
      EventHandler.trigger(this.element, this.constructor.Event.HIDDEN)
Johann-S's avatar
Johann-S committed
376
377
378
      if (this._popper !== null) {
        this._popper.destroy()
      }
379

Johann-S's avatar
Johann-S committed
380
381
382
      if (callback) {
        callback()
      }
383
384
    }

385
386
    const hideEvent = EventHandler.trigger(this.element, this.constructor.Event.HIDE)
    if (hideEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
387
      return
388
389
    }

390
    tip.classList.remove(ClassName.SHOW)
391

Johann-S's avatar
Johann-S committed
392
393
394
    // If this is a touch-enabled device we remove the extra
    // empty mouseover listeners we added for iOS support
    if ('ontouchstart' in document.documentElement) {
395
396
      makeArray(document.body.children)
        .forEach((element) => EventHandler.off(element, 'mouseover', noop))
397
398
    }

Johann-S's avatar
Johann-S committed
399
400
401
    this._activeTrigger[Trigger.CLICK] = false
    this._activeTrigger[Trigger.FOCUS] = false
    this._activeTrigger[Trigger.HOVER] = false
402

403
    if (this.tip.classList.contains(ClassName.FADE)) {
404
405
406
407
      const transitionDuration = getTransitionDurationFromElement(tip)

      EventHandler.one(tip, TRANSITION_END, complete)
      emulateTransitionEnd(tip, transitionDuration)
Johann-S's avatar
Johann-S committed
408
409
    } else {
      complete()
410
411
    }

Johann-S's avatar
Johann-S committed
412
413
    this._hoverState = ''
  }
414

Johann-S's avatar
Johann-S committed
415
416
417
  update() {
    if (this._popper !== null) {
      this._popper.scheduleUpdate()
fat's avatar
fat committed
418
    }
Johann-S's avatar
Johann-S committed
419
  }
fat's avatar
fat committed
420

Johann-S's avatar
Johann-S committed
421
  // Protected
422

Johann-S's avatar
Johann-S committed
423
424
425
  isWithContent() {
    return Boolean(this.getTitle())
  }
426

Johann-S's avatar
Johann-S committed
427
  addAttachmentClass(attachment) {
428
    this.getTipElement().classList.add(`${CLASS_PREFIX}-${attachment}`)
Johann-S's avatar
Johann-S committed
429
  }
430

Johann-S's avatar
Johann-S committed
431
  getTipElement() {
432
433
434
435
436
437
438
439
    if (this.tip) {
      return this.tip
    }

    const element = document.createElement('div')
    element.innerHTML = this.config.template

    this.tip = element.children[0]
Johann-S's avatar
Johann-S committed
440
441
442
443
444
    return this.tip
  }

  setContent() {
    const tip = this.getTipElement()
445
446
447
    this.setElementContent(SelectorEngine.findOne(Selector.TOOLTIP_INNER, tip), this.getTitle())
    tip.classList.remove(ClassName.FADE)
    tip.classList.remove(ClassName.SHOW)
Johann-S's avatar
Johann-S committed
448
449
  }

450
451
452
453
454
  setElementContent(element, content) {
    if (element === null) {
      return
    }

Johann-S's avatar
Johann-S committed
455
    if (typeof content === 'object' && (content.nodeType || content.jquery)) {
456
457
458
459
460
      if (content.jquery) {
        content = content[0]
      }

      // content is a DOM node or a jQuery
461
      if (this.config.html) {
462
463
464
        if (content.parentNode !== element) {
          element.innerHTML = ''
          element.appendChild(content)
465
        }
466
      } else {
467
        element.innerText = content.textContent
468
      }
469
470
471
472
473
474
475
476
477

      return
    }

    if (this.config.html) {
      if (this.config.sanitize) {
        content = sanitizeHtml(content, this.config.whiteList, this.config.sanitizeFn)
      }

478
      element.innerHTML = content
Johann-S's avatar
Johann-S committed
479
    } else {
480
      element.innerText = content
481
    }
Johann-S's avatar
Johann-S committed
482
  }
483

Johann-S's avatar
Johann-S committed
484
485
486
487
488
489
490
  getTitle() {
    let title = this.element.getAttribute('data-original-title')

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

Johann-S's avatar
Johann-S committed
493
494
    return title
  }
fat's avatar
fat committed
495

Johann-S's avatar
Johann-S committed
496
  // Private
497

498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
  _getOffset() {
    const offset = {}

    if (typeof this.config.offset === 'function') {
      offset.fn = (data) => {
        data.offsets = {
          ...data.offsets,
          ...this.config.offset(data.offsets, this.element) || {}
        }

        return data
      }
    } else {
      offset.offset = this.config.offset
    }

    return offset
  }

517
518
519
520
521
  _getContainer() {
    if (this.config.container === false) {
      return document.body
    }

522
    if (isElement(this.config.container)) {
523
      return this.config.container
524
525
    }

526
    return SelectorEngine.findOne(this.config.container)
527
528
  }

Johann-S's avatar
Johann-S committed
529
530
531
532
533
534
535
536
537
  _getAttachment(placement) {
    return AttachmentMap[placement.toUpperCase()]
  }

  _setListeners() {
    const triggers = this.config.trigger.split(' ')

    triggers.forEach((trigger) => {
      if (trigger === 'click') {
538
        EventHandler.on(this.element,
Johann-S's avatar
Johann-S committed
539
540
541
          this.constructor.Event.CLICK,
          this.config.selector,
          (event) => this.toggle(event)
542
        )
Johann-S's avatar
Johann-S committed
543
544
545
546
547
548
549
550
      } else if (trigger !== Trigger.MANUAL) {
        const eventIn = trigger === Trigger.HOVER
          ? this.constructor.Event.MOUSEENTER
          : this.constructor.Event.FOCUSIN
        const eventOut = trigger === Trigger.HOVER
          ? this.constructor.Event.MOUSELEAVE
          : this.constructor.Event.FOCUSOUT

551
552
553
554
555
556
557
558
559
560
        EventHandler.on(this.element,
          eventIn,
          this.config.selector,
          (event) => this._enter(event)
        )
        EventHandler.on(this.element,
          eventOut,
          this.config.selector,
          (event) => this._leave(event)
        )
561
      }
Johann-S's avatar
Johann-S committed
562
    })
563

564
    EventHandler.on(SelectorEngine.closest(this.element, '.modal'),
565
566
567
568
569
570
571
572
      'hide.bs.modal',
      () => {
        if (this.element) {
          this.hide()
        }
      }
    )

Johann-S's avatar
Johann-S committed
573
574
575
576
577
    if (this.config.selector) {
      this.config = {
        ...this.config,
        trigger: 'manual',
        selector: ''
578
      }
Johann-S's avatar
Johann-S committed
579
580
581
582
    } else {
      this._fixTitle()
    }
  }
583

Johann-S's avatar
Johann-S committed
584
585
  _fixTitle() {
    const titleType = typeof this.element.getAttribute('data-original-title')
586
587

    if (this.element.getAttribute('title') || titleType !== 'string') {
Johann-S's avatar
Johann-S committed
588
589
590
591
      this.element.setAttribute(
        'data-original-title',
        this.element.getAttribute('title') || ''
      )
592

Johann-S's avatar
Johann-S committed
593
594
595
      this.element.setAttribute('title', '')
    }
  }
596

Johann-S's avatar
Johann-S committed
597
598
  _enter(event, context) {
    const dataKey = this.constructor.DATA_KEY
599
    context = context || Data.getData(event.delegateTarget, dataKey)
600

Johann-S's avatar
Johann-S committed
601
602
    if (!context) {
      context = new this.constructor(
603
        event.delegateTarget,
Johann-S's avatar
Johann-S committed
604
605
        this._getDelegateConfig()
      )
606
      Data.setData(event.delegateTarget, dataKey, context)
607
608
    }

Johann-S's avatar
Johann-S committed
609
610
611
612
613
    if (event) {
      context._activeTrigger[
        event.type === 'focusin' ? Trigger.FOCUS : Trigger.HOVER
      ] = true
    }
fat's avatar
fat committed
614

615
616
    if (context.getTipElement().classList.contains(ClassName.SHOW) ||
        context._hoverState === HoverState.SHOW) {
Johann-S's avatar
Johann-S committed
617
618
619
      context._hoverState = HoverState.SHOW
      return
    }
620

Johann-S's avatar
Johann-S committed
621
    clearTimeout(context._timeout)
622

Johann-S's avatar
Johann-S committed
623
    context._hoverState = HoverState.SHOW
624

Johann-S's avatar
Johann-S committed
625
626
627
628
    if (!context.config.delay || !context.config.delay.show) {
      context.show()
      return
    }
629

Johann-S's avatar
Johann-S committed
630
631
632
633
634
635
    context._timeout = setTimeout(() => {
      if (context._hoverState === HoverState.SHOW) {
        context.show()
      }
    }, context.config.delay.show)
  }
636

Johann-S's avatar
Johann-S committed
637
638
  _leave(event, context) {
    const dataKey = this.constructor.DATA_KEY
639
    context = context || Data.getData(event.delegateTarget, dataKey)
640

Johann-S's avatar
Johann-S committed
641
642
    if (!context) {
      context = new this.constructor(
643
        event.delegateTarget,
Johann-S's avatar
Johann-S committed
644
645
        this._getDelegateConfig()
      )
646
      Data.setData(event.delegateTarget, dataKey, context)
647
648
    }

Johann-S's avatar
Johann-S committed
649
650
651
652
653
    if (event) {
      context._activeTrigger[
        event.type === 'focusout' ? Trigger.FOCUS : Trigger.HOVER
      ] = false
    }
654

Johann-S's avatar
Johann-S committed
655
656
    if (context._isWithActiveTrigger()) {
      return
657
658
    }

Johann-S's avatar
Johann-S committed
659
    clearTimeout(context._timeout)
660

Johann-S's avatar
Johann-S committed
661
    context._hoverState = HoverState.OUT
662

Johann-S's avatar
Johann-S committed
663
664
665
666
    if (!context.config.delay || !context.config.delay.hide) {
      context.hide()
      return
    }
667

Johann-S's avatar
Johann-S committed
668
669
670
    context._timeout = setTimeout(() => {
      if (context._hoverState === HoverState.OUT) {
        context.hide()
671
      }
Johann-S's avatar
Johann-S committed
672
673
    }, context.config.delay.hide)
  }
674

Johann-S's avatar
Johann-S committed
675
676
677
678
679
  _isWithActiveTrigger() {
    for (const trigger in this._activeTrigger) {
      if (this._activeTrigger[trigger]) {
        return true
      }
680
681
    }

Johann-S's avatar
Johann-S committed
682
683
    return false
  }
684

Johann-S's avatar
Johann-S committed
685
  _getConfig(config) {
686
    const dataAttributes = Manipulator.getDataAttributes(this.element)
687
688
689
690
691
692
693
694

    Object.keys(dataAttributes)
      .forEach((dataAttr) => {
        if (DISALLOWED_ATTRIBUTES.indexOf(dataAttr) !== -1) {
          delete dataAttributes[dataAttr]
        }
      })

Johann-S's avatar
Johann-S committed
695
    if (config && typeof config.container === 'object' && config.container.jquery) {
696
697
698
      config.container = config.container[0]
    }

Johann-S's avatar
Johann-S committed
699
700
    config = {
      ...this.constructor.Default,
701
      ...dataAttributes,
Johann-S's avatar
Johann-S committed
702
      ...typeof config === 'object' && config ? config : {}
703
704
    }

Johann-S's avatar
Johann-S committed
705
706
707
708
    if (typeof config.delay === 'number') {
      config.delay = {
        show: config.delay,
        hide: config.delay
Johann-S's avatar
Johann-S committed
709
      }
Johann-S's avatar
Johann-S committed
710
711
    }

Johann-S's avatar
Johann-S committed
712
713
    if (typeof config.title === 'number') {
      config.title = config.title.toString()
714
    }
715

Johann-S's avatar
Johann-S committed
716
717
    if (typeof config.content === 'number') {
      config.content = config.content.toString()
718
719
    }

720
    typeCheckConfig(
Johann-S's avatar
Johann-S committed
721
722
723
724
      NAME,
      config,
      this.constructor.DefaultType
    )
725

726
727
728
729
    if (config.sanitize) {
      config.template = sanitizeHtml(config.template, config.whiteList, config.sanitizeFn)
    }

Johann-S's avatar
Johann-S committed
730
731
    return config
  }
732

Johann-S's avatar
Johann-S committed
733
734
  _getDelegateConfig() {
    const config = {}
735

Johann-S's avatar
Johann-S committed
736
737
738
739
    if (this.config) {
      for (const key in this.config) {
        if (this.constructor.Default[key] !== this.config[key]) {
          config[key] = this.config[key]
740
        }
Johann-S's avatar
Johann-S committed
741
742
      }
    }
743

Johann-S's avatar
Johann-S committed
744
745
746
747
    return config
  }

  _cleanTipClass() {
748
749
    const tip = this.getTipElement()
    const tabClass = tip.getAttribute('class').match(BSCLS_PREFIX_REGEX)
Johann-S's avatar
Johann-S committed
750
    if (tabClass !== null && tabClass.length) {
751
752
753
      tabClass
        .map((token) => token.trim())
        .forEach((tClass) => tip.classList.remove(tClass))
754
755
756
    }
  }

Johann-S's avatar
Johann-S committed
757
758
759
760
761
762
763
764
765
766
767
768
769
  _handlePopperPlacementChange(popperData) {
    const popperInstance = popperData.instance
    this.tip = popperInstance.popper
    this._cleanTipClass()
    this.addAttachmentClass(this._getAttachment(popperData.placement))
  }

  _fixTransition() {
    const tip = this.getTipElement()
    const initConfigAnimation = this.config.animation
    if (tip.getAttribute('x-placement') !== null) {
      return
    }
770
    tip.classList.remove(ClassName.FADE)
Johann-S's avatar
Johann-S committed
771
772
773
774
775
776
777
778
779
780
    this.config.animation = false
    this.hide()
    this.show()
    this.config.animation = initConfigAnimation
  }

  // Static

  static _jQueryInterface(config) {
    return this.each(function () {
781
      let data      = Data.getData(this, DATA_KEY)
Johann-S's avatar
Johann-S committed
782
783
784
785
786
787
788
789
790
      const _config = typeof config === 'object' && config

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

      if (!data) {
        data = new Tooltip(this, _config)
      }
791

Johann-S's avatar
Johann-S committed
792
793
794
795
796
797
798
      if (typeof config === 'string') {
        if (typeof data[config] === 'undefined') {
          throw new TypeError(`No method named "${config}"`)
        }
        data[config]()
      }
    })
799
  }
800
801
802
803

  static _getInstance(element) {
    return Data.getData(element, DATA_KEY)
  }
Johann-S's avatar
Johann-S committed
804
805
806
807
808
809
}

/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
810
 * add .tooltip to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
811
 */
812

813
814
815
816
817
818
819
820
if (typeof $ !== 'undefined') {
  const JQUERY_NO_CONFLICT  = $.fn[NAME]
  $.fn[NAME]                = Tooltip._jQueryInterface
  $.fn[NAME].Constructor    = Tooltip
  $.fn[NAME].noConflict     = () => {
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Tooltip._jQueryInterface
  }
Johann-S's avatar
Johann-S committed
821
}
822
823

export default Tooltip