Difference between revisions of "MediaWiki:Common.js"

From EncyclopAtys

Jump to: navigation, search
(Fixed directories)
(Removed obsolete code)
Line 8: Line 8:
 
}
 
}
  
βˆ’
/** Collapsible tables *********************************************************
 
βˆ’
*
 
βˆ’
*  Description: Allows tables to be collapsed, showing only the header. See
 
βˆ’
*              http://www.mediawiki.org/wiki/Manual:Collapsible_tables.
 
βˆ’
*  Maintainers: [[**MAINTAINERS**]]
 
βˆ’
*/
 
βˆ’
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 Button2    = document.createElement( 'span' );
 
βˆ’
var Button    = document.createElement( 'span' );
 
βˆ’
var ButtonLink = document.createElement( 'a' );
 
βˆ’
var ButtonText = document.createTextNode( collapseCaption );
 
βˆ’
 
βˆ’
Button2.className = 'collapseButton_outer';
 
βˆ’
Button.className = 'collapseButton'; // Styles are declared in MediaWiki:Common.css
 
βˆ’
                        if(hasClass( Tables[i], 'dark' )) {
 
βˆ’
                          Button.className = Button.className+'_dark';
 
βˆ’
                        }
 
  
βˆ’
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( ']' ) );
 
βˆ’
Button2.appendChild( Button );
 
βˆ’
Header.insertBefore( Button2, 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 );
 
βˆ’
 
 
/** Test if an element has a certain class **************************************
 
/** Test if an element has a certain class **************************************
 
  *
 
  *
Line 103: Line 21:
 
};
 
};
 
})();
 
})();
βˆ’
 
βˆ’
 
βˆ’
/** Dynamic Navigation Bars (experimental) *************************************
 
βˆ’
*
 
βˆ’
*  Description: See [[Wikipedia:NavFrame]].
 
βˆ’
*  Maintainers: UNMAINTAINED
 
βˆ’
*/
 
βˆ’
 
βˆ’
// set up the words in your language
 
βˆ’
var NavigationBarHide = '[' + collapseCaption + ']';
 
βˆ’
var NavigationBarShow = '[' + expandCaption + ']';
 
βˆ’
 
βˆ’
// 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, 'NavContent') || hasClass(NavChild, 'NavPic')) {
 
βˆ’
                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, 'NavContent') || hasClass(NavChild, 'NavPic')) {
 
βˆ’
                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 isCollapsed = hasClass( NavFrame, "collapsed" );
 
βˆ’
            /*
 
βˆ’
            * Check if any children are already hidden.  This loop is here for backwards compatibility:
 
βˆ’
            * the old way of making NavFrames start out collapsed was to manually add style="display:none"
 
βˆ’
            * to all the NavPic/NavContent elements.  Since this was bad for accessibility (no way to make
 
βˆ’
            * the content visible without JavaScript support), the new recommended way is to add the class
 
βˆ’
            * "collapsed" to the NavFrame itself, just like with collapsible tables.
 
βˆ’
            */
 
βˆ’
            for (var NavChild = NavFrame.firstChild; NavChild != null && !isCollapsed; NavChild = NavChild.nextSibling) {
 
βˆ’
                if ( hasClass( NavChild, 'NavPic' ) || hasClass( NavChild, 'NavContent' ) ) {
 
βˆ’
                    if ( NavChild.style.display == 'none' ) {
 
βˆ’
                        isCollapsed = true;
 
βˆ’
                    }
 
βˆ’
                }
 
βˆ’
            }
 
βˆ’
            if (isCollapsed) {
 
βˆ’
                for (var NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling) {
 
βˆ’
                    if ( hasClass( NavChild, 'NavPic' ) || hasClass( NavChild, 'NavContent' ) ) {
 
βˆ’
                        NavChild.style.display = 'none';
 
βˆ’
                    }
 
βˆ’
                }
 
βˆ’
            }
 
βˆ’
            var NavToggleText = document.createTextNode(isCollapsed ? NavigationBarShow : 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);
 
βˆ’
        }
 
βˆ’
    }
 
βˆ’
}
 
βˆ’
 
βˆ’
// addOnloadHook( createNavigationBarToggleButton );
 
  
  
βˆ’
/** <nowiki> **/
+
 
βˆ’
/** additional scripts **/
 
βˆ’
if ( wgIsArticle || window.location.href.indexOf( 'action=submit' ) > -1 )
 
βˆ’
{
 
βˆ’
  addScript( 'MediaWiki:CollapsibleTables.js' );
 
βˆ’
  hookEvent( 'load', function()
 
βˆ’
  {
 
βˆ’
    new CollapsibleTables();
 
βˆ’
    diffwithFix();
 
βˆ’
   
 
βˆ’
    // Extension for the deletion drop down list
 
βˆ’
    if ( wgAction == 'delete' && ( delReasonBtn = document.getElementById( 'wpConfirmB' ) ) )
 
βˆ’
    {
 
βˆ’
      var delReasonList        = document.getElementById( 'wpDeleteReasonList' );
 
βˆ’
      var delLink              = document.createElement( 'a' );
 
βˆ’
      delLink.href            = 'javascript:void(0);'
 
βˆ’
      delLink.title            = document.getElementById( 'wpReason' ).value;
 
βˆ’
      delLink.style.fontSize  = '0.9em';
 
βˆ’
      delLink.style.marginLeft = '1em';
 
βˆ’
      delLink.onclick          = function()
 
βˆ’
      {
 
βˆ’
        document.getElementById( 'wpReason' ).value = this.title;
 
βˆ’
      }
 
βˆ’
      delReasonList.onchange  = function ()
 
βˆ’
      {
 
βˆ’
        document.getElementById( 'wpReason' ).value = '';
 
βˆ’
        this.onchange = null;
 
βˆ’
      }
 
βˆ’
      delLink.appendChild( document.createTextNode( 'restore default reason' ) );
 
βˆ’
      delReasonBtn.parentNode.appendChild( delLink );
 
βˆ’
      delete delLink, delReasonList, delReasonBtn;
 
βˆ’
    }
 
βˆ’
  } );
 
βˆ’
}
 
 
if ( wgPageName == 'Special:Upload' && document.getElementById( 'mw-upload-form' ) !== 'undefined' )
 
if ( wgPageName == 'Special:Upload' && document.getElementById( 'mw-upload-form' ) !== 'undefined' )
 
{
 
{

Revision as of 13:05, 22 October 2017

function hs(id,mod) {
			if(document.getElementById(id).style.display == 'none') {
				document.getElementById(id).style.display=mod;
			}
			else {
				document.getElementById(id).style.display='none';
			}
		}


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



if ( wgPageName == 'Special:Upload' && document.getElementById( 'mw-upload-form' ) !== 'undefined' )
{
  addScript( 'MediaWiki:UploadForm.js' );
  hookEvent( 'load', function()
  {
    new UploadForm();
  } );
}

/**** function addScript.js
 * by Patrick Westerhoff [poke]
 */
function addScript ( pagename )
{
  var script  = document.createElement( 'script' );
  pagename    = encodeURI( pagename.replace( ' ', '_' ) );
  script.src  = '/w/index.php?title=' + pagename + '&action=raw&ctype=text/javascript';
  script.type = 'text/javascript';
  
  document.getElementsByTagName( 'head' )[0].appendChild( script );
}

/**** function diffwithFix.js
 * by Patrick Westerhoff [poke]
 */
function diffwithFix ()
{
  var diffSpan = document.getElementById( 'diffwith' );
  if ( diffSpan == undefined )
    return;
  
  var diffLink  = diffSpan.getElementsByTagName( 'a' )[0];
  var diffTitle = diffSpan.title;
  var xmlHttp;
  
  try
  {
    xmlHttp = new XMLHttpRequest();
  }
  catch ( e )
  {
    try
    {
      xmlHttp = new ActiveXObject( 'Msxml2.XMLHTTP' );
    }
    catch ( e )
    {
      try
      {
        xmlHttp = new ActiveXObject( 'Microsoft.XMLHTTP' );
      }
      catch ( e )
      {
        diffSpan.style.fontSize = '90%';
        diffSpan.innerHTML      = '(Automated diff <b>not available</b>.)';
        return;
      }
    }
  }
  
  xmlHttp.onreadystatechange = function ()
  {
    if ( xmlHttp.readyState != 4 )
      return;
    
    revs = xmlHttp.responseXML.getElementsByTagName( 'rev' );
    
    if ( revs.length > 0 )
    {
      diffLink.href += '&oldid=' + revs[0].getAttribute( 'revid' );
      diffSpan.title = '';
    }  
  } 
  xmlHttp.open( 'GET', '/w/api.php?format=xml&action=query&prop=revisions&rvprop=ids&rvlimit=1&titles=' + diffTitle, true );
  xmlHttp.send( null );
}
/** </nowiki> **/