MediaWiki:Common.js: Difference between revisions

From Linguifex
Jump to navigation Jump to search
No edit summary
m (Reverted edits by EmperorZelos (talk) to last revision by Waahlis)
 
(2 intermediate revisions by 2 users not shown)
Line 91: Line 91:
addOnloadHook( createCollapseButtons );
addOnloadHook( createCollapseButtons );
   
   
=== NavBars ===
<pre>*/
var NavigationBarHide = 'hide ▲';
var NavigationBarShow = 'show ▼';
function NavToggleCategory(navFrame)
{
    var table = navFrame.getElementsByTagName('table')[0];
    if (table && table.className == "translations")
        return "translations";
    var heading = navFrame.previousSibling;
    while (heading) {
        if (/[hH][4-6]/.test(heading.nodeName)) {
            if (heading.getElementsByTagName('span')[1])
                heading = heading.getElementsByTagName('span')[1];
            else
                heading = heading.getElementsByTagName('span')[0];
            return $(heading).text().toLowerCase()
                // jQuery's .text() is inconsistent about whitespace:
                .replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ')
                // remove numbers added by the "Auto-number headings" pref:
                .replace(/^[1-9][0-9.]+ ?/, '');
        }
        else if (/[hH][1-3]/.test(heading.nodeName))
            break;
        heading = heading.previousSibling;
    }
    return "other boxes";
};
function createNavToggle(navFrame){
    var navHead, navToggle, navContent;
    for (var j=0; j < navFrame.childNodes.length; j++) {
        var div = navFrame.childNodes[j];
        switch (div.className) {
            case 'NavHead':
                navHead = div;
                break;
            case 'NavContent':
                navContent = div;
                break;
        }
    }
    if (!navHead || !navContent)
        return;
    // Step 1, don't react when a subitem is clicked.
    for (var i=0; i<navHead.childNodes.length; i++) {
        var child = navHead.childNodes[i];
        if (child.nodeName == "A") {
            child.onclick = function (e)
            {
                if (e && e.stopPropagation)
                    e.stopPropagation();
                else
                    window.event.cancelBubble = true;
            }
        }
    }
    // Step 2, toggle visibility when bar is clicked.
    // NOTE This function was chosen due to some funny behaviour in Safari.
    navToggle = newNode('a', {href: 'javascript:(function(){})()'}, '');
    navHead.insertBefore(newNode('span', {'class': 'NavToggle'}, '[', navToggle, ']'), navHead.firstChild);
    navHead.style.cursor = "pointer";
    navHead.onclick = VisibilityToggles.register(NavToggleCategory(navFrame),
        function show() {
            navToggle.innerHTML = NavigationBarHide;
            if (navContent)
                navContent.style.display = "block";
        },
        function hide() {
            navToggle.innerHTML = NavigationBarShow;
            if (navContent)
                navContent.style.display = "none";
        });
};
addOnloadHook( function ()
{
    var divs = $(".NavFrame");
    for (var i=0; i<divs.length; i++) {
        // NOTE: some templates use a class of NavFrame for the style, but for legacy reasons, are not NavFrames
        if (divs[i].className == "NavFrame") {
            createNavToggle(divs[i]);
        }
    }
});
/*</pre>
/** Test if an element has a certain class **************************************
/** Test if an element has a certain class **************************************
  *
  *
Line 197: Line 105:


/* For Favicon  */
/* For Favicon  */
<!-- document.write("<link rel='shortcut icon' href='http://conlang.wikkii.com/w/images/conlang/uploads/2/26/Favicon.png' type='image/x-icon'>"); -->
document.write("<link rel='shortcut icon' href='http://conlang.wikkii.com/w/images/conlang/uploads/2/26/Favicon.png' type='image/x-icon'>");
 
===Hidden Quotes===
<pre>*/
 
function setupHiddenQuotes(li)
{
  var HQToggle, liComp;
  var HQShow = 'quotations ▼';
  var HQHide = 'quotations ▲';
  for (var k = 0; k < li.childNodes.length; k++) {
      // Look at each component of the definition.
      liComp = li.childNodes[k];
      // If we find a ul or dl, we have quotes or example sentences, and thus need a button.
      if (/^(ul|UL)$/.test(liComp.nodeName)) {
        HQToggle = newNode('a', {href: 'javascript:(function(){})()'}, '');
        li.insertBefore(newNode('span', {'class': 'HQToggle'}, ' [', HQToggle, ']'), liComp);
        HQToggle.onclick = VisibilityToggles.register('quotations',         
            function show() {
              HQToggle.innerHTML = HQHide;
              for (var child = li.firstChild; child != null; child = child.nextSibling) {
                  if (/^(ul|UL)$/.test(child.nodeName)) {
                    child.style.display = 'block';
                    }
                  }
              },
            function hide() {
              HQToggle.innerHTML = HQShow;
              for (var child = li.firstChild; child != null; child = child.nextSibling) {
                  if (/^(ul|UL)$/.test(child.nodeName)) {
                    child.style.display = 'none';
                    }
                  }
              });
 
        break;
        }
      }
  }           
             
addOnloadHook( function ()
{
  if (wgNamespaceNumber == 0) {
      var ols, lis, li;
      // First, find all the ordered lists, i.e. all the series of definitions.
      var ols = document.getElementsByTagName('ol');
      for(var i = 0; i < ols.length; i++) {
        // Then, for every set, find all the individual definitions.
        for (var j = 0; j < ols[i].childNodes.length; j++) {
            li = ols[i].childNodes[j];
            if (li.nodeName.toUpperCase() == 'LI') {
              setupHiddenQuotes(li);
              }
            }
        }
      }
  });
 
/*</pre>

Latest revision as of 19:25, 20 January 2013

/** Collapsible tables *********************************************************
 *
 *  Description: Allows tables to be collapsed, showing only the header. See
 *                         http://www.mediawiki.org/wiki/Manual:Collapsible_tables.
 *  Maintainers: [[en:User:R. Koot]]
 */
 
var autoCollapse = 2;
var collapseCaption = 'hide';
var expandCaption = 'show';
 
function collapseTable( tableIndex ) {
        var Button = document.getElementById( 'collapseButton' + tableIndex );
        var Table = document.getElementById( 'collapsibleTable' + tableIndex );
 
        if ( !Table || !Button ) {
                return false;
        }
 
        var Rows = Table.rows;
 
        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;
        }
}
 
function createCollapseButtons() {
        var tableIndex = 0;
        var NavigationBoxes = new Object();
        var Tables = document.getElementsByTagName( 'table' );
 
        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]]
 */
 
var hasClass = ( function() {
        var reCache = {};
        return function( element, className ) {
                return ( reCache[className] ? reCache[className] : ( reCache[className] = new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ) ) ).test( element.className );
        };
})();

/* For Favicon  */
document.write("<link rel='shortcut icon' href='http://conlang.wikkii.com/w/images/conlang/uploads/2/26/Favicon.png' type='image/x-icon'>");