MediaWiki:Common.js

From Mechabellum Wiki
Revision as of 21:58, 3 July 2023 by HUBA (talk | contribs) (change time widget - add button)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* 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"))
    if (isNaN(d.getDate())) {
      return // skip invalid date
    }
    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 toggle button
    el.after($('<button class="btn btn-primary btn-xs">').html("local").css('marginLeft','.5em').click(function(ev){
      ev.preventDefault()
      var el = $(this).prev()
      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)
    }));
  });
});