MediaWiki:Common.js: Difference between revisions

From Baka-Tsuki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
//<source lang="javascript">
/** Collapsible tables *********************************************************
/** Tooltips and access keys ***************************************************
*
  *
*  Description: Allows tables to be collapsed, showing only the header. See
  *  Description: Adds tooltips and access keys to links part of the MediaWiki
*                         http://www.mediawiki.org/wiki/Manual:Collapsible_tables.
  *               interface.
*  Maintainers: [[en:User:R. Koot]]
  *  Maintainers: [[User:Gwicke|Gwicke]]?, [[User:Simetrical|Simetrical]]?, [[User:Ruud Koot|Ruud Koot]]
*/
  */
   
   
ta = new Object();
var autoCollapse = 2;
ta["n-mainpage"]            = new Array("z","Visit the main page");
var collapseCaption = 'hide';
ta["n-Main-page"]          = new Array("z","Visit the main page");
var expandCaption = 'show';
ta["n-Featured-content"]    = new Array("","Featured content — the best of Wikipedia");
ta["n-help"]                = new Array("","The place to find out about Wikipedia");
ta["n-contact"]            = new Array("","How to contact Wikipedia");
ta["n-sitesupport"]        = new Array("","Help keep Wikipedia running");
ta["t-print"]              = new Array("","Printable version of this page");
ta["t-permalink"]          = new Array("","Permanent link to this version of the page");
ta["t-cite"]                = new Array("","Cite this Wikipedia article");
ta["ca-nstab-project"]      = new Array("c","View the project page");
ta["n-Contents"]            = new Array("","Guides to browsing Wikipedia");
 
/** Import module *************************************************************
  *
  *  Description: Includes a raw wiki page as javascript or CSS,
  *              used for including user made modules.
  *  Maintainers: [[User:AzaToth]]
  */
importedScripts = {}; // object keeping track of included scripts, so a script ain't included twice
function importScript( page ) {
    if( importedScripts[page] ) {
        return;
    }
    importedScripts[page] = true;
    var url = wgScriptPath
            + '/index.php?title='
            + encodeURIComponent( page.replace( ' ', '_' ) )
            + '&action=raw&ctype=text/javascript';
    var scriptElem = document.createElement( 'script' );
    scriptElem.setAttribute( 'src' , url );
    scriptElem.setAttribute( 'type' , 'text/javascript' );
    document.getElementsByTagName( 'head' )[0].appendChild( scriptElem );
}
   
   
function importStylesheet( page ) {
function collapseTable( tableIndex ) {
    var sheet = '@import "'
        var Button = document.getElementById( 'collapseButton' + tableIndex );
              + wgScriptPath
        var Table = document.getElementById( 'collapsibleTable' + tableIndex );
              + '/index.php?title='
              + encodeURIComponent( page.replace( ' ', '_' ) )
              + '&action=raw&ctype=text/css";'
    var styleElem = document.createElement( 'style' );
    styleElem.setAttribute( 'type' , 'text/css' );
    styleElem.appendChild( document.createTextNode( sheet ) );
    document.getElementsByTagName( 'head' )[0].appendChild( styleElem );
}
 
/* 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 () {
        if ( !Table || !Button ) {
    var reCache = {};
                return false;
    return function (element, className) {
        }
        return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
    };
})();
 
/** Internet Explorer bug fix **************************************************
  *
  *  Description: UNDOCUMENTED
  *  Maintainers: [[User:Tom-]]?
  */
   
   
if (window.showModalDialog && document.compatMode && document.compatMode == "CSS1Compat")
        var Rows = Table.rows;
{
  var oldWidth;
  var docEl = document.documentElement;
   
   
  function fixIEScroll()
        if ( Button.firstChild.data == collapseCaption ) {
  {
                for ( var i = 1; i < Rows.length; i++ ) {
    if (!oldWidth || docEl.clientWidth > oldWidth)
                        Rows[i].style.display = 'none';
      doFixIEScroll();
                }
    else
                Button.firstChild.data = expandCaption;
      setTimeout(doFixIEScroll, 1);
        } else {
 
                for ( var i = 1; i < Rows.length; i++ ) {
    oldWidth = docEl.clientWidth;
                        Rows[i].style.display = Rows[0].style.display;
  }
                }
                Button.firstChild.data = collapseCaption;
        }
}
   
   
  function doFixIEScroll() {
function createCollapseButtons() {
    docEl.style.overflowX = (docEl.scrollWidth - docEl.clientWidth < 4) ? "hidden" : "";
        var tableIndex = 0;
  }
        var NavigationBoxes = new Object();
        var Tables = document.getElementsByTagName( 'table' );
   
   
  document.attachEvent("onreadystatechange", fixIEScroll);
        for ( var i = 0; i < Tables.length; i++ ) {
  attachEvent("onresize", fixIEScroll);
                if ( hasClass( Tables[i], 'collapsible' ) ) {
}
 
/** Interwiki links to featured articles ***************************************
  *
  *  Description: Highlights interwiki links to featured articles (or
  *              equivalents) by changing the bullet before the interwiki link
  *              into a star.
  *  Maintainers: [[User:R. Koot]]
  */
   
   
function LinkFA()  
                        /* only add button and increment count if there is a header row to work with */
{
                        var HeaderRow = Tables[i].getElementsByTagName( 'tr' )[0];
    if ( document.getElementById( "p-lang" ) ) {
                        if ( !HeaderRow ) {
        var InterwikiLinks = document.getElementById( "p-lang" ).getElementsByTagName( "li" );
                                continue;
                        }
                        var Header = HeaderRow.getElementsByTagName( 'th' )[0];
                        if ( !Header ) {
                                continue;
                        }
   
   
        for ( var i = 0; i < InterwikiLinks.length; i++ ) {
                        NavigationBoxes[tableIndex] = Tables[i];
            if ( document.getElementById( InterwikiLinks[i].className + "-fa" ) ) {
                        Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );
                InterwikiLinks[i].className += " FA"
                InterwikiLinks[i].title = "This is a featured article in another language.";
            }
        }
    }
}
   
   
addOnloadHook( LinkFA );
                        var Button = document.createElement( 'span' );
 
                        var ButtonLink = document.createElement( 'a' );
/** Collapsible tables *********************************************************
                        var ButtonText = document.createTextNode( collapseCaption );
  *
  *  Description: Allows tables to be collapsed, showing only the header. See
  *              [[Wikipedia:NavFrame]].
  *  Maintainers: [[User:R. Koot]]
  */
   
   
var autoCollapse = 2;
                        Button.className = 'collapseButton'; // Styles are declared in [[MediaWiki:Common.css]]
var collapseCaption = "hide";
var expandCaption = "show";
   
   
function collapseTable( tableIndex )
                        ButtonLink.style.color = Header.style.color;
{
                        ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
    var Button = document.getElementById( "collapseButton" + tableIndex );
                        ButtonLink.setAttribute( 'href', "javascript:collapseTable(" + tableIndex + ");" );
    var Table = document.getElementById( "collapsibleTable" + tableIndex );
                        ButtonLink.appendChild( ButtonText );
   
   
    if ( !Table || !Button ) {
                        Button.appendChild( document.createTextNode( '[' ) );
        return false;
                        Button.appendChild( ButtonLink );
    }
                        Button.appendChild( document.createTextNode( ']' ) );
   
   
    var Rows = Table.getElementsByTagName( "tr" );  
                        Header.insertBefore( Button, Header.childNodes[0] );
                        tableIndex++;
                }
        }
   
   
    if ( Button.firstChild.data == collapseCaption ) {
        for ( var i = 0; i < tableIndex; i++ ) {
        for ( var i = 1; i < Rows.length; i++ ) {
                if ( hasClass( NavigationBoxes[i], 'collapsed' ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], 'autocollapse' ) ) ) {
            Rows[i].style.display = "none";
                        collapseTable( i );
        }
                } else if ( hasClass( NavigationBoxes[i], 'innercollapse' ) ) {
        Button.firstChild.data = expandCaption;
                        var element = NavigationBoxes[i];
    } else {
                        while ( element = element.parentNode ) {
        for ( var i = 1; i < Rows.length; i++ ) {
                                if ( hasClass( element, 'outercollapse' ) ) {
            Rows[i].style.display = Rows[0].style.display;
                                        collapseTable( i );
        }
                                        break;
        Button.firstChild.data = collapseCaption;
                                }
    }
                        }
}
                }
        }
}
   
   
function createCollapseButtons()
addOnloadHook( createCollapseButtons );
{
    var tableIndex = 0;
    var NavigationBoxes = new Object();
    var Tables = document.getElementsByTagName( "table" );
   
   
    for ( var i = 0; i < Tables.length; i++ ) {
/** Test if an element has a certain class **************************************
        if ( hasClass( Tables[i], "collapsible" ) ) {
*
            NavigationBoxes[ tableIndex ] = Tables[i];
* Description: Uses regular expressions and caching for better performance.
            Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
* Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
*/
   
   
            var Button    = document.createElement( "span" );
var hasClass = ( function() {
            var ButtonLink = document.createElement( "a" );
        var reCache = {};
            var ButtonText = document.createTextNode( collapseCaption );
        return function( element, className ) {
                return ( reCache[className] ? reCache[className] : ( reCache[className] = new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ) ) ).test( element.className );
            Button.style.styleFloat = "right";
        };
            Button.style.cssFloat = "right";
})();
            Button.style.fontWeight = "normal";
            Button.style.textAlign = "right";
            Button.style.width = "6em";
            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( "]" ) );
            var Header = Tables[i].getElementsByTagName( "tr" )[0].getElementsByTagName( "th" )[0];
            /* only add button and increment count if there is a header row to work with */
            if (Header) {
                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 );
        }
    }
}
addOnloadHook( createCollapseButtons );
 
/** Dynamic Navigation Bars (experimental) *************************************
  *
  *  Description: See [[Wikipedia:NavFrame]].
  *  Maintainers: UNMAINTAINED
  */
  // set up the words in your language
  var NavigationBarHide = '[' + collapseCaption + ']';
  var NavigationBarShow = '[' + expandCaption + ']';
 
  // set up max count of Navigation Bars on page,
  // if there are more, all will be hidden
  // NavigationBarShowDefault = 0; // all bars will be hidden
  // NavigationBarShowDefault = 1; // on pages with more than 1 bar all bars will be hidden
  var NavigationBarShowDefault = autoCollapse;
 
 
  // shows and hides content and picture (if available) of navigation bars
  // Parameters:
  //    indexNavigationBar: the index of navigation bar to be toggled
  function toggleNavigationBar(indexNavigationBar)
  {
    var NavToggle = document.getElementById("NavToggle" + indexNavigationBar);
    var NavFrame = document.getElementById("NavFrame" + indexNavigationBar);
 
    if (!NavFrame || !NavToggle) {
        return false;
    }
 
    // if shown now
    if (NavToggle.firstChild.data == NavigationBarHide) {
        for (
                var NavChild = NavFrame.firstChild;
                NavChild != null;
                NavChild = NavChild.nextSibling
            ) {
            if ( hasClass( NavChild, 'NavPic' ) ) {
                NavChild.style.display = 'none';
            }
            if ( hasClass( NavChild, 'NavContent') ) {
                NavChild.style.display = 'none';
            }
        }
    NavToggle.firstChild.data = NavigationBarShow;
 
    // if hidden now
    } else if (NavToggle.firstChild.data == NavigationBarShow) {
        for (
                var NavChild = NavFrame.firstChild;
                NavChild != null;
                NavChild = NavChild.nextSibling
            ) {
            if (hasClass(NavChild, 'NavPic')) {
                NavChild.style.display = 'block';
            }
            if (hasClass(NavChild, 'NavContent')) {
                NavChild.style.display = 'block';
            }
        }
    NavToggle.firstChild.data = NavigationBarHide;
    }
  }
 
  // adds show/hide-button to navigation bars
  function createNavigationBarToggleButton()
  {
    var indexNavigationBar = 0;
    // iterate over all < div >-elements
    var divs = document.getElementsByTagName("div");
    for(
            var i=0;
            NavFrame = divs[i];
            i++
        ) {
        // if found a navigation bar
        if (hasClass(NavFrame, "NavFrame")) {
 
            indexNavigationBar++;
            var NavToggle = document.createElement("a");
            NavToggle.className = 'NavToggle';
            NavToggle.setAttribute('id', 'NavToggle' + indexNavigationBar);
            NavToggle.setAttribute('href', 'javascript:toggleNavigationBar(' + indexNavigationBar + ');');
           
            var NavToggleText = document.createTextNode(NavigationBarHide);
            NavToggle.appendChild(NavToggleText);
            // Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked)
            for(
              var j=0;
              j < NavFrame.childNodes.length;
              j++
            ) {
              if (hasClass(NavFrame.childNodes[j], "NavHead")) {
                NavFrame.childNodes[j].appendChild(NavToggle);
              }
            }
            NavFrame.setAttribute('id', 'NavFrame' + indexNavigationBar);
        }
    }
    // if more Navigation Bars found than Default: hide all
    if (NavigationBarShowDefault < indexNavigationBar) {
        for(
                var i=1;
                i<=indexNavigationBar;
                i++
        ) {
            toggleNavigationBar(i);
        }
    }
 
  }
 
  addOnloadHook( createNavigationBarToggleButton );
 
//</source>

Latest revision as of 20:19, 23 July 2011

/** 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 );
        };
})();