2022-01-01 14:14:57 -05:00
const video = document . getElementById ( 'video' )
const item _title = document . getElementById ( 'item_title' )
const audio = document . getElementById ( 'audio' )
const audio _title = document . getElementById ( 'audio_title' )
const modal = document . getElementById ( 'modal' )
2022-01-01 18:06:00 -05:00
const download _button = document . getElementById ( 'download_button' )
// -- HELPERS -- //
const getHash = ( ) => window . location . hash . substring ( 1 )
const getVideoDataById = ( id ) => {
const el = document . getElementById ( id )
if ( el ) {
return el . dataset
}
return false
}
const isModalActive = ( ) => modal . classList . contains ( 'is-active' )
const toggleButton = ( id , force ) => document . getElementById ( id ) ? document . getElementById ( id ) . toggleAttribute ( 'disabled' , force ) : alert ( 'That button does not exist' )
// -- MODAL -- //
2022-01-08 10:03:57 -05:00
const swapData = ( { video _url , desc , video _download , music _title , music _url } ) => {
2022-01-01 18:06:00 -05:00
video . src = video _url
item _title . innerText = desc
download _button . href = video _download
audio _title . innerText = music _title
audio . src = music _url
}
const showModal = ( id ) => {
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-01-01 18:06:00 -05:00
const getPrevOrNext = ( forward ) => {
const hash = getHash ( )
if ( hash ) {
const el = document . getElementById ( hash )
if ( el ) {
if ( forward ) {
return el . parentElement . nextElementSibling ? el . parentElement . nextElementSibling . children [ 0 ] : null
}
return el . parentElement . previousElementSibling ? el . parentElement . previousElementSibling . children [ 0 ] : null
}
}
return null
}
2022-01-01 14:14:57 -05:00
2022-01-01 18:06:00 -05:00
const moveVideo = ( forward ) => {
// Re-enable buttons
toggleButton ( 'back-button' , false )
toggleButton ( 'next-button' , false )
const new _el = getPrevOrNext ( forward )
if ( new _el ) {
window . location . hash = new _el . id
} 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 //
const hashChange = ( ) => {
if ( window . location . hash ) {
const hash = getHash ( )
if ( hash ) {
// Check first if the modal is already active
if ( isModalActive ( ) ) {
// If it is, get hash video id and swap data
const dataset = getVideoDataById ( hash )
if ( dataset ) {
swapData ( dataset )
video . play ( )
}
} else {
showModal ( hash )
}
}
}
}
2022-01-06 18:13:51 -05:00
const swapImages = ( e , mode ) => {
let img = null
let gif = null
if ( mode === 'gif' ) {
const a = e . target
img = a . children [ 0 ]
gif = a . children [ 1 ]
} else if ( mode === 'img' ) {
gif = e . target
img = e . target . parentElement . children [ 0 ]
}
if ( img && gif ) {
if ( ! gif . src ) {
gif . src = gif . dataset . src
}
img . classList . toggle ( 'hidden' )
gif . classList . toggle ( 'hidden' )
2022-01-05 18:11:00 -05:00
}
}
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 ) )
window . addEventListener ( 'hashchange' , hashChange , false )
2022-01-05 18:11:00 -05:00
// Image hover
2022-01-06 18:13:51 -05:00
const images = document . getElementsByClassName ( "clickable-img" )
for ( let i = 0 ; i < images . length ; i ++ ) {
images [ i ] . addEventListener ( 'mouseenter' , e => swapImages ( e , 'gif' ) , false )
images [ i ] . addEventListener ( 'mouseout' , e => swapImages ( e , 'img' ) , false )
2022-01-05 18:11:00 -05:00
}
2022-01-06 18:13:51 -05:00
2022-01-01 18:06:00 -05:00
hashChange ( )