0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-19 11:52:21 +01:00

Tooltip: Change _enter & _leave to work without arguments

This commit is contained in:
GeoSot 2021-11-28 03:09:42 +02:00 committed by XhmikosR
parent a20e4203fe
commit c69ccba08c
2 changed files with 42 additions and 48 deletions

View File

@ -179,17 +179,17 @@ class Tooltip extends BaseComponent {
context._activeTrigger.click = !context._activeTrigger.click context._activeTrigger.click = !context._activeTrigger.click
if (context._isWithActiveTrigger()) { if (context._isWithActiveTrigger()) {
context._enter(null, context) context._enter()
} else { } else {
context._leave(null, context) context._leave()
} }
} else { } else {
if (this.getTipElement().classList.contains(CLASS_NAME_SHOW)) { if (this.getTipElement().classList.contains(CLASS_NAME_SHOW)) {
this._leave(null, this) this._leave()
return return
} }
this._enter(null, this) this._enter()
} }
} }
@ -265,7 +265,7 @@ class Tooltip extends BaseComponent {
EventHandler.trigger(this._element, this.constructor.Event.SHOWN) EventHandler.trigger(this._element, this.constructor.Event.SHOWN)
if (prevHoverState === HOVER_STATE_OUT) { if (prevHoverState === HOVER_STATE_OUT) {
this._leave(null, this) this._leave()
} }
} }
@ -401,8 +401,8 @@ class Tooltip extends BaseComponent {
} }
// Private // Private
_initializeOnDelegatedTarget(event, context) { _initializeOnDelegatedTarget(event) {
return context || this.constructor.getOrCreateInstance(event.delegateTarget, this._getDelegateConfig()) return this.constructor.getOrCreateInstance(event.delegateTarget, this._getDelegateConfig())
} }
_isAnimated() { _isAnimated() {
@ -478,8 +478,18 @@ class Tooltip extends BaseComponent {
this.constructor.Event.MOUSELEAVE : this.constructor.Event.MOUSELEAVE :
this.constructor.Event.FOCUSOUT this.constructor.Event.FOCUSOUT
EventHandler.on(this._element, eventIn, this._config.selector, event => this._enter(event)) EventHandler.on(this._element, eventIn, this._config.selector, event => {
EventHandler.on(this._element, eventOut, this._config.selector, event => this._leave(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()
})
} }
} }
@ -510,63 +520,47 @@ class Tooltip extends BaseComponent {
} }
} }
_enter(event, context) { _enter() {
context = this._initializeOnDelegatedTarget(event, context) if (this.getTipElement().classList.contains(CLASS_NAME_SHOW) || this._hoverState === HOVER_STATE_SHOW) {
this._hoverState = HOVER_STATE_SHOW
if (event) {
context._activeTrigger[
event.type === 'focusin' ? TRIGGER_FOCUS : TRIGGER_HOVER
] = true
}
if (context.getTipElement().classList.contains(CLASS_NAME_SHOW) || context._hoverState === HOVER_STATE_SHOW) {
context._hoverState = HOVER_STATE_SHOW
return return
} }
clearTimeout(context._timeout) clearTimeout(this._timeout)
context._hoverState = HOVER_STATE_SHOW this._hoverState = HOVER_STATE_SHOW
if (!context._config.delay.show) { if (!this._config.delay.show) {
context.show() this.show()
return return
} }
context._timeout = setTimeout(() => { this._timeout = setTimeout(() => {
if (context._hoverState === HOVER_STATE_SHOW) { if (this._hoverState === HOVER_STATE_SHOW) {
context.show() this.show()
} }
}, context._config.delay.show) }, this._config.delay.show)
} }
_leave(event, context) { _leave() {
context = this._initializeOnDelegatedTarget(event, context) if (this._isWithActiveTrigger()) {
if (event) {
context._activeTrigger[
event.type === 'focusout' ? TRIGGER_FOCUS : TRIGGER_HOVER
] = context._element.contains(event.relatedTarget)
}
if (context._isWithActiveTrigger()) {
return return
} }
clearTimeout(context._timeout) clearTimeout(this._timeout)
context._hoverState = HOVER_STATE_OUT this._hoverState = HOVER_STATE_OUT
if (!context._config.delay.hide) { if (!this._config.delay.hide) {
context.hide() this.hide()
return return
} }
context._timeout = setTimeout(() => { this._timeout = setTimeout(() => {
if (context._hoverState === HOVER_STATE_OUT) { if (this._hoverState === HOVER_STATE_OUT) {
context.hide() this.hide()
} }
}, context._config.delay.hide) }, this._config.delay.hide)
} }
_isWithActiveTrigger() { _isWithActiveTrigger() {

View File

@ -702,7 +702,7 @@ describe('Tooltip', () => {
expect(document.querySelectorAll('.tooltip')).toHaveSize(1) expect(document.querySelectorAll('.tooltip')).toHaveSize(1)
done() done()
}, 200) }, 200)
}, 0) }, 3)
tooltipEl.dispatchEvent(createEvent('mouseover')) tooltipEl.dispatchEvent(createEvent('mouseover'))
}) })
@ -765,7 +765,7 @@ describe('Tooltip', () => {
expect(tooltip.getTipElement().getAttribute('data-popper-placement')).toEqual('top') expect(tooltip.getTipElement().getAttribute('data-popper-placement')).toEqual('top')
done() done()
}, 200) }, 200)
}, 0) }, 3)
tooltipEl.dispatchEvent(createEvent('mouseover')) tooltipEl.dispatchEvent(createEvent('mouseover'))
}) })