Skip to content

Commit

Permalink
Merge js-components 'transitionend' listener callbacks into one method
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot committed Apr 12, 2021
1 parent ad10f00 commit 0ceaf3c
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 119 deletions.
15 changes: 3 additions & 12 deletions js/src/alert.js
Expand Up @@ -7,9 +7,7 @@

import {
defineJQueryPlugin,
emulateTransitionEnd,
getElementFromSelector,
getTransitionDurationFromElement
getElementFromSelector
} from './util/index'
import Data from './dom/data'
import EventHandler from './dom/event-handler'
Expand Down Expand Up @@ -75,15 +73,8 @@ class Alert extends BaseComponent {
_removeElement(element) {
element.classList.remove(CLASS_NAME_SHOW)

if (!element.classList.contains(CLASS_NAME_FADE)) {
this._destroyElement(element)
return
}

const transitionDuration = getTransitionDurationFromElement(element)

EventHandler.one(element, 'transitionend', () => this._destroyElement(element))
emulateTransitionEnd(element, transitionDuration)
const isAnimated = element.classList.contains(CLASS_NAME_FADE)
this._queueCallback(() => this._destroyElement(element), element, isAnimated)
}

_destroyElement(element) {
Expand Down
17 changes: 17 additions & 0 deletions js/src/base-component.js
Expand Up @@ -6,6 +6,11 @@
*/

import Data from './dom/data'
import {
emulateTransitionEnd,
execute,
getTransitionDurationFromElement
} from './util/index'
import EventHandler from './dom/event-handler'

/**
Expand Down Expand Up @@ -34,6 +39,18 @@ class BaseComponent {
this._element = null
}

_queueCallback(callback, element, isAnimated = true) {
if (!isAnimated) {
execute(callback)
return
}

const transitionDuration = getTransitionDurationFromElement(element)
EventHandler.one(element, 'transitionend', () => execute(callback))

emulateTransitionEnd(element, transitionDuration)
}

/** Static */

static getInstance(element) {
Expand Down
35 changes: 14 additions & 21 deletions js/src/carousel.js
Expand Up @@ -7,9 +7,7 @@

import {
defineJQueryPlugin,
emulateTransitionEnd,
getElementFromSelector,
getTransitionDurationFromElement,
isRTL,
isVisible,
reflow,
Expand Down Expand Up @@ -454,6 +452,15 @@ class Carousel extends BaseComponent {
this._setActiveIndicatorElement(nextElement)
this._activeElement = nextElement

const triggerSlidEvent = () => {
EventHandler.trigger(this._element, EVENT_SLID, {
relatedTarget: nextElement,
direction: eventDirectionName,
from: activeElementIndex,
to: nextElementIndex
})
}

if (this._element.classList.contains(CLASS_NAME_SLIDE)) {
nextElement.classList.add(orderClassName)

Expand All @@ -462,38 +469,24 @@ class Carousel extends BaseComponent {
activeElement.classList.add(directionalClassName)
nextElement.classList.add(directionalClassName)

const transitionDuration = getTransitionDurationFromElement(activeElement)

EventHandler.one(activeElement, 'transitionend', () => {
const completeCallBack = () => {
nextElement.classList.remove(directionalClassName, orderClassName)
nextElement.classList.add(CLASS_NAME_ACTIVE)

activeElement.classList.remove(CLASS_NAME_ACTIVE, orderClassName, directionalClassName)

this._isSliding = false

setTimeout(() => {
EventHandler.trigger(this._element, EVENT_SLID, {
relatedTarget: nextElement,
direction: eventDirectionName,
from: activeElementIndex,
to: nextElementIndex
})
}, 0)
})
setTimeout(triggerSlidEvent, 0)
}

emulateTransitionEnd(activeElement, transitionDuration)
this._queueCallback(completeCallBack, activeElement, true)
} else {
activeElement.classList.remove(CLASS_NAME_ACTIVE)
nextElement.classList.add(CLASS_NAME_ACTIVE)

this._isSliding = false
EventHandler.trigger(this._element, EVENT_SLID, {
relatedTarget: nextElement,
direction: eventDirectionName,
from: activeElementIndex,
to: nextElementIndex
})
triggerSlidEvent()
}

if (isCycling) {
Expand Down
11 changes: 2 additions & 9 deletions js/src/collapse.js
Expand Up @@ -7,10 +7,8 @@

import {
defineJQueryPlugin,
emulateTransitionEnd,
getSelectorFromElement,
getElementFromSelector,
getTransitionDurationFromElement,
isElement,
reflow,
typeCheckConfig
Expand Down Expand Up @@ -200,11 +198,8 @@ class Collapse extends BaseComponent {

const capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1)
const scrollSize = `scroll${capitalizedDimension}`
const transitionDuration = getTransitionDurationFromElement(this._element)

EventHandler.one(this._element, 'transitionend', complete)

emulateTransitionEnd(this._element, transitionDuration)
this._queueCallback(complete, this._element, true)
this._element.style[dimension] = `${this._element[scrollSize]}px`
}

Expand Down Expand Up @@ -250,10 +245,8 @@ class Collapse extends BaseComponent {
}

this._element.style[dimension] = ''
const transitionDuration = getTransitionDurationFromElement(this._element)

EventHandler.one(this._element, 'transitionend', complete)
emulateTransitionEnd(this._element, transitionDuration)
this._queueCallback(complete, this._element, true)
}

setTransitioning(isTransitioning) {
Expand Down
36 changes: 4 additions & 32 deletions js/src/modal.js
Expand Up @@ -176,14 +176,7 @@ class Modal extends BaseComponent {
EventHandler.off(this._element, EVENT_CLICK_DISMISS)
EventHandler.off(this._dialog, EVENT_MOUSEDOWN_DISMISS)

if (isAnimated) {
const transitionDuration = getTransitionDurationFromElement(this._element)

EventHandler.one(this._element, 'transitionend', event => this._hideModal(event))
emulateTransitionEnd(this._element, transitionDuration)
} else {
this._hideModal()
}
this._queueCallback(() => this._hideModal(), this._element, isAnimated)
}

dispose() {
Expand Down Expand Up @@ -263,14 +256,7 @@ class Modal extends BaseComponent {
})
}

if (isAnimated) {
const transitionDuration = getTransitionDurationFromElement(this._dialog)

EventHandler.one(this._dialog, 'transitionend', transitionComplete)
emulateTransitionEnd(this._dialog, transitionDuration)
} else {
transitionComplete()
}
this._queueCallback(transitionComplete, this._dialog, isAnimated)
}

_enforceFocus() {
Expand Down Expand Up @@ -361,15 +347,7 @@ class Modal extends BaseComponent {

this._backdrop.classList.add(CLASS_NAME_SHOW)

if (!isAnimated) {
callback()
return
}

const backdropTransitionDuration = getTransitionDurationFromElement(this._backdrop)

EventHandler.one(this._backdrop, 'transitionend', callback)
emulateTransitionEnd(this._backdrop, backdropTransitionDuration)
this._queueCallback(callback, this._backdrop, isAnimated)
} else if (!this._isShown && this._backdrop) {
this._backdrop.classList.remove(CLASS_NAME_SHOW)

Expand All @@ -378,13 +356,7 @@ class Modal extends BaseComponent {
callback()
}

if (isAnimated) {
const backdropTransitionDuration = getTransitionDurationFromElement(this._backdrop)
EventHandler.one(this._backdrop, 'transitionend', callbackRemove)
emulateTransitionEnd(this._backdrop, backdropTransitionDuration)
} else {
callbackRemove()
}
this._queueCallback(callbackRemove, this._backdrop, isAnimated)
} else {
callback()
}
Expand Down
5 changes: 2 additions & 3 deletions js/src/offcanvas.js
Expand Up @@ -9,7 +9,6 @@ import {
defineJQueryPlugin,
getElementFromSelector,
getSelectorFromElement,
getTransitionDurationFromElement,
isDisabled,
isVisible,
typeCheckConfig
Expand Down Expand Up @@ -128,7 +127,7 @@ class Offcanvas extends BaseComponent {
this._enforceFocusOnElement(this._element)
}

setTimeout(completeCallBack, getTransitionDurationFromElement(this._element))
this._queueCallback(completeCallBack, this._element, true)
}

hide() {
Expand Down Expand Up @@ -166,7 +165,7 @@ class Offcanvas extends BaseComponent {
this._element.classList.remove(CLASS_NAME_TOGGLING)
}

setTimeout(completeCallback, getTransitionDurationFromElement(this._element))
this._queueCallback(completeCallback, this._element, true)
}

// Private
Expand Down
7 changes: 1 addition & 6 deletions js/src/tab.js
Expand Up @@ -7,9 +7,7 @@

import {
defineJQueryPlugin,
emulateTransitionEnd,
getElementFromSelector,
getTransitionDurationFromElement,
isDisabled,
reflow
} from './util/index'
Expand Down Expand Up @@ -126,11 +124,8 @@ class Tab extends BaseComponent {
const complete = () => this._transitionComplete(element, active, callback)

if (active && isTransitioning) {
const transitionDuration = getTransitionDurationFromElement(active)
active.classList.remove(CLASS_NAME_SHOW)

EventHandler.one(active, 'transitionend', complete)
emulateTransitionEnd(active, transitionDuration)
this._queueCallback(complete, element, true)
} else {
complete()
}
Expand Down
19 changes: 2 additions & 17 deletions js/src/toast.js
Expand Up @@ -7,8 +7,6 @@

import {
defineJQueryPlugin,
emulateTransitionEnd,
getTransitionDurationFromElement,
reflow,
typeCheckConfig
} from './util/index'
Expand Down Expand Up @@ -112,14 +110,8 @@ class Toast extends BaseComponent {
this._element.classList.remove(CLASS_NAME_HIDE)
reflow(this._element)
this._element.classList.add(CLASS_NAME_SHOWING)
if (this._config.animation) {
const transitionDuration = getTransitionDurationFromElement(this._element)

EventHandler.one(this._element, 'transitionend', complete)
emulateTransitionEnd(this._element, transitionDuration)
} else {
complete()
}
this._queueCallback(complete, this._element, this._config.animation)
}

hide() {
Expand All @@ -139,14 +131,7 @@ class Toast extends BaseComponent {
}

this._element.classList.remove(CLASS_NAME_SHOW)
if (this._config.animation) {
const transitionDuration = getTransitionDurationFromElement(this._element)

EventHandler.one(this._element, 'transitionend', complete)
emulateTransitionEnd(this._element, transitionDuration)
} else {
complete()
}
this._queueCallback(complete, this._element, this._config.animation)
}

dispose() {
Expand Down
22 changes: 4 additions & 18 deletions js/src/tooltip.js
Expand Up @@ -9,9 +9,7 @@ import * as Popper from '@popperjs/core'

import {
defineJQueryPlugin,
emulateTransitionEnd,
findShadowRoot,
getTransitionDurationFromElement,
getUID,
isElement,
isRTL,
Expand Down Expand Up @@ -315,13 +313,8 @@ class Tooltip extends BaseComponent {
}
}

if (this.tip.classList.contains(CLASS_NAME_FADE)) {
const transitionDuration = getTransitionDurationFromElement(this.tip)
EventHandler.one(this.tip, 'transitionend', complete)
emulateTransitionEnd(this.tip, transitionDuration)
} else {
complete()
}
const isAnimated = this.tip.classList.contains(CLASS_NAME_FADE)
this._queueCallback(complete, this.tip, isAnimated)
}

hide() {
Expand Down Expand Up @@ -367,15 +360,8 @@ class Tooltip extends BaseComponent {
this._activeTrigger[TRIGGER_FOCUS] = false
this._activeTrigger[TRIGGER_HOVER] = false

if (this.tip.classList.contains(CLASS_NAME_FADE)) {
const transitionDuration = getTransitionDurationFromElement(tip)

EventHandler.one(tip, 'transitionend', complete)
emulateTransitionEnd(tip, transitionDuration)
} else {
complete()
}

const isAnimated = this.tip.classList.contains(CLASS_NAME_FADE)
this._queueCallback(complete, this.tip, isAnimated)
this._hoverState = ''
}

Expand Down
9 changes: 8 additions & 1 deletion js/src/util/index.js
Expand Up @@ -230,6 +230,12 @@ const defineJQueryPlugin = (name, plugin) => {
})
}

const execute = callback => {
if (typeof callback === 'function') {
callback()
}
}

export {
getUID,
getSelectorFromElement,
Expand All @@ -247,5 +253,6 @@ export {
getjQuery,
onDOMContentLoaded,
isRTL,
defineJQueryPlugin
defineJQueryPlugin,
execute
}

0 comments on commit 0ceaf3c

Please sign in to comment.