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,243 +2,265 @@
/* Note: This code expects the global variable configRoot to be set. */ /* Note: This code expects the global variable configRoot to be set. */
if (typeof _ == 'undefined') { if (typeof _ == 'undefined') {
var _ = function(a) { return a; }; var _ = function(a) {
return a;
};
} }
function setupVideo(thumb, url) { function setupVideo(thumb, url) {
if (thumb.videoAlreadySetUp) return; if (thumb.videoAlreadySetUp) {
thumb.videoAlreadySetUp = true; return;
}
thumb.videoAlreadySetUp = true;
var video = null; let video = null;
var videoContainer, videoHide; let videoContainer, videoHide;
var expanded = false; let expanded = false;
var hovering = false; let hovering = false;
var loop = true; let loop = true;
var loopControls = [document.createElement("span"), document.createElement("span")]; let loopControls = [document.createElement("span"), document.createElement("span")];
var fileInfo = thumb.parentNode.querySelector(".fileinfo"); let fileInfo = thumb.parentNode.querySelector(".fileinfo");
var mouseDown = false; let mouseDown = false;
function unexpand() { function unexpand() {
if (expanded) { if (expanded) {
expanded = false; expanded = false;
if (video.pause) video.pause(); if (video.pause) {
videoContainer.style.display = "none"; video.pause();
thumb.style.display = "inline"; }
video.style.maxWidth = "inherit"; videoContainer.style.display = "none";
video.style.maxHeight = "inherit"; thumb.style.display = "inline";
} video.style.maxWidth = "inherit";
} video.style.maxHeight = "inherit";
}
}
function unhover() { function unhover() {
if (hovering) { if (hovering) {
hovering = false; hovering = false;
if (video.pause) video.pause(); if (video.pause) {
videoContainer.style.display = "none"; video.pause();
video.style.maxWidth = "inherit"; }
video.style.maxHeight = "inherit"; videoContainer.style.display = "none";
} video.style.maxWidth = "inherit";
} video.style.maxHeight = "inherit";
}
}
// Create video element if does not exist yet // Create video element if does not exist yet
function getVideo() { function getVideo() {
if (video == null) { if (video == null) {
video = document.createElement("video"); video = document.createElement("video");
video.src = url; video.src = url;
video.loop = loop; video.loop = loop;
video.innerText = _("Your browser does not support HTML5 video."); video.innerText = _("Your browser does not support HTML5 video.");
videoHide = document.createElement("img"); videoHide = document.createElement("img");
videoHide.src = configRoot + "static/collapse.gif"; videoHide.src = configRoot + "static/collapse.gif";
videoHide.alt = "[ - ]"; videoHide.alt = "[ - ]";
videoHide.title = "Collapse video"; videoHide.title = "Collapse video";
videoHide.style.marginLeft = "-15px"; videoHide.style.marginLeft = "-15px";
videoHide.style.cssFloat = "left"; videoHide.style.cssFloat = "left";
videoHide.addEventListener("click", unexpand, false); videoHide.addEventListener("click", unexpand, false);
videoContainer = document.createElement("div"); videoContainer = document.createElement("div");
videoContainer.style.paddingLeft = "15px"; videoContainer.style.paddingLeft = "15px";
videoContainer.style.display = "none"; videoContainer.style.display = "none";
videoContainer.appendChild(videoHide); videoContainer.appendChild(videoHide);
videoContainer.appendChild(video); videoContainer.appendChild(video);
thumb.parentNode.insertBefore(videoContainer, thumb.nextSibling); thumb.parentNode.insertBefore(videoContainer, thumb.nextSibling);
// Dragging to the left collapses the video // Dragging to the left collapses the video
video.addEventListener("mousedown", function(e) { video.addEventListener("mousedown", function(e) {
if (e.button == 0) mouseDown = true; if (e.button == 0) mouseDown = true;
}, false); }, false);
video.addEventListener("mouseup", function(e) { video.addEventListener("mouseup", function(e) {
if (e.button == 0) mouseDown = false; if (e.button == 0) mouseDown = false;
}, false); }, false);
video.addEventListener("mouseenter", function(e) { video.addEventListener("mouseenter", function(e) {
mouseDown = false; mouseDown = false;
}, false); }, false);
video.addEventListener("mouseout", function(e) { video.addEventListener("mouseout", function(e) {
if (mouseDown && e.clientX - video.getBoundingClientRect().left <= 0) { if (mouseDown && e.clientX - video.getBoundingClientRect().left <= 0) {
unexpand(); unexpand();
} }
mouseDown = false; mouseDown = false;
}, false); }, false);
} }
} }
// Clicking on thumbnail expands video // Clicking on thumbnail expands video
thumb.addEventListener("click", function(e) { thumb.addEventListener("click", function(e) {
if (setting("videoexpand") && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) { if (setting("videoexpand") && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
getVideo(); getVideo();
expanded = true; expanded = true;
hovering = false; hovering = false;
video.style.position = "static"; video.style.position = "static";
video.style.pointerEvents = "inherit"; video.style.pointerEvents = "inherit";
video.style.display = "inline"; video.style.display = "inline";
videoHide.style.display = "inline"; videoHide.style.display = "inline";
videoContainer.style.display = "block"; videoContainer.style.display = "block";
videoContainer.style.position = "static"; videoContainer.style.position = "static";
video.parentNode.parentNode.removeAttribute('style'); video.parentNode.parentNode.removeAttribute('style');
thumb.style.display = "none"; thumb.style.display = "none";
video.muted = (setting("videovolume") == 0); video.muted = (setting("videovolume") == 0);
video.volume = setting("videovolume"); video.volume = setting("videovolume");
video.controls = true; video.controls = true;
if (video.readyState == 0) { if (video.readyState == 0) {
video.addEventListener("loadedmetadata", expand2, false); video.addEventListener("loadedmetadata", expand2, false);
} else { } else {
setTimeout(expand2, 0); setTimeout(expand2, 0);
} }
video.play(); video.play();
e.preventDefault(); e.preventDefault();
} }
}, false); }, false);
function expand2() { function expand2() {
video.style.maxWidth = "100%"; video.style.maxWidth = "100%";
video.style.maxHeight = window.innerHeight + "px"; video.style.maxHeight = window.innerHeight + "px";
var bottom = video.getBoundingClientRect().bottom; var bottom = video.getBoundingClientRect().bottom;
if (bottom > window.innerHeight) { if (bottom > window.innerHeight) {
window.scrollBy(0, bottom - window.innerHeight); window.scrollBy(0, bottom - window.innerHeight);
} }
// work around Firefox volume control bug // work around Firefox volume control bug
video.volume = Math.max(setting("videovolume") - 0.001, 0); video.volume = Math.max(setting("videovolume") - 0.001, 0);
video.volume = setting("videovolume"); video.volume = setting("videovolume");
} }
// Hovering over thumbnail displays video // Hovering over thumbnail displays video
thumb.addEventListener("mouseover", function(e) { thumb.addEventListener("mouseover", function(e) {
if (setting("videohover")) { if (setting("videohover")) {
getVideo(); getVideo();
expanded = false; expanded = false;
hovering = true; hovering = true;
var docRight = document.documentElement.getBoundingClientRect().right; let docRight = document.documentElement.getBoundingClientRect().right;
var thumbRight = thumb.querySelector("img, video").getBoundingClientRect().right; let thumbRight = thumb.querySelector("img, video").getBoundingClientRect().right;
var maxWidth = docRight - thumbRight - 20; let maxWidth = docRight - thumbRight - 20;
if (maxWidth < 250) maxWidth = 250; if (maxWidth < 250) {
maxWidth = 250;
}
video.style.position = "fixed"; video.style.position = "fixed";
video.style.right = "0px"; video.style.right = "0px";
video.style.top = "0px"; video.style.top = "0px";
var docRight = document.documentElement.getBoundingClientRect().right; docRight = document.documentElement.getBoundingClientRect().right;
var thumbRight = thumb.querySelector("img, video").getBoundingClientRect().right; thumbRight = thumb.querySelector("img, video").getBoundingClientRect().right;
video.style.maxWidth = maxWidth + "px"; video.style.maxWidth = maxWidth + "px";
video.style.maxHeight = "100%"; video.style.maxHeight = "100%";
video.style.pointerEvents = "none"; video.style.pointerEvents = "none";
video.style.display = "inline"; video.style.display = "inline";
videoHide.style.display = "none"; videoHide.style.display = "none";
videoContainer.style.display = "inline"; videoContainer.style.display = "inline";
videoContainer.style.position = "fixed"; videoContainer.style.position = "fixed";
video.muted = (setting("videovolume") == 0); video.muted = (setting("videovolume") == 0);
video.volume = setting("videovolume"); video.volume = setting("videovolume");
video.controls = false; video.controls = false;
video.play(); video.play();
} }
}, false); }, false);
thumb.addEventListener("mouseout", unhover, false); thumb.addEventListener("mouseout", unhover, false);
// Scroll wheel on thumbnail adjusts default volume // Scroll wheel on thumbnail adjusts default volume
thumb.addEventListener("wheel", function(e) { thumb.addEventListener("wheel", function(e) {
if (setting("videohover")) { if (setting("videohover")) {
var volume = setting("videovolume"); var volume = setting("videovolume");
if (e.deltaY > 0) volume -= 0.1; if (e.deltaY > 0) {
if (e.deltaY < 0) volume += 0.1; volume -= 0.1;
if (volume < 0) volume = 0; }
if (volume > 1) volume = 1; if (e.deltaY < 0) {
if (video != null) { volume += 0.1;
video.muted = (volume == 0); }
video.volume = volume; if (volume < 0) {
} volume = 0;
changeSetting("videovolume", volume); }
e.preventDefault(); if (volume > 1) {
} volume = 1;
}, false); }
if (video != null) {
video.muted = (volume == 0);
video.volume = volume;
}
changeSetting("videovolume", volume);
e.preventDefault();
}
}, false);
// [play once] vs [loop] controls // [play once] vs [loop] controls
function setupLoopControl(i) { function setupLoopControl(i) {
loopControls[i].addEventListener("click", function(e) { loopControls[i].addEventListener("click", function(e) {
loop = (i != 0); loop = (i != 0);
thumb.href = thumb.href.replace(/([\?&])loop=\d+/, "$1loop=" + i); thumb.href = thumb.href.replace(/([\?&])loop=\d+/, "$1loop=" + i);
if (video != null) { if (video != null) {
video.loop = loop; video.loop = loop;
if (loop && video.currentTime >= video.duration) { if (loop && video.currentTime >= video.duration) {
video.currentTime = 0; video.currentTime = 0;
} }
} }
loopControls[i].style.fontWeight = "bold"; loopControls[i].style.fontWeight = "bold";
loopControls[1-i].style.fontWeight = "inherit"; loopControls[1-i].style.fontWeight = "inherit";
}, false); }, false);
} }
loopControls[0].textContent = _("[play once]"); loopControls[0].textContent = _("[play once]");
loopControls[1].textContent = _("[loop]"); loopControls[1].textContent = _("[loop]");
loopControls[1].style.fontWeight = "bold"; loopControls[1].style.fontWeight = "bold";
for (var i = 0; i < 2; i++) { for (var i = 0; i < 2; i++) {
setupLoopControl(i); setupLoopControl(i);
loopControls[i].style.whiteSpace = "nowrap"; loopControls[i].style.whiteSpace = "nowrap";
fileInfo.appendChild(document.createTextNode(" ")); fileInfo.appendChild(document.createTextNode(" "));
fileInfo.appendChild(loopControls[i]); fileInfo.appendChild(loopControls[i]);
} }
} }
function setupVideosIn(element) { function setupVideosIn(element) {
var thumbs = element.querySelectorAll("a.file"); let thumbs = element.querySelectorAll("a.file");
for (var i = 0; i < thumbs.length; i++) { for (let i = 0; i < thumbs.length; i++) {
if (/\.webm$|\.mp4$/.test(thumbs[i].pathname)) { if (/\.webm$|\.mp4$/.test(thumbs[i].pathname)) {
setupVideo(thumbs[i], thumbs[i].href); setupVideo(thumbs[i], thumbs[i].href);
} else { } else {
var m = thumbs[i].search.match(/\bv=([^&]*)/); let m = thumbs[i].search.match(/\bv=([^&]*)/);
if (m != null) { if (m != null) {
var url = decodeURIComponent(m[1]); let url = decodeURIComponent(m[1]);
if (/\.webm$|\.mp4$/.test(url)) setupVideo(thumbs[i], url); if (/\.webm$|\.mp4$/.test(url)) {
} setupVideo(thumbs[i], url);
} }
} }
}
}
} }
onready(function(){ onready(function(){
// Insert menu from settings.js // 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]); document.body.insertBefore(settingsMenu, document.getElementsByTagName("hr")[0]);
}
// Setup Javascript events for videos in document now // Setup Javascript events for videos in document now
setupVideosIn(document); setupVideosIn(document);
// Setup Javascript events for videos added by updater // Setup Javascript events for videos added by updater
if (window.MutationObserver) { if (window.MutationObserver) {
var observer = new MutationObserver(function(mutations) { let observer = new MutationObserver(function(mutations) {
for (var i = 0; i < mutations.length; i++) { for (let i = 0; i < mutations.length; i++) {
var additions = mutations[i].addedNodes; let additions = mutations[i].addedNodes;
if (additions == null) continue; if (additions == null) {
for (var j = 0; j < additions.length; j++) { continue;
var node = additions[j]; }
if (node.nodeType == 1) { for (let j = 0; j < additions.length; j++) {
setupVideosIn(node); let node = additions[j];
} if (node.nodeType == 1) {
} setupVideosIn(node);
} }
}); }
observer.observe(document.body, {childList: true, subtree: true}); }
} });
observer.observe(document.body, {childList: true, subtree: true});
}
}); });