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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  setContent() {
    const tip = this.getTipElement()
XhmikosR's avatar
XhmikosR committed
403
404
405
    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
406
407
  }

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

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

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

      return
    }

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

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

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

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

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

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

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

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

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

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

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

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

    return offset
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  // Static

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

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

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

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

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

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

800
801
const $ = getjQuery()

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

export default Tooltip