tooltip.js 19.6 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
import {
9
  getjQuery,
10
11
12
13
14
15
16
17
  TRANSITION_END,
  emulateTransitionEnd,
  findShadowRoot,
  getTransitionDurationFromElement,
  getUID,
  isElement,
  noop,
  typeCheckConfig
18
} from './util/index'
19
20
21
import {
  DefaultWhitelist,
  sanitizeHtml
22
23
24
25
} from './util/sanitizer'
import Data from './dom/data'
import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
26
import Popper from 'popper.js'
27
import SelectorEngine from './dom/selector-engine'
28

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

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

const DefaultType = {
XhmikosR's avatar
XhmikosR committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  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)',
58
59
  whiteList: 'object',
  popperConfig: '(null|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
  trigger: 'hover focus',
  title: '',
  delay: 0,
  html: false,
  selector: false,
  placement: 'top',
  offset: 0,
  container: false,
  fallbackPlacement: 'flip',
  boundary: 'scrollParent',
  sanitize: true,
  sanitizeFn: null,
87
88
  whiteList: DefaultWhitelist,
  popperConfig: null
Johann-S's avatar
Johann-S committed
89
90
91
}

const Event = {
XhmikosR's avatar
XhmikosR committed
92
93
94
95
96
97
98
99
100
101
  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
102
103
}

XhmikosR's avatar
XhmikosR committed
104
105
106
const CLASS_NAME_FADE = 'fade'
const CLASS_NAME_MODAL = 'modal'
const CLASS_NAME_SHOW = 'show'
Johann-S's avatar
Johann-S committed
107

XhmikosR's avatar
XhmikosR committed
108
109
const HOVER_STATE_SHOW = 'show'
const HOVER_STATE_OUT = 'out'
Johann-S's avatar
Johann-S committed
110

XhmikosR's avatar
XhmikosR committed
111
112
113
114
115
116
const SELECTOR_TOOLTIP_INNER = '.tooltip-inner'

const TRIGGER_HOVER = 'hover'
const TRIGGER_FOCUS = 'focus'
const TRIGGER_CLICK = 'click'
const TRIGGER_MANUAL = 'manual'
117

Johann-S's avatar
Johann-S committed
118
119
120
121
122
/**
 * ------------------------------------------------------------------------
 * Class Definition
 * ------------------------------------------------------------------------
 */
123

Johann-S's avatar
Johann-S committed
124
125
126
class Tooltip {
  constructor(element, config) {
    if (typeof Popper === 'undefined') {
127
      throw new TypeError('Bootstrap\'s tooltips require Popper.js (https://popper.js.org)')
128
129
    }

Johann-S's avatar
Johann-S committed
130
    // private
XhmikosR's avatar
XhmikosR committed
131
132
133
    this._isEnabled = true
    this._timeout = 0
    this._hoverState = ''
Johann-S's avatar
Johann-S committed
134
    this._activeTrigger = {}
XhmikosR's avatar
XhmikosR committed
135
    this._popper = null
136

Johann-S's avatar
Johann-S committed
137
138
    // Protected
    this.element = element
XhmikosR's avatar
XhmikosR committed
139
140
    this.config = this._getConfig(config)
    this.tip = null
fat's avatar
fat committed
141

Johann-S's avatar
Johann-S committed
142
    this._setListeners()
143
    Data.setData(element, this.constructor.DATA_KEY, this)
Johann-S's avatar
Johann-S committed
144
  }
fat's avatar
fat committed
145

Johann-S's avatar
Johann-S committed
146
  // Getters
fat's avatar
fat committed
147

Johann-S's avatar
Johann-S committed
148
149
150
  static get VERSION() {
    return VERSION
  }
fat's avatar
fat committed
151

Johann-S's avatar
Johann-S committed
152
153
154
  static get Default() {
    return Default
  }
fat's avatar
fat committed
155

Johann-S's avatar
Johann-S committed
156
157
158
  static get NAME() {
    return NAME
  }
159

Johann-S's avatar
Johann-S committed
160
161
162
  static get DATA_KEY() {
    return DATA_KEY
  }
163

Johann-S's avatar
Johann-S committed
164
165
166
  static get Event() {
    return Event
  }
167

Johann-S's avatar
Johann-S committed
168
169
170
  static get EVENT_KEY() {
    return EVENT_KEY
  }
171

Johann-S's avatar
Johann-S committed
172
173
174
  static get DefaultType() {
    return DefaultType
  }
175

Johann-S's avatar
Johann-S committed
176
  // Public
177

Johann-S's avatar
Johann-S committed
178
179
180
  enable() {
    this._isEnabled = true
  }
181

Johann-S's avatar
Johann-S committed
182
183
184
  disable() {
    this._isEnabled = false
  }
185

Johann-S's avatar
Johann-S committed
186
187
188
  toggleEnabled() {
    this._isEnabled = !this._isEnabled
  }
Jacob Thornton's avatar
Jacob Thornton committed
189

Johann-S's avatar
Johann-S committed
190
191
192
  toggle(event) {
    if (!this._isEnabled) {
      return
193
194
    }

Johann-S's avatar
Johann-S committed
195
196
    if (event) {
      const dataKey = this.constructor.DATA_KEY
197
      let context = Data.getData(event.delegateTarget, dataKey)
fat's avatar
fat committed
198

Johann-S's avatar
Johann-S committed
199
200
      if (!context) {
        context = new this.constructor(
201
          event.delegateTarget,
Johann-S's avatar
Johann-S committed
202
203
          this._getDelegateConfig()
        )
204
        Data.setData(event.delegateTarget, dataKey, context)
Johann-S's avatar
Johann-S committed
205
      }
fat's avatar
fat committed
206

Johann-S's avatar
Johann-S committed
207
      context._activeTrigger.click = !context._activeTrigger.click
fat's avatar
fat committed
208

Johann-S's avatar
Johann-S committed
209
210
211
212
      if (context._isWithActiveTrigger()) {
        context._enter(null, context)
      } else {
        context._leave(null, context)
fat's avatar
fat committed
213
      }
Johann-S's avatar
Johann-S committed
214
    } else {
XhmikosR's avatar
XhmikosR committed
215
      if (this.getTipElement().classList.contains(CLASS_NAME_SHOW)) {
Johann-S's avatar
Johann-S committed
216
217
        this._leave(null, this)
        return
218
      }
fat's avatar
fat committed
219

Johann-S's avatar
Johann-S committed
220
      this._enter(null, this)
221
    }
Johann-S's avatar
Johann-S committed
222
  }
223

Johann-S's avatar
Johann-S committed
224
225
  dispose() {
    clearTimeout(this._timeout)
226

227
    Data.removeData(this.element, this.constructor.DATA_KEY)
228

229
    EventHandler.off(this.element, this.constructor.EVENT_KEY)
XhmikosR's avatar
XhmikosR committed
230
    EventHandler.off(SelectorEngine.closest(this.element, `.${CLASS_NAME_MODAL}`), 'hide.bs.modal', this._hideModalHandler)
231

Johann-S's avatar
Johann-S committed
232
    if (this.tip) {
233
      this.tip.parentNode.removeChild(this.tip)
Johann-S's avatar
Johann-S committed
234
235
    }

XhmikosR's avatar
XhmikosR committed
236
237
238
    this._isEnabled = null
    this._timeout = null
    this._hoverState = null
Johann-S's avatar
Johann-S committed
239
    this._activeTrigger = null
240
    if (this._popper) {
Johann-S's avatar
Johann-S committed
241
242
      this._popper.destroy()
    }
243

Johann-S's avatar
Johann-S committed
244
245
    this._popper = null
    this.element = null
XhmikosR's avatar
XhmikosR committed
246
247
    this.config = null
    this.tip = null
Johann-S's avatar
Johann-S committed
248
  }
249

Johann-S's avatar
Johann-S committed
250
  show() {
251
    if (this.element.style.display === 'none') {
Johann-S's avatar
Johann-S committed
252
253
      throw new Error('Please use show on visible elements')
    }
254

Johann-S's avatar
Johann-S committed
255
    if (this.isWithContent() && this._isEnabled) {
256
      const showEvent = EventHandler.trigger(this.element, this.constructor.Event.SHOW)
257
      const shadowRoot = findShadowRoot(this.element)
XhmikosR's avatar
XhmikosR committed
258
259
260
      const isInTheDom = shadowRoot === null ?
        this.element.ownerDocument.documentElement.contains(this.element) :
        shadowRoot.contains(this.element)
261

262
      if (showEvent.defaultPrevented || !isInTheDom) {
Johann-S's avatar
Johann-S committed
263
264
        return
      }
265

XhmikosR's avatar
XhmikosR committed
266
      const tip = this.getTipElement()
267
      const tipId = getUID(this.constructor.NAME)
268

Johann-S's avatar
Johann-S committed
269
270
      tip.setAttribute('id', tipId)
      this.element.setAttribute('aria-describedby', tipId)
271

Johann-S's avatar
Johann-S committed
272
      this.setContent()
273

Johann-S's avatar
Johann-S committed
274
      if (this.config.animation) {
XhmikosR's avatar
XhmikosR committed
275
        tip.classList.add(CLASS_NAME_FADE)
Johann-S's avatar
Johann-S committed
276
      }
277

XhmikosR's avatar
XhmikosR committed
278
279
280
      const placement = typeof this.config.placement === 'function' ?
        this.config.placement.call(this, tip, this.element) :
        this.config.placement
281

Johann-S's avatar
Johann-S committed
282
      const attachment = this._getAttachment(placement)
Johann-S's avatar
Johann-S committed
283
      this._addAttachmentClass(attachment)
284

285
      const container = this._getContainer()
286
      Data.setData(tip, this.constructor.DATA_KEY, this)
Johann-S's avatar
Johann-S committed
287

288
289
      if (!this.element.ownerDocument.documentElement.contains(this.tip)) {
        container.appendChild(tip)
Johann-S's avatar
Johann-S committed
290
      }
291

292
      EventHandler.trigger(this.element, this.constructor.Event.INSERTED)
293

294
      this._popper = new Popper(this.element, tip, this._getPopperConfig(attachment))
295

XhmikosR's avatar
XhmikosR committed
296
      tip.classList.add(CLASS_NAME_SHOW)
297

Johann-S's avatar
Johann-S committed
298
299
300
301
302
      // 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) {
303
        [].concat(...document.body.children).forEach(element => {
304
          EventHandler.on(element, 'mouseover', noop())
305
        })
306
307
      }

XhmikosR's avatar
XhmikosR committed
308
      const complete = () => {
Johann-S's avatar
Johann-S committed
309
310
        if (this.config.animation) {
          this._fixTransition()
311
        }
XhmikosR's avatar
XhmikosR committed
312

Johann-S's avatar
Johann-S committed
313
        const prevHoverState = this._hoverState
XhmikosR's avatar
XhmikosR committed
314
        this._hoverState = null
315

316
        EventHandler.trigger(this.element, this.constructor.Event.SHOWN)
317

XhmikosR's avatar
XhmikosR committed
318
        if (prevHoverState === HOVER_STATE_OUT) {
Johann-S's avatar
Johann-S committed
319
          this._leave(null, this)
320
321
322
        }
      }

XhmikosR's avatar
XhmikosR committed
323
      if (this.tip.classList.contains(CLASS_NAME_FADE)) {
324
325
326
        const transitionDuration = getTransitionDurationFromElement(this.tip)
        EventHandler.one(this.tip, TRANSITION_END, complete)
        emulateTransitionEnd(this.tip, transitionDuration)
327
328
329
330
      } else {
        complete()
      }
    }
Johann-S's avatar
Johann-S committed
331
  }
332

Johann-S's avatar
Johann-S committed
333
  hide() {
XhmikosR's avatar
XhmikosR committed
334
335
    const tip = this.getTipElement()
    const complete = () => {
XhmikosR's avatar
XhmikosR committed
336
      if (this._hoverState !== HOVER_STATE_SHOW && tip.parentNode) {
Johann-S's avatar
Johann-S committed
337
        tip.parentNode.removeChild(tip)
338
      }
339

Johann-S's avatar
Johann-S committed
340
341
      this._cleanTipClass()
      this.element.removeAttribute('aria-describedby')
342
      EventHandler.trigger(this.element, this.constructor.Event.HIDDEN)
Johann-S's avatar
Johann-S committed
343
      this._popper.destroy()
344
345
    }

346
347
    const hideEvent = EventHandler.trigger(this.element, this.constructor.Event.HIDE)
    if (hideEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
348
      return
349
350
    }

XhmikosR's avatar
XhmikosR committed
351
    tip.classList.remove(CLASS_NAME_SHOW)
352

Johann-S's avatar
Johann-S committed
353
354
355
    // If this is a touch-enabled device we remove the extra
    // empty mouseover listeners we added for iOS support
    if ('ontouchstart' in document.documentElement) {
356
      [].concat(...document.body.children)
XhmikosR's avatar
XhmikosR committed
357
        .forEach(element => EventHandler.off(element, 'mouseover', noop))
358
359
    }

XhmikosR's avatar
XhmikosR committed
360
361
362
    this._activeTrigger[TRIGGER_CLICK] = false
    this._activeTrigger[TRIGGER_FOCUS] = false
    this._activeTrigger[TRIGGER_HOVER] = false
363

XhmikosR's avatar
XhmikosR committed
364
    if (this.tip.classList.contains(CLASS_NAME_FADE)) {
365
366
367
368
      const transitionDuration = getTransitionDurationFromElement(tip)

      EventHandler.one(tip, TRANSITION_END, complete)
      emulateTransitionEnd(tip, transitionDuration)
Johann-S's avatar
Johann-S committed
369
370
    } else {
      complete()
371
372
    }

Johann-S's avatar
Johann-S committed
373
374
    this._hoverState = ''
  }
375

Johann-S's avatar
Johann-S committed
376
377
378
  update() {
    if (this._popper !== null) {
      this._popper.scheduleUpdate()
fat's avatar
fat committed
379
    }
Johann-S's avatar
Johann-S committed
380
  }
fat's avatar
fat committed
381

Johann-S's avatar
Johann-S committed
382
  // Protected
383

Johann-S's avatar
Johann-S committed
384
385
386
  isWithContent() {
    return Boolean(this.getTitle())
  }
387

Johann-S's avatar
Johann-S committed
388
  getTipElement() {
389
390
391
392
393
394
395
396
    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
397
398
399
400
401
    return this.tip
  }

  setContent() {
    const tip = this.getTipElement()
XhmikosR's avatar
XhmikosR committed
402
403
404
    this.setElementContent(SelectorEngine.findOne(SELECTOR_TOOLTIP_INNER, tip), this.getTitle())
    tip.classList.remove(CLASS_NAME_FADE)
    tip.classList.remove(CLASS_NAME_SHOW)
Johann-S's avatar
Johann-S committed
405
406
  }

407
408
409
410
411
  setElementContent(element, content) {
    if (element === null) {
      return
    }

Johann-S's avatar
Johann-S committed
412
    if (typeof content === 'object' && isElement(content)) {
413
414
415
416
417
      if (content.jquery) {
        content = content[0]
      }

      // content is a DOM node or a jQuery
418
      if (this.config.html) {
419
420
421
        if (content.parentNode !== element) {
          element.innerHTML = ''
          element.appendChild(content)
422
        }
423
      } else {
424
        element.innerText = content.textContent
425
      }
426
427
428
429
430
431
432
433
434

      return
    }

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

435
      element.innerHTML = content
Johann-S's avatar
Johann-S committed
436
    } else {
437
      element.innerText = content
438
    }
Johann-S's avatar
Johann-S committed
439
  }
440

Johann-S's avatar
Johann-S committed
441
442
443
444
  getTitle() {
    let title = this.element.getAttribute('data-original-title')

    if (!title) {
XhmikosR's avatar
XhmikosR committed
445
446
447
      title = typeof this.config.title === 'function' ?
        this.config.title.call(this.element) :
        this.config.title
448
449
    }

Johann-S's avatar
Johann-S committed
450
451
    return title
  }
fat's avatar
fat committed
452

Johann-S's avatar
Johann-S committed
453
  // Private
454

455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
  _getPopperConfig(attachment) {
    const defaultBsConfig = {
      placement: attachment,
      modifiers: {
        offset: this._getOffset(),
        flip: {
          behavior: this.config.fallbackPlacement
        },
        arrow: {
          element: `.${this.constructor.NAME}-arrow`
        },
        preventOverflow: {
          boundariesElement: this.config.boundary
        }
      },
      onCreate: data => {
        if (data.originalPlacement !== data.placement) {
          this._handlePopperPlacementChange(data)
        }
      },
      onUpdate: data => this._handlePopperPlacementChange(data)
    }

478
479
480
    return {
      ...defaultBsConfig,
      ...this.config.popperConfig
481
482
483
    }
  }

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

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

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

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

    return offset
  }

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

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

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

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

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

XhmikosR's avatar
XhmikosR committed
526
    triggers.forEach(trigger => {
Johann-S's avatar
Johann-S committed
527
      if (trigger === 'click') {
528
        EventHandler.on(this.element,
Johann-S's avatar
Johann-S committed
529
530
          this.constructor.Event.CLICK,
          this.config.selector,
XhmikosR's avatar
XhmikosR committed
531
          event => this.toggle(event)
532
        )
XhmikosR's avatar
XhmikosR committed
533
534
      } else if (trigger !== TRIGGER_MANUAL) {
        const eventIn = trigger === TRIGGER_HOVER ?
XhmikosR's avatar
XhmikosR committed
535
536
          this.constructor.Event.MOUSEENTER :
          this.constructor.Event.FOCUSIN
XhmikosR's avatar
XhmikosR committed
537
        const eventOut = trigger === TRIGGER_HOVER ?
XhmikosR's avatar
XhmikosR committed
538
539
          this.constructor.Event.MOUSELEAVE :
          this.constructor.Event.FOCUSOUT
Johann-S's avatar
Johann-S committed
540

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

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

XhmikosR's avatar
XhmikosR committed
560
    EventHandler.on(SelectorEngine.closest(this.element, `.${CLASS_NAME_MODAL}`),
561
      'hide.bs.modal',
562
      this._hideModalHandler
563
564
    )

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

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

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

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

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

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

Johann-S's avatar
Johann-S committed
601
602
    if (event) {
      context._activeTrigger[
XhmikosR's avatar
XhmikosR committed
603
        event.type === 'focusin' ? TRIGGER_FOCUS : TRIGGER_HOVER
Johann-S's avatar
Johann-S committed
604
605
      ] = true
    }
fat's avatar
fat committed
606

XhmikosR's avatar
XhmikosR committed
607
608
609
    if (context.getTipElement().classList.contains(CLASS_NAME_SHOW) ||
        context._hoverState === HOVER_STATE_SHOW) {
      context._hoverState = HOVER_STATE_SHOW
Johann-S's avatar
Johann-S committed
610
611
      return
    }
612

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

XhmikosR's avatar
XhmikosR committed
615
    context._hoverState = HOVER_STATE_SHOW
616

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

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

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

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

Johann-S's avatar
Johann-S committed
641
642
    if (event) {
      context._activeTrigger[
XhmikosR's avatar
XhmikosR committed
643
        event.type === 'focusout' ? TRIGGER_FOCUS : TRIGGER_HOVER
Johann-S's avatar
Johann-S committed
644
645
      ] = false
    }
646

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

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

XhmikosR's avatar
XhmikosR committed
653
    context._hoverState = HOVER_STATE_OUT
654

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

XhmikosR's avatar
XhmikosR committed
762
    tip.classList.remove(CLASS_NAME_FADE)
Johann-S's avatar
Johann-S committed
763
764
765
766
767
768
769
770
    this.config.animation = false
    this.hide()
    this.show()
    this.config.animation = initConfigAnimation
  }

  // Static

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

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

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

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

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

794
  static getInstance(element) {
795
796
    return Data.getData(element, DATA_KEY)
  }
Johann-S's avatar
Johann-S committed
797
798
}

799
800
const $ = getjQuery()

Johann-S's avatar
Johann-S committed
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 ($) {
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