Zorn/src/Controls/Seek.astro

114 lines
4.5 KiB
Text
Raw Normal View History

---
const {
PlayerName
} = Astro.props
---
<script define:vars={{PlayerName}}>
2024-08-16 22:26:16 -04:00
/**
* @licstart The following is the entire license notice for the
* JavaScript code in this page.
*
* Copyright (C) 2024 SudoVanilla
*
*
* The JavaScript code in this page is free software: you can
* redistribute it and/or modify it under the terms of the GNU
* General Public License (GNU GPL) as published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
2024-11-04 13:37:52 -05:00
* any later version. The code is distributed WITHOUT ANY WARRANTY
2024-08-16 22:26:16 -04:00
* without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
*
* As additional permission under GNU GPL version 3 section 7, you
* may distribute non-source (e.g., minimized or compacted) forms of
* that code without the copy of the GNU GPL normally required by
* section 4, provided you include this license notice and a URL
* through which recipients can access the Corresponding Source.
*
* @licend The above is the entire license notice
* for the JavaScript code in this page.
*
*/
2024-08-16 16:20:41 -04:00
function Seek() {
var Player = document.querySelector('#zorn-player-' + PlayerName + ' video')
const timeElapsed = document.querySelector('#zorn-player-' + PlayerName + ' #current')
const duration = document.querySelector('#zorn-player-' + PlayerName + ' #duration')
2024-08-16 16:20:41 -04:00
function formatTime(timeInSeconds) {
const result = new Date(timeInSeconds * 1e3)
.toISOString()
2024-11-04 13:37:52 -05:00
.substr(11, 8)
2024-08-16 16:20:41 -04:00
return {
minutes: result.substr(3, 2),
seconds: result.substr(6, 2),
2024-11-04 13:37:52 -05:00
}
2024-08-16 16:20:41 -04:00
}
function initializeVideo() {
2024-11-04 13:37:52 -05:00
const videoDuration = Math.round(Player.duration)
const time = formatTime(videoDuration)
duration.innerText = `${time.minutes}:${time.seconds}`
2024-08-16 16:20:41 -04:00
duration.setAttribute(
"datetime",
`${time.minutes}m ${time.seconds}s`,
2024-11-04 13:37:52 -05:00
)
2024-08-16 16:20:41 -04:00
}
2024-11-04 13:37:52 -05:00
Player.addEventListener("loadedmetadata", initializeVideo)
2024-08-16 16:20:41 -04:00
function updateTimeElapsed() {
2024-11-04 13:37:52 -05:00
const time = formatTime(Math.round(Player.currentTime))
timeElapsed.innerText = `${time.minutes}:${time.seconds}`
2024-08-16 16:20:41 -04:00
timeElapsed.setAttribute(
"datetime",
`${time.minutes}m ${time.seconds}s`,
2024-11-04 13:37:52 -05:00
)
2024-08-16 16:20:41 -04:00
}
2024-11-04 13:37:52 -05:00
Player.addEventListener("timeupdate", updateTimeElapsed)
const progressBar = document.querySelector('#zorn-player-' + PlayerName + ' .vc-progress-bar')
const seek = document.querySelector('#zorn-player-' + PlayerName + ' #seek')
2024-08-16 16:20:41 -04:00
function initializeVideo() {
2024-11-04 13:37:52 -05:00
const videoDuration = Math.round(Player.duration)
seek.setAttribute("max", videoDuration)
progressBar.setAttribute("max", videoDuration)
const time = formatTime(videoDuration)
duration.innerText = `${time.minutes}:${time.seconds}`
2024-08-16 16:20:41 -04:00
duration.setAttribute(
"datetime",
`${time.minutes}m ${time.seconds}s`,
2024-11-04 13:37:52 -05:00
)
2024-08-16 16:20:41 -04:00
}
function updateProgress() {
2024-11-04 13:37:52 -05:00
seek.value = Math.floor(Player.currentTime)
document.querySelector('#zorn-player-' + PlayerName + ' .vc-progress-bar').style.width = Player.currentTime / Player.duration * 100 + '%'
2024-08-16 16:20:41 -04:00
}
2024-11-04 13:37:52 -05:00
Player.addEventListener("timeupdate", updateProgress)
const seekTooltip = document.querySelector('#zorn-player-' + PlayerName + ' #seek-tooltip')
2024-08-16 16:20:41 -04:00
function updateSeekTooltip(event) {
const skipTo = Math.round(
(event.offsetX / event.target.clientWidth) *
parseInt(event.target.getAttribute("max"), 10),
2024-11-04 13:37:52 -05:00
)
seek.setAttribute("data-seek", skipTo)
const t = formatTime(skipTo)
seekTooltip.textContent = `${t.minutes}:${t.seconds}`
const rect = Player.getBoundingClientRect()
seekTooltip.style.left = `${event.pageX - rect.left}px`
2024-08-16 16:20:41 -04:00
seekTooltip.style.opacity = '1'
document.querySelector('#zorn-player-' + PlayerName + ' .vc-progress-bar').style.width = Player.currentTime / Player.duration * 100 + '%'
2024-08-16 16:20:41 -04:00
seek.addEventListener('mouseleave', () => {
seekTooltip.style.opacity = '0'
})
}
2024-11-04 13:37:52 -05:00
seek.addEventListener("mousemove", updateSeekTooltip)
2024-08-16 16:20:41 -04:00
function skipAhead(event) {
const skipTo = event.target.dataset.seek
? event.target.dataset.seek
2024-11-04 13:37:52 -05:00
: event.target.value
Player.currentTime = skipTo
progressBar.value = skipTo
seek.value = skipTo
2024-08-16 16:20:41 -04:00
}
2024-11-04 13:37:52 -05:00
seek.addEventListener("input", skipAhead)
2024-08-16 16:20:41 -04:00
2024-11-04 13:37:52 -05:00
initializeVideo()
2024-08-16 16:20:41 -04:00
}
2024-11-04 13:37:52 -05:00
Seek()
2024-08-16 16:20:41 -04:00
</script>