MediaWiki:Common.js: Difference between revisions
From Mechabellum Wiki
(enable tooltips) |
(add time widget) |
||
Line 3: | Line 3: | ||
$('[data-toggle="tooltip"]').tooltip() | $('[data-toggle="tooltip"]').tooltip() | ||
}) | }) | ||
// time widget | |||
$(function(){ | |||
$("time").each(function(){ | |||
var el = $(this) | |||
var d = new Date(el.attr("datetime")) | |||
var opt = {timeStyle: "short"} | |||
switch (el.data("type")) { | |||
default: | |||
case "short": | |||
opt.dateStyle = "medium" | |||
break | |||
case "full": | |||
opt.dateStyle = "full" | |||
break | |||
case "time": | |||
break | |||
case "weekday": | |||
opt = {weekday: "long", hour: "numeric", minute: "numeric"} | |||
break | |||
} | |||
// get next date when interval is set | |||
var iv = parseInt(el.data('interval')) | |||
if (iv > 0 && d.getFullYear() > 2020) { | |||
var now = new Date(); | |||
while (d<now) { | |||
d.setDate(d.getDate()+iv) | |||
} | |||
} | |||
// set date-data | |||
el.data("local", d.toLocaleString([], opt)) | |||
opt.timeZone = "UTC" | |||
el.data("UTC", new Intl.DateTimeFormat("en-US", opt).format(d)); | |||
el.html(el.data("local")) | |||
el.data("view", "local") | |||
// add toogle button | |||
el.before($('<a href="#">').html("local:").css('paddingRight','.5em').click(function(){ | |||
var el = $(this).next() | |||
var view = (el.data("view")==="local") ? "UTC" : "local" | |||
el.fadeOut(200, function() {el.html(el.data(view)).fadeIn(200)}) | |||
el.data("view", view) | |||
$(this).html(view+":") | |||
})); | |||
}); | |||
}); |
Revision as of 20:34, 19 June 2023
/* Any JavaScript here will be loaded for all users on every page load. */ $(function () { $('[data-toggle="tooltip"]').tooltip() }) // time widget $(function(){ $("time").each(function(){ var el = $(this) var d = new Date(el.attr("datetime")) var opt = {timeStyle: "short"} switch (el.data("type")) { default: case "short": opt.dateStyle = "medium" break case "full": opt.dateStyle = "full" break case "time": break case "weekday": opt = {weekday: "long", hour: "numeric", minute: "numeric"} break } // get next date when interval is set var iv = parseInt(el.data('interval')) if (iv > 0 && d.getFullYear() > 2020) { var now = new Date(); while (d<now) { d.setDate(d.getDate()+iv) } } // set date-data el.data("local", d.toLocaleString([], opt)) opt.timeZone = "UTC" el.data("UTC", new Intl.DateTimeFormat("en-US", opt).format(d)); el.html(el.data("local")) el.data("view", "local") // add toogle button el.before($('<a href="#">').html("local:").css('paddingRight','.5em').click(function(){ var el = $(this).next() var view = (el.data("view")==="local") ? "UTC" : "local" el.fadeOut(200, function() {el.html(el.data(view)).fadeIn(200)}) el.data("view", view) $(this).html(view+":") })); }); });