MediaWiki:Timewidget/code.js

var tzdiff = (new Date.getTimezoneOffset / 60) + 9; var tzdiff_ms = tzdiff * 60 * 60 * 1000; var maintenance_day = 4; var maintenance_hour = 10; var maintenance_duration = 5;

function get_maintenance_status(jptime) { if(jptime.getDay == maintenance_day) { if(jptime.getHours >= maintenance_hour) { if(jptime.getHours < maintenance_hour + maintenance_duration) { return "ongoing"; } else { return "finished"; }		}	} }

function get_maintenance_timestamp(jptime, maintenance_status) { var timestamp = new Date(jptime.getFullYear,		jptime.getMonth,		jptime.getDate + (7 + maintenance_day - jptime.getDay) % 7,		maintenance_hour); if(maintenance_status === "ongoing") { timestamp.setHours(timestamp.getHours + maintenance_duration); } else if(maintenance_status === "finished") { timestamp.setDate(timestamp.getDate + 7); }

target_date = timestamp; target_date.setHours(target_date.getHours - tzdiff); return target_date; }

function update_widget(widget, now, target_timestamp, maintenance_status) { var widget_date = widget.find(".date").first; var widget_body = widget.find(".body").first; var vanish_when_done = widget.attr("data-vanish") !== undefined;

widget_date.html(target_timestamp.toLocaleString([], { timeZone: "Asia/Tokyo", hour12: false, year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", timeZoneName: "short" }));	if(maintenance_status === "ongoing") { widget_date.html("Maintenance ongoing") .addClass("maintenance_ongoing"); } else { widget_date.removeClass("maintenance_ongoing"); }	var ms_til_target = target_timestamp - now; var diff_seconds = ('0' + Math.floor(ms_til_target / 1000) % 60           ).slice(-2); var diff_minutes = ('0' + Math.floor(ms_til_target / 1000 / 60) % 60      ).slice(-2); var diff_hours  = ('0' + Math.floor(ms_til_target / 1000  / 60  / 60) % 24).slice(-2); var diff_days   =        Math.floor(ms_til_target / 1000  / 60  / 60  / 24); if(ms_til_target < 0) { if(vanish_when_done) { widget.remove; } else { widget_body.empty .html(widget.attr("data-end") || "Event finished"); }	} else { widget_body.empty .html(diff_days + ":" + diff_hours + ":" + diff_minutes + ":" + diff_seconds); }

}

function do_countdown(widget) { var now = new Date; var jptime = new Date(now.valueOf + tzdiff_ms); var start = widget.attr("data-start"); var target = widget.attr("data-target"); var start_date; var target_date; var is_maintenance_countdown = (widget.attr("data-auto") !== undefined); var maintenance_status = get_maintenance_status(jptime); if(start !== undefined) { start_date = Date.parse(start); if(now < start_date) { widget.addClass(".timewidget-hidden"); $(widget).siblings.addClass("timewidget-hidden"); return; } else { widget.removeClass("timewidget-hidden"); $(widget).siblings.removeClass("timewidget-hidden"); }	}

if(is_maintenance_countdown) { target_date = get_maintenance_timestamp(jptime, maintenance_status); } else { target_date = Date.parse(target); }

var target_stamp = new Date(target_date.valueOf); update_widget(widget, now, target_stamp, maintenance_status); }

function initialize_time_widget {

var update = function { $(".timewidget").each(function(index, widget) {			do_countdown($(widget));		}); };	if($(".timewidget").length > 0) { setInterval(update, 1000); }	update; }

$(window).load(initialize_time_widget);