2015-05-12 14:32:37 +08:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2023-03-22 15:12:33 +08:00
|
|
|
* Bootstrap tooltip.js
|
2020-06-17 02:41:47 +08:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
2015-05-12 14:32:37 +08:00
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2020-06-19 16:17:01 +08:00
|
|
|
import * as Popper from '@popperjs/core'
|
2022-10-26 13:26:51 +08:00
|
|
|
import { defineJQueryPlugin, execute, findShadowRoot, getElement, getUID, isRTL, noop } from './util/index.js'
|
|
|
|
import { DefaultAllowlist } from './util/sanitizer.js'
|
|
|
|
import EventHandler from './dom/event-handler.js'
|
|
|
|
import Manipulator from './dom/manipulator.js'
|
|
|
|
import BaseComponent from './base-component.js'
|
|
|
|
import TemplateFactory from './util/template-factory.js'
|
2018-11-14 17:16:56 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2019-02-26 19:20:34 +08:00
|
|
|
const NAME = 'tooltip'
|
2020-05-02 21:56:23 +08:00
|
|
|
const DISALLOWED_ATTRIBUTES = new Set(['sanitize', 'allowList', 'sanitizeFn'])
|
2018-09-26 16:39:01 +08:00
|
|
|
|
2021-10-13 20:19:28 +08:00
|
|
|
const CLASS_NAME_FADE = 'fade'
|
|
|
|
const CLASS_NAME_MODAL = 'modal'
|
|
|
|
const CLASS_NAME_SHOW = 'show'
|
|
|
|
|
|
|
|
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 16:39:01 +08:00
|
|
|
|
2022-02-19 21:10:47 +08:00
|
|
|
const EVENT_HIDE = 'hide'
|
|
|
|
const EVENT_HIDDEN = 'hidden'
|
|
|
|
const EVENT_SHOW = 'show'
|
|
|
|
const EVENT_SHOWN = 'shown'
|
|
|
|
const EVENT_INSERTED = 'inserted'
|
|
|
|
const EVENT_CLICK = 'click'
|
|
|
|
const EVENT_FOCUSIN = 'focusin'
|
|
|
|
const EVENT_FOCUSOUT = 'focusout'
|
|
|
|
const EVENT_MOUSEENTER = 'mouseenter'
|
|
|
|
const EVENT_MOUSELEAVE = 'mouseleave'
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
const AttachmentMap = {
|
2019-02-26 19:20:34 +08:00
|
|
|
AUTO: 'auto',
|
|
|
|
TOP: 'top',
|
2021-02-16 16:14:05 +08:00
|
|
|
RIGHT: isRTL() ? 'left' : 'right',
|
2019-02-26 19:20:34 +08:00
|
|
|
BOTTOM: 'bottom',
|
2021-02-16 16:14:05 +08:00
|
|
|
LEFT: isRTL() ? 'right' : 'left'
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Default = {
|
2022-05-19 21:35:44 +08:00
|
|
|
allowList: DefaultAllowlist,
|
2019-02-26 19:20:34 +08:00
|
|
|
animation: true,
|
2022-05-19 21:35:44 +08:00
|
|
|
boundary: 'clippingParents',
|
|
|
|
container: false,
|
|
|
|
customClass: '',
|
2019-02-26 19:20:34 +08:00
|
|
|
delay: 0,
|
2022-05-19 21:35:44 +08:00
|
|
|
fallbackPlacements: ['top', 'right', 'bottom', 'left'],
|
2019-02-26 19:20:34 +08:00
|
|
|
html: false,
|
2023-03-01 15:32:58 +08:00
|
|
|
offset: [0, 6],
|
2022-05-19 21:35:44 +08:00
|
|
|
placement: 'top',
|
|
|
|
popperConfig: null,
|
2019-02-26 19:20:34 +08:00
|
|
|
sanitize: true,
|
|
|
|
sanitizeFn: null,
|
2022-05-19 21:35:44 +08:00
|
|
|
selector: false,
|
|
|
|
template: '<div class="tooltip" role="tooltip">' +
|
|
|
|
'<div class="tooltip-arrow"></div>' +
|
|
|
|
'<div class="tooltip-inner"></div>' +
|
|
|
|
'</div>',
|
|
|
|
title: '',
|
|
|
|
trigger: 'hover focus'
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
|
|
|
|
2021-10-13 20:19:28 +08:00
|
|
|
const DefaultType = {
|
2022-05-19 21:35:44 +08:00
|
|
|
allowList: 'object',
|
2021-10-13 20:19:28 +08:00
|
|
|
animation: 'boolean',
|
2022-05-19 21:35:44 +08:00
|
|
|
boundary: '(string|element)',
|
|
|
|
container: '(string|element|boolean)',
|
|
|
|
customClass: '(string|function)',
|
2021-10-13 20:19:28 +08:00
|
|
|
delay: '(number|object)',
|
2022-05-19 21:35:44 +08:00
|
|
|
fallbackPlacements: 'array',
|
2021-10-13 20:19:28 +08:00
|
|
|
html: 'boolean',
|
|
|
|
offset: '(array|string|function)',
|
2022-05-19 21:35:44 +08:00
|
|
|
placement: '(string|function)',
|
|
|
|
popperConfig: '(null|object|function)',
|
2021-10-13 20:19:28 +08:00
|
|
|
sanitize: 'boolean',
|
|
|
|
sanitizeFn: '(null|function)',
|
2022-05-19 21:35:44 +08:00
|
|
|
selector: '(string|boolean)',
|
|
|
|
template: 'string',
|
|
|
|
title: '(string|element|function)',
|
|
|
|
trigger: 'string'
|
2021-10-13 20:19:28 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
/**
|
2021-10-13 20:19:28 +08:00
|
|
|
* Class definition
|
2018-09-26 16:39:01 +08:00
|
|
|
*/
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2019-09-04 22:58:29 +08:00
|
|
|
class Tooltip extends BaseComponent {
|
2018-09-26 16:39:01 +08:00
|
|
|
constructor(element, config) {
|
|
|
|
if (typeof Popper === 'undefined') {
|
2020-11-21 22:22:08 +08:00
|
|
|
throw new TypeError('Bootstrap\'s tooltips require Popper (https://popper.js.org)')
|
2015-05-12 14:32:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-15 16:47:32 +08:00
|
|
|
super(element, config)
|
2019-09-04 22:58:29 +08:00
|
|
|
|
2021-10-13 20:19:28 +08:00
|
|
|
// Private
|
2019-02-26 19:20:34 +08:00
|
|
|
this._isEnabled = true
|
|
|
|
this._timeout = 0
|
2022-09-07 16:47:06 +08:00
|
|
|
this._isHovered = null
|
2018-09-26 16:39:01 +08:00
|
|
|
this._activeTrigger = {}
|
2019-02-26 19:20:34 +08:00
|
|
|
this._popper = null
|
2021-11-26 01:14:02 +08:00
|
|
|
this._templateFactory = null
|
2022-06-27 17:58:27 +08:00
|
|
|
this._newContent = null
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
// Protected
|
2019-02-26 19:20:34 +08:00
|
|
|
this.tip = null
|
2015-05-13 05:28:11 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
this._setListeners()
|
2022-09-14 21:24:37 +08:00
|
|
|
|
|
|
|
if (!this._config.selector) {
|
|
|
|
this._fixTitle()
|
|
|
|
}
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2015-05-13 05:28:11 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
// Getters
|
|
|
|
static get Default() {
|
|
|
|
return Default
|
|
|
|
}
|
2015-05-14 05:46:50 +08:00
|
|
|
|
2021-12-11 00:18:18 +08:00
|
|
|
static get DefaultType() {
|
|
|
|
return DefaultType
|
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
static get NAME() {
|
|
|
|
return NAME
|
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
// Public
|
|
|
|
enable() {
|
|
|
|
this._isEnabled = true
|
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
disable() {
|
|
|
|
this._isEnabled = false
|
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
toggleEnabled() {
|
|
|
|
this._isEnabled = !this._isEnabled
|
|
|
|
}
|
2015-08-19 10:22:46 +08:00
|
|
|
|
2022-09-14 21:24:37 +08:00
|
|
|
toggle() {
|
2018-09-26 16:39:01 +08:00
|
|
|
if (!this._isEnabled) {
|
|
|
|
return
|
2015-05-12 14:32:37 +08:00
|
|
|
}
|
|
|
|
|
2022-09-14 21:24:37 +08:00
|
|
|
this._activeTrigger.click = !this._activeTrigger.click
|
2022-01-30 23:39:03 +08:00
|
|
|
if (this._isShown()) {
|
|
|
|
this._leave()
|
|
|
|
return
|
2015-05-12 14:32:37 +08:00
|
|
|
}
|
2022-01-30 23:39:03 +08:00
|
|
|
|
|
|
|
this._enter()
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
dispose() {
|
|
|
|
clearTimeout(this._timeout)
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2021-07-30 06:32:07 +08:00
|
|
|
EventHandler.off(this._element.closest(SELECTOR_MODAL), EVENT_MODAL_HIDE, this._hideModalHandler)
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2022-09-14 21:24:37 +08:00
|
|
|
if (this._element.getAttribute('data-bs-original-title')) {
|
|
|
|
this._element.setAttribute('title', this._element.getAttribute('data-bs-original-title'))
|
2022-07-27 22:40:05 +08:00
|
|
|
}
|
|
|
|
|
2021-08-31 21:11:22 +08:00
|
|
|
this._disposePopper()
|
2020-11-20 18:13:11 +08:00
|
|
|
super.dispose()
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
show() {
|
2019-09-04 22:58:29 +08:00
|
|
|
if (this._element.style.display === 'none') {
|
2018-09-26 16:39:01 +08:00
|
|
|
throw new Error('Please use show on visible elements')
|
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2021-11-29 22:25:12 +08:00
|
|
|
if (!(this._isWithContent() && this._isEnabled)) {
|
2021-01-28 01:01:24 +08:00
|
|
|
return
|
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2022-02-19 21:10:47 +08:00
|
|
|
const showEvent = EventHandler.trigger(this._element, this.constructor.eventName(EVENT_SHOW))
|
2021-01-28 01:01:24 +08:00
|
|
|
const shadowRoot = findShadowRoot(this._element)
|
2022-01-27 16:43:27 +08:00
|
|
|
const isInTheDom = (shadowRoot || this._element.ownerDocument.documentElement).contains(this._element)
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2021-01-28 01:01:24 +08:00
|
|
|
if (showEvent.defaultPrevented || !isInTheDom) {
|
|
|
|
return
|
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2022-06-27 17:58:27 +08:00
|
|
|
// todo v6 remove this OR make it optional
|
2022-10-06 16:31:38 +08:00
|
|
|
this._disposePopper()
|
2022-06-27 17:58:27 +08:00
|
|
|
|
2021-11-29 22:25:12 +08:00
|
|
|
const tip = this._getTipElement()
|
2017-03-29 05:43:16 +08:00
|
|
|
|
2021-11-26 01:14:02 +08:00
|
|
|
this._element.setAttribute('aria-describedby', tip.getAttribute('id'))
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2021-05-13 23:17:20 +08:00
|
|
|
const { container } = this._config
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2021-01-28 01:01:24 +08:00
|
|
|
if (!this._element.ownerDocument.documentElement.contains(this.tip)) {
|
2021-07-30 06:23:00 +08:00
|
|
|
container.append(tip)
|
2022-02-19 21:10:47 +08:00
|
|
|
EventHandler.trigger(this._element, this.constructor.eventName(EVENT_INSERTED))
|
2021-01-28 01:01:24 +08:00
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2022-10-06 16:31:38 +08:00
|
|
|
this._popper = this._createPopper(tip)
|
2021-03-07 05:57:23 +08:00
|
|
|
|
2021-01-28 01:01:24 +08:00
|
|
|
tip.classList.add(CLASS_NAME_SHOW)
|
2020-11-25 15:16:22 +08:00
|
|
|
|
2021-01-28 01:01:24 +08: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 14:28:51 +08:00
|
|
|
for (const element of [].concat(...document.body.children)) {
|
2021-03-28 00:08:45 +08:00
|
|
|
EventHandler.on(element, 'mouseover', noop)
|
2021-07-30 14:28:51 +08:00
|
|
|
}
|
2021-01-28 01:01:24 +08:00
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2021-01-28 01:01:24 +08:00
|
|
|
const complete = () => {
|
2022-02-19 21:10:47 +08:00
|
|
|
EventHandler.trigger(this._element, this.constructor.eventName(EVENT_SHOWN))
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2022-09-07 16:47:06 +08:00
|
|
|
if (this._isHovered === false) {
|
2021-11-28 09:09:42 +08:00
|
|
|
this._leave()
|
2015-05-12 14:32:37 +08:00
|
|
|
}
|
2022-09-07 16:47:06 +08:00
|
|
|
|
|
|
|
this._isHovered = false
|
2015-05-12 14:32:37 +08:00
|
|
|
}
|
2021-01-28 01:01:24 +08:00
|
|
|
|
2021-11-26 08:15:24 +08:00
|
|
|
this._queueCallback(complete, this.tip, this._isAnimated())
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2019-05-11 03:57:27 +08:00
|
|
|
hide() {
|
2022-01-30 23:39:03 +08:00
|
|
|
if (!this._isShown()) {
|
2020-09-24 19:55:38 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-19 21:10:47 +08:00
|
|
|
const hideEvent = EventHandler.trigger(this._element, this.constructor.eventName(EVENT_HIDE))
|
2017-09-22 00:04:47 +08:00
|
|
|
if (hideEvent.defaultPrevented) {
|
2018-09-26 16:39:01 +08:00
|
|
|
return
|
2015-05-12 14:32:37 +08:00
|
|
|
}
|
|
|
|
|
2021-11-29 22:25:12 +08:00
|
|
|
const tip = this._getTipElement()
|
2020-03-07 17:31:42 +08:00
|
|
|
tip.classList.remove(CLASS_NAME_SHOW)
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2018-09-26 16:39:01 +08: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 14:28:51 +08:00
|
|
|
for (const element of [].concat(...document.body.children)) {
|
|
|
|
EventHandler.off(element, 'mouseover', noop)
|
|
|
|
}
|
2015-08-31 07:57:16 +08:00
|
|
|
}
|
|
|
|
|
2020-03-07 17:31:42 +08:00
|
|
|
this._activeTrigger[TRIGGER_CLICK] = false
|
|
|
|
this._activeTrigger[TRIGGER_FOCUS] = false
|
|
|
|
this._activeTrigger[TRIGGER_HOVER] = false
|
2022-09-07 16:47:06 +08:00
|
|
|
this._isHovered = null // it is a trick to support manual triggering
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2021-11-29 22:16:42 +08:00
|
|
|
const complete = () => {
|
|
|
|
if (this._isWithActiveTrigger()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._isHovered) {
|
2022-10-06 16:31:38 +08:00
|
|
|
this._disposePopper()
|
2021-11-29 22:16:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this._element.removeAttribute('aria-describedby')
|
2022-02-19 21:10:47 +08:00
|
|
|
EventHandler.trigger(this._element, this.constructor.eventName(EVENT_HIDDEN))
|
2021-11-29 22:16:42 +08:00
|
|
|
}
|
|
|
|
|
2021-11-26 08:15:24 +08:00
|
|
|
this._queueCallback(complete, this.tip, this._isAnimated())
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
update() {
|
2021-11-28 20:18:59 +08:00
|
|
|
if (this._popper) {
|
2020-06-19 16:17:01 +08:00
|
|
|
this._popper.update()
|
2015-05-13 05:28:11 +08:00
|
|
|
}
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2015-05-13 05:28:11 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
// Protected
|
2021-11-29 22:25:12 +08:00
|
|
|
_isWithContent() {
|
|
|
|
return Boolean(this._getTitle())
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2016-11-30 01:45:14 +08:00
|
|
|
|
2021-11-29 22:25:12 +08:00
|
|
|
_getTipElement() {
|
2021-12-01 21:10:10 +08:00
|
|
|
if (!this.tip) {
|
2022-06-27 17:58:27 +08:00
|
|
|
this.tip = this._createTipElement(this._newContent || this._getContentForTemplate())
|
2017-09-22 00:04:47 +08:00
|
|
|
}
|
|
|
|
|
2021-12-01 21:10:10 +08:00
|
|
|
return this.tip
|
|
|
|
}
|
|
|
|
|
|
|
|
_createTipElement(content) {
|
|
|
|
const tip = this._getTemplateFactory(content).toHtml()
|
|
|
|
|
|
|
|
// todo: remove this check on v6
|
|
|
|
if (!tip) {
|
|
|
|
return null
|
|
|
|
}
|
2017-09-22 00:04:47 +08:00
|
|
|
|
2021-06-10 15:58:41 +08:00
|
|
|
tip.classList.remove(CLASS_NAME_FADE, CLASS_NAME_SHOW)
|
2021-12-01 21:10:10 +08:00
|
|
|
// todo: on v6 the following can be achieved with CSS only
|
2021-11-26 02:08:11 +08:00
|
|
|
tip.classList.add(`bs-${this.constructor.NAME}-auto`)
|
2021-06-10 15:58:41 +08:00
|
|
|
|
2021-11-26 01:14:02 +08:00
|
|
|
const tipId = getUID(this.constructor.NAME).toString()
|
2021-06-10 16:00:05 +08:00
|
|
|
|
2021-11-26 01:14:02 +08:00
|
|
|
tip.setAttribute('id', tipId)
|
2021-08-03 16:59:33 +08:00
|
|
|
|
2021-11-26 08:15:24 +08:00
|
|
|
if (this._isAnimated()) {
|
2021-11-26 01:14:02 +08:00
|
|
|
tip.classList.add(CLASS_NAME_FADE)
|
2021-06-10 16:00:05 +08:00
|
|
|
}
|
|
|
|
|
2021-12-01 21:10:10 +08:00
|
|
|
return tip
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
|
|
|
|
2021-11-26 01:14:02 +08:00
|
|
|
setContent(content) {
|
2022-06-27 17:58:27 +08:00
|
|
|
this._newContent = content
|
|
|
|
if (this._isShown()) {
|
|
|
|
this._disposePopper()
|
2021-11-26 01:14:02 +08:00
|
|
|
this.show()
|
2019-02-11 22:59:39 +08:00
|
|
|
}
|
2021-11-26 01:14:02 +08:00
|
|
|
}
|
2019-02-11 22:59:39 +08:00
|
|
|
|
2021-11-26 01:14:02 +08:00
|
|
|
_getTemplateFactory(content) {
|
|
|
|
if (this._templateFactory) {
|
|
|
|
this._templateFactory.changeContent(content)
|
2018-09-26 16:39:01 +08:00
|
|
|
} else {
|
2021-11-26 01:14:02 +08: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 14:32:37 +08:00
|
|
|
}
|
2021-11-26 01:14:02 +08:00
|
|
|
|
|
|
|
return this._templateFactory
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2021-11-26 01:14:02 +08:00
|
|
|
_getContentForTemplate() {
|
|
|
|
return {
|
2021-11-29 22:25:12 +08:00
|
|
|
[SELECTOR_TOOLTIP_INNER]: this._getTitle()
|
2021-11-26 01:14:02 +08:00
|
|
|
}
|
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2021-11-29 22:25:12 +08:00
|
|
|
_getTitle() {
|
2022-09-14 21:24:37 +08:00
|
|
|
return this._resolvePossibleFunction(this._config.title) || this._element.getAttribute('data-bs-original-title')
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2015-05-13 05:28:11 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
// Private
|
2021-11-28 09:09:42 +08:00
|
|
|
_initializeOnDelegatedTarget(event) {
|
|
|
|
return this.constructor.getOrCreateInstance(event.delegateTarget, this._getDelegateConfig())
|
2021-01-28 01:01:24 +08:00
|
|
|
}
|
|
|
|
|
2021-11-26 08:15:24 +08:00
|
|
|
_isAnimated() {
|
|
|
|
return this._config.animation || (this.tip && this.tip.classList.contains(CLASS_NAME_FADE))
|
|
|
|
}
|
|
|
|
|
2022-01-30 23:39:03 +08:00
|
|
|
_isShown() {
|
|
|
|
return this.tip && this.tip.classList.contains(CLASS_NAME_SHOW)
|
|
|
|
}
|
|
|
|
|
|
|
|
_createPopper(tip) {
|
2022-10-07 20:25:00 +08:00
|
|
|
const placement = execute(this._config.placement, [this, tip, this._element])
|
2022-01-30 23:39:03 +08:00
|
|
|
const attachment = AttachmentMap[placement.toUpperCase()]
|
2022-06-27 17:58:27 +08:00
|
|
|
return Popper.createPopper(this._element, tip, this._getPopperConfig(attachment))
|
2022-01-30 23:39:03 +08:00
|
|
|
}
|
|
|
|
|
2020-12-17 02:07:27 +08:00
|
|
|
_getOffset() {
|
2021-05-12 17:15:59 +08:00
|
|
|
const { offset } = this._config
|
2020-12-17 02:07:27 +08:00
|
|
|
|
|
|
|
if (typeof offset === 'string') {
|
2021-10-29 15:38:35 +08:00
|
|
|
return offset.split(',').map(value => Number.parseInt(value, 10))
|
2020-12-17 02:07:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof offset === 'function') {
|
|
|
|
return popperData => offset(popperData, this._element)
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset
|
|
|
|
}
|
|
|
|
|
2021-11-26 01:14:02 +08:00
|
|
|
_resolvePossibleFunction(arg) {
|
2022-10-07 20:25:00 +08:00
|
|
|
return execute(arg, [this._element])
|
2021-06-10 15:55:34 +08:00
|
|
|
}
|
|
|
|
|
2019-08-14 23:27:58 +08:00
|
|
|
_getPopperConfig(attachment) {
|
2021-02-10 03:16:13 +08:00
|
|
|
const defaultBsPopperConfig = {
|
2019-08-14 23:27:58 +08:00
|
|
|
placement: attachment,
|
2020-06-19 16:17:01 +08:00
|
|
|
modifiers: [
|
2020-12-15 02:51:14 +08:00
|
|
|
{
|
|
|
|
name: 'flip',
|
|
|
|
options: {
|
2021-05-12 17:15:59 +08:00
|
|
|
fallbackPlacements: this._config.fallbackPlacements
|
2020-12-17 02:07:27 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'offset',
|
|
|
|
options: {
|
|
|
|
offset: this._getOffset()
|
2020-12-15 02:51:14 +08:00
|
|
|
}
|
|
|
|
},
|
2020-06-19 16:17:01 +08:00
|
|
|
{
|
|
|
|
name: 'preventOverflow',
|
|
|
|
options: {
|
2021-05-12 17:15:59 +08:00
|
|
|
boundary: this._config.boundary
|
2020-06-19 16:17:01 +08:00
|
|
|
}
|
2019-08-14 23:27:58 +08:00
|
|
|
},
|
2020-06-19 16:17:01 +08:00
|
|
|
{
|
|
|
|
name: 'arrow',
|
|
|
|
options: {
|
2021-12-01 21:10:10 +08:00
|
|
|
element: `.${this.constructor.NAME}-arrow`
|
2020-06-19 16:17:01 +08:00
|
|
|
}
|
2021-12-21 23:19:29 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'preSetPlacement',
|
|
|
|
enabled: true,
|
|
|
|
phase: 'beforeMain',
|
|
|
|
fn: data => {
|
|
|
|
// Pre-set Popper's placement attribute in order to read the arrow sizes properly.
|
|
|
|
// Otherwise, Popper mixes up the width and height dimensions since the initial arrow style is for top placement
|
|
|
|
this._getTipElement().setAttribute('data-popper-placement', data.state.placement)
|
|
|
|
}
|
2019-08-14 23:27:58 +08:00
|
|
|
}
|
2021-11-26 02:08:11 +08:00
|
|
|
]
|
2019-08-14 23:27:58 +08:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:02:41 +08:00
|
|
|
return {
|
2021-02-10 03:16:13 +08:00
|
|
|
...defaultBsPopperConfig,
|
2022-10-07 20:25:00 +08:00
|
|
|
...execute(this._config.popperConfig, [defaultBsPopperConfig])
|
2019-08-14 23:27:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
_setListeners() {
|
2021-05-12 17:15:59 +08:00
|
|
|
const triggers = this._config.trigger.split(' ')
|
2018-09-26 16:39:01 +08:00
|
|
|
|
2021-07-30 14:28:51 +08:00
|
|
|
for (const trigger of triggers) {
|
2018-09-26 16:39:01 +08:00
|
|
|
if (trigger === 'click') {
|
2022-09-14 21:24:37 +08:00
|
|
|
EventHandler.on(this._element, this.constructor.eventName(EVENT_CLICK), this._config.selector, event => {
|
|
|
|
const context = this._initializeOnDelegatedTarget(event)
|
|
|
|
context.toggle()
|
|
|
|
})
|
2020-03-07 17:31:42 +08:00
|
|
|
} else if (trigger !== TRIGGER_MANUAL) {
|
|
|
|
const eventIn = trigger === TRIGGER_HOVER ?
|
2022-02-19 21:10:47 +08:00
|
|
|
this.constructor.eventName(EVENT_MOUSEENTER) :
|
|
|
|
this.constructor.eventName(EVENT_FOCUSIN)
|
2020-03-07 17:31:42 +08:00
|
|
|
const eventOut = trigger === TRIGGER_HOVER ?
|
2022-02-19 21:10:47 +08:00
|
|
|
this.constructor.eventName(EVENT_MOUSELEAVE) :
|
|
|
|
this.constructor.eventName(EVENT_FOCUSOUT)
|
2018-09-26 16:39:01 +08:00
|
|
|
|
2021-11-28 09:09:42 +08:00
|
|
|
EventHandler.on(this._element, eventIn, this._config.selector, event => {
|
|
|
|
const context = this._initializeOnDelegatedTarget(event)
|
|
|
|
context._activeTrigger[event.type === 'focusin' ? TRIGGER_FOCUS : TRIGGER_HOVER] = true
|
|
|
|
context._enter()
|
|
|
|
})
|
|
|
|
EventHandler.on(this._element, eventOut, this._config.selector, event => {
|
|
|
|
const context = this._initializeOnDelegatedTarget(event)
|
|
|
|
context._activeTrigger[event.type === 'focusout' ? TRIGGER_FOCUS : TRIGGER_HOVER] =
|
|
|
|
context._element.contains(event.relatedTarget)
|
|
|
|
|
|
|
|
context._leave()
|
|
|
|
})
|
2015-05-12 14:32:37 +08:00
|
|
|
}
|
2021-07-30 14:28:51 +08:00
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2019-06-13 16:42:54 +08:00
|
|
|
this._hideModalHandler = () => {
|
2019-09-04 22:58:29 +08:00
|
|
|
if (this._element) {
|
2019-06-13 16:42:54 +08:00
|
|
|
this.hide()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-30 06:32:07 +08:00
|
|
|
EventHandler.on(this._element.closest(SELECTOR_MODAL), EVENT_MODAL_HIDE, this._hideModalHandler)
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
_fixTitle() {
|
2022-09-14 21:24:37 +08:00
|
|
|
const title = this._element.getAttribute('title')
|
2018-11-14 17:16:56 +08:00
|
|
|
|
2021-12-07 21:51:56 +08:00
|
|
|
if (!title) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-06 13:15:50 +08:00
|
|
|
if (!this._element.getAttribute('aria-label') && !this._element.textContent.trim()) {
|
2021-11-26 01:14:02 +08:00
|
|
|
this._element.setAttribute('aria-label', title)
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2021-12-07 21:51:56 +08:00
|
|
|
|
2022-09-14 21:24:37 +08:00
|
|
|
this._element.setAttribute('data-bs-original-title', title) // DO NOT USE IT. Is only for backwards compatibility
|
2021-12-07 21:51:56 +08:00
|
|
|
this._element.removeAttribute('title')
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2021-11-28 09:09:42 +08:00
|
|
|
_enter() {
|
2022-01-30 23:39:03 +08:00
|
|
|
if (this._isShown() || this._isHovered) {
|
2021-11-28 10:02:10 +08:00
|
|
|
this._isHovered = true
|
2018-09-26 16:39:01 +08:00
|
|
|
return
|
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2021-11-28 10:02:10 +08:00
|
|
|
this._isHovered = true
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2021-11-28 09:58:59 +08:00
|
|
|
this._setTimeout(() => {
|
2021-11-28 10:02:10 +08:00
|
|
|
if (this._isHovered) {
|
2021-11-28 09:09:42 +08:00
|
|
|
this.show()
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2021-11-28 09:09:42 +08:00
|
|
|
}, this._config.delay.show)
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2021-11-28 09:09:42 +08:00
|
|
|
_leave() {
|
|
|
|
if (this._isWithActiveTrigger()) {
|
2018-09-26 16:39:01 +08:00
|
|
|
return
|
2015-05-12 14:32:37 +08:00
|
|
|
}
|
|
|
|
|
2021-11-28 10:02:10 +08:00
|
|
|
this._isHovered = false
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2021-11-28 09:58:59 +08:00
|
|
|
this._setTimeout(() => {
|
2021-11-28 10:02:10 +08:00
|
|
|
if (!this._isHovered) {
|
2021-11-28 09:09:42 +08:00
|
|
|
this.hide()
|
2017-03-31 16:03:54 +08:00
|
|
|
}
|
2021-11-28 09:09:42 +08:00
|
|
|
}, this._config.delay.hide)
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2017-03-31 16:03:54 +08:00
|
|
|
|
2021-11-28 09:58:59 +08:00
|
|
|
_setTimeout(handler, timeout) {
|
|
|
|
clearTimeout(this._timeout)
|
|
|
|
this._timeout = setTimeout(handler, timeout)
|
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
_isWithActiveTrigger() {
|
2021-11-26 01:39:13 +08:00
|
|
|
return Object.values(this._activeTrigger).includes(true)
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
_getConfig(config) {
|
2019-09-04 22:58:29 +08:00
|
|
|
const dataAttributes = Manipulator.getDataAttributes(this._element)
|
2019-02-11 22:59:39 +08:00
|
|
|
|
2021-10-29 15:38:35 +08:00
|
|
|
for (const dataAttribute of Object.keys(dataAttributes)) {
|
|
|
|
if (DISALLOWED_ATTRIBUTES.has(dataAttribute)) {
|
|
|
|
delete dataAttributes[dataAttribute]
|
2020-06-21 00:00:53 +08:00
|
|
|
}
|
2021-07-30 14:28:51 +08:00
|
|
|
}
|
2019-02-11 22:59:39 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
config = {
|
2019-02-11 22:59:39 +08:00
|
|
|
...dataAttributes,
|
2020-06-26 23:50:04 +08:00
|
|
|
...(typeof config === 'object' && config ? config : {})
|
2015-05-12 14:32:37 +08:00
|
|
|
}
|
2021-12-11 00:18:18 +08:00
|
|
|
config = this._mergeConfigObj(config)
|
|
|
|
config = this._configAfterMerge(config)
|
|
|
|
this._typeCheckConfig(config)
|
|
|
|
return config
|
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2021-12-11 00:18:18 +08:00
|
|
|
_configAfterMerge(config) {
|
2021-05-13 23:17:20 +08:00
|
|
|
config.container = config.container === false ? document.body : getElement(config.container)
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
if (typeof config.delay === 'number') {
|
|
|
|
config.delay = {
|
|
|
|
show: config.delay,
|
|
|
|
hide: config.delay
|
2017-04-09 03:13:15 +08:00
|
|
|
}
|
2017-04-07 19:20:34 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
if (typeof config.title === 'number') {
|
|
|
|
config.title = config.title.toString()
|
2017-04-19 16:20:50 +08:00
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
if (typeof config.content === 'number') {
|
|
|
|
config.content = config.content.toString()
|
2017-05-12 15:39:27 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
return config
|
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
_getDelegateConfig() {
|
|
|
|
const config = {}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2022-11-13 15:19:11 +08:00
|
|
|
for (const [key, value] of Object.entries(this._config)) {
|
|
|
|
if (this.constructor.Default[key] !== value) {
|
|
|
|
config[key] = value
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
|
|
|
}
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2022-09-14 21:24:37 +08:00
|
|
|
config.selector = false
|
|
|
|
config.trigger = 'manual'
|
|
|
|
|
2021-06-10 15:52:35 +08: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 16:39:01 +08:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2021-08-31 21:11:22 +08:00
|
|
|
_disposePopper() {
|
|
|
|
if (this._popper) {
|
|
|
|
this._popper.destroy()
|
|
|
|
this._popper = null
|
|
|
|
}
|
2022-10-06 16:31:38 +08:00
|
|
|
|
|
|
|
if (this.tip) {
|
|
|
|
this.tip.remove()
|
|
|
|
this.tip = null
|
|
|
|
}
|
2021-08-31 21:11:22 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
// Static
|
2019-07-28 21:24:46 +08:00
|
|
|
static jQueryInterface(config) {
|
2018-09-26 16:39:01 +08:00
|
|
|
return this.each(function () {
|
2021-06-03 23:53:27 +08:00
|
|
|
const data = Tooltip.getOrCreateInstance(this, config)
|
2015-05-12 14:32:37 +08:00
|
|
|
|
2021-11-26 02:33:42 +08:00
|
|
|
if (typeof config !== 'string') {
|
|
|
|
return
|
|
|
|
}
|
2019-02-26 19:20:34 +08:00
|
|
|
|
2021-11-26 02:33:42 +08:00
|
|
|
if (typeof data[config] === 'undefined') {
|
|
|
|
throw new TypeError(`No method named "${config}"`)
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2021-11-26 02:33:42 +08:00
|
|
|
|
|
|
|
data[config]()
|
2018-09-26 16:39:01 +08:00
|
|
|
})
|
2015-05-12 14:32:37 +08:00
|
|
|
}
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* jQuery
|
|
|
|
*/
|
2020-11-01 21:32:36 +08:00
|
|
|
|
2021-05-11 15:49:30 +08:00
|
|
|
defineJQueryPlugin(Tooltip)
|
2015-05-12 14:32:37 +08:00
|
|
|
|
|
|
|
export default Tooltip
|