0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-01 13:24:25 +01:00
Bootstrap/js/src/tooltip.js

824 lines
20 KiB
JavaScript
Raw Normal View History

/**
* --------------------------------------------------------------------------
2019-02-13 17:01:40 +01:00
* Bootstrap (v4.3.1): tooltip.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
import {
jQuery as $,
TRANSITION_END,
emulateTransitionEnd,
findShadowRoot,
getTransitionDurationFromElement,
getUID,
isElement,
makeArray,
noop,
typeCheckConfig
} from './util/index'
import {
DefaultWhitelist,
sanitizeHtml
} from './util/sanitizer'
2017-09-21 18:04:47 +02:00
import Data from './dom/data'
import EventHandler from './dom/eventHandler'
import Manipulator from './dom/manipulator'
import Popper from 'popper.js'
2017-09-21 18:04:47 +02:00
import SelectorEngine from './dom/selectorEngine'
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
const NAME = 'tooltip'
2019-02-13 17:01:40 +01:00
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')
const DISALLOWED_ATTRIBUTES = ['sanitize', 'whiteList', 'sanitizeFn']
2018-09-26 10:39:01 +02:00
2017-09-21 18:04:47 +02:00
2018-09-26 10:39:01 +02:00
const DefaultType = {
animation : 'boolean',
template : 'string',
title : '(string|element|function)',
trigger : 'string',
delay : '(number|object)',
html : 'boolean',
selector : '(string|boolean)',
placement : '(string|function)',
offset : '(number|string|function)',
container : '(string|element|boolean)',
fallbackPlacement : '(string|array)',
boundary : '(string|element)',
sanitize : 'boolean',
sanitizeFn : '(null|function)',
whiteList : 'object'
2018-09-26 10:39:01 +02:00
}
const AttachmentMap = {
AUTO : 'auto',
TOP : 'top',
RIGHT : 'right',
BOTTOM : 'bottom',
LEFT : 'left'
}
const Default = {
animation : true,
template : '<div class="tooltip" role="tooltip">' +
2019-02-11 11:27:14 +01:00
'<div class="tooltip-arrow"></div>' +
'<div class="tooltip-inner"></div></div>',
trigger : 'hover focus',
title : '',
delay : 0,
html : false,
selector : false,
placement : 'top',
offset : 0,
container : false,
fallbackPlacement : 'flip',
boundary : 'scrollParent',
sanitize : true,
sanitizeFn : null,
whiteList : DefaultWhitelist
2018-09-26 10:39:01 +02:00
}
const HoverState = {
SHOW : 'show',
OUT : 'out'
}
const Event = {
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}`
}
const ClassName = {
FADE : 'fade',
SHOW : 'show'
}
const Selector = {
TOOLTIP : '.tooltip',
TOOLTIP_INNER : '.tooltip-inner',
2019-02-11 11:27:14 +01:00
TOOLTIP_ARROW : '.tooltip-arrow'
2018-09-26 10:39:01 +02:00
}
const Trigger = {
HOVER : 'hover',
FOCUS : 'focus',
CLICK : 'click',
MANUAL : 'manual'
}
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2018-09-26 10:39:01 +02:00
class Tooltip {
constructor(element, config) {
/**
* Check for Popper dependency
* Popper - https://popper.js.org
*/
if (typeof Popper === 'undefined') {
2018-09-14 14:27:30 +02:00
throw new TypeError('Bootstrap\'s tooltips require Popper.js (https://popper.js.org)')
}
2018-09-26 10:39:01 +02:00
// private
this._isEnabled = true
this._timeout = 0
this._hoverState = ''
this._activeTrigger = {}
this._popper = null
2018-09-26 10:39:01 +02:00
// Protected
this.element = element
this.config = this._getConfig(config)
this.tip = null
2015-05-12 23:28:11 +02:00
2018-09-26 10:39:01 +02:00
this._setListeners()
Data.setData(element, this.constructor.DATA_KEY, this)
2018-09-26 10:39:01 +02:00
}
2015-05-12 23:28:11 +02:00
2018-09-26 10:39:01 +02:00
// Getters
2015-05-12 23:28:11 +02:00
2018-09-26 10:39:01 +02:00
static get VERSION() {
return VERSION
}
2015-05-12 23:28:11 +02:00
2018-09-26 10:39:01 +02:00
static get Default() {
return Default
}
2015-05-13 23:46:50 +02:00
2018-09-26 10:39:01 +02:00
static get NAME() {
return NAME
}
2018-09-26 10:39:01 +02:00
static get DATA_KEY() {
return DATA_KEY
}
2018-09-26 10:39:01 +02:00
static get Event() {
return Event
}
2018-09-26 10:39:01 +02:00
static get EVENT_KEY() {
return EVENT_KEY
}
2018-09-26 10:39:01 +02:00
static get DefaultType() {
return DefaultType
}
2018-09-26 10:39:01 +02:00
// Public
2018-09-26 10:39:01 +02:00
enable() {
this._isEnabled = true
}
2018-09-26 10:39:01 +02:00
disable() {
this._isEnabled = false
}
2018-09-26 10:39:01 +02:00
toggleEnabled() {
this._isEnabled = !this._isEnabled
}
2015-08-19 04:22:46 +02:00
2018-09-26 10:39:01 +02:00
toggle(event) {
if (!this._isEnabled) {
return
}
2018-09-26 10:39:01 +02:00
if (event) {
const dataKey = this.constructor.DATA_KEY
2017-09-21 18:04:47 +02:00
let context = Data.getData(event.delegateTarget, dataKey)
2015-05-12 23:28:11 +02:00
2018-09-26 10:39:01 +02:00
if (!context) {
context = new this.constructor(
2018-09-14 14:27:30 +02:00
event.delegateTarget,
2018-09-26 10:39:01 +02:00
this._getDelegateConfig()
)
2017-09-21 18:04:47 +02:00
Data.setData(event.delegateTarget, dataKey, context)
2018-09-26 10:39:01 +02:00
}
2015-05-12 23:28:11 +02:00
2018-09-26 10:39:01 +02:00
context._activeTrigger.click = !context._activeTrigger.click
2015-05-13 21:48:34 +02:00
2018-09-26 10:39:01 +02:00
if (context._isWithActiveTrigger()) {
context._enter(null, context)
} else {
context._leave(null, context)
2015-05-13 21:48:34 +02:00
}
2018-09-26 10:39:01 +02:00
} else {
2017-09-21 18:04:47 +02:00
if (this.getTipElement().classList.contains(ClassName.SHOW)) {
2018-09-26 10:39:01 +02:00
this._leave(null, this)
return
}
2015-05-13 21:48:34 +02:00
2018-09-26 10:39:01 +02:00
this._enter(null, this)
}
2018-09-26 10:39:01 +02:00
}
2018-09-26 10:39:01 +02:00
dispose() {
clearTimeout(this._timeout)
2017-09-21 18:04:47 +02:00
Data.removeData(this.element, this.constructor.DATA_KEY)
2017-09-21 18:04:47 +02:00
EventHandler.off(this.element, this.constructor.EVENT_KEY)
EventHandler.off(SelectorEngine.closest(this.element, '.modal'), 'hide.bs.modal')
2018-09-26 10:39:01 +02:00
if (this.tip) {
2017-09-21 18:04:47 +02:00
this.tip.parentNode.removeChild(this.tip)
2018-09-26 10:39:01 +02:00
}
this._isEnabled = null
this._timeout = null
this._hoverState = null
this._activeTrigger = null
if (this._popper !== null) {
this._popper.destroy()
}
2018-09-26 10:39:01 +02:00
this._popper = null
this.element = null
this.config = null
this.tip = null
}
2018-09-26 10:39:01 +02:00
show() {
2017-09-21 18:04:47 +02:00
if (this.element.style.display === 'none') {
2018-09-26 10:39:01 +02:00
throw new Error('Please use show on visible elements')
}
2018-09-26 10:39:01 +02:00
if (this.isWithContent() && this._isEnabled) {
2017-09-21 18:04:47 +02:00
const showEvent = EventHandler.trigger(this.element, this.constructor.Event.SHOW)
const shadowRoot = findShadowRoot(this.element)
2017-09-21 18:04:47 +02:00
const isInTheDom = shadowRoot !== null
? shadowRoot.contains(this.element)
: this.element.ownerDocument.documentElement.contains(this.element)
2017-09-21 18:04:47 +02:00
if (showEvent.defaultPrevented || !isInTheDom) {
2018-09-26 10:39:01 +02:00
return
}
2018-09-26 10:39:01 +02:00
const tip = this.getTipElement()
const tipId = getUID(this.constructor.NAME)
2018-09-26 10:39:01 +02:00
tip.setAttribute('id', tipId)
this.element.setAttribute('aria-describedby', tipId)
2018-09-26 10:39:01 +02:00
this.setContent()
2018-09-26 10:39:01 +02:00
if (this.config.animation) {
2017-09-21 18:04:47 +02:00
tip.classList.add(ClassName.FADE)
2018-09-26 10:39:01 +02:00
}
2018-09-26 10:39:01 +02:00
const placement = typeof this.config.placement === 'function'
? this.config.placement.call(this, tip, this.element)
: this.config.placement
2018-09-26 10:39:01 +02:00
const attachment = this._getAttachment(placement)
this.addAttachmentClass(attachment)
const container = this._getContainer()
2017-09-21 18:04:47 +02:00
Data.setData(tip, this.constructor.DATA_KEY, this)
2018-09-26 10:39:01 +02:00
2017-09-21 18:04:47 +02:00
if (!this.element.ownerDocument.documentElement.contains(this.tip)) {
container.appendChild(tip)
2018-09-26 10:39:01 +02:00
}
2017-09-21 18:04:47 +02:00
EventHandler.trigger(this.element, this.constructor.Event.INSERTED)
2018-09-26 10:39:01 +02:00
this._popper = new Popper(this.element, tip, {
placement: attachment,
modifiers: {
offset: this._getOffset(),
2018-09-26 10:39:01 +02:00
flip: {
behavior: this.config.fallbackPlacement
},
arrow: {
2019-02-11 11:27:14 +01:00
element: Selector.TOOLTIP_ARROW
2018-09-26 10:39:01 +02:00
},
preventOverflow: {
boundariesElement: this.config.boundary
}
},
onCreate: (data) => {
if (data.originalPlacement !== data.placement) {
this._handlePopperPlacementChange(data)
}
2018-09-26 10:39:01 +02:00
},
onUpdate: (data) => this._handlePopperPlacementChange(data)
2018-09-26 10:39:01 +02:00
})
2017-09-21 18:04:47 +02:00
tip.classList.add(ClassName.SHOW)
2018-03-13 09:59:20 +01:00
2018-09-26 10:39:01 +02:00
// 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) {
makeArray(document.body.children).forEach((element) => {
EventHandler.on(element, 'mouseover', noop())
2017-09-21 18:04:47 +02:00
})
}
2017-12-16 13:00:38 +01:00
const complete = () => {
2018-09-26 10:39:01 +02:00
if (this.config.animation) {
this._fixTransition()
}
2018-09-26 10:39:01 +02:00
const prevHoverState = this._hoverState
this._hoverState = null
2017-09-21 18:04:47 +02:00
EventHandler.trigger(this.element, this.constructor.Event.SHOWN)
2018-09-26 10:39:01 +02:00
if (prevHoverState === HoverState.OUT) {
this._leave(null, this)
}
}
2017-09-21 18:04:47 +02:00
if (this.tip.classList.contains(ClassName.FADE)) {
const transitionDuration = getTransitionDurationFromElement(this.tip)
EventHandler.one(this.tip, TRANSITION_END, complete)
emulateTransitionEnd(this.tip, transitionDuration)
} else {
complete()
}
}
2018-09-26 10:39:01 +02:00
}
2018-09-26 10:39:01 +02:00
hide(callback) {
const tip = this.getTipElement()
2017-09-21 18:04:47 +02:00
const complete = () => {
2018-09-26 10:39:01 +02:00
if (this._hoverState !== HoverState.SHOW && tip.parentNode) {
tip.parentNode.removeChild(tip)
}
2018-09-26 10:39:01 +02:00
this._cleanTipClass()
this.element.removeAttribute('aria-describedby')
2017-09-21 18:04:47 +02:00
EventHandler.trigger(this.element, this.constructor.Event.HIDDEN)
2018-09-26 10:39:01 +02:00
if (this._popper !== null) {
this._popper.destroy()
}
2018-09-26 10:39:01 +02:00
if (callback) {
callback()
}
}
2017-09-21 18:04:47 +02:00
const hideEvent = EventHandler.trigger(this.element, this.constructor.Event.HIDE)
if (hideEvent.defaultPrevented) {
2018-09-26 10:39:01 +02:00
return
}
2017-09-21 18:04:47 +02:00
tip.classList.remove(ClassName.SHOW)
2018-09-26 10:39:01 +02:00
// If this is a touch-enabled device we remove the extra
// empty mouseover listeners we added for iOS support
if ('ontouchstart' in document.documentElement) {
makeArray(document.body.children)
.forEach((element) => EventHandler.off(element, 'mouseover', noop))
}
2018-09-26 10:39:01 +02:00
this._activeTrigger[Trigger.CLICK] = false
this._activeTrigger[Trigger.FOCUS] = false
this._activeTrigger[Trigger.HOVER] = false
2017-09-21 18:04:47 +02:00
if (this.tip.classList.contains(ClassName.FADE)) {
const transitionDuration = getTransitionDurationFromElement(tip)
EventHandler.one(tip, TRANSITION_END, complete)
emulateTransitionEnd(tip, transitionDuration)
2018-09-26 10:39:01 +02:00
} else {
complete()
}
2018-09-26 10:39:01 +02:00
this._hoverState = ''
}
2018-09-26 10:39:01 +02:00
update() {
if (this._popper !== null) {
this._popper.scheduleUpdate()
2015-05-12 23:28:11 +02:00
}
2018-09-26 10:39:01 +02:00
}
2015-05-12 23:28:11 +02:00
2018-09-26 10:39:01 +02:00
// Protected
2018-09-26 10:39:01 +02:00
isWithContent() {
return Boolean(this.getTitle())
}
2018-09-26 10:39:01 +02:00
addAttachmentClass(attachment) {
2017-09-21 18:04:47 +02:00
this.getTipElement().classList.add(`${CLASS_PREFIX}-${attachment}`)
2018-09-26 10:39:01 +02:00
}
2018-09-26 10:39:01 +02:00
getTipElement() {
2017-09-21 18:04:47 +02:00
if (this.tip) {
return this.tip
}
const element = document.createElement('div')
element.innerHTML = this.config.template
this.tip = element.children[0]
2018-09-26 10:39:01 +02:00
return this.tip
}
setContent() {
const tip = this.getTipElement()
2017-09-21 18:04:47 +02:00
this.setElementContent(SelectorEngine.findOne(Selector.TOOLTIP_INNER, tip), this.getTitle())
tip.classList.remove(ClassName.FADE)
tip.classList.remove(ClassName.SHOW)
2018-09-26 10:39:01 +02:00
}
2017-09-21 18:04:47 +02:00
setElementContent(element, content) {
if (element === null) {
return
}
2018-09-26 10:39:01 +02:00
if (typeof content === 'object' && (content.nodeType || content.jquery)) {
2017-09-21 18:04:47 +02:00
if (content.jquery) {
content = content[0]
}
// content is a DOM node or a jQuery
if (this.config.html) {
2017-09-21 18:04:47 +02:00
if (content.parentNode !== element) {
element.innerHTML = ''
element.appendChild(content)
}
} else {
2017-09-21 18:04:47 +02:00
element.innerText = content.textContent
}
return
}
if (this.config.html) {
if (this.config.sanitize) {
content = sanitizeHtml(content, this.config.whiteList, this.config.sanitizeFn)
}
2017-09-21 18:04:47 +02:00
element.innerHTML = content
2018-09-26 10:39:01 +02:00
} else {
2017-09-21 18:04:47 +02:00
element.innerText = content
}
2018-09-26 10:39:01 +02:00
}
2018-09-26 10:39:01 +02:00
getTitle() {
let title = this.element.getAttribute('data-original-title')
if (!title) {
title = typeof this.config.title === 'function'
? this.config.title.call(this.element)
: this.config.title
}
2018-09-26 10:39:01 +02:00
return title
}
2015-05-12 23:28:11 +02:00
2018-09-26 10:39:01 +02:00
// Private
_getOffset() {
const offset = {}
if (typeof this.config.offset === 'function') {
offset.fn = (data) => {
data.offsets = {
...data.offsets,
...this.config.offset(data.offsets, this.element) || {}
}
return data
}
} else {
offset.offset = this.config.offset
}
return offset
}
_getContainer() {
if (this.config.container === false) {
return document.body
}
if (isElement(this.config.container)) {
2017-09-21 18:04:47 +02:00
return this.config.container
}
2017-09-21 18:04:47 +02:00
return SelectorEngine.findOne(this.config.container)
}
2018-09-26 10:39:01 +02:00
_getAttachment(placement) {
return AttachmentMap[placement.toUpperCase()]
}
_setListeners() {
const triggers = this.config.trigger.split(' ')
triggers.forEach((trigger) => {
if (trigger === 'click') {
2017-09-21 18:04:47 +02:00
EventHandler.on(this.element,
2018-09-26 10:39:01 +02:00
this.constructor.Event.CLICK,
this.config.selector,
(event) => this.toggle(event)
)
2018-09-26 10:39:01 +02:00
} else if (trigger !== Trigger.MANUAL) {
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
2017-09-21 18:04:47 +02:00
EventHandler.on(this.element,
eventIn,
this.config.selector,
(event) => this._enter(event)
)
EventHandler.on(this.element,
eventOut,
this.config.selector,
(event) => this._leave(event)
)
}
2018-09-26 10:39:01 +02:00
})
2017-09-21 18:04:47 +02:00
EventHandler.on(SelectorEngine.closest(this.element, '.modal'),
'hide.bs.modal',
() => {
if (this.element) {
this.hide()
}
}
)
2018-09-26 10:39:01 +02:00
if (this.config.selector) {
this.config = {
...this.config,
trigger: 'manual',
selector: ''
}
2018-09-26 10:39:01 +02:00
} else {
this._fixTitle()
}
}
2018-09-26 10:39:01 +02:00
_fixTitle() {
const titleType = typeof this.element.getAttribute('data-original-title')
if (this.element.getAttribute('title') || titleType !== 'string') {
2018-09-26 10:39:01 +02:00
this.element.setAttribute(
'data-original-title',
this.element.getAttribute('title') || ''
)
2018-09-26 10:39:01 +02:00
this.element.setAttribute('title', '')
}
}
2018-09-26 10:39:01 +02:00
_enter(event, context) {
const dataKey = this.constructor.DATA_KEY
2017-09-21 18:04:47 +02:00
context = context || Data.getData(event.delegateTarget, dataKey)
2018-09-26 10:39:01 +02:00
if (!context) {
context = new this.constructor(
2017-09-21 18:04:47 +02:00
event.delegateTarget,
2018-09-26 10:39:01 +02:00
this._getDelegateConfig()
)
2017-09-21 18:04:47 +02:00
Data.setData(event.delegateTarget, dataKey, context)
}
2018-09-26 10:39:01 +02:00
if (event) {
context._activeTrigger[
event.type === 'focusin' ? Trigger.FOCUS : Trigger.HOVER
] = true
}
2015-05-12 23:28:11 +02:00
2017-09-21 18:04:47 +02:00
if (context.getTipElement().classList.contains(ClassName.SHOW) ||
context._hoverState === HoverState.SHOW) {
2018-09-26 10:39:01 +02:00
context._hoverState = HoverState.SHOW
return
}
2018-09-26 10:39:01 +02:00
clearTimeout(context._timeout)
2018-09-26 10:39:01 +02:00
context._hoverState = HoverState.SHOW
2018-09-26 10:39:01 +02:00
if (!context.config.delay || !context.config.delay.show) {
context.show()
return
}
2018-09-26 10:39:01 +02:00
context._timeout = setTimeout(() => {
if (context._hoverState === HoverState.SHOW) {
context.show()
}
}, context.config.delay.show)
}
2018-09-26 10:39:01 +02:00
_leave(event, context) {
const dataKey = this.constructor.DATA_KEY
2017-09-21 18:04:47 +02:00
context = context || Data.getData(event.delegateTarget, dataKey)
2018-09-26 10:39:01 +02:00
if (!context) {
context = new this.constructor(
2017-09-21 18:04:47 +02:00
event.delegateTarget,
2018-09-26 10:39:01 +02:00
this._getDelegateConfig()
)
2017-09-21 18:04:47 +02:00
Data.setData(event.delegateTarget, dataKey, context)
}
2018-09-26 10:39:01 +02:00
if (event) {
context._activeTrigger[
event.type === 'focusout' ? Trigger.FOCUS : Trigger.HOVER
] = false
}
2018-09-26 10:39:01 +02:00
if (context._isWithActiveTrigger()) {
return
}
2018-09-26 10:39:01 +02:00
clearTimeout(context._timeout)
2018-09-26 10:39:01 +02:00
context._hoverState = HoverState.OUT
2018-09-26 10:39:01 +02:00
if (!context.config.delay || !context.config.delay.hide) {
context.hide()
return
}
2018-09-26 10:39:01 +02:00
context._timeout = setTimeout(() => {
if (context._hoverState === HoverState.OUT) {
context.hide()
}
2018-09-26 10:39:01 +02:00
}, context.config.delay.hide)
}
2018-09-26 10:39:01 +02:00
_isWithActiveTrigger() {
for (const trigger in this._activeTrigger) {
if (this._activeTrigger[trigger]) {
return true
}
}
2018-09-26 10:39:01 +02:00
return false
}
2018-09-26 10:39:01 +02:00
_getConfig(config) {
const dataAttributes = Manipulator.getDataAttributes(this.element)
Object.keys(dataAttributes)
.forEach((dataAttr) => {
if (DISALLOWED_ATTRIBUTES.indexOf(dataAttr) !== -1) {
delete dataAttributes[dataAttr]
}
})
2018-07-25 11:29:16 +02:00
if (config && typeof config.container === 'object' && config.container.jquery) {
2017-09-21 18:04:47 +02:00
config.container = config.container[0]
}
2018-09-26 10:39:01 +02:00
config = {
...this.constructor.Default,
...dataAttributes,
2018-09-26 10:39:01 +02:00
...typeof config === 'object' && config ? config : {}
}
2018-09-26 10:39:01 +02:00
if (typeof config.delay === 'number') {
config.delay = {
show: config.delay,
hide: config.delay
2017-04-08 21:13:15 +02:00
}
2017-04-07 13:20:34 +02:00
}
2018-09-26 10:39:01 +02:00
if (typeof config.title === 'number') {
config.title = config.title.toString()
}
2018-09-26 10:39:01 +02:00
if (typeof config.content === 'number') {
config.content = config.content.toString()
}
typeCheckConfig(
2018-09-26 10:39:01 +02:00
NAME,
config,
this.constructor.DefaultType
)
if (config.sanitize) {
config.template = sanitizeHtml(config.template, config.whiteList, config.sanitizeFn)
}
2018-09-26 10:39:01 +02:00
return config
}
2018-09-26 10:39:01 +02:00
_getDelegateConfig() {
const config = {}
2018-09-26 10:39:01 +02:00
if (this.config) {
for (const key in this.config) {
if (this.constructor.Default[key] !== this.config[key]) {
config[key] = this.config[key]
}
2018-09-26 10:39:01 +02:00
}
}
2018-09-26 10:39:01 +02:00
return config
}
_cleanTipClass() {
2017-09-21 18:04:47 +02:00
const tip = this.getTipElement()
const tabClass = tip.getAttribute('class').match(BSCLS_PREFIX_REGEX)
2018-09-26 10:39:01 +02:00
if (tabClass !== null && tabClass.length) {
2017-09-21 18:04:47 +02:00
tabClass
.map((token) => token.trim())
.forEach((tClass) => tip.classList.remove(tClass))
}
}
2018-09-26 10:39:01 +02:00
_handlePopperPlacementChange(popperData) {
const popperInstance = popperData.instance
this.tip = popperInstance.popper
this._cleanTipClass()
this.addAttachmentClass(this._getAttachment(popperData.placement))
}
_fixTransition() {
const tip = this.getTipElement()
const initConfigAnimation = this.config.animation
if (tip.getAttribute('x-placement') !== null) {
return
}
2017-09-21 18:04:47 +02:00
tip.classList.remove(ClassName.FADE)
2018-09-26 10:39:01 +02:00
this.config.animation = false
this.hide()
this.show()
this.config.animation = initConfigAnimation
}
// Static
static _jQueryInterface(config) {
return this.each(function () {
2017-09-21 18:04:47 +02:00
let data = Data.getData(this, DATA_KEY)
2018-09-26 10:39:01 +02:00
const _config = typeof config === 'object' && config
if (!data && /dispose|hide/.test(config)) {
return
}
if (!data) {
data = new Tooltip(this, _config)
}
2018-09-26 10:39:01 +02:00
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
data[config]()
}
})
}
static _getInstance(element) {
return Data.getData(element, DATA_KEY)
}
2018-09-26 10:39:01 +02:00
}
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
* add .tooltip to jQuery only if jQuery is present
2018-09-26 10:39:01 +02:00
*/
2017-09-21 18:04:47 +02:00
if (typeof $ !== 'undefined') {
const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Tooltip._jQueryInterface
$.fn[NAME].Constructor = Tooltip
$.fn[NAME].noConflict = () => {
$.fn[NAME] = JQUERY_NO_CONFLICT
return Tooltip._jQueryInterface
}
2018-09-26 10:39:01 +02:00
}
export default Tooltip