tooltip.js 19.4 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
import {
  jQuery as $,
  TRANSITION_END,
  emulateTransitionEnd,
  findShadowRoot,
  getTransitionDurationFromElement,
  getUID,
  isElement,
  makeArray,
  noop,
  typeCheckConfig
Johann-S's avatar
Johann-S committed
19
} from '../util/index'
20
21
22
import {
  DefaultWhitelist,
  sanitizeHtml
Johann-S's avatar
Johann-S committed
23
24
25
26
} from '../util/sanitizer'
import Data from '../dom/data'
import EventHandler from '../dom/event-handler'
import Manipulator from '../dom/manipulator'
27
import Popper from 'popper.js'
Johann-S's avatar
Johann-S committed
28
import SelectorEngine from '../dom/selector-engine'
29

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

XhmikosR's avatar
XhmikosR committed
36
37
38
39
40
41
const NAME = 'tooltip'
const VERSION = '4.3.1'
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')
42
const DISALLOWED_ATTRIBUTES = ['sanitize', 'whiteList', 'sanitizeFn']
Johann-S's avatar
Johann-S committed
43
44

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

const AttachmentMap = {
XhmikosR's avatar
XhmikosR committed
63
64
65
66
67
  AUTO: 'auto',
  TOP: 'top',
  RIGHT: 'right',
  BOTTOM: 'bottom',
  LEFT: 'left'
Johann-S's avatar
Johann-S committed
68
69
70
}

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

const HoverState = {
XhmikosR's avatar
XhmikosR committed
91
92
  SHOW: 'show',
  OUT: 'out'
Johann-S's avatar
Johann-S committed
93
94
95
}

const Event = {
XhmikosR's avatar
XhmikosR committed
96
97
98
99
100
101
102
103
104
105
  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}`
Johann-S's avatar
Johann-S committed
106
107
108
}

const ClassName = {
XhmikosR's avatar
XhmikosR committed
109
110
  FADE: 'fade',
  SHOW: 'show'
Johann-S's avatar
Johann-S committed
111
112
113
}

const Selector = {
114
  TOOLTIP_INNER: '.tooltip-inner'
Johann-S's avatar
Johann-S committed
115
116
117
}

const Trigger = {
XhmikosR's avatar
XhmikosR committed
118
119
120
121
  HOVER: 'hover',
  FOCUS: 'focus',
  CLICK: 'click',
  MANUAL: 'manual'
Johann-S's avatar
Johann-S committed
122
}
123

Johann-S's avatar
Johann-S committed
124
125
126
127
128
/**
 * ------------------------------------------------------------------------
 * Class Definition
 * ------------------------------------------------------------------------
 */
129

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

Johann-S's avatar
Johann-S committed
140
    // private
XhmikosR's avatar
XhmikosR committed
141
142
143
    this._isEnabled = true
    this._timeout = 0
    this._hoverState = ''
Johann-S's avatar
Johann-S committed
144
    this._activeTrigger = {}
XhmikosR's avatar
XhmikosR committed
145
    this._popper = null
146

Johann-S's avatar
Johann-S committed
147
148
    // Protected
    this.element = element
XhmikosR's avatar
XhmikosR committed
149
150
    this.config = this._getConfig(config)
    this.tip = null
fat's avatar
fat committed
151

Johann-S's avatar
Johann-S committed
152
    this._setListeners()
153
    Data.setData(element, this.constructor.DATA_KEY, this)
Johann-S's avatar
Johann-S committed
154
  }
fat's avatar
fat committed
155

Johann-S's avatar
Johann-S committed
156
  // Getters
fat's avatar
fat committed
157

Johann-S's avatar
Johann-S committed
158
159
160
  static get VERSION() {
    return VERSION
  }
fat's avatar
fat committed
161

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

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

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

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

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

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

Johann-S's avatar
Johann-S committed
186
  // Public
187

Johann-S's avatar
Johann-S committed
188
189
190
  enable() {
    this._isEnabled = true
  }
191

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

Johann-S's avatar
Johann-S committed
196
197
198
  toggleEnabled() {
    this._isEnabled = !this._isEnabled
  }
Jacob Thornton's avatar
Jacob Thornton committed
199

Johann-S's avatar
Johann-S committed
200
201
202
  toggle(event) {
    if (!this._isEnabled) {
      return
203
204
    }

Johann-S's avatar
Johann-S committed
205
206
    if (event) {
      const dataKey = this.constructor.DATA_KEY
207
      let context = Data.getData(event.delegateTarget, dataKey)
fat's avatar
fat committed
208

Johann-S's avatar
Johann-S committed
209
210
      if (!context) {
        context = new this.constructor(
211
          event.delegateTarget,
Johann-S's avatar
Johann-S committed
212
213
          this._getDelegateConfig()
        )
214
        Data.setData(event.delegateTarget, dataKey, context)
Johann-S's avatar
Johann-S committed
215
      }
fat's avatar
fat committed
216

Johann-S's avatar
Johann-S committed
217
      context._activeTrigger.click = !context._activeTrigger.click
fat's avatar
fat committed
218

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

Johann-S's avatar
Johann-S committed
230
      this._enter(null, this)
231
    }
Johann-S's avatar
Johann-S committed
232
  }
233

Johann-S's avatar
Johann-S committed
234
235
  dispose() {
    clearTimeout(this._timeout)
236

237
    Data.removeData(this.element, this.constructor.DATA_KEY)
238

239
    EventHandler.off(this.element, this.constructor.EVENT_KEY)
240
    EventHandler.off(SelectorEngine.closest(this.element, '.modal'), 'hide.bs.modal', this._hideModalHandler)
241

Johann-S's avatar
Johann-S committed
242
    if (this.tip) {
243
      this.tip.parentNode.removeChild(this.tip)
Johann-S's avatar
Johann-S committed
244
245
    }

XhmikosR's avatar
XhmikosR committed
246
247
248
    this._isEnabled = null
    this._timeout = null
    this._hoverState = null
Johann-S's avatar
Johann-S committed
249
250
251
252
    this._activeTrigger = null
    if (this._popper !== null) {
      this._popper.destroy()
    }
253

Johann-S's avatar
Johann-S committed
254
255
    this._popper = null
    this.element = null
XhmikosR's avatar
XhmikosR committed
256
257
    this.config = null
    this.tip = null
Johann-S's avatar
Johann-S committed
258
  }
259

Johann-S's avatar
Johann-S committed
260
  show() {
261
    if (this.element.style.display === 'none') {
Johann-S's avatar
Johann-S committed
262
263
      throw new Error('Please use show on visible elements')
    }
264

Johann-S's avatar
Johann-S committed
265
    if (this.isWithContent() && this._isEnabled) {
266
      const showEvent = EventHandler.trigger(this.element, this.constructor.Event.SHOW)
267
      const shadowRoot = findShadowRoot(this.element)
XhmikosR's avatar
XhmikosR committed
268
269
270
      const isInTheDom = shadowRoot === null ?
        this.element.ownerDocument.documentElement.contains(this.element) :
        shadowRoot.contains(this.element)
271

272
      if (showEvent.defaultPrevented || !isInTheDom) {
Johann-S's avatar
Johann-S committed
273
274
        return
      }
275

XhmikosR's avatar
XhmikosR committed
276
      const tip = this.getTipElement()
277
      const tipId = getUID(this.constructor.NAME)
278

Johann-S's avatar
Johann-S committed
279
280
      tip.setAttribute('id', tipId)
      this.element.setAttribute('aria-describedby', tipId)
281

Johann-S's avatar
Johann-S committed
282
      this.setContent()
283

Johann-S's avatar
Johann-S committed
284
      if (this.config.animation) {
285
        tip.classList.add(ClassName.FADE)
Johann-S's avatar
Johann-S committed
286
      }
287

XhmikosR's avatar
XhmikosR committed
288
289
290
      const placement = typeof this.config.placement === 'function' ?
        this.config.placement.call(this, tip, this.element) :
        this.config.placement
291

Johann-S's avatar
Johann-S committed
292
      const attachment = this._getAttachment(placement)
Johann-S's avatar
Johann-S committed
293
      this._addAttachmentClass(attachment)
294

295
      const container = this._getContainer()
296
      Data.setData(tip, this.constructor.DATA_KEY, this)
Johann-S's avatar
Johann-S committed
297

298
299
      if (!this.element.ownerDocument.documentElement.contains(this.tip)) {
        container.appendChild(tip)
Johann-S's avatar
Johann-S committed
300
      }
301

302
      EventHandler.trigger(this.element, this.constructor.Event.INSERTED)
303

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

326
      tip.classList.add(ClassName.SHOW)
327

Johann-S's avatar
Johann-S committed
328
329
330
331
332
      // 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) {
XhmikosR's avatar
XhmikosR committed
333
        makeArray(document.body.children).forEach(element => {
334
          EventHandler.on(element, 'mouseover', noop())
335
        })
336
337
      }

XhmikosR's avatar
XhmikosR committed
338
      const complete = () => {
Johann-S's avatar
Johann-S committed
339
340
        if (this.config.animation) {
          this._fixTransition()
341
        }
XhmikosR's avatar
XhmikosR committed
342

Johann-S's avatar
Johann-S committed
343
        const prevHoverState = this._hoverState
XhmikosR's avatar
XhmikosR committed
344
        this._hoverState = null
345

346
        EventHandler.trigger(this.element, this.constructor.Event.SHOWN)
347

Johann-S's avatar
Johann-S committed
348
349
        if (prevHoverState === HoverState.OUT) {
          this._leave(null, this)
350
351
352
        }
      }

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

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

Johann-S's avatar
Johann-S committed
370
371
      this._cleanTipClass()
      this.element.removeAttribute('aria-describedby')
372
      EventHandler.trigger(this.element, this.constructor.Event.HIDDEN)
Johann-S's avatar
Johann-S committed
373
      this._popper.destroy()
374
375
    }

376
377
    const hideEvent = EventHandler.trigger(this.element, this.constructor.Event.HIDE)
    if (hideEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
378
      return
379
380
    }

381
    tip.classList.remove(ClassName.SHOW)
382

Johann-S's avatar
Johann-S committed
383
384
385
    // If this is a touch-enabled device we remove the extra
    // empty mouseover listeners we added for iOS support
    if ('ontouchstart' in document.documentElement) {
386
      makeArray(document.body.children)
XhmikosR's avatar
XhmikosR committed
387
        .forEach(element => EventHandler.off(element, 'mouseover', noop))
388
389
    }

Johann-S's avatar
Johann-S committed
390
391
392
    this._activeTrigger[Trigger.CLICK] = false
    this._activeTrigger[Trigger.FOCUS] = false
    this._activeTrigger[Trigger.HOVER] = false
393

394
    if (this.tip.classList.contains(ClassName.FADE)) {
395
396
397
398
      const transitionDuration = getTransitionDurationFromElement(tip)

      EventHandler.one(tip, TRANSITION_END, complete)
      emulateTransitionEnd(tip, transitionDuration)
Johann-S's avatar
Johann-S committed
399
400
    } else {
      complete()
401
402
    }

Johann-S's avatar
Johann-S committed
403
404
    this._hoverState = ''
  }
405

Johann-S's avatar
Johann-S committed
406
407
408
  update() {
    if (this._popper !== null) {
      this._popper.scheduleUpdate()
fat's avatar
fat committed
409
    }
Johann-S's avatar
Johann-S committed
410
  }
fat's avatar
fat committed
411

Johann-S's avatar
Johann-S committed
412
  // Protected
413

Johann-S's avatar
Johann-S committed
414
415
416
  isWithContent() {
    return Boolean(this.getTitle())
  }
417

Johann-S's avatar
Johann-S committed
418
  getTipElement() {
419
420
421
422
423
424
425
426
    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
427
428
429
430
431
    return this.tip
  }

  setContent() {
    const tip = this.getTipElement()
432
433
434
    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
435
436
  }

437
438
439
440
441
  setElementContent(element, content) {
    if (element === null) {
      return
    }

Johann-S's avatar
Johann-S committed
442
    if (typeof content === 'object' && isElement(content)) {
443
444
445
446
447
      if (content.jquery) {
        content = content[0]
      }

      // content is a DOM node or a jQuery
448
      if (this.config.html) {
449
450
451
        if (content.parentNode !== element) {
          element.innerHTML = ''
          element.appendChild(content)
452
        }
453
      } else {
454
        element.innerText = content.textContent
455
      }
456
457
458
459
460
461
462
463
464

      return
    }

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

465
      element.innerHTML = content
Johann-S's avatar
Johann-S committed
466
    } else {
467
      element.innerText = content
468
    }
Johann-S's avatar
Johann-S committed
469
  }
470

Johann-S's avatar
Johann-S committed
471
472
473
474
  getTitle() {
    let title = this.element.getAttribute('data-original-title')

    if (!title) {
XhmikosR's avatar
XhmikosR committed
475
476
477
      title = typeof this.config.title === 'function' ?
        this.config.title.call(this.element) :
        this.config.title
478
479
    }

Johann-S's avatar
Johann-S committed
480
481
    return title
  }
fat's avatar
fat committed
482

Johann-S's avatar
Johann-S committed
483
  // Private
484

Johann-S's avatar
Johann-S committed
485
486
487
488
  _addAttachmentClass(attachment) {
    this.getTipElement().classList.add(`${CLASS_PREFIX}-${attachment}`)
  }

489
490
491
492
  _getOffset() {
    const offset = {}

    if (typeof this.config.offset === 'function') {
XhmikosR's avatar
XhmikosR committed
493
      offset.fn = data => {
494
495
496
497
498
499
500
501
502
503
504
505
506
507
        data.offsets = {
          ...data.offsets,
          ...this.config.offset(data.offsets, this.element) || {}
        }

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

    return offset
  }

508
509
510
511
512
  _getContainer() {
    if (this.config.container === false) {
      return document.body
    }

513
    if (isElement(this.config.container)) {
514
      return this.config.container
515
516
    }

517
    return SelectorEngine.findOne(this.config.container)
518
519
  }

Johann-S's avatar
Johann-S committed
520
521
522
523
524
525
526
  _getAttachment(placement) {
    return AttachmentMap[placement.toUpperCase()]
  }

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

XhmikosR's avatar
XhmikosR committed
527
    triggers.forEach(trigger => {
Johann-S's avatar
Johann-S committed
528
      if (trigger === 'click') {
529
        EventHandler.on(this.element,
Johann-S's avatar
Johann-S committed
530
531
          this.constructor.Event.CLICK,
          this.config.selector,
XhmikosR's avatar
XhmikosR committed
532
          event => this.toggle(event)
533
        )
Johann-S's avatar
Johann-S committed
534
      } else if (trigger !== Trigger.MANUAL) {
XhmikosR's avatar
XhmikosR committed
535
536
537
538
539
540
        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
Johann-S's avatar
Johann-S committed
541

542
543
544
        EventHandler.on(this.element,
          eventIn,
          this.config.selector,
XhmikosR's avatar
XhmikosR committed
545
          event => this._enter(event)
546
547
548
549
        )
        EventHandler.on(this.element,
          eventOut,
          this.config.selector,
XhmikosR's avatar
XhmikosR committed
550
          event => this._leave(event)
551
        )
552
      }
Johann-S's avatar
Johann-S committed
553
    })
554

555
556
557
558
559
560
    this._hideModalHandler = () => {
      if (this.element) {
        this.hide()
      }
    }

561
    EventHandler.on(SelectorEngine.closest(this.element, '.modal'),
562
      'hide.bs.modal',
563
      this._hideModalHandler
564
565
    )

Johann-S's avatar
Johann-S committed
566
567
568
569
570
    if (this.config.selector) {
      this.config = {
        ...this.config,
        trigger: 'manual',
        selector: ''
571
      }
Johann-S's avatar
Johann-S committed
572
573
574
575
    } else {
      this._fixTitle()
    }
  }
576

Johann-S's avatar
Johann-S committed
577
578
  _fixTitle() {
    const titleType = typeof this.element.getAttribute('data-original-title')
579
580

    if (this.element.getAttribute('title') || titleType !== 'string') {
Johann-S's avatar
Johann-S committed
581
582
583
584
      this.element.setAttribute(
        'data-original-title',
        this.element.getAttribute('title') || ''
      )
585

Johann-S's avatar
Johann-S committed
586
587
588
      this.element.setAttribute('title', '')
    }
  }
589

Johann-S's avatar
Johann-S committed
590
591
  _enter(event, context) {
    const dataKey = this.constructor.DATA_KEY
592
    context = context || Data.getData(event.delegateTarget, dataKey)
593

Johann-S's avatar
Johann-S committed
594
595
    if (!context) {
      context = new this.constructor(
596
        event.delegateTarget,
Johann-S's avatar
Johann-S committed
597
598
        this._getDelegateConfig()
      )
599
      Data.setData(event.delegateTarget, dataKey, context)
600
601
    }

Johann-S's avatar
Johann-S committed
602
603
604
605
606
    if (event) {
      context._activeTrigger[
        event.type === 'focusin' ? Trigger.FOCUS : Trigger.HOVER
      ] = true
    }
fat's avatar
fat committed
607

608
609
    if (context.getTipElement().classList.contains(ClassName.SHOW) ||
        context._hoverState === HoverState.SHOW) {
Johann-S's avatar
Johann-S committed
610
611
612
      context._hoverState = HoverState.SHOW
      return
    }
613

Johann-S's avatar
Johann-S committed
614
    clearTimeout(context._timeout)
615

Johann-S's avatar
Johann-S committed
616
    context._hoverState = HoverState.SHOW
617

Johann-S's avatar
Johann-S committed
618
619
620
621
    if (!context.config.delay || !context.config.delay.show) {
      context.show()
      return
    }
622

Johann-S's avatar
Johann-S committed
623
624
625
626
627
628
    context._timeout = setTimeout(() => {
      if (context._hoverState === HoverState.SHOW) {
        context.show()
      }
    }, context.config.delay.show)
  }
629

Johann-S's avatar
Johann-S committed
630
631
  _leave(event, context) {
    const dataKey = this.constructor.DATA_KEY
632
    context = context || Data.getData(event.delegateTarget, dataKey)
633

Johann-S's avatar
Johann-S committed
634
635
    if (!context) {
      context = new this.constructor(
636
        event.delegateTarget,
Johann-S's avatar
Johann-S committed
637
638
        this._getDelegateConfig()
      )
639
      Data.setData(event.delegateTarget, dataKey, context)
640
641
    }

Johann-S's avatar
Johann-S committed
642
643
644
645
646
    if (event) {
      context._activeTrigger[
        event.type === 'focusout' ? Trigger.FOCUS : Trigger.HOVER
      ] = false
    }
647

Johann-S's avatar
Johann-S committed
648
649
    if (context._isWithActiveTrigger()) {
      return
650
651
    }

Johann-S's avatar
Johann-S committed
652
    clearTimeout(context._timeout)
653

Johann-S's avatar
Johann-S committed
654
    context._hoverState = HoverState.OUT
655

Johann-S's avatar
Johann-S committed
656
657
658
659
    if (!context.config.delay || !context.config.delay.hide) {
      context.hide()
      return
    }
660

Johann-S's avatar
Johann-S committed
661
662
663
    context._timeout = setTimeout(() => {
      if (context._hoverState === HoverState.OUT) {
        context.hide()
664
      }
Johann-S's avatar
Johann-S committed
665
666
    }, context.config.delay.hide)
  }
667

Johann-S's avatar
Johann-S committed
668
669
670
671
672
  _isWithActiveTrigger() {
    for (const trigger in this._activeTrigger) {
      if (this._activeTrigger[trigger]) {
        return true
      }
673
674
    }

Johann-S's avatar
Johann-S committed
675
676
    return false
  }
677

Johann-S's avatar
Johann-S committed
678
  _getConfig(config) {
679
    const dataAttributes = Manipulator.getDataAttributes(this.element)
680
681

    Object.keys(dataAttributes)
XhmikosR's avatar
XhmikosR committed
682
      .forEach(dataAttr => {
683
684
685
686
687
        if (DISALLOWED_ATTRIBUTES.indexOf(dataAttr) !== -1) {
          delete dataAttributes[dataAttr]
        }
      })

Johann-S's avatar
Johann-S committed
688
    if (config && typeof config.container === 'object' && config.container.jquery) {
689
690
691
      config.container = config.container[0]
    }

Johann-S's avatar
Johann-S committed
692
693
    config = {
      ...this.constructor.Default,
694
      ...dataAttributes,
Johann-S's avatar
Johann-S committed
695
      ...typeof config === 'object' && config ? config : {}
696
697
    }

Johann-S's avatar
Johann-S committed
698
699
700
701
    if (typeof config.delay === 'number') {
      config.delay = {
        show: config.delay,
        hide: config.delay
Johann-S's avatar
Johann-S committed
702
      }
Johann-S's avatar
Johann-S committed
703
704
    }

Johann-S's avatar
Johann-S committed
705
706
    if (typeof config.title === 'number') {
      config.title = config.title.toString()
707
    }
708

Johann-S's avatar
Johann-S committed
709
710
    if (typeof config.content === 'number') {
      config.content = config.content.toString()
711
712
    }

713
    typeCheckConfig(
Johann-S's avatar
Johann-S committed
714
715
716
717
      NAME,
      config,
      this.constructor.DefaultType
    )
718

719
720
721
722
    if (config.sanitize) {
      config.template = sanitizeHtml(config.template, config.whiteList, config.sanitizeFn)
    }

Johann-S's avatar
Johann-S committed
723
724
    return config
  }
725

Johann-S's avatar
Johann-S committed
726
727
  _getDelegateConfig() {
    const config = {}
728

Johann-S's avatar
Johann-S committed
729
730
731
732
    if (this.config) {
      for (const key in this.config) {
        if (this.constructor.Default[key] !== this.config[key]) {
          config[key] = this.config[key]
733
        }
Johann-S's avatar
Johann-S committed
734
735
      }
    }
736

Johann-S's avatar
Johann-S committed
737
738
739
740
    return config
  }

  _cleanTipClass() {
741
742
    const tip = this.getTipElement()
    const tabClass = tip.getAttribute('class').match(BSCLS_PREFIX_REGEX)
Johann-S's avatar
Johann-S committed
743
    if (tabClass !== null && tabClass.length) {
744
      tabClass
XhmikosR's avatar
XhmikosR committed
745
746
        .map(token => token.trim())
        .forEach(tClass => tip.classList.remove(tClass))
747
748
749
    }
  }

Johann-S's avatar
Johann-S committed
750
751
752
753
  _handlePopperPlacementChange(popperData) {
    const popperInstance = popperData.instance
    this.tip = popperInstance.popper
    this._cleanTipClass()
Johann-S's avatar
Johann-S committed
754
    this._addAttachmentClass(this._getAttachment(popperData.placement))
Johann-S's avatar
Johann-S committed
755
756
757
758
759
760
761
762
  }

  _fixTransition() {
    const tip = this.getTipElement()
    const initConfigAnimation = this.config.animation
    if (tip.getAttribute('x-placement') !== null) {
      return
    }
XhmikosR's avatar
XhmikosR committed
763

764
    tip.classList.remove(ClassName.FADE)
Johann-S's avatar
Johann-S committed
765
766
767
768
769
770
771
772
    this.config.animation = false
    this.hide()
    this.show()
    this.config.animation = initConfigAnimation
  }

  // Static

773
  static jQueryInterface(config) {
Johann-S's avatar
Johann-S committed
774
    return this.each(function () {
XhmikosR's avatar
XhmikosR committed
775
      let data = Data.getData(this, DATA_KEY)
Johann-S's avatar
Johann-S committed
776
777
778
779
780
781
782
783
784
      const _config = typeof config === 'object' && config

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

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

Johann-S's avatar
Johann-S committed
786
787
788
789
      if (typeof config === 'string') {
        if (typeof data[config] === 'undefined') {
          throw new TypeError(`No method named "${config}"`)
        }
XhmikosR's avatar
XhmikosR committed
790

Johann-S's avatar
Johann-S committed
791
792
793
        data[config]()
      }
    })
794
  }
795

796
  static getInstance(element) {
797
798
    return Data.getData(element, DATA_KEY)
  }
Johann-S's avatar
Johann-S committed
799
800
801
802
803
804
}

/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
805
 * add .tooltip to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
806
 */
Johann-S's avatar
Johann-S committed
807
/* istanbul ignore if */
808
if (typeof $ !== 'undefined') {
XhmikosR's avatar
XhmikosR committed
809
  const JQUERY_NO_CONFLICT = $.fn[NAME]
810
  $.fn[NAME] = Tooltip.jQueryInterface
XhmikosR's avatar
XhmikosR committed
811
812
  $.fn[NAME].Constructor = Tooltip
  $.fn[NAME].noConflict = () => {
813
    $.fn[NAME] = JQUERY_NO_CONFLICT
814
    return Tooltip.jQueryInterface
815
  }
Johann-S's avatar
Johann-S committed
816
}
817
818

export default Tooltip