MediaWiki:Common.js: Difference between revisions

From Puella Magi Wiki
Jump to navigation Jump to search
(Trying to enable the collapsible tables)
 
No edit summary
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */
$.fx.off = true;


/** Collapsible tables *********************************************************
/* Super simple HTML countdown timer. Seriously, it couldn't get any simpler.
* Written by Maru <maru@puella-magi.net>. Released under public domain.
* Requires jQuery.
  *
  *
  * Description: Allows tables to be collapsed, showing only the header. See
  * <span class="countdown" data-until="2012/12/25 00:00:00">
  *               [[Wikipedia:NavFrame]].
  *   {DAY} days {HOUR} hours {MINUTE} minutes {SECOND} seconds
  * Source: Wikipedia's MediaWiki:Common.js table
  * </span>
  */
  */
(function(self){
var autoCollapse = 2;
  $.each(self, function(i, elem){
var collapseCaption = "hide";
    var elem = $(elem);
var expandCaption = "show";
    var targetDate = new Date(elem.data('until'));
    var template = elem.html();
function collapseTable( tableIndex ){
    var counter = function(){
    var Button = document.getElementById( "collapseButton" + tableIndex );
      var text = template;
    var Table = document.getElementById( "collapsibleTable" + tableIndex );
      var dateDiff = Math.floor((targetDate - new Date()) / 1000);
 
    if ( !Table || !Button ) {
      $.each({
        return false;
        day: (Math.floor(dateDiff/86400) % 100000),
        hour: (Math.floor(dateDiff/3600) % 24),
        minute: (Math.floor(dateDiff/60) % 60),
        second: (Math.floor(dateDiff) % 60)
      }, function(k, v){
        text = text.replace("{" + k.toUpperCase() + "}", v);
      });
 
      elem.html(text);
      setTimeout(counter, 1000);
     }
     }
     counter();
     var Rows = Table.rows;
  });
})($('.countdown'));
    if ( Button.firstChild.data == collapseCaption ) {
 
        for ( var i = 1; i < Rows.length; i++ ) {
/* Restore collapsible state for any element with "pm-persist" class
            Rows[i].style.display = "none";
* This method extends $.makeCollapsible adding ability to persist elements
        }
* collapsible state based on first headline ID.
        Button.firstChild.data = expandCaption;
* Written by Maru <maru@puella-magi.net>. Released under public domain.
    } else {
*
        for ( var i = 1; i < Rows.length; i++ ) {
* <div class="mw-collapsible pm-persist">
            Rows[i].style.display = Rows[0].style.display;
*  ...
        }
* </div>
         Button.firstChild.data = collapseCaption;
*/
$(window).ready(function(){
  mw.loader.using(['jquery.cookie', 'jquery.makeCollapsible'], function(){
    function cookieName(container) {
      var section = container.find('.mw-headline').prop('id');
      if (section) {
         return 'pm-collapse-' + section;
      }
     }
     }
}
 
     // Flag default state for collapsible element.
function createCollapseButtons(){
     $('.pm-persist .mw-collapsible-toggle a').click(function(e){
     var tableIndex = 0;
      var container = $(this).parents('.mw-collapsible');
     var NavigationBoxes = new Object();
      var idName = cookieName(container);
    var Tables = document.getElementsByTagName( "table" );
      if (idName) {
        // Note that the state is reversed; false means collapsed
    for ( var i = 0; i < Tables.length; i++ ) {
        // and true means expanded. This is because the click event
        if ( hasClass( Tables[i], "collapsible" ) ) {
        // is run before makeCollapsible is run.
        $.cookie(idName, container.hasClass('mw-collapsed'));
            /* only add button and increment count if there is a header row to work with */
      }
            var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0];
    });
            if (!HeaderRow) continue;
 
            var Header = HeaderRow.getElementsByTagName( "th" )[0];
    // Restore default state on page load.
            if (!Header) continue;
    $('.pm-persist').each(function(i, elem){
      var container = $(this), idName = cookieName(container);
            NavigationBoxes[ tableIndex ] = Tables[i];
      var expanded = $.cookie(idName);
            Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
      if (expanded === "true") {
        if (container.hasClass('mw-collapsed')) {
            var Button    = document.createElement( "span" );
          container.find('.mw-collapsible-toggle a').click();
            var ButtonLink = document.createElement( "a" );
            var ButtonText = document.createTextNode( collapseCaption );
            Button.className = "collapseButton";  //Styles are declared in Common.css
            ButtonLink.style.color = Header.style.color;
            ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
            ButtonLink.setAttribute( "href", "#" );
            addHandler( ButtonLink, "click", new Function( "evt", "collapseTable(" + tableIndex + " ); return killEvt( evt );") );
            ButtonLink.appendChild( ButtonText );
            Button.appendChild( document.createTextNode( "[" ) );
            Button.appendChild( ButtonLink );
            Button.appendChild( document.createTextNode( "]" ) );
            Header.insertBefore( Button, Header.childNodes[0] );
            tableIndex++;
         }
         }
    }
      } else if (expanded === "false") {
         if (!container.hasClass('mw-collapsed')) {
    for ( var i = 0;  i < tableIndex; i++ ) {
          container.find('.mw-collapsible-toggle a').click();
        if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {
            collapseTable( i );
        }
         else if ( hasClass( NavigationBoxes[i], "innercollapse" ) ) {
            var element = NavigationBoxes[i];
            while (element = element.parentNode) {
                if ( hasClass( element, "outercollapse" ) ) {
                    collapseTable ( i );
                    break;
                }
            }
         }
         }
      }
    });
  });
});
/* Makes sure the 1st tab is hilited if and only if no other tabs are.
* Written by Celtic Minstrel
*/
function checkTabsOnHashChange(evt) {
  $("div.tabs > div:first-child").each(function() {
    if($(this).siblings(":target").length == 0) {
      $(this).addClass("default")
    } else {
      $(this).removeClass("default")
     }
     }
  })
}
}
   
 
$( createCollapseButtons );
$(window).ready(checkTabsOnHashChange).bind('hashchange', checkTabsOnHashChange);
 
/* Make tab content scrollable.
* Written by Celtic Minstrel
  */
$(window).ready(function() {
  $("div.tabs > .tab-nav ~ div > a")[0].style.marginLeft = "1.6em";
  $("div.tabs > .tab-nav.left").each(function() {
    var interval;
    var self = this
    $(this).on('mousedown', function(evt) {
      interval = setInterval(function() {
        var tabs = $(self).nextAll().children("a");
        if(tabs.last().position().left <= 200) return;
        tabs.offset(function(i, coords){
          coords.left -= 5;
          return coords;
        });
      }, 10);
    });
    $(this).on('mouseup', function(evt) {
      clearInterval(interval);
    });
  });
  $("div.tabs > .tab-nav.right").each(function() {
    var interval;
    var self = this
    $(this).on('mousedown', function(evt) {
      interval = setInterval(function() {
        var tabs = $(self).nextAll().children("a");
        if(tabs.first().position().left >= 0) return;
        tabs.offset(function(i, coords){
          coords.left += 5;
          return coords;
        });
      }, 10);
    });
    $(this).on('mouseup', function(evt) {
      clearInterval(interval);
    });
  });
});

Latest revision as of 08:31, 10 September 2023

/* Any JavaScript here will be loaded for all users on every page load. */
$.fx.off = true;

/* Super simple HTML countdown timer. Seriously, it couldn't get any simpler.
 * Written by Maru <maru@puella-magi.net>. Released under public domain.
 * Requires jQuery.
 *
 * <span class="countdown" data-until="2012/12/25 00:00:00">
 *   {DAY} days {HOUR} hours {MINUTE} minutes {SECOND} seconds
 * </span>
 */
(function(self){
  $.each(self, function(i, elem){
    var elem = $(elem);
    var targetDate = new Date(elem.data('until'));
    var template = elem.html();
    var counter = function(){
      var text = template;
      var dateDiff = Math.floor((targetDate - new Date()) / 1000);

      $.each({
        day: (Math.floor(dateDiff/86400) % 100000),
        hour: (Math.floor(dateDiff/3600) % 24),
        minute: (Math.floor(dateDiff/60) % 60),
        second: (Math.floor(dateDiff) % 60)
      }, function(k, v){
        text = text.replace("{" + k.toUpperCase() + "}", v);
      });

      elem.html(text);
      setTimeout(counter, 1000);
    }
    counter();
  });
})($('.countdown'));

/* Restore collapsible state for any element with "pm-persist" class
 * This method extends $.makeCollapsible adding ability to persist elements
 * collapsible state based on first headline ID.
 * Written by Maru <maru@puella-magi.net>. Released under public domain.
 *
 * <div class="mw-collapsible pm-persist">
 *   ...
 * </div>
 */
$(window).ready(function(){
  mw.loader.using(['jquery.cookie', 'jquery.makeCollapsible'], function(){
    function cookieName(container) {
      var section = container.find('.mw-headline').prop('id');
      if (section) {
        return 'pm-collapse-' + section;
      }
    }

    // Flag default state for collapsible element.
    $('.pm-persist .mw-collapsible-toggle a').click(function(e){
      var container = $(this).parents('.mw-collapsible');
      var idName = cookieName(container);
      if (idName) {
        // Note that the state is reversed; false means collapsed
        // and true means expanded. This is because the click event
        // is run before makeCollapsible is run.
        $.cookie(idName, container.hasClass('mw-collapsed'));
      }
    });

    // Restore default state on page load.
    $('.pm-persist').each(function(i, elem){
      var container = $(this), idName = cookieName(container);
      var expanded = $.cookie(idName);
      if (expanded === "true") {
        if (container.hasClass('mw-collapsed')) {
          container.find('.mw-collapsible-toggle a').click();
        }
      } else if (expanded === "false") {
        if (!container.hasClass('mw-collapsed')) {
          container.find('.mw-collapsible-toggle a').click();
        }
      }
    });
  });
});


/* Makes sure the 1st tab is hilited if and only if no other tabs are.
 * Written by Celtic Minstrel 
 */
function checkTabsOnHashChange(evt) {
  $("div.tabs > div:first-child").each(function() {
    if($(this).siblings(":target").length == 0) {
      $(this).addClass("default")
    } else {
      $(this).removeClass("default")
    }
  })
}

$(window).ready(checkTabsOnHashChange).bind('hashchange', checkTabsOnHashChange);

/* Make tab content scrollable.
 * Written by Celtic Minstrel 
 */
$(window).ready(function() {
  $("div.tabs > .tab-nav ~ div > a")[0].style.marginLeft = "1.6em";
  $("div.tabs > .tab-nav.left").each(function() {
    var interval;
    var self = this
    $(this).on('mousedown', function(evt) {
      interval = setInterval(function() {
        var tabs = $(self).nextAll().children("a");
        if(tabs.last().position().left <= 200) return;
        tabs.offset(function(i, coords){
          coords.left -= 5;
          return coords;
        });
      }, 10);
    });
    $(this).on('mouseup', function(evt) {
      clearInterval(interval);
    });
  });
  $("div.tabs > .tab-nav.right").each(function() {
    var interval;
    var self = this
    $(this).on('mousedown', function(evt) {
      interval = setInterval(function() {
        var tabs = $(self).nextAll().children("a");
        if(tabs.first().position().left >= 0) return;
        tabs.offset(function(i, coords){
          coords.left += 5;
          return coords;
        });
      }, 10);
    });
    $(this).on('mouseup', function(evt) {
      clearInterval(interval);
    });
  });
});