0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-30 22:52:24 +01:00

Carousel: add a getItems helper

This commit is contained in:
GeoSot 2022-03-02 02:04:03 +02:00 committed by XhmikosR
parent b7cce49dbc
commit fcc2c80976

View File

@ -95,7 +95,6 @@ class Carousel extends BaseComponent {
constructor(element, config) { constructor(element, config) {
super(element, config) super(element, config)
this._items = null
this._interval = null this._interval = null
this._activeElement = null this._activeElement = null
this._isPaused = false this._isPaused = false
@ -165,10 +164,8 @@ class Carousel extends BaseComponent {
} }
to(index) { to(index) {
this._activeElement = this._getActive() const items = this._getItems()
const activeIndex = this._getItemIndex(this._activeElement) if (index > items.length - 1 || index < 0) {
if (index > this._items.length - 1 || index < 0) {
return return
} }
@ -177,17 +174,16 @@ class Carousel extends BaseComponent {
return return
} }
const activeIndex = this._getItemIndex(this._getActive())
if (activeIndex === index) { if (activeIndex === index) {
this.pause() this.pause()
this.cycle() this.cycle()
return return
} }
const order = index > activeIndex ? const order = index > activeIndex ? ORDER_NEXT : ORDER_PREV
ORDER_NEXT :
ORDER_PREV
this._slide(order, this._items[index]) this._slide(order, items[index])
} }
dispose() { dispose() {
@ -267,14 +263,12 @@ class Carousel extends BaseComponent {
} }
_getItemIndex(element) { _getItemIndex(element) {
this._items = SelectorEngine.find(SELECTOR_ITEM, this._element) return this._getItems().indexOf(element)
return this._items.indexOf(element)
} }
_getItemByOrder(order, activeElement) { _getItemByOrder(order, activeElement) {
const isNext = order === ORDER_NEXT const isNext = order === ORDER_NEXT
return getNextActiveElement(this._items, activeElement, isNext, this._config.wrap) return getNextActiveElement(this._getItems(), activeElement, isNext, this._config.wrap)
} }
_setActiveIndicatorElement(index) { _setActiveIndicatorElement(index) {
@ -392,6 +386,10 @@ class Carousel extends BaseComponent {
return SelectorEngine.findOne(SELECTOR_ACTIVE_ITEM, this._element) return SelectorEngine.findOne(SELECTOR_ACTIVE_ITEM, this._element)
} }
_getItems() {
return SelectorEngine.find(SELECTOR_ITEM, this._element)
}
_clearInterval() { _clearInterval() {
if (this._interval) { if (this._interval) {
clearInterval(this._interval) clearInterval(this._interval)