MediaWiki:Common.js: Difference between revisions

From Mechabellum Wiki
(show host on external links)
mNo edit summary
 
(3 intermediate revisions by the same user not shown)
Line 31: Line 31:
     var iv = parseInt(el.data('interval'))
     var iv = parseInt(el.data('interval'))
     if (iv > 0 && d.getFullYear() > 2020) {
     if (iv > 0 && d.getFullYear() > 2020) {
       var now = new Date();
       var now = new Date(new Date().getTime() - 12*3600*1000);
       while (d<now) {
       while (d<now) {
         d.setDate(d.getDate()+iv)
         d.setDate(d.getDate()+iv)
Line 61: Line 61:
     var host = this.href.match('.*//(www\.)?([^/]+)')[2]
     var host = this.href.match('.*//(www\.)?([^/]+)')[2]
     this.dataset.host = host
     this.dataset.host = host
  })
})
// table filter
$(function(){
  // inject html
  $('#table-filter-insert').html('<label>Filter: <input type="text" id="table-filter" class="form-control form-control-sm" value=""></label> &nbsp; <input type="button" class="btn btn-sm btn-primary" value="clear" onClick="$(\'#table-filter\').val(\'\').trigger(\'input\')">')
  var filter_timer = 0
  $('#table-filter').on('input', function() {
    clearTimeout(filter_timer)
    var target = ($(this).data('target') || '.table') + ' tr'
    filter_timer = setTimeout(function(val, tgt) {
      $(tgt).each(function(){
        var show = this.firstElementChild.tagName === 'TH' || this.innerText.toLowerCase().indexOf(val) != -1
        if(show) {
          $(this).show()
        } else {
          $(this).hide()
        }
      })
    }, 600, this.value.toLowerCase(), target)
   })
   })
})
})

Latest revision as of 20:27, 16 July 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"))
    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(new Date().getTime() - 12*3600*1000);
      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)
    }));
  });
});

// show host on external links
$(function(){
  $('a[href].external').each(function(){
    var host = this.href.match('.*//(www\.)?([^/]+)')[2]
    this.dataset.host = host
  })
})

// table filter
$(function(){
  // inject html
  $('#table-filter-insert').html('<label>Filter: <input type="text" id="table-filter" class="form-control form-control-sm" value=""></label> &nbsp; <input type="button" class="btn btn-sm btn-primary" value="clear" onClick="$(\'#table-filter\').val(\'\').trigger(\'input\')">')
  var filter_timer = 0
  $('#table-filter').on('input', function() {
    clearTimeout(filter_timer)
    var target = ($(this).data('target') || '.table') + ' tr'
    filter_timer = setTimeout(function(val, tgt) {
      $(tgt).each(function(){
        var show = this.firstElementChild.tagName === 'TH' || this.innerText.toLowerCase().indexOf(val) != -1
        if(show) {
          $(this).show()
        } else {
          $(this).hide()
        }
      })
    }, 600, this.value.toLowerCase(), target)
  })
})