decentralized-video-chat/public/js/carousel.js

196 lines
6.2 KiB
JavaScript
Raw Normal View History

2020-03-31 02:16:08 +08:00
(function () {
"use strict";
2020-03-24 11:20:28 +08:00
// Swipe detector
// https://gist.github.com/chrishaensel/e17c9f3838f246d75fe3bd19d6bb92e8#file-swipe-js
let swipe = {
touchStartX: 0,
touchEndX: 0,
minSwipePixels: 30,
detectionZone: undefined,
2020-03-31 02:16:08 +08:00
swipeCallback: function () {},
2020-03-24 11:20:28 +08:00
init: function (detectionZone, callback) {
2020-03-31 02:16:08 +08:00
swipe.swipeCallback = callback;
detectionZone.addEventListener(
"touchstart",
function (event) {
swipe.touchStartX = event.changedTouches[0].screenX;
},
false
);
detectionZone.addEventListener(
"touchend",
function (event) {
swipe.touchEndX = event.changedTouches[0].screenX;
swipe.handleSwipeGesture();
},
false
);
2020-03-24 11:20:28 +08:00
},
handleSwipeGesture: function () {
2020-03-31 02:16:08 +08:00
let direction, moved;
2020-03-24 11:20:28 +08:00
if (swipe.touchEndX <= swipe.touchStartX) {
2020-03-31 02:16:08 +08:00
moved = swipe.touchStartX - swipe.touchEndX;
direction = "left";
2020-03-24 11:20:28 +08:00
}
if (swipe.touchEndX >= swipe.touchStartX) {
2020-03-31 02:16:08 +08:00
moved = swipe.touchEndX - swipe.touchStartX;
direction = "right";
2020-03-24 11:20:28 +08:00
}
2020-03-31 02:16:08 +08:00
if (moved > swipe.minSwipePixels && direction !== "undefined") {
swipe.swipe(direction, moved);
2020-03-24 11:20:28 +08:00
}
},
swipe: function (direction, movedPixels) {
2020-03-31 02:16:08 +08:00
let ret = {};
ret.direction = direction;
ret.movedPixels = movedPixels;
swipe.swipeCallback(ret);
},
};
2020-03-24 11:20:28 +08:00
2020-03-31 02:16:08 +08:00
const carousels = document.getElementsByClassName("carousel-items");
2020-03-24 11:20:28 +08:00
// Rotate the carousel forward or backward
function rotateCarousel(el, dir) {
if (dir === undefined) {
2020-03-31 02:16:08 +08:00
dir = "next";
2020-03-24 11:20:28 +08:00
}
2020-03-31 02:16:08 +08:00
let currentItem = el.getElementsByClassName("carousel-item is-active")[0];
let nextItem =
dir === "next"
? currentItem.nextElementSibling
: currentItem.previousElementSibling;
let index = currentItem.getAttribute("data-carousel");
let currentBullet = el.parentNode.getElementsByClassName("carousel-bullet")[
index
];
let nextBullet =
dir === "next"
? currentBullet.nextElementSibling
: currentBullet.previousElementSibling;
currentItem.classList.remove("is-active");
currentBullet.classList.remove("is-active");
2020-03-24 11:20:28 +08:00
if (nextItem) {
2020-03-31 02:16:08 +08:00
nextItem.classList.add("is-active");
nextBullet.classList.add("is-active");
2020-03-24 11:20:28 +08:00
} else {
2020-03-31 02:16:08 +08:00
if (dir === "next") {
el.firstElementChild.classList.add("is-active");
el.parentNode
.getElementsByClassName("carousel-bullets")[0]
.firstElementChild.classList.add("is-active");
2020-03-24 11:20:28 +08:00
} else {
2020-03-31 02:16:08 +08:00
el.lastElementChild.classList.add("is-active");
el.parentNode
.getElementsByClassName("carousel-bullets")[0]
.lastElementChild.classList.add("is-active");
2020-03-24 11:20:28 +08:00
}
}
}
// Equal heights fix
function equalHeightCarousel(carousel, items) {
2020-03-31 02:16:08 +08:00
let taller = 0;
let height;
2020-03-24 11:20:28 +08:00
for (let i = 0; i < items.length; i++) {
2020-03-31 02:16:08 +08:00
items[0].parentNode.style.minHeight = taller + "px";
items[i].classList.add("is-loading");
height = items[i].offsetHeight;
items[i].classList.remove("is-loading");
2020-03-24 11:20:28 +08:00
if (height > taller) {
2020-03-31 02:16:08 +08:00
taller = height;
2020-03-24 11:20:28 +08:00
}
}
2020-03-31 02:16:08 +08:00
items[0].parentNode.style.minHeight = taller + "px";
2020-03-24 11:20:28 +08:00
}
// Clear autorotate
function clearAutorotate(autorotate) {
if (autorotate) {
2020-03-31 02:16:08 +08:00
clearInterval(autorotate);
2020-03-24 11:20:28 +08:00
}
}
if (carousels.length > 0) {
for (let i = 0; i < carousels.length; i++) {
2020-03-31 02:16:08 +08:00
let carousel = carousels[i];
let items = carousel.getElementsByClassName("carousel-item");
let activeItem = 0;
let autorotateTiming = carousel.getAttribute("data-autorotate");
2020-03-24 11:20:28 +08:00
// Generate bullets container
2020-03-31 02:16:08 +08:00
const bulletsContainer = document.createElement("div");
bulletsContainer.className = "carousel-bullets";
carousel.parentNode.insertBefore(bulletsContainer, carousel.nextSibling);
2020-03-24 11:20:28 +08:00
for (let i = 0; i < items.length; i++) {
// Add data attributes
2020-03-31 02:16:08 +08:00
items[i].setAttribute("data-carousel", i);
2020-03-24 11:20:28 +08:00
// Determine a new active item, if any
2020-03-31 02:16:08 +08:00
if (items[i].classList.contains("is-active")) activeItem = i;
2020-03-24 11:20:28 +08:00
// Generate bullets
2020-03-31 02:16:08 +08:00
let bullet = document.createElement("button");
bullet.className = "carousel-bullet";
bullet.setAttribute("data-bullet", i);
carousel.parentNode
.getElementsByClassName("carousel-bullets")[0]
.appendChild(bullet);
2020-03-24 11:20:28 +08:00
}
// Add is-active class to first carousel item and bullet
2020-03-31 02:16:08 +08:00
items[activeItem].classList.add("is-active");
let bullets = carousel.parentNode.getElementsByClassName(
"carousel-bullet"
);
bullets[activeItem].classList.add("is-active");
2020-03-24 11:20:28 +08:00
// Equal height items
2020-03-31 02:16:08 +08:00
equalHeightCarousel(carousel, items);
window.addEventListener("resize", function () {
equalHeightCarousel(carousel, items);
});
2020-03-24 11:20:28 +08:00
// Autorotate
2020-03-31 02:16:08 +08:00
let autorotate = false;
2020-03-24 11:20:28 +08:00
if (autorotateTiming) {
autorotate = setInterval(function () {
2020-03-31 02:16:08 +08:00
rotateCarousel(carousel, "next");
}, autorotateTiming);
2020-03-24 11:20:28 +08:00
}
// Rotate by bullet click
for (let i = 0; i < bullets.length; i++) {
2020-03-31 02:16:08 +08:00
let bullet = bullets[i];
bullet.addEventListener("click", function (e) {
e.preventDefault();
2020-03-24 11:20:28 +08:00
// Do nothing if item is active
2020-03-31 02:16:08 +08:00
if (bullet.classList.contains("is-active")) {
return;
2020-03-24 11:20:28 +08:00
}
// Remove active classes
for (let i = 0; i < bullets.length; i++) {
2020-03-31 02:16:08 +08:00
bullets[i].classList.remove("is-active");
2020-03-24 11:20:28 +08:00
}
for (let i = 0; i < items.length; i++) {
2020-03-31 02:16:08 +08:00
items[i].classList.remove("is-active");
2020-03-24 11:20:28 +08:00
}
// Add active classes to corresponding items and bullets
2020-03-31 02:16:08 +08:00
let index = this.getAttribute("data-bullet");
items[index].classList.add("is-active");
this.classList.add("is-active");
2020-03-24 11:20:28 +08:00
// Clear autorotate timing
2020-03-31 02:16:08 +08:00
clearAutorotate(autorotate);
});
2020-03-24 11:20:28 +08:00
}
// Rotate on swipe
swipe.init(carousel, function (e) {
2020-03-31 02:16:08 +08:00
if (e.direction === "left") {
rotateCarousel(carousel, "next");
} else if (e.direction === "right") {
rotateCarousel(carousel, "prev");
2020-03-24 11:20:28 +08:00
}
// Clear autorotate timing
2020-03-31 02:16:08 +08:00
clearAutorotate(autorotate);
});
2020-03-24 11:20:28 +08:00
}
}
2020-03-31 02:16:08 +08:00
})();