Difference between revisions of "MediaWiki:Gadget-fix-schedule-times.js"

From WikiConference North America
Jump to navigation Jump to search
(fix)
(Undo revision 17935 by Enterprisey (talk))
Tag: Undo
Line 18: Line 18:
   
 
function formatRelativeTime( minutesUntilStart, minutesUntilEnd ) {
 
function formatRelativeTime( minutesUntilStart, minutesUntilEnd ) {
console.log(minutesUntilStart,minutesUntilEnd);
 
 
if ( minutesUntilStart > /* one day */ 1440 ) {
 
if ( minutesUntilStart > /* one day */ 1440 ) {
 
return "Starts in " + ( minutesUntilStart / 1440 ).toFixed( 0 ) + " days";
 
return "Starts in " + ( minutesUntilStart / 1440 ).toFixed( 0 ) + " days";
Line 76: Line 75:
   
 
function go() {
 
function go() {
$( "td" ).first().each( function () {
+
$( "td" ).each( function () {
 
var fixed = $( this ).find( "span.fixed" );
 
var fixed = $( this ).find( "span.fixed" );
 
if ( fixed.length ) {
 
if ( fixed.length ) {

Revision as of 08:55, 6 October 2021

// vim: ts=4 sw=4 et ai si
$.when(
    $.ready
).then( function () {

    // Configuration
    var CONFERENCE_DAYS = {
        "Friday, October 8": Date.UTC( 2021, /* month index, not month number! */ 9, 8 ),
        "Saturday, October 9": Date.UTC( 2021, 9, 9 ),
        "Sunday, October 10": Date.UTC( 2021, 9, 10 )
    };
    var WRITTEN_SCHEDULE_OFFSET_SECONDS = -4 * 3600; // written schedule for WCNA 2021 is Eastern Time which is UTC-4

    // Other constants
    var TIME_REGEX = /(\d\d):(\d\d) - (\d\d):(\d\d)/;

    var userTimeCorrectionSeconds = mw.user.options.get( "timecorrection" ) * 60;

    function formatRelativeTime( minutesUntilStart, minutesUntilEnd ) {
        if ( minutesUntilStart > /* one day */ 1440 ) {
            return "Starts in " + ( minutesUntilStart / 1440 ).toFixed( 0 ) + " days";
        } else if ( minutesUntilStart > /* 3 hours */ 180 ) {
            return "Starts in " + ( minutesUntilStart / 60 ).toFixed( 0 ) + " hours";
        } else if ( minutesUntilStart > 120 ) {
            return "Starts in 2 hours and " + ( minutesUntilStart % 60 ).toFixed( 0 ) + " minutes";
        } else if ( minutesUntilStart > 60 ) {
            return "Starts in an hour and " + ( minutesUntilStart % 60 ).toFixed( 0 ) + " minutes";
        } else if ( minutesUntilStart > 1 ) {
            return "Starts in " + minutesUntilStart.toFixed( 0 ) + " minutes";
        } else if ( minutesUntilStart > 0 ) {
            return "Starts in less than a minute";
        } else if ( minutesUntilStart > -1 ) {
            return "Started just now";
        } else if ( minutesUntilStart > -5 ) {
            return "Started " + ( -minutesUntilStart ).toFixed( 0 ) + " minutes ago";
        } else if ( minutesUntilEnd > 1 ) {
            return "Ends in " + minutesUntilEnd.toFixed( 0 ) + " minutes";
        } else if ( minutesUntilEnd > 0 ) {
            return "Ends in one minute";
        } else if ( minutesUntilEnd > -1 ) {
            return "Ended just now";
        } else if ( minutesUntilEnd > -60 ) {
            return "Ended " + ( -minutesUntilEnd ).toFixed( 0 ) + " minutes ago";
        } else if ( minutesUntilEnd > -1440 ) {
            return "Ended " + ( -minutesUntilEnd / 60 ).toFixed( 0 ) + " hours ago";
        } else {
            return "Ended " + ( -minutesUntilEnd / 1440 ).toFixed( 0 ) + " days ago";
        }
    }

    function formatTime( unixTimestamp ) {
        return new Date( unixTimestamp * 1000 ).toLocaleTimeString().replace( /(\d\d?:\d\d):00/, "$1" );
    }

    function makeHtml( originalTimeMatch, tableCell ) {
        var dayHeading = $( tableCell ).closest( "table" ).prev().prev(); // skip icon key
        var dayHeadingText = dayHeading.find( ".mw-headline" ).text();
        var baseDateUnix = Math.floor( CONFERENCE_DAYS[dayHeadingText] / 1000 );

        // First, get the actual timestamps
        var startTimeUnix = baseDateUnix + parseInt( originalTimeMatch[1], 10 ) * 3600 +
                parseInt( originalTimeMatch[2], 10 ) * 60 - WRITTEN_SCHEDULE_OFFSET_SECONDS;
        var endTimeUnix = baseDateUnix + parseInt( originalTimeMatch[3], 10 ) * 3600 +
                parseInt( originalTimeMatch[4], 10 ) * 60 - WRITTEN_SCHEDULE_OFFSET_SECONDS;

        // Now, get some text like "Starts in 10 minutes".
        var nowUnix = Math.floor( new Date().getTime() / 1000 );
        var relativeTimeFormatted = formatRelativeTime(
            ( startTimeUnix - nowUnix ) / 60,
            ( endTimeUnix - nowUnix ) / 60
        );

        return formatTime( startTimeUnix ) + " - " + formatTime( endTimeUnix ) + "<br />(" + relativeTimeFormatted + ")";
    }

    function go() {
        $( "td" ).each( function () {
            var fixed = $( this ).find( "span.fixed" );
            if ( fixed.length ) {
                fixed.html( makeHtml( TIME_REGEX.exec( fixed.data( "original" ) ), this ) );
            } else {
                var match = TIME_REGEX.exec( this.textContent );
                if ( match ) {
                    this.innerHTML = this.innerHTML.replace( match[0], "<span class='fixed' data-original='" + match[0] + "'>" + makeHtml( match, this ) + "</span>" );
                }
            }
        } );
    }

    go();
    window.setInterval( go, 10000 );
} );