webpack-bootstrap-ui-kit/src/js_old/_components/_ui.carousel.js

136 lines
3.4 KiB
JavaScript
Raw Normal View History

2022-05-03 20:50:57 +02:00
'use strict'
2020-12-24 23:42:33 +01:00
2022-05-03 20:50:57 +02:00
import $ from 'jquery'
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
import 'hammerjs/hammer'
import 'jquery-hammerjs/jquery.hammer'
2019-07-10 20:59:57 +02:00
2022-05-03 20:50:57 +02:00
import Events from '../_events'
2019-06-08 17:20:51 +02:00
const CarouselUI = (($) => {
2019-06-08 17:20:51 +02:00
// Constants
2022-05-03 20:50:57 +02:00
const NAME = 'CarouselUI'
2019-06-08 17:20:51 +02:00
class CarouselUI {
// Static methods
2022-05-03 20:50:57 +02:00
static each (callback) {
$(`js${NAME}, .js-carousel`).each((i, e) => {
2022-05-03 20:50:57 +02:00
callback(i, $(e))
})
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
static init () {
this.dispose()
console.log(`${NAME}: init`)
2019-06-08 17:20:51 +02:00
this.each((i, e) => {
2022-05-03 20:50:57 +02:00
const $e = $(e)
const id = `Carousel${i}`
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
$e.attr('id', id)
$e.data('id', i)
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
const $items = $(e).find('.carousel-item')
const count = $items.length
2019-06-08 17:20:51 +02:00
if (!count) {
2022-05-03 20:50:57 +02:00
return
2019-06-08 17:20:51 +02:00
}
// create carousel-controls
2022-05-03 20:50:57 +02:00
if ($e.data('indicators')) {
const $indicators = $('<ol class="carousel-indicators"></ol>')
$indicators.append(
2021-08-18 20:51:15 +02:00
`<li data-target="#${id}" data-slide-to="0" class="active"></li>`
2022-05-03 20:50:57 +02:00
)
2019-06-08 17:20:51 +02:00
for (let i = 1; i < count; i++) {
$indicators.append(
2021-08-18 20:51:15 +02:00
`<li data-target="#${id}" data-slide-to="${i}"></li>`
2022-05-03 20:50:57 +02:00
)
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
$e.prepend($indicators)
2019-06-08 17:20:51 +02:00
}
// create arrows
2022-05-03 20:50:57 +02:00
if ($e.data('arrows')) {
$e.prepend(
2021-08-18 20:51:15 +02:00
`<i class="carousel-control-prev" data-target="#${id}" role="button" data-slide="prev"><i class="fas fa-chevron-left" aria-hidden="true"></i><i class="sr-only">Previous</i></i>`
2022-05-03 20:50:57 +02:00
)
$e.prepend(
2021-08-18 20:51:15 +02:00
`<i class="carousel-control-next" data-target="#${id}" role="button" data-slide="next"><i class="fas fa-chevron-right" aria-hidden="true"></i><i class="sr-only">Next</i></i>`
2022-05-03 20:50:57 +02:00
)
2019-06-08 17:20:51 +02:00
}
// init carousel
2022-05-03 20:50:57 +02:00
$e.carousel()
2019-06-08 17:20:51 +02:00
const $youtubeSlides = $e.find(
2021-08-18 20:51:15 +02:00
'iframe[src^="https://www.youtube.com/embed/"]'
2022-05-03 20:50:57 +02:00
)
2019-08-27 17:25:41 +02:00
2022-05-03 20:50:57 +02:00
$e.on('slide.bs.carousel', () => {
2019-08-27 17:25:41 +02:00
if ($youtubeSlides.length) {
$youtubeSlides.each((i, e) => {
2022-05-03 20:50:57 +02:00
const $e = $(e)
2019-08-27 17:25:41 +02:00
try {
$e.data(
2022-05-03 20:50:57 +02:00
'player',
new YT.Player(e, {
events: {
onReady: () => {
2022-05-03 20:50:57 +02:00
$e.data('player').pauseVideo()
},
2019-10-20 01:40:40 +02:00
},
2021-08-18 20:51:15 +02:00
})
2022-05-03 20:50:57 +02:00
)
2019-08-27 17:25:41 +02:00
2022-05-03 20:50:57 +02:00
$e.data('player').pauseVideo()
2019-08-27 17:25:41 +02:00
} catch (e) {}
2022-05-03 20:50:57 +02:00
})
2019-08-27 17:25:41 +02:00
}
2022-05-03 20:50:57 +02:00
})
2019-08-27 17:25:41 +02:00
2022-05-03 20:50:57 +02:00
$e.find('.carousel-control-prev').on('click', (e) => {
e.preventDefault()
$e.carousel('prev')
})
2019-08-27 17:25:41 +02:00
2022-05-03 20:50:57 +02:00
$e.find('.carousel-control-next').on('click', (e) => {
e.preventDefault()
$e.carousel('next')
})
2019-08-27 17:25:41 +02:00
2019-06-08 17:20:51 +02:00
// init touch swipes
$e.hammer().bind(Events.SWIPELEFT, (e) => {
2022-05-03 20:50:57 +02:00
$(event.target).carousel('next')
})
2019-06-08 17:20:51 +02:00
$e.hammer().bind(Events.SWIPERIGHT, (e) => {
2022-05-03 20:50:57 +02:00
$(event.target).carousel('prev')
})
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
/* $e.find('.carousel-item').hammer().bind('tap', (event) => {
2019-06-08 17:20:51 +02:00
$(event.target).carousel('next');
2022-05-03 20:50:57 +02:00
}); */
2022-05-03 20:50:57 +02:00
$e.addClass(`js${NAME}-active`)
$e.trigger(Events.CAROUSEL_READY)
})
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
static dispose () {
2019-06-08 17:20:51 +02:00
this.each((i, e) => {
2022-05-03 20:50:57 +02:00
$(e).carousel('dispose')
})
2019-06-08 17:20:51 +02:00
}
}
$(window).on(`${Events.LODEDANDREADY}`, () => {
2022-05-03 20:50:57 +02:00
CarouselUI.init()
})
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
return CarouselUI
})($)
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
export default CarouselUI