tooltip.js 19.5 KB
Newer Older
1
2
/**
 * --------------------------------------------------------------------------
3
 * Bootstrap (v5.0.0-alpha1): tooltip.js
4
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
6
7
 * --------------------------------------------------------------------------
 */

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
import {
20
  DefaultAllowlist,
21
  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
const NAME = 'tooltip'
36
const VERSION = '5.0.0-alpha1'
XhmikosR's avatar
XhmikosR committed
37
38
39
40
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', 'allowList', '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
  allowList: 'object',
59
  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
  allowList: DefaultAllowlist,
88
  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)
230
    EventHandler.off(this.element.closest(`.${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
    this.setElementContent(SelectorEngine.findOne(SELECTOR_TOOLTIP_INNER, tip), this.getTitle())
403
    tip.classList.remove(CLASS_NAME_FADE, CLASS_NAME_SHOW)
Johann-S's avatar
Johann-S committed
404
405
  }

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

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

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

      return
    }

    if (this.config.html) {
      if (this.config.sanitize) {
431
        content = sanitizeHtml(content, this.config.allowList, this.config.sanitizeFn)
432
433
      }

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

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

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

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

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

454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
  _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)
    }

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

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

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

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

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

    return offset
  }

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

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

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

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

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

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

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

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

559
    EventHandler.on(this.element.closest(`.${CLASS_NAME_MODAL}`),
560
      'hide.bs.modal',
561
      this._hideModalHandler
562
563
    )

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

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

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

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

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

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

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

XhmikosR's avatar
XhmikosR committed
606
607
608
    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
609
610
      return
    }
611

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

XhmikosR's avatar
XhmikosR committed
710
    typeCheckConfig(NAME, config, this.constructor.DefaultType)
711

712
    if (config.sanitize) {
713
      config.template = sanitizeHtml(config.template, config.allowList, config.sanitizeFn)
714
715
    }

Johann-S's avatar
Johann-S committed
716
717
    return config
  }
718

Johann-S's avatar
Johann-S committed
719
720
  _getDelegateConfig() {
    const config = {}
721

Johann-S's avatar
Johann-S committed
722
723
724
725
    if (this.config) {
      for (const key in this.config) {
        if (this.constructor.Default[key] !== this.config[key]) {
          config[key] = this.config[key]
726
        }
Johann-S's avatar
Johann-S committed
727
728
      }
    }
729

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

  _cleanTipClass() {
734
735
    const tip = this.getTipElement()
    const tabClass = tip.getAttribute('class').match(BSCLS_PREFIX_REGEX)
736
737
    if (tabClass !== null && tabClass.length > 0) {
      tabClass.map(token => token.trim())
XhmikosR's avatar
XhmikosR committed
738
        .forEach(tClass => tip.classList.remove(tClass))
739
740
741
    }
  }

Johann-S's avatar
Johann-S committed
742
743
744
745
  _handlePopperPlacementChange(popperData) {
    const popperInstance = popperData.instance
    this.tip = popperInstance.popper
    this._cleanTipClass()
Johann-S's avatar
Johann-S committed
746
    this._addAttachmentClass(this._getAttachment(popperData.placement))
Johann-S's avatar
Johann-S committed
747
748
749
750
751
752
753
754
  }

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

XhmikosR's avatar
XhmikosR committed
756
    tip.classList.remove(CLASS_NAME_FADE)
Johann-S's avatar
Johann-S committed
757
758
759
760
761
762
763
764
    this.config.animation = false
    this.hide()
    this.show()
    this.config.animation = initConfigAnimation
  }

  // Static

765
  static jQueryInterface(config) {
Johann-S's avatar
Johann-S committed
766
    return this.each(function () {
XhmikosR's avatar
XhmikosR committed
767
      let data = Data.getData(this, DATA_KEY)
Johann-S's avatar
Johann-S committed
768
769
770
771
772
773
774
775
776
      const _config = typeof config === 'object' && config

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

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

Johann-S's avatar
Johann-S committed
778
779
780
781
      if (typeof config === 'string') {
        if (typeof data[config] === 'undefined') {
          throw new TypeError(`No method named "${config}"`)
        }
XhmikosR's avatar
XhmikosR committed
782

Johann-S's avatar
Johann-S committed
783
784
785
        data[config]()
      }
    })
786
  }
787

788
  static getInstance(element) {
789
790
    return Data.getData(element, DATA_KEY)
  }
Johann-S's avatar
Johann-S committed
791
792
}

793
794
const $ = getjQuery()

Johann-S's avatar
Johann-S committed
795
796
797
798
/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
799
 * add .tooltip to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
800
 */
Johann-S's avatar
Johann-S committed
801
/* istanbul ignore if */
802
if ($) {
XhmikosR's avatar
XhmikosR committed
803
  const JQUERY_NO_CONFLICT = $.fn[NAME]
804
  $.fn[NAME] = Tooltip.jQueryInterface
XhmikosR's avatar
XhmikosR committed
805
806
  $.fn[NAME].Constructor = Tooltip
  $.fn[NAME].noConflict = () => {
807
    $.fn[NAME] = JQUERY_NO_CONFLICT
808
    return Tooltip.jQueryInterface
809
  }
Johann-S's avatar
Johann-S committed
810
}
811
812

export default Tooltip