2015-05-12 08:32:37 +02:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2021-10-09 08:33:12 +02:00
|
|
|
* Bootstrap (v5.1.3): tooltip.js
|
2020-06-16 20:41:47 +02:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
2015-05-12 08:32:37 +02:00
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2020-06-19 10:17:01 +02:00
|
|
|
import * as Popper from '@popperjs/core'
|
2019-02-22 23:37:55 +01:00
|
|
|
import {
|
2020-12-08 07:16:50 +01:00
|
|
|
defineJQueryPlugin,
|
2019-02-22 23:37:55 +01:00
|
|
|
findShadowRoot,
|
2021-05-13 17:17:20 +02:00
|
|
|
getElement,
|
2019-02-22 23:37:55 +01:00
|
|
|
getUID,
|
2020-06-26 16:06:20 +02:00
|
|
|
isRTL,
|
2019-02-22 23:37:55 +01:00
|
|
|
noop,
|
|
|
|
typeCheckConfig
|
2019-10-02 11:43:54 +02:00
|
|
|
} from './util/index'
|
2021-11-25 18:14:02 +01:00
|
|
|
import { DefaultAllowlist } from './util/sanitizer'
|
2019-10-02 11:43:54 +02:00
|
|
|
import Data from './dom/data'
|
|
|
|
import EventHandler from './dom/event-handler'
|
|
|
|
import Manipulator from './dom/manipulator'
|
2019-09-04 16:58:29 +02:00
|
|
|
import BaseComponent from './base-component'
|
2021-11-25 18:14:02 +01:00
|
|
|
import TemplateFactory from './util/template-factory'
|
2018-11-14 10:16:56 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2019-02-26 12:20:34 +01:00
|
|
|
const NAME = 'tooltip'
|
|
|
|
const DATA_KEY = 'bs.tooltip'
|
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
|
|
|
const CLASS_PREFIX = 'bs-tooltip'
|
2020-05-02 15:56:23 +02:00
|
|
|
const DISALLOWED_ATTRIBUTES = new Set(['sanitize', 'allowList', 'sanitizeFn'])
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2021-10-13 14:19:28 +02:00
|
|
|
const CLASS_NAME_FADE = 'fade'
|
|
|
|
const CLASS_NAME_MODAL = 'modal'
|
|
|
|
const CLASS_NAME_SHOW = 'show'
|
|
|
|
|
|
|
|
const HOVER_STATE_SHOW = 'show'
|
|
|
|
const HOVER_STATE_OUT = 'out'
|
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
const SELECTOR_TOOLTIP_ARROW = '.tooltip-arrow'
|
2021-10-13 14:19:28 +02:00
|
|
|
const SELECTOR_TOOLTIP_INNER = '.tooltip-inner'
|
|
|
|
const SELECTOR_MODAL = `.${CLASS_NAME_MODAL}`
|
|
|
|
|
|
|
|
const EVENT_MODAL_HIDE = 'hide.bs.modal'
|
|
|
|
|
|
|
|
const TRIGGER_HOVER = 'hover'
|
|
|
|
const TRIGGER_FOCUS = 'focus'
|
|
|
|
const TRIGGER_CLICK = 'click'
|
|
|
|
const TRIGGER_MANUAL = 'manual'
|
2018-09-26 10:39:01 +02:00
|
|
|
|
|
|
|
const AttachmentMap = {
|
2019-02-26 12:20:34 +01:00
|
|
|
AUTO: 'auto',
|
|
|
|
TOP: 'top',
|
2021-02-16 09:14:05 +01:00
|
|
|
RIGHT: isRTL() ? 'left' : 'right',
|
2019-02-26 12:20:34 +01:00
|
|
|
BOTTOM: 'bottom',
|
2021-02-16 09:14:05 +01:00
|
|
|
LEFT: isRTL() ? 'right' : 'left'
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Default = {
|
2019-02-26 12:20:34 +01:00
|
|
|
animation: true,
|
|
|
|
template: '<div class="tooltip" role="tooltip">' +
|
2020-12-02 05:45:15 +01:00
|
|
|
'<div class="tooltip-arrow"></div>' +
|
|
|
|
'<div class="tooltip-inner"></div>' +
|
|
|
|
'</div>',
|
2019-02-26 12:20:34 +01:00
|
|
|
trigger: 'hover focus',
|
|
|
|
title: '',
|
|
|
|
delay: 0,
|
|
|
|
html: false,
|
|
|
|
selector: false,
|
|
|
|
placement: 'top',
|
2020-12-16 19:07:27 +01:00
|
|
|
offset: [0, 0],
|
2019-02-26 12:20:34 +01:00
|
|
|
container: false,
|
2020-12-14 19:51:14 +01:00
|
|
|
fallbackPlacements: ['top', 'right', 'bottom', 'left'],
|
2020-06-19 10:17:01 +02:00
|
|
|
boundary: 'clippingParents',
|
2020-11-25 08:16:22 +01:00
|
|
|
customClass: '',
|
2019-02-26 12:20:34 +01:00
|
|
|
sanitize: true,
|
|
|
|
sanitizeFn: null,
|
2020-06-19 10:31:37 +02:00
|
|
|
allowList: DefaultAllowlist,
|
2019-08-14 17:27:58 +02:00
|
|
|
popperConfig: null
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 14:19:28 +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: '(array|string|function)',
|
|
|
|
container: '(string|element|boolean)',
|
|
|
|
fallbackPlacements: 'array',
|
|
|
|
boundary: '(string|element)',
|
|
|
|
customClass: '(string|function)',
|
|
|
|
sanitize: 'boolean',
|
|
|
|
sanitizeFn: '(null|function)',
|
|
|
|
allowList: 'object',
|
|
|
|
popperConfig: '(null|object|function)'
|
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
const Event = {
|
2019-02-26 12:20:34 +01:00
|
|
|
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}`
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-13 14:19:28 +02:00
|
|
|
* Class definition
|
2018-09-26 10:39:01 +02:00
|
|
|
*/
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2019-09-04 16:58:29 +02:00
|
|
|
class Tooltip extends BaseComponent {
|
2018-09-26 10:39:01 +02:00
|
|
|
constructor(element, config) {
|
|
|
|
if (typeof Popper === 'undefined') {
|
2020-11-21 15:22:08 +01:00
|
|
|
throw new TypeError('Bootstrap\'s tooltips require Popper (https://popper.js.org)')
|
2015-05-12 08:32:37 +02:00
|
|
|
}
|
|
|
|
|
2019-09-04 16:58:29 +02:00
|
|
|
super(element)
|
|
|
|
|
2021-10-13 14:19:28 +02:00
|
|
|
// Private
|
2019-02-26 12:20:34 +01:00
|
|
|
this._isEnabled = true
|
|
|
|
this._timeout = 0
|
|
|
|
this._hoverState = ''
|
2018-09-26 10:39:01 +02:00
|
|
|
this._activeTrigger = {}
|
2019-02-26 12:20:34 +01:00
|
|
|
this._popper = null
|
2021-11-25 18:14:02 +01:00
|
|
|
this._templateFactory = null
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Protected
|
2021-05-12 11:15:59 +02:00
|
|
|
this._config = this._getConfig(config)
|
2019-02-26 12:20:34 +01:00
|
|
|
this.tip = null
|
2015-05-12 23:28:11 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
this._setListeners()
|
|
|
|
}
|
2015-05-12 23:28:11 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Getters
|
|
|
|
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
|
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
static get Event() {
|
|
|
|
return Event
|
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
static get DefaultType() {
|
|
|
|
return DefaultType
|
|
|
|
}
|
2017-08-24 09:40:11 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Public
|
|
|
|
enable() {
|
|
|
|
this._isEnabled = true
|
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
disable() {
|
|
|
|
this._isEnabled = false
|
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
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
|
2015-05-12 08:32:37 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (event) {
|
2021-01-27 18:01:24 +01:00
|
|
|
const context = this._initializeOnDelegatedTarget(event)
|
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 {
|
2020-03-07 10:31:42 +01:00
|
|
|
if (this.getTipElement().classList.contains(CLASS_NAME_SHOW)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
this._leave(null, this)
|
|
|
|
return
|
2017-04-19 15:08:06 +02:00
|
|
|
}
|
2015-05-13 21:48:34 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
this._enter(null, this)
|
2015-05-12 08:32:37 +02:00
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
dispose() {
|
|
|
|
clearTimeout(this._timeout)
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-07-30 00:32:07 +02:00
|
|
|
EventHandler.off(this._element.closest(SELECTOR_MODAL), EVENT_MODAL_HIDE, this._hideModalHandler)
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-05-25 17:30:38 +02:00
|
|
|
if (this.tip) {
|
|
|
|
this.tip.remove()
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
2021-08-31 15:11:22 +02:00
|
|
|
this._disposePopper()
|
2020-11-20 11:13:11 +01:00
|
|
|
super.dispose()
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
show() {
|
2019-09-04 16:58:29 +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')
|
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-01-27 18:01:24 +01:00
|
|
|
if (!(this.isWithContent() && this._isEnabled)) {
|
|
|
|
return
|
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-01-27 18:01:24 +01:00
|
|
|
const showEvent = EventHandler.trigger(this._element, this.constructor.Event.SHOW)
|
|
|
|
const shadowRoot = findShadowRoot(this._element)
|
|
|
|
const isInTheDom = shadowRoot === null ?
|
|
|
|
this._element.ownerDocument.documentElement.contains(this._element) :
|
|
|
|
shadowRoot.contains(this._element)
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-01-27 18:01:24 +01:00
|
|
|
if (showEvent.defaultPrevented || !isInTheDom) {
|
|
|
|
return
|
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-01-27 18:01:24 +01:00
|
|
|
const tip = this.getTipElement()
|
2017-03-28 23:43:16 +02:00
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
this._element.setAttribute('aria-describedby', tip.getAttribute('id'))
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-05-12 11:15:59 +02:00
|
|
|
const placement = typeof this._config.placement === 'function' ?
|
|
|
|
this._config.placement.call(this, tip, this._element) :
|
|
|
|
this._config.placement
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-01-27 18:01:24 +01:00
|
|
|
const attachment = this._getAttachment(placement)
|
|
|
|
this._addAttachmentClass(attachment)
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2021-05-13 17:17:20 +02:00
|
|
|
const { container } = this._config
|
2021-03-02 15:55:44 +01:00
|
|
|
Data.set(tip, this.constructor.DATA_KEY, this)
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-01-27 18:01:24 +01:00
|
|
|
if (!this._element.ownerDocument.documentElement.contains(this.tip)) {
|
2021-07-30 00:23:00 +02:00
|
|
|
container.append(tip)
|
2021-03-07 16:09:17 +01:00
|
|
|
EventHandler.trigger(this._element, this.constructor.Event.INSERTED)
|
2021-01-27 18:01:24 +01:00
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-03-06 22:57:23 +01:00
|
|
|
if (this._popper) {
|
2021-03-07 14:28:41 +01:00
|
|
|
this._popper.update()
|
|
|
|
} else {
|
|
|
|
this._popper = Popper.createPopper(this._element, tip, this._getPopperConfig(attachment))
|
2021-03-06 22:57:23 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:01:24 +01:00
|
|
|
tip.classList.add(CLASS_NAME_SHOW)
|
2020-11-25 08:16:22 +01:00
|
|
|
|
2021-01-27 18:01:24 +01: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) {
|
2021-07-30 08:28:51 +02:00
|
|
|
for (const element of [].concat(...document.body.children)) {
|
2021-03-27 17:08:45 +01:00
|
|
|
EventHandler.on(element, 'mouseover', noop)
|
2021-07-30 08:28:51 +02:00
|
|
|
}
|
2021-01-27 18:01:24 +01:00
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-01-27 18:01:24 +01:00
|
|
|
const complete = () => {
|
|
|
|
const prevHoverState = this._hoverState
|
2017-04-07 11:12:17 +02:00
|
|
|
|
2021-01-27 18:01:24 +01:00
|
|
|
this._hoverState = null
|
|
|
|
EventHandler.trigger(this._element, this.constructor.Event.SHOWN)
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-01-27 18:01:24 +01:00
|
|
|
if (prevHoverState === HOVER_STATE_OUT) {
|
|
|
|
this._leave(null, this)
|
2015-05-12 08:32:37 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-27 18:01:24 +01:00
|
|
|
|
2021-04-11 01:27:18 +02:00
|
|
|
const isAnimated = this.tip.classList.contains(CLASS_NAME_FADE)
|
|
|
|
this._queueCallback(complete, this.tip, isAnimated)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2019-05-10 21:57:27 +02:00
|
|
|
hide() {
|
2020-09-24 13:55:38 +02:00
|
|
|
if (!this._popper) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-26 12:20:34 +01:00
|
|
|
const tip = this.getTipElement()
|
|
|
|
const complete = () => {
|
2021-03-06 02:35:28 +01:00
|
|
|
if (this._isWithActiveTrigger()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-25 17:30:38 +02:00
|
|
|
if (this._hoverState !== HOVER_STATE_SHOW) {
|
|
|
|
tip.remove()
|
2017-04-19 15:08:06 +02:00
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
this._cleanTipClass()
|
2019-09-04 16:58:29 +02:00
|
|
|
this._element.removeAttribute('aria-describedby')
|
|
|
|
EventHandler.trigger(this._element, this.constructor.Event.HIDDEN)
|
2020-06-19 10:17:01 +02:00
|
|
|
|
2021-08-31 15:11:22 +02:00
|
|
|
this._disposePopper()
|
2015-05-12 08:32:37 +02:00
|
|
|
}
|
|
|
|
|
2019-09-04 16:58:29 +02:00
|
|
|
const hideEvent = EventHandler.trigger(this._element, this.constructor.Event.HIDE)
|
2017-09-21 18:04:47 +02:00
|
|
|
if (hideEvent.defaultPrevented) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return
|
2015-05-12 08:32:37 +02:00
|
|
|
}
|
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
tip.classList.remove(CLASS_NAME_SHOW)
|
2015-05-12 08:32:37 +02:00
|
|
|
|
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) {
|
2021-07-30 08:28:51 +02:00
|
|
|
for (const element of [].concat(...document.body.children)) {
|
|
|
|
EventHandler.off(element, 'mouseover', noop)
|
|
|
|
}
|
2015-08-31 01:57:16 +02:00
|
|
|
}
|
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
this._activeTrigger[TRIGGER_CLICK] = false
|
|
|
|
this._activeTrigger[TRIGGER_FOCUS] = false
|
|
|
|
this._activeTrigger[TRIGGER_HOVER] = false
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-04-11 01:27:18 +02:00
|
|
|
const isAnimated = this.tip.classList.contains(CLASS_NAME_FADE)
|
|
|
|
this._queueCallback(complete, this.tip, isAnimated)
|
2018-09-26 10:39:01 +02:00
|
|
|
this._hoverState = ''
|
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
update() {
|
|
|
|
if (this._popper !== null) {
|
2020-06-19 10:17:01 +02:00
|
|
|
this._popper.update()
|
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
|
|
|
|
isWithContent() {
|
|
|
|
return Boolean(this.getTitle())
|
|
|
|
}
|
2016-11-29 18:45:14 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
getTipElement() {
|
2017-09-21 18:04:47 +02:00
|
|
|
if (this.tip) {
|
|
|
|
return this.tip
|
|
|
|
}
|
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
const templateFactory = this._getTemplateFactory(this._getContentForTemplate())
|
2017-09-21 18:04:47 +02:00
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
const tip = templateFactory.toHtml()
|
2021-06-10 09:58:41 +02:00
|
|
|
tip.classList.remove(CLASS_NAME_FADE, CLASS_NAME_SHOW)
|
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
const tipId = getUID(this.constructor.NAME).toString()
|
2021-06-10 10:00:05 +02:00
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
tip.setAttribute('id', tipId)
|
2021-08-03 10:59:33 +02:00
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
if (this._config.animation) {
|
|
|
|
tip.classList.add(CLASS_NAME_FADE)
|
2021-06-10 10:00:05 +02:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
this.tip = tip
|
|
|
|
return this.tip
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
setContent(content) {
|
|
|
|
let isShown = false
|
|
|
|
if (this.tip) {
|
|
|
|
isShown = this.tip.classList.contains(CLASS_NAME_SHOW)
|
|
|
|
this.tip.remove()
|
2017-09-21 18:04:47 +02:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
this._disposePopper()
|
2017-09-21 18:04:47 +02:00
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
this.tip = this._getTemplateFactory(content).toHtml()
|
2019-02-11 15:59:39 +01:00
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
if (isShown) {
|
|
|
|
this.show()
|
2019-02-11 15:59:39 +01:00
|
|
|
}
|
2021-11-25 18:14:02 +01:00
|
|
|
}
|
2019-02-11 15:59:39 +01:00
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
_getTemplateFactory(content) {
|
|
|
|
if (this._templateFactory) {
|
|
|
|
this._templateFactory.changeContent(content)
|
2018-09-26 10:39:01 +02:00
|
|
|
} else {
|
2021-11-25 18:14:02 +01:00
|
|
|
this._templateFactory = new TemplateFactory({
|
|
|
|
...this._config,
|
|
|
|
// the `content` var has to be after `this._config`
|
|
|
|
// to override config.content in case of popover
|
|
|
|
content,
|
|
|
|
extraClass: this._resolvePossibleFunction(this._config.customClass)
|
|
|
|
})
|
2015-05-12 08:32:37 +02:00
|
|
|
}
|
2021-11-25 18:14:02 +01:00
|
|
|
|
|
|
|
return this._templateFactory
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
_getContentForTemplate() {
|
|
|
|
return {
|
|
|
|
[SELECTOR_TOOLTIP_INNER]: this.getTitle()
|
|
|
|
}
|
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
getTitle() {
|
|
|
|
return this._resolvePossibleFunction(this._config.title) || this._element.getAttribute('title')
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-12 23:28:11 +02:00
|
|
|
|
2020-06-26 16:06:20 +02:00
|
|
|
updateAttachment(attachment) {
|
|
|
|
if (attachment === 'right') {
|
|
|
|
return 'end'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attachment === 'left') {
|
|
|
|
return 'start'
|
|
|
|
}
|
|
|
|
|
|
|
|
return attachment
|
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Private
|
2021-01-27 18:01:24 +01:00
|
|
|
_initializeOnDelegatedTarget(event, context) {
|
2021-06-10 09:53:59 +02:00
|
|
|
return context || this.constructor.getOrCreateInstance(event.delegateTarget, this._getDelegateConfig())
|
2021-01-27 18:01:24 +01:00
|
|
|
}
|
|
|
|
|
2020-12-16 19:07:27 +01:00
|
|
|
_getOffset() {
|
2021-05-12 11:15:59 +02:00
|
|
|
const { offset } = this._config
|
2020-12-16 19:07:27 +01:00
|
|
|
|
|
|
|
if (typeof offset === 'string') {
|
|
|
|
return offset.split(',').map(val => Number.parseInt(val, 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof offset === 'function') {
|
|
|
|
return popperData => offset(popperData, this._element)
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset
|
|
|
|
}
|
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
_resolvePossibleFunction(arg) {
|
|
|
|
return typeof arg === 'function' ? arg.call(this._element) : arg
|
2021-06-10 09:55:34 +02:00
|
|
|
}
|
|
|
|
|
2019-08-14 17:27:58 +02:00
|
|
|
_getPopperConfig(attachment) {
|
2021-02-09 20:16:13 +01:00
|
|
|
const defaultBsPopperConfig = {
|
2019-08-14 17:27:58 +02:00
|
|
|
placement: attachment,
|
2020-06-19 10:17:01 +02:00
|
|
|
modifiers: [
|
2020-12-14 19:51:14 +01:00
|
|
|
{
|
|
|
|
name: 'flip',
|
|
|
|
options: {
|
2021-05-12 11:15:59 +02:00
|
|
|
fallbackPlacements: this._config.fallbackPlacements
|
2020-12-16 19:07:27 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'offset',
|
|
|
|
options: {
|
|
|
|
offset: this._getOffset()
|
2020-12-14 19:51:14 +01:00
|
|
|
}
|
|
|
|
},
|
2020-06-19 10:17:01 +02:00
|
|
|
{
|
|
|
|
name: 'preventOverflow',
|
|
|
|
options: {
|
2021-05-12 11:15:59 +02:00
|
|
|
boundary: this._config.boundary
|
2020-06-19 10:17:01 +02:00
|
|
|
}
|
2019-08-14 17:27:58 +02:00
|
|
|
},
|
2020-06-19 10:17:01 +02:00
|
|
|
{
|
|
|
|
name: 'arrow',
|
|
|
|
options: {
|
2021-11-25 18:14:02 +01:00
|
|
|
element: SELECTOR_TOOLTIP_ARROW
|
2020-06-19 10:17:01 +02:00
|
|
|
}
|
2019-08-14 17:27:58 +02:00
|
|
|
},
|
2020-06-19 10:17:01 +02:00
|
|
|
{
|
|
|
|
name: 'onChange',
|
|
|
|
enabled: true,
|
|
|
|
phase: 'afterWrite',
|
|
|
|
fn: data => this._handlePopperPlacementChange(data)
|
2019-08-14 17:27:58 +02:00
|
|
|
}
|
2020-06-19 10:17:01 +02:00
|
|
|
],
|
|
|
|
onFirstUpdate: data => {
|
|
|
|
if (data.options.placement !== data.placement) {
|
2019-08-14 17:27:58 +02:00
|
|
|
this._handlePopperPlacementChange(data)
|
|
|
|
}
|
2020-06-19 10:17:01 +02:00
|
|
|
}
|
2019-08-14 17:27:58 +02:00
|
|
|
}
|
|
|
|
|
2019-08-14 18:02:41 +02:00
|
|
|
return {
|
2021-02-09 20:16:13 +01:00
|
|
|
...defaultBsPopperConfig,
|
2021-05-12 11:15:59 +02:00
|
|
|
...(typeof this._config.popperConfig === 'function' ? this._config.popperConfig(defaultBsPopperConfig) : this._config.popperConfig)
|
2019-08-14 17:27:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 21:57:27 +02:00
|
|
|
_addAttachmentClass(attachment) {
|
2021-06-10 09:48:35 +02:00
|
|
|
this.getTipElement().classList.add(`${this._getBasicClassPrefix()}-${this.updateAttachment(attachment)}`)
|
2019-05-10 21:57:27 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_getAttachment(placement) {
|
|
|
|
return AttachmentMap[placement.toUpperCase()]
|
|
|
|
}
|
|
|
|
|
|
|
|
_setListeners() {
|
2021-05-12 11:15:59 +02:00
|
|
|
const triggers = this._config.trigger.split(' ')
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2021-07-30 08:28:51 +02:00
|
|
|
for (const trigger of triggers) {
|
2018-09-26 10:39:01 +02:00
|
|
|
if (trigger === 'click') {
|
2021-05-12 11:15:59 +02:00
|
|
|
EventHandler.on(this._element, this.constructor.Event.CLICK, this._config.selector, event => this.toggle(event))
|
2020-03-07 10:31:42 +01:00
|
|
|
} else if (trigger !== TRIGGER_MANUAL) {
|
|
|
|
const eventIn = trigger === TRIGGER_HOVER ?
|
2019-02-26 12:20:34 +01:00
|
|
|
this.constructor.Event.MOUSEENTER :
|
|
|
|
this.constructor.Event.FOCUSIN
|
2020-03-07 10:31:42 +01:00
|
|
|
const eventOut = trigger === TRIGGER_HOVER ?
|
2019-02-26 12:20:34 +01:00
|
|
|
this.constructor.Event.MOUSELEAVE :
|
|
|
|
this.constructor.Event.FOCUSOUT
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2021-05-12 11:15:59 +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))
|
2015-05-12 08:32:37 +02:00
|
|
|
}
|
2021-07-30 08:28:51 +02:00
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2019-06-13 10:42:54 +02:00
|
|
|
this._hideModalHandler = () => {
|
2019-09-04 16:58:29 +02:00
|
|
|
if (this._element) {
|
2019-06-13 10:42:54 +02:00
|
|
|
this.hide()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-30 00:32:07 +02:00
|
|
|
EventHandler.on(this._element.closest(SELECTOR_MODAL), EVENT_MODAL_HIDE, this._hideModalHandler)
|
2018-11-02 10:24:35 +01:00
|
|
|
|
2021-05-12 11:15:59 +02:00
|
|
|
if (this._config.selector) {
|
|
|
|
this._config = {
|
|
|
|
...this._config,
|
2018-09-26 10:39:01 +02:00
|
|
|
trigger: 'manual',
|
|
|
|
selector: ''
|
2015-05-12 08:32:37 +02:00
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
} else {
|
|
|
|
this._fixTitle()
|
|
|
|
}
|
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_fixTitle() {
|
2019-09-04 16:58:29 +02:00
|
|
|
const title = this._element.getAttribute('title')
|
2018-11-14 10:16:56 +01:00
|
|
|
|
2021-11-25 18:14:02 +01:00
|
|
|
if (title && !this._element.getAttribute('aria-label') && !this._element.textContent) {
|
|
|
|
this._element.setAttribute('aria-label', title)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_enter(event, context) {
|
2021-01-27 18:01:24 +01:00
|
|
|
context = this._initializeOnDelegatedTarget(event, context)
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (event) {
|
|
|
|
context._activeTrigger[
|
2020-03-07 10:31:42 +01:00
|
|
|
event.type === 'focusin' ? TRIGGER_FOCUS : TRIGGER_HOVER
|
2018-09-26 10:39:01 +02:00
|
|
|
] = true
|
|
|
|
}
|
2015-05-12 23:28:11 +02:00
|
|
|
|
2020-12-02 05:45:15 +01:00
|
|
|
if (context.getTipElement().classList.contains(CLASS_NAME_SHOW) || context._hoverState === HOVER_STATE_SHOW) {
|
2020-03-07 10:31:42 +01:00
|
|
|
context._hoverState = HOVER_STATE_SHOW
|
2018-09-26 10:39:01 +02:00
|
|
|
return
|
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
clearTimeout(context._timeout)
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
context._hoverState = HOVER_STATE_SHOW
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-05-12 11:15:59 +02:00
|
|
|
if (!context._config.delay || !context._config.delay.show) {
|
2018-09-26 10:39:01 +02:00
|
|
|
context.show()
|
|
|
|
return
|
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
context._timeout = setTimeout(() => {
|
2020-03-07 10:31:42 +01:00
|
|
|
if (context._hoverState === HOVER_STATE_SHOW) {
|
2018-09-26 10:39:01 +02:00
|
|
|
context.show()
|
|
|
|
}
|
2021-05-12 11:15:59 +02:00
|
|
|
}, context._config.delay.show)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_leave(event, context) {
|
2021-01-27 18:01:24 +01:00
|
|
|
context = this._initializeOnDelegatedTarget(event, context)
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (event) {
|
|
|
|
context._activeTrigger[
|
2020-03-07 10:31:42 +01:00
|
|
|
event.type === 'focusout' ? TRIGGER_FOCUS : TRIGGER_HOVER
|
2021-03-06 22:57:23 +01:00
|
|
|
] = context._element.contains(event.relatedTarget)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (context._isWithActiveTrigger()) {
|
|
|
|
return
|
2015-05-12 08:32:37 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
clearTimeout(context._timeout)
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
context._hoverState = HOVER_STATE_OUT
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-05-12 11:15:59 +02:00
|
|
|
if (!context._config.delay || !context._config.delay.hide) {
|
2018-09-26 10:39:01 +02:00
|
|
|
context.hide()
|
|
|
|
return
|
|
|
|
}
|
2017-03-31 10:03:54 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
context._timeout = setTimeout(() => {
|
2020-03-07 10:31:42 +01:00
|
|
|
if (context._hoverState === HOVER_STATE_OUT) {
|
2018-09-26 10:39:01 +02:00
|
|
|
context.hide()
|
2017-03-31 10:03:54 +02:00
|
|
|
}
|
2021-05-12 11:15:59 +02:00
|
|
|
}, context._config.delay.hide)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2017-03-31 10:03:54 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_isWithActiveTrigger() {
|
2021-11-25 18:39:13 +01:00
|
|
|
return Object.values(this._activeTrigger).includes(true)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_getConfig(config) {
|
2019-09-04 16:58:29 +02:00
|
|
|
const dataAttributes = Manipulator.getDataAttributes(this._element)
|
2019-02-11 15:59:39 +01:00
|
|
|
|
2021-07-30 08:28:51 +02:00
|
|
|
for (const dataAttr of Object.keys(dataAttributes)) {
|
2020-05-02 15:56:23 +02:00
|
|
|
if (DISALLOWED_ATTRIBUTES.has(dataAttr)) {
|
2020-06-20 18:00:53 +02:00
|
|
|
delete dataAttributes[dataAttr]
|
|
|
|
}
|
2021-07-30 08:28:51 +02:00
|
|
|
}
|
2019-02-11 15:59:39 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
config = {
|
|
|
|
...this.constructor.Default,
|
2019-02-11 15:59:39 +01:00
|
|
|
...dataAttributes,
|
2020-06-26 17:50:04 +02:00
|
|
|
...(typeof config === 'object' && config ? config : {})
|
2015-05-12 08:32:37 +02:00
|
|
|
}
|
|
|
|
|
2021-05-13 17:17:20 +02:00
|
|
|
config.container = config.container === false ? document.body : getElement(config.container)
|
|
|
|
|
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()
|
2017-04-19 10:20:50 +02:00
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (typeof config.content === 'number') {
|
|
|
|
config.content = config.content.toString()
|
2017-05-12 09:39:27 +02:00
|
|
|
}
|
|
|
|
|
2020-06-10 17:40:52 +02:00
|
|
|
typeCheckConfig(NAME, config, this.constructor.DefaultType)
|
2018-09-26 10:39:01 +02:00
|
|
|
return config
|
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_getDelegateConfig() {
|
|
|
|
const config = {}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-06-10 09:52:35 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2021-06-10 09:52:35 +02:00
|
|
|
// In the future can be replaced with:
|
|
|
|
// const keysWithDifferentValues = Object.entries(this._config).filter(entry => this.constructor.Default[entry[0]] !== this._config[entry[0]])
|
|
|
|
// `Object.fromEntries(keysWithDifferentValues)`
|
2018-09-26 10:39:01 +02:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
_cleanTipClass() {
|
2017-09-21 18:04:47 +02:00
|
|
|
const tip = this.getTipElement()
|
2021-06-10 09:48:35 +02:00
|
|
|
const basicClassPrefixRegex = new RegExp(`(^|\\s)${this._getBasicClassPrefix()}\\S+`, 'g')
|
|
|
|
const tabClass = tip.getAttribute('class').match(basicClassPrefixRegex)
|
2020-01-07 21:07:51 +01:00
|
|
|
if (tabClass !== null && tabClass.length > 0) {
|
2021-07-30 08:28:51 +02:00
|
|
|
for (const tClass of tabClass.map(token => token.trim())) {
|
|
|
|
tip.classList.remove(tClass)
|
|
|
|
}
|
2015-05-12 08:32:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 09:48:35 +02:00
|
|
|
_getBasicClassPrefix() {
|
|
|
|
return CLASS_PREFIX
|
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_handlePopperPlacementChange(popperData) {
|
2020-06-19 10:17:01 +02:00
|
|
|
const { state } = popperData
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2020-06-19 10:17:01 +02:00
|
|
|
if (!state) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return
|
|
|
|
}
|
2019-02-26 12:20:34 +01:00
|
|
|
|
2020-06-19 10:17:01 +02:00
|
|
|
this.tip = state.elements.popper
|
|
|
|
this._cleanTipClass()
|
|
|
|
this._addAttachmentClass(this._getAttachment(state.placement))
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
2021-08-31 15:11:22 +02:00
|
|
|
_disposePopper() {
|
|
|
|
if (this._popper) {
|
|
|
|
this._popper.destroy()
|
|
|
|
this._popper = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Static
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
static jQueryInterface(config) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return this.each(function () {
|
2021-06-03 17:53:27 +02:00
|
|
|
const data = Tooltip.getOrCreateInstance(this, config)
|
2015-05-12 08:32:37 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (typeof config === 'string') {
|
|
|
|
if (typeof data[config] === 'undefined') {
|
|
|
|
throw new TypeError(`No method named "${config}"`)
|
|
|
|
}
|
2019-02-26 12:20:34 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
data[config]()
|
|
|
|
}
|
|
|
|
})
|
2015-05-12 08:32:37 +02:00
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* jQuery
|
|
|
|
*/
|
2020-11-01 14:32:36 +01:00
|
|
|
|
2021-05-11 09:49:30 +02:00
|
|
|
defineJQueryPlugin(Tooltip)
|
2015-05-12 08:32:37 +02:00
|
|
|
|
|
|
|
export default Tooltip
|