Difference between revisions of "MediaWiki:Common.js"

From Free Software Directory
Jump to: navigation, search
(Added test log script.)
(Limit loading of single-page tracker.)
Line 3: Line 3:
 
/* Any JavaScript here will be loaded by every visitor on every page load. */
 
/* Any JavaScript here will be loaded by every visitor on every page load. */
  
mw.loader.load("/w/index.php?title=MediaWiki:Single-page tracker.js&action=raw&ctype=text/javascript", "text/javascript");
+
// Single-page tracker, thanks to kindling! ;)
 +
if (
 +
    /Free_Software_Directory:Backlog_(?:active|archive)/
 +
      .test(mw.config.get("wgPageName"))
 +
  ) {
 +
 
 +
  mw.loader.load(
 +
    "/w/index.php?title=MediaWiki:Single-page tracker.js&action=raw&ctype=text/javascript",
 +
    "text/javascript"
 +
  );
 +
}
  
 
/* Active Backlog: fix. "Fix search results, so that template data doesn't get dumped..." */
 
/* Active Backlog: fix. "Fix search results, so that template data doesn't get dumped..." */

Revision as of 14:52, 18 June 2021

// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later

/* Any JavaScript here will be loaded by every visitor on every page load. */

// Single-page tracker, thanks to kindling! ;)
if (
    /Free_Software_Directory:Backlog_(?:active|archive)/
      .test(mw.config.get("wgPageName"))
  ) {

  mw.loader.load(
    "/w/index.php?title=MediaWiki:Single-page tracker.js&action=raw&ctype=text/javascript",
    "text/javascript"
  );
}

/* Active Backlog: fix. "Fix search results, so that template data doesn't get dumped..." */
function fixSrTmplBug() {
 function iskSearchCurPage() {
   return window.location.href.match(/\w+\/wiki\?search=\w+/i) !== null;
 }
 function detectSrTmplBug(srcontent) {
   var containsMeta = srcontent.match(/.*\|.*=.*/i);
   var containsRedi = srcontent.match(/#REDIRECT .*/i);
   var containsTmpl = srcontent.match(/.*(\{\{|\}\}).*/i);
   if(containsMeta !== null) return 1;
   else if(containsRedi !== null) return 2;
   else if(containsTmpl !== null) return 3;
   else return 0;
 }
 function correctSrTmplBug(i, el) {
   el = $(el);
   var tres = detectSrTmplBug(el.html());
   switch(tres) {
    case 0: break;
    case 1:
    case 3: el.html('(structured)'); 
     break;
    case 2: el.html('(alias)');
     break;
    default: el.hide();
   }
   el = null;
   tres = null;
   return;
 }

  if(iskSearchCurPage() === false) return false;
  else {
   var sresults = $('.searchresult');
   if(sresults.length > 0) {
     sresults.each(correctSrTmplBug);
     sresults = null;
   }
  }
  return true;
};

/* Active Backlog: fix. ""Preview" button does not work for Template:Entry. It shows the unedited version" */
function fixEntryPrevBug() {
  function iskEntryRawEditCurPage() {
    var ftmpl = $(".templatesUsed");
    if(ftmpl.length <= 0 || ftmpl.html().indexOf("Template:Entry") == -1) return false;
  }
  function hidePreviewButton() {$("#wpPreviewWidget").remove();}
  function boldEditFormTab() {$("#ca-formedit a").css("font-weight", "bold");}
  
  if(iskEntryRawEditCurPage() === false) return false;
  else {hidePreviewButton();boldEditFormTab();}
  
  return true;
};

/* Active Backlog: fix. "Countdown timer/JavaScript enhancement" */
var dyntimersupd = null;
function dynTimers() {
  function updateTimers(timers, loc, tz) {
    var ts = new Date();
    var opts = { timeZone:tz, weekday: 'long', hour: '2-digit', minute: '2-digit', second: '2-digit' };
    var CESTopts = { timeZone:"Europe/Rome", weekday: 'long', hour: '2-digit', minute: '2-digit', second: '2-digit' };

    var cdate = ts.toLocaleTimeString(locales=loc, opts);
    var UTCdate = ts.toUTCString();
    var CESTdate = ts.toLocaleTimeString(locales=undefined, CESTopts);
    EDTtimersBlnk = !EDTtimersBlnk;
    if(EDTtimersBlnk == false) cdate = cdate.replace(/[:\.]/gi,' ');
    UTCdate = UTCdate.replace('GMT', 'UTC');
    timers.html(cdate + ' EDT');
    timers.attr('title', UTCdate + ' | ' + CESTdate + ' CEST');
    cdate = null;
    UTCdate = null;
    ts = null;
  }

  var EDTtimers = $('.currentEDT');
  var EDTtimersBlnk = true;
  if(EDTtimers.length >= 0) {
    window.setInterval(function(){updateTimers(EDTtimers, "en-US", "America/New_York");}, 1000);
  } else {
    EDTtimers = null;
    EDTtimersBlnk = null;
  }
}

function advBacklog() {
  // R: true if the page is part of the Backlog
  function isBacklogPage() {
    var isbl =  window.location.href.match(/\w+\/wiki(\/|\?title\=)?Free_Software_Directory:Backlog_\w+/i) !== null;
    return isbl;
  }
  // R: transmogrified ID for a list option
  function IDToTitle(istr, endchr) {
    var tstr = istr[0].toUpperCase() + istr.slice(1);
    tstr = tstr.replace(/[-><%]/g, " ");
    tstr = tstr.trim();
    if(!tstr.endsWith(";")) {
      if(tstr.slice(-1).match(/[:.\n\r]/)) {
        tstr = tstr.slice(0, -1) + endchr;
      } else {
        tstr = tstr + endchr;
      }
    }
    return tstr;
  }
  // Adds el (option) to curog (optgroup)
  function addBEl(i, el) {
    var iopt = $("<option class='atocopt'>");
    var elem = $(el);
    if(otype == 1) // Page headers
      iopt.text(elem.html());
    else if(otype == 2) // Active Backlog tasks
      iopt.text( IDToTitle(elem.attr("id"), ";") );
    iopt.data("iid", elem.attr("id"));
    iopt.click(function(){location.hash=iopt.data("iid");});
    curog.append(iopt);
  }
  
  // Interactive ToC
  var toc = $("#toc");
  if(!isBacklogPage() || toc === 'undefined') return 1;
  
  var otype = null, curog = null; // obj type, c. optgroup
  var cbox = $("<select id='atoc'>");

  otype = 1; // Page headers
  curog = $('<optgroup class="itocS" label="Sections">');
  cbox.append(curog);
  var bHeadlines = $("#bodyContent .mw-headline");
  if(bHeadlines.length > 0) bHeadlines.each(addBEl);

  otype = 2; // Active Backlog tasks
  curog = $('<optgroup class="itocTPT" label="Top Priority Tasks">');
  cbox.append(curog);
  var tpBacklogIssues = $(".backlogIssue.topPriority");
  if(tpBacklogIssues.length > 0) tpBacklogIssues.each(addBEl);
  curog = $('<optgroup class="itocT" label="Tasks">');
  cbox.append(curog);
  var bTasks = $(".backlogIssue:not(.topPriority)");
  if(bTasks.length > 0) bTasks.each(addBEl);

  toc.replaceWith(cbox);

  if(tpBacklogIssues.length > 0) {
    cbox.after('<input id="onlyTopPriority" type="checkbox" hidden>',
               '<label for="onlyTopPriority" title="Only Top-Priority">⭐</label>');
    var tpchk = $("#onlyTopPriority");
    function filterOnlyTopPrio(onlyTop) {
      $(".backlogIssue:not(.topPriority), dd, #atoc~p").css("display", onlyTop?'none':'');
    }
    tpchk.change(function(){filterOnlyTopPrio(tpchk.prop('checked'));});
  }
}

$(document).ready(function(){fixSrTmplBug();fixEntryPrevBug();dynTimers();advBacklog();});
// @license-end


Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the page “GNU Free Documentation License”.

The copyright and license notices on this page only apply to the text on this page. Any software or copyright-licenses or other similar notices described in this text has its own copyright notice and license, which can usually be found in the distribution or license text itself.