@ -7,12 +7,36 @@ var globalVideoTimeReference = TIME_FRAME_AGO;
window . addEventListener ( "DOMContentLoaded" , async ( event ) => {
window . addEventListener ( "DOMContentLoaded" , async ( event ) => {
commonPageSetup ( ) ;
commonPageSetup ( ) ;
await loadDefaults ( ) ;
const timeSettingsForm = document . getElementById ( "stream-time-settings" ) ;
const timeSettingsForm = document . getElementById ( "stream-time-settings" ) ;
timeSettingsForm . addEventListener ( "submit" , ( event ) => {
timeSettingsForm . addEventListener ( "submit" , ( event ) => {
event . preventDefault ( ) ;
event . preventDefault ( ) ;
updateTimeSettings ( ) ;
updateTimeSettings ( ) ;
} ) ;
} ) ;
await loadDefaults ( ) ;
const timeConversionForm = document . getElementById ( "time-converter" ) ;
timeConversionForm . addEventListener ( "submit" , ( event ) => {
event . preventDefault ( ) ;
convertEnteredTimes ( ) ;
} ) ;
const timeConversionLink = document . getElementById ( "time-converter-link" ) ;
timeConversionLink . addEventListener ( "click" , ( _event ) => {
const timeConversionForm = document . getElementById ( "time-converter" ) ;
timeConversionForm . classList . toggle ( "hidden" ) ;
} ) ;
const addTimeConversionButton = document . getElementById ( "time-converter-add-time" ) ;
addTimeConversionButton . addEventListener ( "click" , ( _event ) => {
const newField = document . createElement ( "input" ) ;
newField . classList . add ( "time-converter-time" ) ;
newField . type = "text" ;
newField . placeholder = "Time to convert" ;
const container = document . getElementById ( "time-converter-time-container" ) ;
container . appendChild ( newField ) ;
} ) ;
updateTimeSettings ( ) ;
updateTimeSettings ( ) ;
} ) ;
} ) ;
@ -34,14 +58,7 @@ async function loadDefaults() {
// Gets the start time of the video from settings. Returns an invalid date object if the user entered bad data.
// Gets the start time of the video from settings. Returns an invalid date object if the user entered bad data.
function getStartTime ( ) {
function getStartTime ( ) {
switch ( globalVideoTimeReference ) {
return dateTimeFromTimeString ( globalStartTimeString , globalVideoTimeReference ) ;
case 1 :
return dateTimeFromWubloaderTime ( globalStartTimeString ) ;
case 2 :
return dateTimeFromBusTime ( globalStartTimeString ) ;
case 3 :
return DateTime . now ( ) . minus ( parseHumanTimeStringAsDateTimeMathObject ( globalStartTimeString ) ) ;
}
}
}
// Gets the end time of the video from settings. Returns null if there's no end time. Returns an invalid date object if the user entered bad data.
// Gets the end time of the video from settings. Returns null if there's no end time. Returns an invalid date object if the user entered bad data.
@ -49,13 +66,17 @@ function getEndTime() {
if ( globalEndTimeString === "" ) {
if ( globalEndTimeString === "" ) {
return null ;
return null ;
}
}
switch ( globalVideoTimeReference ) {
return dateTimeFromTimeString ( globalEndTimeString , globalVideoTimeReference ) ;
}
function dateTimeFromTimeString ( timeString , timeStringFormat ) {
switch ( timeStringFormat ) {
case 1 :
case 1 :
return dateTimeFromWubloaderTime ( globalEndTimeString ) ;
return dateTimeFromWubloaderTime ( t imeString) ;
case 2 :
case 2 :
return dateTimeFromBusTime ( globalEndTimeString ) ;
return dateTimeFromBusTime ( t imeString) ;
case 3 :
case 3 :
return DateTime . now ( ) . minus( parseHumanTimeStringAsDateTime MathObject( globalEndT imeString) ) ;
return DateTime . now ( ) . setZone( "utc" ) . minus( parseHumanTimeStringAsDateTime ( t imeString) ) ;
}
}
}
}
@ -114,3 +135,69 @@ function updateStoredTimeSettings() {
}
}
}
}
}
}
function convertEnteredTimes ( ) {
let timeConvertFrom = undefined ;
const timeConvertFromSelection = document . querySelectorAll (
"#time-converter input[name=time-converter-from]"
) ;
for ( const convertFromItem of timeConvertFromSelection ) {
if ( convertFromItem . checked ) {
timeConvertFrom = + convertFromItem . value ;
break ;
}
}
if ( ! timeConvertFrom ) {
addError ( "Failed to convert times - input format not specified" ) ;
return ;
}
let timeConvertTo = undefined ;
const timeConvertToSelection = document . querySelectorAll (
"#time-converter input[name=time-converter-to]"
) ;
for ( const convertToItem of timeConvertToSelection ) {
if ( convertToItem . checked ) {
timeConvertTo = + convertToItem . value ;
break ;
}
}
if ( ! timeConvertTo ) {
addError ( "Failed to convert times - output format not specified" ) ;
return ;
}
const timeFieldList = document . getElementsByClassName ( "time-converter-time" ) ;
const now = DateTime . now ( ) . setZone ( "utc" ) ;
for ( const timeField of timeFieldList ) {
const enteredTime = timeField . value ;
if ( enteredTime === "" ) {
continue ;
}
let time = dateTimeFromTimeString ( enteredTime , timeConvertFrom ) ;
if ( ! time ) {
addError (
` Failed to parse the time ' ${ enteredTime } ' as a value of the selected "convert from" time format. `
) ;
continue ;
}
if ( timeConvertTo === TIME _FRAME _UTC ) {
timeField . value = wubloaderTimeFromDateTime ( time ) ;
} else if ( timeConvertTo === TIME _FRAME _BUS ) {
timeField . value = busTimeFromDateTime ( time ) ;
} else if ( timeConvertTo === TIME _FRAME _AGO ) {
const difference = now . diff ( time ) ;
timeField . value = formatIntervalForDisplay ( difference ) ;
}
}
if ( timeConvertTo === TIME _FRAME _UTC ) {
document . getElementById ( "time-converter-from-utc" ) . checked = true ;
} else if ( timeConvertTo === TIME _FRAME _BUS ) {
document . getElementById ( "time-converter-from-bus" ) . checked = true ;
} else if ( timeConvertTo === TIME _FRAME _AGO ) {
document . getElementById ( "time-converter-from-ago" ) . checked = true ;
}
}