MediaWiki:Common.js: Difference between revisions

From Puella Magi Wiki
Jump to navigation Jump to search
m (2nd try)
No edit summary
 
(7 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">
  *                         http://www.mediawiki.org/wiki/Manual:Collapsible_tables.
  *   {DAY} days {HOUR} hours {MINUTE} minutes {SECOND} seconds
  * Maintainers: [[en:User:R. Koot]]
  * </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();
  });
})($('.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") {
        var Rows = Table.rows;
        if (!container.hasClass('mw-collapsed')) {
          container.find('.mw-collapsible-toggle a').click();
        if ( Button.firstChild.data == collapseCaption ) {
                for ( var i = 1; i < Rows.length; i++ ) {
                        Rows[i].style.display = 'none';
                }
                Button.firstChild.data = expandCaption;
        } else {
                for ( var i = 1; i < Rows.length; i++ ) {
                        Rows[i].style.display = Rows[0].style.display;
                }
                Button.firstChild.data = collapseCaption;
         }
         }
      }
    });
  });
});
/* 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")
    }
  })
}
}
 
function createCollapseButtons() {
$(window).ready(checkTabsOnHashChange).bind('hashchange', checkTabsOnHashChange);
        var tableIndex = 0;
 
        var NavigationBoxes = new Object();
/* Make tab content scrollable.
        var Tables = document.getElementsByTagName( 'table' );
  * Written by Celtic Minstrel
        for ( var i = 0; i < Tables.length; i++ ) {
                if ( hasClass( Tables[i], 'collapsible' ) ) {
                        /* 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];
                        if ( !Header ) {
                                continue;
                        }
                        NavigationBoxes[tableIndex] = Tables[i];
                        Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );
                        var Button = document.createElement( 'span' );
                        var ButtonLink = document.createElement( 'a' );
                        var ButtonText = document.createTextNode( collapseCaption );
                        Button.className = 'collapseButton'; // Styles are declared in [[MediaWiki:Common.css]]
                        ButtonLink.style.color = Header.style.color;
                        ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
                        ButtonLink.setAttribute( 'href', "javascript:collapseTable(" + tableIndex + ");" );
                        ButtonLink.appendChild( ButtonText );
                        Button.appendChild( document.createTextNode( '[' ) );
                        Button.appendChild( ButtonLink );
                        Button.appendChild( document.createTextNode( ']' ) );
                        Header.insertBefore( Button, Header.childNodes[0] );
                        tableIndex++;
                }
        }
        for ( var i = 0;  i < tableIndex; i++ ) {
                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;
                                }
                        }
                }
        }
}
addOnloadHook( createCollapseButtons );
/** Test if an element has a certain class **************************************
*
* Description: Uses regular expressions and caching for better performance.
  * Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
  */
  */
$(window).ready(function() {
var hasClass = ( function() {
  $("div.tabs > .tab-nav ~ div > a")[0].style.marginLeft = "1.6em";
         var reCache = {};
  $("div.tabs > .tab-nav.left").each(function() {
         return function( element, className ) {
    var interval;
                return ( reCache[className] ? reCache[className] : ( reCache[className] = new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ) ) ).test( element.className );
    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);
    });
  });
});