MediaWiki:Common.js

Z VarenWiki
Přejít na: navigace, hledání
/*<source lang="javascript">*/
 
/* hvězdičky pro featured+good articles v jiných jazycích */
function LinkFA() 
{
    var langbox = document.getElementById("p-lang");
    if (!langbox) return;
    var interwiki = langbox.getElementsByTagName("li");
    // iterace přes všechny mezijazykové odkazy
    for (var i = 0; i < interwiki.length; ++i)
    {
        var link = interwiki[i];
        var languageMatch = /\binterwiki-([a-z-]+)\b/.exec(link.className);
        if (!languageMatch) continue;
        var language = languageMatch[1];
        // zkusit najít odpovídající FA nebo GA element
        var falink = document.getElementById("fa-link-" + language) && link.className.indexOf("badge-featuredarticle") === -1;
        if (falink)
        {
            link.className += " featured";
            link.title = "Tento článek patří mezi nejlepší";
        } else {
            var galink = document.getElementById("ga-link-" + language) && link.className.indexOf("badge-goodarticle") === -1;
            if (galink)
            {
                link.className += " good";
                link.title = "Tento článek patří mezi dobré";
            }
        }
    }
}

/*******************************\
|*     Podpora pro navboxy     *|
\*******************************/

/* Pro další informace vizte [[:en:Help:Collapsing]]. */

var autoCollapse = 2;
var collapseCaption = "skrýt";
var expandCaption = "zobrazit";
 
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 = {};
    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 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;
                }
            }
        }
    }
} 
 

/* Test if an element has a certain class *
 *
 * Převzato z [[:en:MediaWiki:Common.js]]
 */
 
var hasClass = (function () {
    var reCache = {};
    return function (element, className) {
        return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
    };
})();


/**********************\
|* Změna názvu článku *|
|* vytvořil Danny B.  *|
\**********************/
function SpravnyNazev ()
  {
  if (mw.config.get('wgNamespaceNumber') === 0) return;

  var nodSpravnyNazev = document.getElementById ("Sablona__Nazev_Spravny-nazev");

  if (nodSpravnyNazev)
    {
    var nodSablonaNazev = document.getElementById ("Sablona__Nazev");
    var nodNadpis = document.getElementsByTagName ("h1") [0];
    var anodNadpisObsah = nodNadpis.getElementsByTagName("span");
    var nodPuvodniNazev = null;
    for (var i = 0; i < anodNadpisObsah.length; ++i)
      {
      var n = anodNadpisObsah[i];
      if (n.className != "editsection")
        {
        nodPuvodniNazev = n;
        break;
        }
      }
    if (nodPuvodniNazev == null) return;
    nodNadpis.replaceChild (nodSpravnyNazev, nodPuvodniNazev);
    nodSablonaNazev.parentNode.removeChild (nodSablonaNazev);
    }
  }


/*****************\
|*  csWikipedia  *|
|* customization *|
\*****************/
 
function CustomizeCsWikipedia ()
  {
  LinkFA();
  SpravnyNazev();
  createCollapseButtons ();
  
  }

if (window.addEventListener)
  {
  window.addEventListener ("load", CustomizeCsWikipedia, false);
  }
else if (window.attachEvent)
  {
  window.attachEvent ("onload", CustomizeCsWikipedia);
  };
 
/*</source>*/

/*************************\
|* české řazení dle ÚJČ AV ČR (prirucka.ujc.cas.cz/?id=900) *|
|* vytvořil Littledogboy                                    *|
\*************************/
mw.config.set( 'tableSorterCollation', {'á':'a','č':'cÑÑ','ď':'d','é':'e','ě':'e','í':'i','ň':'n','ó':'o','ř':'rÑÑ','š':'sÑÑ','ť':'t','ú':'u','ů':'u','ý':'y','ž':'zÑÑ','Á':'A','Č':'CÑÑ','Ď':'D','É':'E','Ě':'E','Í':'I','Ň':'N','Ó':'O','Ř':'RÑÑ','Š':'SÑÑ','Ť':'T','Ú':'U','Ů':'U','Ý':'Y','Ž':'ZÑÑ'} );

/**
 * @source https://www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
 * @revision 2016-03-26
 */
mw.loader.using( ['mediawiki.util', 'mediawiki.notify'], function () {
	var extraCSS = mw.util.getParamValue( 'withCSS' ),
		extraJS = mw.util.getParamValue( 'withJS' ),
		extraModule = mw.util.getParamValue( 'withModule' );

	if ( extraCSS ) {
		// DONT REMOVE THIS IF (unless you are OK with CSRF attacks)
		if ( /^MediaWiki:[^&<>=%#]*\.css$/.test( extraCSS ) ) {
			mw.loader.load( '/w/index.php?title=' + extraCSS + '&action=raw&ctype=text/css', 'text/css' );
		} else {
			mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
		}
	}

	if ( extraJS ) {
		// DONT REMOVE THIS IF (unless you are OK with CSRF attacks)
		if ( /^MediaWiki:[^&<>=%#]*\.js$/.test( extraJS ) ) {
			mw.loader.load( '/w/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript' );
		} else {
			mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
		}
	}

	if ( extraModule ) {
		if ( /^ext\.gadget\.[^,\|]+$/.test( extraModule ) ) {
			mw.loader.load( extraModule );
		} else {
			mw.notify( 'Only gadget modules are allowed.', { title: 'Invalid withModule value' } );
		}
	}
} );

// Results from Wikidata
// [[File:Wdsearch_script_screenshot.png]]
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Search' ||  ( mw.config.get( 'wgArticleId' ) === 0 && mw.config.get( 'wgCanonicalSpecialPageName' ) === false ) ) {
	mw.loader.load("//en.wikipedia.org/w/index.php?title=MediaWiki:Wdsearch.js&action=raw&ctype=text/javascript");
}

// vyvolání stylu klikatelných tlačítek
mw.hook('wikipage.content').add(function() {
	if ((mw.loader.getState('oojs-ui') === 'registered') && ($('.oo-ui-widget').length)) {
		mw.loader.using(['oojs-ui']);
	}
});