tooltip.js 19.5 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
18
  TRANSITION_END,
  emulateTransitionEnd,
  findShadowRoot,
  getTransitionDurationFromElement,
  getUID,
  isElement,
  makeArray,
  noop,
  typeCheckConfig
19
} from './util/index'
20
21
22
import {
  DefaultWhitelist,
  sanitizeHtml
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'
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
  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)',
59
60
  whiteList: 'object',
  popperConfig: '(null|object)'
Johann-S's avatar
Johann-S committed
61
62
63
}

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

const Default = {
XhmikosR's avatar
XhmikosR committed
72
73
  animation: true,
  template: '<div class="tooltip" role="tooltip">' +
74
                    '<div class="tooltip-arrow"></div>' +
75
                    '<div class="tooltip-inner"></div></div>',
XhmikosR's avatar
XhmikosR committed
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,
88
89
  whiteList: DefaultWhitelist,
  popperConfig: null
Johann-S's avatar
Johann-S committed
90
91
92
}

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

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

const ClassName = {
XhmikosR's avatar
XhmikosR committed
111
  FADE: 'fade',
112
  MODAL: 'modal',
XhmikosR's avatar
XhmikosR committed
113
  SHOW: 'show'
Johann-S's avatar
Johann-S committed
114
115
116
}

const Selector = {
117
  TOOLTIP_INNER: '.tooltip-inner'
Johann-S's avatar
Johann-S committed
118
119
120
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

238
    EventHandler.off(this.element, this.constructor.EVENT_KEY)
239
    EventHandler.off(SelectorEngine.closest(this.element, `.${ClassName.MODAL}`), 'hide.bs.modal', this._hideModalHandler)
240

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

303
      this._popper = new Popper(this.element, tip, this._getPopperConfig(attachment))
304

305
      tip.classList.add(ClassName.SHOW)
306

Johann-S's avatar
Johann-S committed
307
308
309
310
311
      // 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
312
        makeArray(document.body.children).forEach(element => {
313
          EventHandler.on(element, 'mouseover', noop())
314
        })
315
316
      }

XhmikosR's avatar
XhmikosR committed
317
      const complete = () => {
Johann-S's avatar
Johann-S committed
318
319
        if (this.config.animation) {
          this._fixTransition()
320
        }
XhmikosR's avatar
XhmikosR committed
321

Johann-S's avatar
Johann-S committed
322
        const prevHoverState = this._hoverState
XhmikosR's avatar
XhmikosR committed
323
        this._hoverState = null
324

325
        EventHandler.trigger(this.element, this.constructor.Event.SHOWN)
326

Johann-S's avatar
Johann-S committed
327
328
        if (prevHoverState === HoverState.OUT) {
          this._leave(null, this)
329
330
331
        }
      }

332
      if (this.tip.classList.contains(ClassName.FADE)) {
333
334
335
        const transitionDuration = getTransitionDurationFromElement(this.tip)
        EventHandler.one(this.tip, TRANSITION_END, complete)
        emulateTransitionEnd(this.tip, transitionDuration)
336
337
338
339
      } else {
        complete()
      }
    }
Johann-S's avatar
Johann-S committed
340
  }
341

Johann-S's avatar
Johann-S committed
342
  hide() {
XhmikosR's avatar
XhmikosR committed
343
344
    const tip = this.getTipElement()
    const complete = () => {
Johann-S's avatar
Johann-S committed
345
346
      if (this._hoverState !== HoverState.SHOW && tip.parentNode) {
        tip.parentNode.removeChild(tip)
347
      }
348

Johann-S's avatar
Johann-S committed
349
350
      this._cleanTipClass()
      this.element.removeAttribute('aria-describedby')
351
      EventHandler.trigger(this.element, this.constructor.Event.HIDDEN)
Johann-S's avatar
Johann-S committed
352
      this._popper.destroy()
353
354
    }

355
356
    const hideEvent = EventHandler.trigger(this.element, this.constructor.Event.HIDE)
    if (hideEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
357
      return
358
359
    }

360
    tip.classList.remove(ClassName.SHOW)
361

Johann-S's avatar
Johann-S committed
362
363
364
    // If this is a touch-enabled device we remove the extra
    // empty mouseover listeners we added for iOS support
    if ('ontouchstart' in document.documentElement) {
365
      makeArray(document.body.children)
XhmikosR's avatar
XhmikosR committed
366
        .forEach(element => EventHandler.off(element, 'mouseover', noop))
367
368
    }

Johann-S's avatar
Johann-S committed
369
370
371
    this._activeTrigger[Trigger.CLICK] = false
    this._activeTrigger[Trigger.FOCUS] = false
    this._activeTrigger[Trigger.HOVER] = false
372

373
    if (this.tip.classList.contains(ClassName.FADE)) {
374
375
376
377
      const transitionDuration = getTransitionDurationFromElement(tip)

      EventHandler.one(tip, TRANSITION_END, complete)
      emulateTransitionEnd(tip, transitionDuration)
Johann-S's avatar
Johann-S committed
378
379
    } else {
      complete()
380
381
    }

Johann-S's avatar
Johann-S committed
382
383
    this._hoverState = ''
  }
384

Johann-S's avatar
Johann-S committed
385
386
387
  update() {
    if (this._popper !== null) {
      this._popper.scheduleUpdate()
fat's avatar
fat committed
388
    }
Johann-S's avatar
Johann-S committed
389
  }
fat's avatar
fat committed
390

Johann-S's avatar
Johann-S committed
391
  // Protected
392

Johann-S's avatar
Johann-S committed
393
394
395
  isWithContent() {
    return Boolean(this.getTitle())
  }
396

Johann-S's avatar
Johann-S committed
397
  getTipElement() {
398
399
400
401
402
403
404
405
    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
406
407
408
409
410
    return this.tip
  }

  setContent() {
    const tip = this.getTipElement()
411
412
413
    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
414
415
  }

416
417
418
419
420
  setElementContent(element, content) {
    if (element === null) {
      return
    }

Johann-S's avatar
Johann-S committed
421
    if (typeof content === 'object' && isElement(content)) {
422
423
424
425
426
      if (content.jquery) {
        content = content[0]
      }

      // content is a DOM node or a jQuery
427
      if (this.config.html) {
428
429
430
        if (content.parentNode !== element) {
          element.innerHTML = ''
          element.appendChild(content)
431
        }
432
      } else {
433
        element.innerText = content.textContent
434
      }
435
436
437
438
439
440
441
442
443

      return
    }

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

444
      element.innerHTML = content
Johann-S's avatar
Johann-S committed
445
    } else {
446
      element.innerText = content
447
    }
Johann-S's avatar
Johann-S committed
448
  }
449

Johann-S's avatar
Johann-S committed
450
451
452
453
  getTitle() {
    let title = this.element.getAttribute('data-original-title')

    if (!title) {
XhmikosR's avatar
XhmikosR committed
454
455
456
      title = typeof this.config.title === 'function' ?
        this.config.title.call(this.element) :
        this.config.title
457
458
    }

Johann-S's avatar
Johann-S committed
459
460
    return title
  }
fat's avatar
fat committed
461

Johann-S's avatar
Johann-S committed
462
  // Private
463

464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
  _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)
    }

487
488
489
    return {
      ...defaultBsConfig,
      ...this.config.popperConfig
490
491
492
    }
  }

Johann-S's avatar
Johann-S committed
493
494
495
496
  _addAttachmentClass(attachment) {
    this.getTipElement().classList.add(`${CLASS_PREFIX}-${attachment}`)
  }

497
498
499
500
  _getOffset() {
    const offset = {}

    if (typeof this.config.offset === 'function') {
XhmikosR's avatar
XhmikosR committed
501
      offset.fn = data => {
502
503
504
505
506
507
508
509
510
511
512
513
514
515
        data.offsets = {
          ...data.offsets,
          ...this.config.offset(data.offsets, this.element) || {}
        }

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

    return offset
  }

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

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

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

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

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

XhmikosR's avatar
XhmikosR committed
535
    triggers.forEach(trigger => {
Johann-S's avatar
Johann-S committed
536
      if (trigger === 'click') {
537
        EventHandler.on(this.element,
Johann-S's avatar
Johann-S committed
538
539
          this.constructor.Event.CLICK,
          this.config.selector,
XhmikosR's avatar
XhmikosR committed
540
          event => this.toggle(event)
541
        )
Johann-S's avatar
Johann-S committed
542
      } else if (trigger !== Trigger.MANUAL) {
XhmikosR's avatar
XhmikosR committed
543
544
545
546
547
548
        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
549

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

563
564
565
566
567
568
    this._hideModalHandler = () => {
      if (this.element) {
        this.hide()
      }
    }

569
    EventHandler.on(SelectorEngine.closest(this.element, `.${ClassName.MODAL}`),
570
      'hide.bs.modal',
571
      this._hideModalHandler
572
573
    )

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    Object.keys(dataAttributes)
XhmikosR's avatar
XhmikosR committed
690
      .forEach(dataAttr => {
691
692
693
694
695
        if (DISALLOWED_ATTRIBUTES.indexOf(dataAttr) !== -1) {
          delete dataAttributes[dataAttr]
        }
      })

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

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

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

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

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

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

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

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

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

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

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

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

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

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

771
    tip.classList.remove(ClassName.FADE)
Johann-S's avatar
Johann-S committed
772
773
774
775
776
777
778
779
    this.config.animation = false
    this.hide()
    this.show()
    this.config.animation = initConfigAnimation
  }

  // Static

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

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

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

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

Johann-S's avatar
Johann-S committed
798
799
800
        data[config]()
      }
    })
801
  }
802

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

808
809
const $ = getjQuery()

Johann-S's avatar
Johann-S committed
810
811
812
813
/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
814
 * add .tooltip to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
815
 */
Johann-S's avatar
Johann-S committed
816
/* istanbul ignore if */
817
if ($) {
XhmikosR's avatar
XhmikosR committed
818
  const JQUERY_NO_CONFLICT = $.fn[NAME]
819
  $.fn[NAME] = Tooltip.jQueryInterface
XhmikosR's avatar
XhmikosR committed
820
821
  $.fn[NAME].Constructor = Tooltip
  $.fn[NAME].noConflict = () => {
822
    $.fn[NAME] = JQUERY_NO_CONFLICT
823
    return Tooltip.jQueryInterface
824
  }
Johann-S's avatar
Johann-S committed
825
}
826
827

export default Tooltip