Common.js – MediaWiki

From Bohemia Interactive Community
Jump to navigation Jump to search
m (actual version of Common.js from mediaWiki, supports e.g. collapsible class)
m (Fix typo)
 
(82 intermediate revisions by 2 users not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/*****************************************************************************************
* This JavaScript file will be loaded for all users on every page load
* Its content must be kept as minimal as possible.
*****************************************************************************************/


/** Collapsible tables *********************************************************
mw.loader.using( ['mediawiki.util', 'jquery.client'], function() { /* BEGIN mw.loader WRAPPER */
*
*  Description: Allows tables to be collapsed, showing only the header. See
*              [[Wikipedia:NavFrame]].
*  Maintainers: [[User:R. Koot]] (copied from Wikipedia Common.js)
*/


/**
/*****************************************************************************************
* Redirect User:Name/skin.js and skin.css to the current skin's pages
* Conditionally import further JavaScript for specific situations.
* (unless the 'skin' page really exists)
*/
* @source: http://www.mediawiki.org/wiki/Snippets/Redirect_skin.js
* @rev: 2
*/


if ( mw.config.get( 'wgArticleId' ) === 0 && mw.config.get( 'wgNamespaceNumber' ) == 2 ) {
function conditionallyImport() {
var titleParts = mw.config.get( 'wgPageName' ).split( '/' );
if ( mw.config.get( 'wgAction' ) === 'edit' || mw.config.get( 'wgAction' ) === 'submit' ) {
// Make sure there was a part before and after the slash
importScript( 'MediaWiki:Editing.js' );
// And that the latter is 'skin.js' or 'skin.css'
// mw.loader.load( '/wiki/index.php?title=MediaWiki:Editing.js&action=raw&ctype=text/javascript' ); // proper thing to do - for future reference
if ( titleParts.length == 2 ) {
}
var userSkinPage = titleParts.shift() + '/' + mw.config.get( 'skin' );
if ( mw.config.get( 'wgCanonicalNamespace' ) != 'MediaWiki' && mw.config.get( 'wgArticleId' ) === 0 ) {
if ( titleParts.slice(-1) == 'skin.js' ) {
importScript( 'MediaWiki:Creation.js' );
window.location.href = mw.util.wikiGetlink( userSkinPage + '.js' );
}
} else if ( titleParts.slice(-1) == 'skin.css' ) {
if ( mw.config.get( 'wgPageName' ) === 'Special:Version' ) {
window.location.href = mw.util.wikiGetlink( userSkinPage + '.css' );
importScript( 'MediaWiki:SpecialVersion.js' );
}
}
}
}
}
mw.hook( 'wikipage.content' ).add( conditionallyImport );


/** Map addPortletLink to mw.util
/*****************************************************************************************
*/
* Add a Purge button
window.addPortletLink = function(){
* Maintainer: [[User:Lou Montana]]
return mw.util.addPortletLink.apply( mw.util, arguments );
*/
};


/** extract a URL parameter from the current URL **********
function offerPurgeButton() {
*
var protectButton = document.getElementById('ca-protect');
* @deprecated: Use mw.util.getParamValue with proper escaping
if (protectButton == null) {
*/
protectButton = document.getElementById('ca-unprotect');
function getURLParamValue(paramName, url) {
if (protectButton == null) {
return mw.util.getParamValue(paramName, url);
return; // quick'n'not that dirty
}
}
}
var newButton = protectButton.cloneNode(true),
link = newButton.getElementsByTagName('a')[0];
link.removeAttribute('accesskey');
link.setAttribute('title', 'Purge the page\'s cache');
link.setAttribute('href', document.location.href + (document.location.href.indexOf('?') === -1 ? '?action=purge' : '&action=purge'));
link.innerHTML = 'Purge';
protectButton.parentNode.appendChild(newButton);
}
mw.hook( 'wikipage.content' ).add( offerPurgeButton );


/** &withCSS= and &withJS= URL parameters *******
/*****************************************************************************************
* Allow to try custom scripts from MediaWiki space
* Add a Fix access button
* without editing personal .css or .js files
* Maintainer: [[User:Lou Montana]]
*/
*/
var extraCSS = mw.util.getParamValue("withCSS");
if ( extraCSS && extraCSS.match(/^MediaWiki:[^&<>=%]*\.css$/) ) {
    importStylesheet(extraCSS);
}
var extraJS = mw.util.getParamValue("withJS");
if ( extraJS && extraJS.match(/^MediaWiki:[^&<>=%]*\.js$/) ) {
    importScript(extraJS);
}


function offerDeleteSessionCookieButton() {
var protectButton = document.getElementById('ca-protect');
if (protectButton == null) {
protectButton = document.getElementById('ca-unprotect');
if (protectButton == null) {
return; // quick'n'not that dirty
}
}
var newButton = protectButton.cloneNode(true),
link = newButton.getElementsByTagName('a')[0];
link.removeAttribute('accesskey');
link.setAttribute('title', 'Delete the community_session cookie to fix the DbQueryError issue');
link.setAttribute('href', '/clearcookie.php?url=' + document.location.href);
link.innerHTML = 'Fix access';
protectButton.parentNode.appendChild(newButton);
}
// mw.hook( 'wikipage.content' ).add( offerDeleteSessionCookieButton ); // disabled for now, -should- be fixed


/* Import more specific scripts if necessary */
/*****************************************************************************************
if (wgAction == 'edit' || wgAction == 'submit' || wgPageName == 'Special:Upload') { //scripts specific to editing pages
* Disable Talk pages
    importScript('MediaWiki:Common.js/edit.js');
* Maintainer: [[User:Lou Montana]]
}
*/
else if (mw.config.get('wgPageName') == 'Special:Watchlist') { //watchlist scripts
    mw.loader.load(mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=MediaWiki:Common.js/watchlist.js&action=raw&ctype=text/javascript&smaxage=21600&maxage=86400');
}


if ( wgNamespaceNumber == 6 ) {
function disableTalkPages() {
    importScript('MediaWiki:Common.js/file.js');
if ($.inArray('sysop', mw.config.get('wgUserGroups')) === -1) { // not admin
}


/** For sysops and accountcreators *****************************************
if (mw.config.get('wgCanonicalNamespace').indexOf('talk') !== -1 && // talk page
*
mw.config.get('wgNamespaceNumber') !== 3) { // User Talk exception
*  Description: Allows for sysop-specific Javascript at [[MediaWiki:Sysop.js]],
*              and accountcreator-specific CSS at [[MediaWiki:Accountcreator.css]].
*/
if ( $.inArray( 'sysop', wgUserGroups) > -1 ) {
if ( !window.disableSysopJS ) {
  $(function(){
  importScript('MediaWiki:Sysop.js');
  });
}
}


if (document.getElementById('ca-edit') != null) {
document.getElementById('ca-edit').remove();
}


/** WikiMiniAtlas *******************************************************
if (document.getElementById('ca-addsection') != null) {
  *
document.getElementById('ca-addsection').remove();
  *  Description: WikiMiniAtlas is a popup click and drag world map.
}
  *              This script causes all of our coordinate links to display the WikiMiniAtlas popup button.
}
  *              The script itself is located on meta because it is used by many projects.
  *              See [[Meta:WikiMiniAtlas]] for more information.  
  *  Maintainers: [[User:Dschwen]]
  */


mw.loader.load('//meta.wikimedia.org/w/index.php?title=MediaWiki:Wikiminiatlas.js&action=raw&ctype=text/javascript&smaxage=21600&maxage=86400');
var talkTab = document.getElementById('ca-talk');
if (talkTab != null && talkTab.classList.contains('new')) {
talkTab.remove();
}
}
}
mw.hook( 'wikipage.content' ).add( disableTalkPages );


/* Scripts specific to Internet Explorer */
/*****************************************************************************************
if (navigator.appName == 'Microsoft Internet Explorer'){
* Function that adds a Sandbox button
    /** Internet Explorer bug fix **************************************************
* Maintainer: [[User:Lou Montana]]
    *
*/
    * Description: Fixes IE horizontal scrollbar bug
    * Maintainers: [[User:Tom-]]?
    */
   
    var oldWidth;
    var docEl = document.documentElement;
   
    var fixIEScroll = function() {
        if (!oldWidth || docEl.clientWidth > oldWidth) {
            doFixIEScroll();
        } else {
            setTimeout(doFixIEScroll, 1);
        }
       
        oldWidth = docEl.clientWidth;
    };
   
    var doFixIEScroll = function () {
        docEl.style.overflowX = (docEl.scrollWidth - docEl.clientWidth < 4) ? "hidden" : "";
    };
   
    document.attachEvent("onreadystatechange", fixIEScroll);
    document.attachEvent("onresize", fixIEScroll);
   
    // In print IE (7?) does not like line-height
    mw.util.addCSS( '@media print { sup, sub, p, .documentDescription { line-height: normal; }}');


    // IE overflow bug
function addSandboxButton() {
    mw.util.addCSS('div.overflowbugx { overflow-x: scroll !important; overflow-y: hidden !important; } div.overflowbugy { overflow-y: scroll !important; overflow-x: hidden !important; }');
var userpageLink = document.getElementById('pt-userpage');
var talkLink = document.getElementById('pt-mytalk');
if (talkLink == null) {
return;
}
var sandboxLink = talkLink.cloneNode(true),
link = sandboxLink.getElementsByTagName('a')[0];
link.removeAttribute('accesskey');
link.setAttribute('title', 'Your Sandbox');
link.setAttribute('href', userpageLink.getElementsByTagName('a')[0].href + '/Sandbox');
link.innerHTML = 'Sandbox';
talkLink.parentNode.insertBefore(sandboxLink, talkLink);
}
mw.hook( 'wikipage.content' ).add( addSandboxButton );


    // IE zoomfix
/*****************************************************************************************
    // Use to fix right floating div/table inside tables
* Functions that adds Tabs
    mw.util.addCSS('.iezoomfix div, .iezoomfix table { zoom: 1;}');
* Maintainer: [[User:Lou Montana]]
*/


    // .hlist fix for IE - remove dot after last list item
function tabSystem_selectTab(titles, contents, tabIndex) {
    if ( $.browser.version < '9.0' ) {
      $(function() {
        $( '.hlist' ).find( 'li:last-child' ).css( 'background', 'none' ).css( 'padding-right', '0' );
      });
    }
   
    // Import scripts specific to Internet Explorer 6
    if (navigator.appVersion.substr(22, 1) == '6') {
        importScript('MediaWiki:Common.js/IE60Fixes.js');
    }
}


/* Load fixes for Windows font rendering */
var i;
if( navigator.platform.indexOf( "Win" ) != -1 ) {
for (i = 0; i < titles.length; i++) {
    importStylesheet( 'MediaWiki:Common.css/WinFixes.css' );
titles[i].classList.remove('selected');
}
}
for (i = 0; i < contents.length; i++) {
contents[i].style.display = 'none';
}


/* Test if an element has a certain class
if (tabIndex < 0 || tabIndex > titles.length -1 || tabIndex > contents.length -1) {
* Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
return;
*
}
* @deprecated:  Use $(element).hasClass() instead.
titles[tabIndex].classList.add('selected');
*/
contents[tabIndex].style.display = 'block';
}


var hasClass = (function () {
function tabSystem_implementTabs() {
    var reCache = {};
    return function (element, className) {
        return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
    };
})();


var tabBlocks = document.getElementsByClassName('biki-tabs');


/** Interwiki links to featured articles ***************************************
for (var i = 0; i < tabBlocks.length; i++) {
*
*  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() {
var tabBlock = tabBlocks[i];
    if ( document.getElementById( "p-lang" ) ) {
        var InterwikiLinks = document.getElementById( "p-lang" ).getElementsByTagName( "li" );


        for ( var i = 0; i < InterwikiLinks.length; i++ ) {
var titles = tabBlock.getElementsByClassName('biki-tab-title');
            if ( document.getElementById( InterwikiLinks[i].className + "-fa" ) ) {
var contents = tabBlock.getElementsByClassName('biki-tab-content');
                InterwikiLinks[i].className += " FA";
if (titles.length < 1 || contents.length < 1 || titles.length != contents.length) {
                InterwikiLinks[i].title = "This is a featured article in another language.";
continue;
            } else if ( document.getElementById( InterwikiLinks[i].className + "-ga" ) ) {
}
                InterwikiLinks[i].className += " GA";
                InterwikiLinks[i].title = "This is a good article in another language.";
            }
        }
    }
}


$( LinkFA );
for (var j = 0; j < titles.length; j++) {
(function(titles, contents, index) {
titles[j].addEventListener('click', function () { tabSystem_selectTab(titles, contents, index) })
})(titles, contents, j);
}


var selectedTab = 0;
if (tabBlock.dataset && tabBlock.dataset.selectedtab) {
var dataSelectedTab = +tabBlock.dataset.selectedtab;
if (!isNaN(dataSelectedTab)) {
selectedTab = Math.max(0, Math.min(dataSelectedTab, titles.length -1));
}
}


/** Collapsible tables *********************************************************
tabSystem_selectTab(titles, contents, selectedTab);
*
}
*  Description: Allows tables to be collapsed, showing only the header. See
}
*              [[Wikipedia:NavFrame]].
mw.hook( 'wikipage.content' ).add( tabSystem_implementTabs );
*  Maintainers: [[User:R. Koot]]
*/


var autoCollapse = 2;
/*****************************************************************************************
var collapseCaption = "hide";
* Function that adds Dark/Default Mode button for non logged-in users
var expandCaption = "show";
* (logged-in users can select their skin in Parameters)
* Maintainer: [[User:Lou Montana]]
*/


function collapseTable( tableIndex ){
const SKIN_USAGE = 'useskin=';
    var Button = document.getElementById( "collapseButton" + tableIndex );
    var Table = document.getElementById( "collapsibleTable" + tableIndex );


    if ( !Table || !Button ) {
function addDarkOrDefaultModeButton() {
        return false;
const DARKMODE_SKIN = 'darkvector';
    }
const DARKMODE_SKIN_USAGE = SKIN_USAGE + DARKMODE_SKIN;


    var Rows = Table.rows;
var leftTabHolder = document.getElementById('p-namespaces').getElementsByTagName('ul')[0]; // <ul>
if (leftTabHolder == null)
return;


    if ( Button.firstChild.data == collapseCaption ) {
var firstButton = leftTabHolder .children[0]; // <li>
        for ( var i = 1; i < Rows.length; i++ ) {
if (firstButton == null)
            Rows[i].style.display = "none";
return; // quick'n'not that dirty
        }
        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 buttonName;
    var tableIndex = 0;
var title;
    var NavigationBoxes = new Object();
var href = document.documentURI;
    var Tables = document.getElementsByTagName( "table" );
if (href.indexOf(DARKMODE_SKIN_USAGE) !== -1)
{
buttonName = 'Default Mode';
title = 'Go back to the default skin';
href = href.replace(/[?&]useskin=[a-zA-Z0-9_]+/, '');
}
else // not in URL
{
buttonName = 'Dark Mode';
title = 'Use Dark Mode';
if (href.includes('?'))
href += '&' + DARKMODE_SKIN_USAGE;
else
href += '?' + DARKMODE_SKIN_USAGE;
}


    for ( var i = 0; i < Tables.length; i++ ) {
var newButton = firstButton.cloneNode(true),
        if ( hasClass( Tables[i], "collapsible" ) ) {
link = newButton.getElementsByTagName('a')[0];


            /* only add button and increment count if there is a header row to work with */
newButton.removeAttribute('class');
            var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0];
link.removeAttribute('accesskey');
            if (!HeaderRow) continue;
link.removeAttribute('rel');
            var Header = HeaderRow.getElementsByTagName( "th" )[0];
link.setAttribute('title', title);
            if (!Header) continue;
link.setAttribute('href', href);
link.innerHTML = buttonName;
leftTabHolder.appendChild(newButton);
}


            NavigationBoxes[ tableIndex ] = Tables[i];
/*****************************************************************************************
            Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
* Function that adds the currently-used 'useskin' URL parameter to all relevant links in the page
* Maintainer: [[User:Lou Montana]]
*/


            var Button    = document.createElement( "span" );
function setAllLinksToUseForcedSkin() {
            var ButtonLink = document.createElement( "a" );
var usedSkin = mw.util.getParamValue('useskin');
            var ButtonText = document.createTextNode( collapseCaption );
if (usedSkin == null || usedSkin == '')
return;


            Button.className = "collapseButton"; //Styles are declared in Common.css
var allLinks = document.getElementsByTagName('a');
var link;
var href;
for (var i = 0, count = allLinks.length - 1; i < count; i++)
{
link = allLinks[i];
href = link.href;
if (href == null || href == '')
continue;


            ButtonLink.style.color = Header.style.color;
// only httpS wiki links, no javascript:, no #, no enfusion:// or other protocol
            ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
if (!(href.startsWith('https://community.bistudio.com/') || href.startsWith('https://community.bohemia.net/') || href.startsWith('/wiki/')))
            ButtonLink.setAttribute( "href", "#" );
continue;
            addHandler( ButtonLink,  "click", new Function( "evt", "collapseTable(" + tableIndex + " ); return killEvt( evt );") );
            ButtonLink.appendChild( ButtonText );


            Button.appendChild( document.createTextNode( "[" ) );
if (href.indexOf(SKIN_USAGE) !== -1)
            Button.appendChild( ButtonLink );
continue; // already has a skin assigned
            Button.appendChild( document.createTextNode( "]" ) );


            Header.insertBefore( Button, Header.childNodes[0] );
var poundIndex = href.indexOf('#');
            tableIndex++;
if (href.indexOf('?') !== -1)
        }
{
    }
if (poundIndex !== -1)
href = href.replace('#', '&' + SKIN_USAGE + usedSkin + '#');
else
href += '&' + SKIN_USAGE + usedSkin;
}
else // no ?x=y parameters
{
if (poundIndex !== -1)
href = href.replace('#', '?' + SKIN_USAGE + usedSkin + '#');
else
href += '?' + SKIN_USAGE + usedSkin;
}


    for ( var i = 0;  i < tableIndex; i++ ) {
link.href = href;
        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;
                }
            }
        }
    }
}


$( createCollapseButtons );
// if 'useskin' is used, apply it to all links
if (mw.util.getParamValue('useskin'))
mw.hook( 'wikipage.content' ).add( setAllLinksToUseForcedSkin );


// no ES5 promise, use jQuery
$.getJSON('/wikidata/api.php?action=query&meta=userinfo&format=json').done(function(response) {
if (response != null && response.query.userinfo.id === 0) { // not logged in
mw.hook( 'wikipage.content' ).add( addDarkOrDefaultModeButton );
// hopefully -after- setAllLinksToUseForcedSkin so the button's URL is not changed by it
}
});


/** Dynamic Navigation Bars (experimental) *************************************
}); /* END mw.loader WRAPPER */
*
*  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")) {
                    NavToggle.style.color = NavFrame.childNodes[j].style.color;
                    NavFrame.childNodes[j].appendChild(NavToggle);
                }
            }
            NavFrame.setAttribute('id', 'NavFrame' + indexNavigationBar);
        }
    }
}
 
$( createNavigationBarToggleButton );
 
 
/** Main Page layout fixes *********************************************************
*
*  Description: Adds an additional link to the complete list of languages available.
*  Maintainers: [[User:AzaToth]], [[User:R. Koot]], [[User:Alex Smotrov]]
*/
 
if (wgPageName == 'Main_Page' || wgPageName == 'Talk:Main_Page') {
    $(function () {
        mw.util.addPortletLink('p-lang', '//meta.wikimedia.org/wiki/List_of_Wikipedias',
            'Complete list', 'interwiki-completelist', 'Complete list of Wikipedias');
    });
}
 
 
/** Table sorting fixes ************************************************
  *
  *  Description: Disables code in table sorting routine to set classes on even/odd rows
  *  Maintainers: [[User:Random832]]
  */
ts_alternate_row_colors = false;
 
 
/***** uploadwizard_newusers ********
* Switches in a message for non-autoconfirmed users at [[Wikipedia:Upload]]
*
*  Maintainers: [[User:Krimpet]]
*/
function uploadwizard_newusers() {
  if (wgNamespaceNumber == 4 && wgTitle == "Upload" && wgAction == "view") {
    var oldDiv = document.getElementById("autoconfirmedusers"),
        newDiv = document.getElementById("newusers");
    if (oldDiv && newDiv) {
      if (typeof wgUserGroups == "object" && wgUserGroups) {
        for (i = 0; i < wgUserGroups.length; i++) {
          if (wgUserGroups[i] == "autoconfirmed") {
            oldDiv.style.display = "block";
            newDiv.style.display = "none";
            return;
          }
        }
      }
      oldDiv.style.display = "none";
      newDiv.style.display = "block";
      return;
    }
  }
}
$(uploadwizard_newusers);
 
 
/** IPv6 AAAA connectivity testing
 
var __ipv6wwwtest_factor = 100;
var __ipv6wwwtest_done = 0;
if ((wgServer != "https://secure.wikimedia.org") && (Math.floor(Math.random()*__ipv6wwwtest_factor)==42)) {
    importScript("MediaWiki:Common.js/IPv6.js");
}
**/
 
/** Magic editintros ****************************************************
*
*  Description: Adds editintros on disambiguation pages and BLP pages.
*  Maintainers: [[User:RockMFR]]
*/
 
function addEditIntro( name ) {
  $( '.editsection, #ca-edit' ).find( 'a' ).each( function( i, el ) {
    el.href = $(this).attr("href") + '&editintro=' + name;
  });
}
 
if (wgNamespaceNumber === 0) {
  $(function(){
    if (document.getElementById('disambigbox')) {
      addEditIntro('Template:Disambig_editintro');
    }
  });
 
  $(function(){
    var cats = document.getElementById('mw-normal-catlinks');
    if (!cats) {
      return;
    }
    cats = cats.getElementsByTagName('a');
    for (var i = 0; i < cats.length; i++) {
      if (cats[i].title == 'Category:Living people' || cats[i].title == 'Category:Possibly living people') {
        addEditIntro('Template:BLP_editintro');
        break;
      }
    }
  });
}
 
/**
* Description: Stay on the secure server as much as possible
* Maintainers: [[User:TheDJ]]
*/
if ( mw.config.get('wgServer') == 'https://secure.wikimedia.org' ) {
    /* Old secure server */
    importScript( 'MediaWiki:Common.js/secure.js');
} else if( document.location && document.location.protocol  && document.location.protocol == "https:" ) {
  /* New secure servers */
  importScript('MediaWiki:Common.js/secure new.js');
}
 
/** Text area function for the account creation process */
jQuery(function(){
  if (!(document.getElementById('signupuserpagefillmagic'))) return;
  /*
  * Puts an userpage edit-box inside a div with the ID 'signupuserpagefillmagic'
  * Created for [[:outreach:Account Creation Improvement Project]] by
  * [[:sv:User:Sertion]] on the behalf of [[:outreach:User:Hannibal]]
  *
  * Below are variables for internationalization. Please use \n for linebreaks
  * and escape all single quotation marks (') with \'
  */
  var preComment = '<!-- BELOW IS THE TEXT ABOUT YOU. YOU CAN CHANGE IT COMPLETELY OR IN PARTS AND THEN COME BACK TO IT. AFTER YOU ARE DONE, SCROLL DOWN A BIT FURTHER AND CLICK "SAVE PAGE".--\>{'+'{New user bar}}\n',
      postComment = '\n\n<!-- NOW, CLICK THE "SAVE PAGE" BUTTON. CONGRATULATIONS, YOU\'VE JUST MADE YOUR FIRST EDIT TO WIKIPEDIA. --\>',
      preSubmitButton ='Do not forget to click SAVE PAGE when you get to the next page!',
      submitText = 'Create my user page for me now!',
      SUPeditSummary = 'New user page through [[:outreach:Account Creation Improvement Project|Outreach:ACIP]]',
      preFilltemplate = 'Replace this example text with information about you. \n\n';
  /*
    * The actual magic:
    * Inserts a form with a single visible field that simulates the normal
    * edit-field. It uses the variables from above to set a text example (pre
    * filled), an automated edit summary and the label of the submit button.
    * 'fakewpTextbox1' is used to hide the assembling of the final output that
    * is made below.
    */
  jQuery('#signupuserpagefillmagic').html('<form action="'+wgServer+wgScript+'?title='+wgFormattedNamespaces[2]+':'+wgUserName+'&action=submit" method="post"><textarea id="fakewpTextbox1" style="width:46em;height:20em;">'+preFilltemplate+'</textarea><textarea name="wpTextbox1" id="wpTextbox1" style="display:none;"></textarea><input type="hidden" name="wpSummary" id="wpSummary" value="'+SUPeditSummary+'" /><br/>'+preSubmitButton+'<br/><input type="submit" value="'+submitText+'"/></form>');
  // Waits for the form to be submitted.
  jQuery('#signupuserpagefillmagic form').live('submit',function(r){
    // Stops the form from submitting
    r.preventDefault();
    /*
      * Uses the previously defined variables preComment and postComment
      * to assemble the final output in the hidden textarea.
      */
    $('#wpTextbox1').text( preComment + jQuery('#fakewpTextbox1').text() + postComment );
    /*
      * Submits the form.
      * For unknown reasons jQuery('#signupuserpagefillmagic form').submit() crashes
      * Firefox (only tested in version 4.0). This method seam to work cross browser.
      */
    document.getElementById('signupuserpagefillmagic').getElementsByTagName('form')[0].submit();
  });
});

Latest revision as of 23:46, 10 March 2024

/*****************************************************************************************
 * This JavaScript file will be loaded for all users on every page load
 * Its content must be kept as minimal as possible.
 *****************************************************************************************/

mw.loader.using( ['mediawiki.util', 'jquery.client'], function() { /* BEGIN mw.loader WRAPPER */

	/*****************************************************************************************
	 * Conditionally import further JavaScript for specific situations.
	 */

	function conditionallyImport() {
		if ( mw.config.get( 'wgAction' ) === 'edit' || mw.config.get( 'wgAction' ) === 'submit' ) {
			importScript( 'MediaWiki:Editing.js' );
			// mw.loader.load( '/wiki/index.php?title=MediaWiki:Editing.js&action=raw&ctype=text/javascript' ); // proper thing to do - for future reference
		}
		if ( mw.config.get( 'wgCanonicalNamespace' ) != 'MediaWiki' && mw.config.get( 'wgArticleId' ) === 0 ) {
			importScript( 'MediaWiki:Creation.js' );
		}
		if ( mw.config.get( 'wgPageName' ) === 'Special:Version' ) {
			importScript( 'MediaWiki:SpecialVersion.js' );
		}
	}
	mw.hook( 'wikipage.content' ).add( conditionallyImport );

	/*****************************************************************************************
	 * Add a Purge button
	 * Maintainer: [[User:Lou Montana]]
	 */

	function offerPurgeButton() {
		var protectButton = document.getElementById('ca-protect');
		if (protectButton == null) {
			protectButton = document.getElementById('ca-unprotect');
			if (protectButton == null) {
				return; // quick'n'not that dirty
			}
		}
		var newButton = protectButton.cloneNode(true),
			link = newButton.getElementsByTagName('a')[0];
		link.removeAttribute('accesskey');
		link.setAttribute('title', 'Purge the page\'s cache');
		link.setAttribute('href', document.location.href + (document.location.href.indexOf('?') === -1 ? '?action=purge' : '&action=purge'));
		link.innerHTML = 'Purge';
		protectButton.parentNode.appendChild(newButton);
	}
	mw.hook( 'wikipage.content' ).add( offerPurgeButton );

	/*****************************************************************************************
	 * Add a Fix access button
	 * Maintainer: [[User:Lou Montana]]
	 */

	function offerDeleteSessionCookieButton() {
		var protectButton = document.getElementById('ca-protect');
		if (protectButton == null) {
			protectButton = document.getElementById('ca-unprotect');
			if (protectButton == null) {
				return; // quick'n'not that dirty
			}
		}
		var newButton = protectButton.cloneNode(true),
			link = newButton.getElementsByTagName('a')[0];
		link.removeAttribute('accesskey');
		link.setAttribute('title', 'Delete the community_session cookie to fix the DbQueryError issue');
		link.setAttribute('href', '/clearcookie.php?url=' + document.location.href);
		link.innerHTML = 'Fix access';
		protectButton.parentNode.appendChild(newButton);
	}
	// mw.hook( 'wikipage.content' ).add( offerDeleteSessionCookieButton ); // disabled for now, -should- be fixed

	/*****************************************************************************************
	 * Disable Talk pages
	 * Maintainer: [[User:Lou Montana]]
	 */

	function disableTalkPages() {
		if ($.inArray('sysop', mw.config.get('wgUserGroups')) === -1) { // not admin

			if (mw.config.get('wgCanonicalNamespace').indexOf('talk') !== -1 && // talk page
				mw.config.get('wgNamespaceNumber') !== 3) { // User Talk exception

				if (document.getElementById('ca-edit') != null) {
					document.getElementById('ca-edit').remove();
				}

				if (document.getElementById('ca-addsection') != null) {
					document.getElementById('ca-addsection').remove();
				}
			}

			var talkTab = document.getElementById('ca-talk');
			if (talkTab != null && talkTab.classList.contains('new')) {
				talkTab.remove();
			}
		}
	}
	mw.hook( 'wikipage.content' ).add( disableTalkPages );

	/*****************************************************************************************
	 * Function that adds a Sandbox button
	 * Maintainer: [[User:Lou Montana]]
	 */

	function addSandboxButton() {
		var userpageLink = document.getElementById('pt-userpage');
		var talkLink = document.getElementById('pt-mytalk');
		if (talkLink == null) {
			return;
		}
		var sandboxLink = talkLink.cloneNode(true),
			link = sandboxLink.getElementsByTagName('a')[0];
		link.removeAttribute('accesskey');
		link.setAttribute('title', 'Your Sandbox');
		link.setAttribute('href', userpageLink.getElementsByTagName('a')[0].href + '/Sandbox');
		link.innerHTML = 'Sandbox';
		talkLink.parentNode.insertBefore(sandboxLink, talkLink);
	}
	mw.hook( 'wikipage.content' ).add( addSandboxButton );

	/*****************************************************************************************
	 * Functions that adds Tabs
	 * Maintainer: [[User:Lou Montana]]
	 */

	function tabSystem_selectTab(titles, contents, tabIndex) {

		var i;
		for (i = 0; i < titles.length; i++) {
			titles[i].classList.remove('selected');
		}
		for (i = 0; i < contents.length; i++) {
			contents[i].style.display = 'none';
		}

		if (tabIndex < 0 || tabIndex > titles.length -1 || tabIndex > contents.length -1) {
			return;
		}
		titles[tabIndex].classList.add('selected');
		contents[tabIndex].style.display = 'block';
	}

	function tabSystem_implementTabs() {

		var tabBlocks = document.getElementsByClassName('biki-tabs');

		for (var i = 0; i < tabBlocks.length; i++) {

			var tabBlock = tabBlocks[i];

			var titles = tabBlock.getElementsByClassName('biki-tab-title');
			var contents = tabBlock.getElementsByClassName('biki-tab-content');
			if (titles.length < 1 || contents.length < 1 || titles.length != contents.length) {
				continue;
			}

			for (var j = 0; j < titles.length; j++) {
				(function(titles, contents, index) {
					titles[j].addEventListener('click', function () { tabSystem_selectTab(titles, contents, index) })
				})(titles, contents, j);
			}

			var selectedTab = 0;
			if (tabBlock.dataset && tabBlock.dataset.selectedtab) {
				var dataSelectedTab = +tabBlock.dataset.selectedtab;
				if (!isNaN(dataSelectedTab)) {
					selectedTab = Math.max(0, Math.min(dataSelectedTab, titles.length -1));
				}
			}

			tabSystem_selectTab(titles, contents, selectedTab);
		}
	}
	mw.hook( 'wikipage.content' ).add( tabSystem_implementTabs );

	/*****************************************************************************************
	 * Function that adds Dark/Default Mode button for non logged-in users
	 * (logged-in users can select their skin in Parameters)
	 * Maintainer: [[User:Lou Montana]]
	 */

	const SKIN_USAGE = 'useskin=';

	function addDarkOrDefaultModeButton() {
		const DARKMODE_SKIN = 'darkvector';
		const DARKMODE_SKIN_USAGE = SKIN_USAGE + DARKMODE_SKIN;

		var leftTabHolder = document.getElementById('p-namespaces').getElementsByTagName('ul')[0]; // <ul>
		if (leftTabHolder == null)
			return;

		var firstButton = leftTabHolder .children[0]; // <li>
		if (firstButton == null)
			return; // quick'n'not that dirty

		var buttonName;
		var title;
		var href = document.documentURI;
		if (href.indexOf(DARKMODE_SKIN_USAGE) !== -1)
		{
			buttonName = 'Default Mode';
			title = 'Go back to the default skin';
			href = href.replace(/[?&]useskin=[a-zA-Z0-9_]+/, '');
		}
		else // not in URL
		{
			buttonName = 'Dark Mode';
			title = 'Use Dark Mode';
			if (href.includes('?'))
				href += '&' + DARKMODE_SKIN_USAGE;
			else
				href += '?' + DARKMODE_SKIN_USAGE;
		}

		var newButton = firstButton.cloneNode(true),
			link = newButton.getElementsByTagName('a')[0];

		newButton.removeAttribute('class');
		link.removeAttribute('accesskey');
		link.removeAttribute('rel');
		link.setAttribute('title', title);
		link.setAttribute('href', href);
		link.innerHTML = buttonName;
		leftTabHolder.appendChild(newButton);
	}

	/*****************************************************************************************
	 * Function that adds the currently-used 'useskin' URL parameter to all relevant links in the page
	 * Maintainer: [[User:Lou Montana]]
	 */

	function setAllLinksToUseForcedSkin() {
		var usedSkin = mw.util.getParamValue('useskin');
		if (usedSkin == null || usedSkin == '')
			return;

		var allLinks = document.getElementsByTagName('a');
		var link;
		var href;
		for (var i = 0, count = allLinks.length - 1; i < count; i++)
		{
			link = allLinks[i];
			href = link.href;
			if (href == null || href == '')
				continue;

			// only httpS wiki links, no javascript:, no #, no enfusion:// or other protocol
			if (!(href.startsWith('https://community.bistudio.com/') || href.startsWith('https://community.bohemia.net/') || href.startsWith('/wiki/')))
				continue;

			if (href.indexOf(SKIN_USAGE) !== -1)
				continue; // already has a skin assigned

			var poundIndex = href.indexOf('#');
			if (href.indexOf('?') !== -1)
			{
				if (poundIndex !== -1)
					href = href.replace('#', '&' + SKIN_USAGE + usedSkin + '#');
				else
					href += '&' + SKIN_USAGE + usedSkin;
			}
			else // no ?x=y parameters
			{
				if (poundIndex !== -1)
					href = href.replace('#', '?' + SKIN_USAGE + usedSkin + '#');
				else
					href += '?' + SKIN_USAGE + usedSkin;
			}

			link.href = href;
		}
	}

	// if 'useskin' is used, apply it to all links
	if (mw.util.getParamValue('useskin'))
		mw.hook( 'wikipage.content' ).add( setAllLinksToUseForcedSkin );

	// no ES5 promise, use jQuery
	$.getJSON('/wikidata/api.php?action=query&meta=userinfo&format=json').done(function(response) {
		if (response != null && response.query.userinfo.id === 0) {	// not logged in
			mw.hook( 'wikipage.content' ).add( addDarkOrDefaultModeButton );
			// hopefully -after- setAllLinksToUseForcedSkin so the button's URL is not changed by it
		}
	});

}); /* END mw.loader WRAPPER */