1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2025-02-25 22:48:00 +01:00

expand-video.js: format

This commit is contained in:
Zankaria 2024-08-05 19:20:55 +02:00
parent d9a333a69f
commit 85b03c0fb0

View File

@ -2,26 +2,32 @@
/* Note: This code expects the global variable configRoot to be set. */
if (typeof _ == 'undefined') {
var _ = function(a) { return a; };
var _ = function(a) {
return a;
};
}
function setupVideo(thumb, url) {
if (thumb.videoAlreadySetUp) return;
if (thumb.videoAlreadySetUp) {
return;
}
thumb.videoAlreadySetUp = true;
var video = null;
var videoContainer, videoHide;
var expanded = false;
var hovering = false;
var loop = true;
var loopControls = [document.createElement("span"), document.createElement("span")];
var fileInfo = thumb.parentNode.querySelector(".fileinfo");
var mouseDown = false;
let video = null;
let videoContainer, videoHide;
let expanded = false;
let hovering = false;
let loop = true;
let loopControls = [document.createElement("span"), document.createElement("span")];
let fileInfo = thumb.parentNode.querySelector(".fileinfo");
let mouseDown = false;
function unexpand() {
if (expanded) {
expanded = false;
if (video.pause) video.pause();
if (video.pause) {
video.pause();
}
videoContainer.style.display = "none";
thumb.style.display = "inline";
video.style.maxWidth = "inherit";
@ -32,7 +38,9 @@ function setupVideo(thumb, url) {
function unhover() {
if (hovering) {
hovering = false;
if (video.pause) video.pause();
if (video.pause) {
video.pause();
}
videoContainer.style.display = "none";
video.style.maxWidth = "inherit";
video.style.maxHeight = "inherit";
@ -129,16 +137,18 @@ function setupVideo(thumb, url) {
expanded = false;
hovering = true;
var docRight = document.documentElement.getBoundingClientRect().right;
var thumbRight = thumb.querySelector("img, video").getBoundingClientRect().right;
var maxWidth = docRight - thumbRight - 20;
if (maxWidth < 250) maxWidth = 250;
let docRight = document.documentElement.getBoundingClientRect().right;
let thumbRight = thumb.querySelector("img, video").getBoundingClientRect().right;
let maxWidth = docRight - thumbRight - 20;
if (maxWidth < 250) {
maxWidth = 250;
}
video.style.position = "fixed";
video.style.right = "0px";
video.style.top = "0px";
var docRight = document.documentElement.getBoundingClientRect().right;
var thumbRight = thumb.querySelector("img, video").getBoundingClientRect().right;
docRight = document.documentElement.getBoundingClientRect().right;
thumbRight = thumb.querySelector("img, video").getBoundingClientRect().right;
video.style.maxWidth = maxWidth + "px";
video.style.maxHeight = "100%";
video.style.pointerEvents = "none";
@ -161,10 +171,18 @@ function setupVideo(thumb, url) {
thumb.addEventListener("wheel", function(e) {
if (setting("videohover")) {
var volume = setting("videovolume");
if (e.deltaY > 0) volume -= 0.1;
if (e.deltaY < 0) volume += 0.1;
if (volume < 0) volume = 0;
if (volume > 1) volume = 1;
if (e.deltaY > 0) {
volume -= 0.1;
}
if (e.deltaY < 0) {
volume += 0.1;
}
if (volume < 0) {
volume = 0;
}
if (volume > 1) {
volume = 1;
}
if (video != null) {
video.muted = (volume == 0);
video.volume = volume;
@ -202,15 +220,17 @@ function setupVideo(thumb, url) {
}
function setupVideosIn(element) {
var thumbs = element.querySelectorAll("a.file");
for (var i = 0; i < thumbs.length; i++) {
let thumbs = element.querySelectorAll("a.file");
for (let i = 0; i < thumbs.length; i++) {
if (/\.webm$|\.mp4$/.test(thumbs[i].pathname)) {
setupVideo(thumbs[i], thumbs[i].href);
} else {
var m = thumbs[i].search.match(/\bv=([^&]*)/);
let m = thumbs[i].search.match(/\bv=([^&]*)/);
if (m != null) {
var url = decodeURIComponent(m[1]);
if (/\.webm$|\.mp4$/.test(url)) setupVideo(thumbs[i], url);
let url = decodeURIComponent(m[1]);
if (/\.webm$|\.mp4$/.test(url)) {
setupVideo(thumbs[i], url);
}
}
}
}
@ -218,20 +238,23 @@ function setupVideosIn(element) {
onready(function(){
// Insert menu from settings.js
if (typeof settingsMenu != "undefined" && typeof Options == "undefined")
if (typeof settingsMenu != "undefined" && typeof Options == "undefined") {
document.body.insertBefore(settingsMenu, document.getElementsByTagName("hr")[0]);
}
// Setup Javascript events for videos in document now
setupVideosIn(document);
// Setup Javascript events for videos added by updater
if (window.MutationObserver) {
var observer = new MutationObserver(function(mutations) {
for (var i = 0; i < mutations.length; i++) {
var additions = mutations[i].addedNodes;
if (additions == null) continue;
for (var j = 0; j < additions.length; j++) {
var node = additions[j];
let observer = new MutationObserver(function(mutations) {
for (let i = 0; i < mutations.length; i++) {
let additions = mutations[i].addedNodes;
if (additions == null) {
continue;
}
for (let j = 0; j < additions.length; j++) {
let node = additions[j];
if (node.nodeType == 1) {
setupVideosIn(node);
}
@ -241,4 +264,3 @@ onready(function(){
observer.observe(document.body, {childList: true, subtree: true});
}
});