proxitok/scripts/themes/card.js

128 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-02-07 16:23:33 -05:00
var opened_video_id = null
2022-01-01 14:14:57 -05:00
const video = document.getElementById('video')
const item_title = document.getElementById('item_title')
2022-06-12 07:17:56 -04:00
const item_date = document.getElementById('item_date')
2022-01-01 14:14:57 -05:00
const audio = document.getElementById('audio')
const audio_title = document.getElementById('audio_title')
const modal = document.getElementById('modal')
2022-02-07 15:07:51 -05:00
const download_watermark = document.getElementById('download_watermark')
const download_nowatermark = document.getElementById('download_nowatermark')
2022-02-07 16:23:33 -05:00
const share_input = document.getElementById('share_input')
2022-01-01 18:06:00 -05:00
2022-02-07 16:23:33 -05:00
const getVideoDataById = id => {
2022-01-01 18:06:00 -05:00
const el = document.getElementById(id)
if (el) {
2022-02-07 16:23:33 -05:00
opened_video_id = id
2022-01-01 18:06:00 -05:00
return el.dataset
}
2022-06-12 07:17:56 -04:00
return null
2022-01-01 18:06:00 -05:00
}
const isModalActive = () => modal.classList.contains('is-active')
2022-02-07 16:23:33 -05:00
const toggleButton = (id, force) => document.getElementById(id).toggleAttribute('disabled', force)
2022-01-01 18:06:00 -05:00
// -- MODAL -- //
2022-06-12 07:17:56 -04:00
const swapData = ({ video_url, desc, createtime, video_download_watermark, video_download_nowatermark, video_share_url, music_title, music_url }) => {
2022-01-01 18:06:00 -05:00
video.src = video_url
item_title.innerText = desc
2022-06-12 07:17:56 -04:00
item_date.innerText = new Date(createtime * 1000).toLocaleString()
2022-02-07 15:07:51 -05:00
download_watermark.href = video_download_watermark
download_nowatermark.href = video_download_nowatermark
2022-02-07 16:23:33 -05:00
share_input.value = video_share_url
2022-01-01 18:06:00 -05:00
audio_title.innerText = music_title
audio.src = music_url
}
2022-02-07 16:23:33 -05:00
const showModal = id => {
2022-01-01 18:06:00 -05:00
const dataset = getVideoDataById(id)
if (dataset) {
swapData(dataset)
2022-01-01 14:14:57 -05:00
modal.classList.toggle('is-active')
video.play()
2022-01-01 18:06:00 -05:00
}
2022-01-01 14:14:57 -05:00
}
const hideModel = () => {
2022-01-01 18:06:00 -05:00
video.pause()
audio.pause()
video.currentTime = 0
modal.classList.toggle('is-active')
toggleButton('back-button', false)
toggleButton('next-button', false)
history.pushState('', document.title, window.location.pathname + window.location.search)
2022-01-01 14:14:57 -05:00
}
2022-02-07 16:23:33 -05:00
const getPrevOrNext = forward => {
if (opened_video_id) {
const el = document.getElementById(opened_video_id)
2022-01-01 18:06:00 -05:00
if (el) {
if (forward) {
2022-02-07 16:23:33 -05:00
return el.nextElementSibling
2022-01-01 18:06:00 -05:00
}
2022-02-07 16:23:33 -05:00
return el.previousElementSibling
2022-01-01 18:06:00 -05:00
}
}
return null
}
2022-01-01 14:14:57 -05:00
2022-02-07 16:23:33 -05:00
const moveVideo = forward => {
2022-01-01 18:06:00 -05:00
// Re-enable buttons
toggleButton('back-button', false)
toggleButton('next-button', false)
const new_el = getPrevOrNext(forward)
if (new_el) {
2022-02-07 16:23:33 -05:00
opened_video_id = new_el.id
swapData(new_el.dataset)
2022-01-01 18:06:00 -05:00
} else {
// Max reached, disable buttons depending on direction
if (forward) {
toggleButton('next-button', true)
} else {
toggleButton('back-button', true)
}
}
2022-01-01 14:14:57 -05:00
}
2022-01-01 18:06:00 -05:00
// EVENTS //
2022-02-07 16:23:33 -05:00
const openVideo = video_id => {
if (isModalActive()) {
const dataset = getVideoDataById(video_id)
if (dataset) {
swapData(dataset)
2022-01-01 18:06:00 -05:00
}
2022-02-07 16:23:33 -05:00
} else {
showModal(video_id)
2022-01-01 18:06:00 -05:00
}
}
2022-02-07 16:23:33 -05:00
const swapImages = e => {
const div = e.target
const img = div.children[0]
const gif = div.children[1]
if (!gif.src) {
gif.src = gif.dataset.src
2022-01-06 18:13:51 -05:00
}
2022-02-07 16:23:33 -05:00
img.classList.toggle('hidden')
gif.classList.toggle('hidden')
}
2022-01-06 18:13:51 -05:00
2022-02-07 16:23:33 -05:00
const copyShare = () => {
share_input.select();
navigator.clipboard.writeText(share_input.value);
alert('Copied!')
}
2022-01-05 18:55:24 -05:00
document.getElementById('modal-background').addEventListener('click', hideModel)
document.getElementById('modal-close').addEventListener('click', hideModel)
2022-01-01 18:06:00 -05:00
document.getElementById('back-button').addEventListener('click', () => moveVideo(false))
document.getElementById('next-button').addEventListener('click', () => moveVideo(true))
// Image hover
2022-01-06 18:13:51 -05:00
const images = document.getElementsByClassName("clickable-img")
for (let i = 0; i < images.length; i++) {
2022-02-07 16:23:33 -05:00
images[i].addEventListener('mouseenter', swapImages, false)
images[i].addEventListener('mouseout', swapImages, false)
}