Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carousel tweaks #35983

Merged
merged 4 commits into from Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 35 additions & 37 deletions js/src/carousel.js
Expand Up @@ -71,19 +71,19 @@ const KEY_TO_DIRECTION = {
const Default = {
interval: 5000,
keyboard: true,
slide: false,
pause: 'hover',
wrap: true,
touch: true
ride: false,
touch: true,
wrap: true
}

const DefaultType = {
interval: '(number|boolean)',
keyboard: 'boolean',
slide: '(boolean|string)',
ride: '(boolean|string)',
pause: '(string|boolean)',
wrap: 'boolean',
touch: 'boolean'
touch: 'boolean',
wrap: 'boolean'
}

/**
Expand All @@ -96,13 +96,16 @@ class Carousel extends BaseComponent {

this._interval = null
this._activeElement = null
this._stayPaused = false
this._isSliding = false
this.touchTimeout = null
this._swipeHelper = null

this._indicatorsElement = SelectorEngine.findOne(SELECTOR_INDICATORS, this._element)
this._addEventListeners()

if (this._config.ride === CLASS_NAME_CAROUSEL) {
this.cycle()
}
}

// Getters
Expand Down Expand Up @@ -136,30 +139,32 @@ class Carousel extends BaseComponent {
this._slide(ORDER_PREV)
}

pause(event) {
if (!event) {
this._stayPaused = true
}

pause() {
if (this._isSliding) {
triggerTransitionEnd(this._element)
this.cycle(true)
}

this._clearInterval()
}

cycle(event) {
if (!event) {
this._stayPaused = false
}

cycle() {
this._clearInterval()
if (this._config.interval && !this._stayPaused) {
this._updateInterval()
this._updateInterval()

this._interval = setInterval(() => this.nextWhenVisible(), this._config.interval)
this._interval = setInterval(() => this.nextWhenVisible(), this._config.interval)
}

_maybeEnableCycle() {
if (!this._config.ride) {
return
}

if (this._isSliding) {
EventHandler.one(this._element, EVENT_SLID, () => this.cycle())
return
}

this.cycle()
}

to(index) {
Expand All @@ -175,8 +180,6 @@ class Carousel extends BaseComponent {

const activeIndex = this._getItemIndex(this._getActive())
if (activeIndex === index) {
this.pause()
this.cycle()
return
}

Expand Down Expand Up @@ -205,8 +208,8 @@ class Carousel extends BaseComponent {
}

if (this._config.pause === 'hover') {
EventHandler.on(this._element, EVENT_MOUSEENTER, event => this.pause(event))
EventHandler.on(this._element, EVENT_MOUSELEAVE, event => this.cycle(event))
EventHandler.on(this._element, EVENT_MOUSEENTER, () => this.pause())
EventHandler.on(this._element, EVENT_MOUSELEAVE, () => this._maybeEnableCycle())
}

if (this._config.touch && Swipe.isSupported()) {
Expand Down Expand Up @@ -237,7 +240,7 @@ class Carousel extends BaseComponent {
clearTimeout(this.touchTimeout)
}

this.touchTimeout = setTimeout(event => this.cycle(event), TOUCHEVENT_COMPAT_WAIT + this._config.interval)
this.touchTimeout = setTimeout(() => this._maybeEnableCycle(), TOUCHEVENT_COMPAT_WAIT + this._config.interval)
}

const swipeConfig = {
Expand Down Expand Up @@ -331,12 +334,10 @@ class Carousel extends BaseComponent {
return
}

this._isSliding = true

const isCycling = Boolean(this._interval)
if (isCycling) {
this.pause()
}
this.pause()

this._isSliding = true

this._setActiveIndicatorElement(nextElementIndex)
this._activeElement = nextElement
Expand Down Expand Up @@ -420,12 +421,6 @@ class Carousel extends BaseComponent {
}

data[config]()
return
}

if (data._config.interval && data._config.ride) {
data.pause()
data.cycle()
}
})
}
Expand All @@ -449,15 +444,18 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_SLIDE, function (e

if (slideIndex) {
carousel.to(slideIndex)
carousel._maybeEnableCycle()
return
}

if (Manipulator.getDataAttribute(this, 'slide') === 'next') {
carousel.next()
carousel._maybeEnableCycle()
return
}

carousel.prev()
carousel._maybeEnableCycle()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just thinking if these calls could be inside the _slide function. I mean I tried and they could, but I'm not sure about the aim of the _maybeEnableCycle function. If the actual solution makes more sense to you, let's keep it as is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • cycle is called programmatically, and should always work triggering a slide action.
  • _maybeEnableCycle is called as a result of a user interaction, so it might not lead to a slide action.
    Inside _slidewe do not know who called the method in order to initiate the cycle process.

(saying all these, I wish I do not miss something)

As for the places it is placed the _maybeEnableCycle , Ii tried to keep it as close a possible to the initial code.
We may keep it for now and we can revisit later for more improvement

})

EventHandler.on(window, EVENT_LOAD_DATA_API, () => {
Expand Down