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

136 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-08-18 20:51:15 +02:00
"use strict";
2020-12-24 23:42:33 +01:00
2021-08-18 20:51:15 +02:00
import $ from "jquery";
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
import "hammerjs/hammer";
import "jquery-hammerjs/jquery.hammer";
2019-07-10 20:59:57 +02:00
2021-08-18 20:51:15 +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
2021-08-18 20:51:15 +02:00
const NAME = "CarouselUI";
2019-06-08 17:20:51 +02:00
class CarouselUI {
// Static methods
static each(callback) {
$(`js${NAME}, .js-carousel`).each((i, e) => {
2019-06-08 17:20:51 +02:00
callback(i, $(e));
});
}
static init() {
this.dispose();
2020-09-09 17:40:58 +02:00
console.log(`${NAME}: init`);
2019-06-08 17:20:51 +02:00
this.each((i, e) => {
const $e = $(e),
id = `Carousel${i}`;
2021-08-18 20:51:15 +02:00
$e.attr("id", id);
$e.data("id", i);
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
const $items = $(e).find(".carousel-item"),
2019-06-08 17:20:51 +02:00
count = $items.length;
if (!count) {
return;
}
// create carousel-controls
2021-08-18 20:51:15 +02:00
if ($e.data("indicators")) {
2019-06-08 17:20:51 +02:00
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>`
);
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>`
);
2019-06-08 17:20:51 +02:00
}
$e.prepend($indicators);
}
// create arrows
2021-08-18 20:51:15 +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>`
);
$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>`
);
2019-06-08 17:20:51 +02:00
}
// init carousel
$e.carousel();
const $youtubeSlides = $e.find(
2021-08-18 20:51:15 +02:00
'iframe[src^="https://www.youtube.com/embed/"]'
);
2019-08-27 17:25:41 +02:00
2021-08-18 20:51:15 +02:00
$e.on("slide.bs.carousel", () => {
2019-08-27 17:25:41 +02:00
if ($youtubeSlides.length) {
$youtubeSlides.each((i, e) => {
const $e = $(e);
try {
$e.data(
2021-08-18 20:51:15 +02:00
"player",
new YT.Player(e, {
events: {
onReady: () => {
2021-08-18 20:51:15 +02:00
$e.data("player").pauseVideo();
},
2019-10-20 01:40:40 +02:00
},
2021-08-18 20:51:15 +02:00
})
);
2019-08-27 17:25:41 +02:00
2021-08-18 20:51:15 +02:00
$e.data("player").pauseVideo();
2019-08-27 17:25:41 +02:00
} catch (e) {}
});
}
});
2021-08-18 20:51:15 +02:00
$e.find(".carousel-control-prev").on("click", (e) => {
2019-08-27 17:25:41 +02:00
e.preventDefault();
2021-08-18 20:51:15 +02:00
$e.carousel("prev");
2019-08-27 17:25:41 +02:00
});
2021-08-18 20:51:15 +02:00
$e.find(".carousel-control-next").on("click", (e) => {
2019-08-27 17:25:41 +02:00
e.preventDefault();
2021-08-18 20:51:15 +02:00
$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) => {
2021-08-18 20:51:15 +02:00
$(event.target).carousel("next");
2019-06-08 17:20:51 +02:00
});
$e.hammer().bind(Events.SWIPERIGHT, (e) => {
2021-08-18 20:51:15 +02:00
$(event.target).carousel("prev");
2019-06-08 17:20:51 +02:00
});
2019-10-20 01:40:40 +02:00
/*$e.find('.carousel-item').hammer().bind('tap', (event) => {
2019-06-08 17:20:51 +02:00
$(event.target).carousel('next');
2019-10-20 01:40:40 +02:00
});*/
$e.addClass(`js${NAME}-active`);
$e.trigger(Events.CAROUSEL_READY);
2019-06-08 17:20:51 +02:00
});
}
static dispose() {
this.each((i, e) => {
2021-08-18 20:51:15 +02:00
$(e).carousel("dispose");
2019-06-08 17:20:51 +02:00
});
}
}
$(window).on(`${Events.LODEDANDREADY}`, () => {
2019-06-08 17:20:51 +02:00
CarouselUI.init();
});
return CarouselUI;
})($);
2019-07-10 20:59:57 +02:00
export default CarouselUI;