MediaWiki:Common.js: Difference between revisions

Till Kraemer (talk | contribs)
No edit summary
Till Kraemer (talk | contribs)
Test.
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/**
* Keep code in MediaWiki:Common.js to a minimum as it is unconditionally
* loaded for all users on every wiki page. If possible create a gadget that is
* enabled by default instead of adding it here (since gadgets are fully
* optimized ResourceLoader modules with possibility to add dependencies etc.)
*
* Since Common.js isn't a gadget, there is no place to declare its
* dependencies, so we have to lazy load them with mw.loader.using on demand and
* then execute the rest in the callback. In most cases these dependencies will
* be loaded (or loading) already and the callback will not be delayed. In case a
* dependency hasn't arrived yet it'll make sure those are loaded before this.
*/
 
/* global mw, $ */
/* jshint strict:false, browser:true */
 
mw.loader.using( ['mediawiki.user', 'mediawiki.util', 'mediawiki.notify', 'jquery.client'] ).done( function () {
/* Begin of mw.loader.using callback */


if ( /^MediaWiki(\/.+)?$/.test( wgPageName ) && wgAction == "view" ) {
/**
     addOnloadHook(function(){
* Main Page layout fixes
         document.body.className+=" mainpage";
*
        document.write('<style type="text/css">/*<![CDATA[*/ #lastmod, #siteSub, h1.firstHeading { display: none !important; } #content { padding-top: 1em; }/*]]>*/</style>'); /*REMOVE THIS LINE AFTER 22/01/2009 */
* Description: Adds an additional link to the complete list of languages available.
     })
* Maintainers: [[User:AzaToth]], [[User:R. Koot]], [[User:Alex Smotrov]]
*/
if ( mw.config.get( 'wgPageName' ) === 'Main_Page' || mw.config.get( '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' );
     } );
}
}


/* Force preview for anons */
/**
/* by Marc Mongenet, 2006, fr.wikipedia */
* Redirect User:Name/skin.js and skin.css to the current skin's pages
 
* (unless the 'skin' page really exists)
function forcePreview() {
* @source: http://www.mediawiki.org/wiki/Snippets/Redirect_skin.js
  if (wgUserName != null || wgAction != "edit") return;
* @rev: 2
  saveButton = document.getElementById("wpSave");
*/
  if (!saveButton) return;
if ( mw.config.get( 'wgArticleId' ) === 0 && mw.config.get( 'wgNamespaceNumber' ) === 2 ) {
  saveButton.disabled = true;
    var titleParts = mw.config.get( 'wgPageName' ).split( '/' );
  saveButton.value = "Save page (use preview first)";
    /* Make sure there was a part before and after the slash
  saveButton.style.fontWeight = "normal";
      and that the latter is 'skin.js' or 'skin.css' */
  document.getElementById("wpPreview").style.fontWeight = "bold";
    if ( titleParts.length == 2 ) {
        var userSkinPage = titleParts.shift() + '/' + mw.config.get( 'skin' );
        if ( titleParts.slice( -1 ) == 'skin.js' ) {
            window.location.href = mw.util.getUrl( userSkinPage + '.js' );
        } else if ( titleParts.slice( -1 ) == 'skin.css' ) {
            window.location.href = mw.util.getUrl( userSkinPage + '.css' );
        }
    }
}
}
addOnloadHook(forcePreview);


/* End of forcePreview */
/**
* Map addPortletLink to mw.util
* @deprecated: Use mw.util.addPortletLink instead.
*/
mw.log.deprecate( window, 'addPortletLink', mw.util.addPortletLink, 'Use mw.util.addPortletLink instead' );


/** includePage ************
/**
  * force the loading of another JavaScript file
  * Extract a URL parameter from the current URL
*
  * @deprecated: Use mw.util.getParamValue with proper escaping
  * Maintainer: [[Commons:User:Dschwen]]
  */
  */
mw.log.deprecate( window, 'getURLParamValue', mw.util.getParamValue, 'Use mw.util.getParamValue instead' );
function includePage( name )
{
document.write('<script type="text/javascript" src="/w/index.php?title='
  + name
  + '&action=raw&ctype=text/javascript"><\/script>'  
);
}
/* End of includePage */


/* Including extra .js pages */  
/**
* Test if an element has a certain class
* @deprecated:  Use $(element).hasClass() instead.
*/
mw.log.deprecate( window, 'hasClass', function ( element, className ) {
    return $( element ).hasClass( className );
}, 'Use jQuery.hasClass() instead' );


// switches for scripts
/**
// TODO: migrate to JSConfig
* @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
// var load_extratabs = true;
* @rev 6
var load_edittools = true;
*/
var extraCSS = mw.util.getParamValue( 'withCSS' ),
    extraJS = mw.util.getParamValue( 'withJS' );


// extra drop down menu on editing for adding special characters
if ( extraCSS ) {
includePage( 'MediaWiki:Edittools.js' );
    if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
        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' } );
    }
}


//Editpage scripts
if ( extraJS ) {
if (wgAction=='edit' || wgAction == 'submit')
    if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
  importScript('MediaWiki:Editpage.js')
        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' } );
    }
}


/* End of extra pages */
/**
* WikiMiniAtlas
*
* 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]]
*/
( function () {
    var require_wikiminiatlas = false;
    var coord_filter = /geohack/;
    $( function () {
        $( 'a.external.text' ).each( function( key, link ) {
            if ( link.href && coord_filter.exec( link.href ) ) {
                require_wikiminiatlas = true;
                // break from loop
                return false;
            }
        } );
        if ( $( 'div.kmldata' ).length ) {
            require_wikiminiatlas = true;
        }
        if ( require_wikiminiatlas ) {
            mw.loader.load( '//meta.wikimedia.org/w/index.php?title=MediaWiki:Wikiminiatlas.js&action=raw&ctype=text/javascript' );
        }
    } );
} )();


/**
/**
Line 291: Line 359:


mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton );
mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton );
/**
* Uploadwizard_newusers
* Switches in a message for non-autoconfirmed users at [[Wikipedia:Upload]]
*
* Maintainers: [[User:Krimpet]]
*/
function uploadwizard_newusers() {
    if ( mw.config.get( 'wgNamespaceNumber' ) === 4 && mw.config.get( 'wgTitle' ) === 'Upload' && mw.config.get( 'wgAction' ) === 'view' ) {
        var oldDiv = document.getElementById( 'autoconfirmedusers' ),
            newDiv = document.getElementById( 'newusers' );
        if ( oldDiv && newDiv ) {
            var userGroups = mw.config.get( 'wgUserGroups' );
            if ( userGroups ) {
                for ( var i = 0; i < userGroups.length; i++ ) {
                    if ( userGroups[i] === 'autoconfirmed' ) {
                        oldDiv.style.display = 'block';
                        newDiv.style.display = 'none';
                        return;
                    }
                }
            }
            oldDiv.style.display = 'none';
            newDiv.style.display = 'block';
            return;
        }
    }
}
$(uploadwizard_newusers);
/**
* Magic editintros ****************************************************
*
* Description: Adds editintros on disambiguation pages and BLP pages.
* Maintainers: [[User:RockMFR]]
*/
function addEditIntro( name ) {
    $( '.mw-editsection, #ca-edit' ).find( 'a' ).each( function ( i, el ) {
        el.href = $( this ).attr( 'href' ) + '&editintro=' + name;
    } );
}
if ( mw.config.get( 'wgNamespaceNumber' ) === 0 ) {
    $( function () {
        if ( document.getElementById( 'disambigbox' ) ) {
            addEditIntro( 'Template:Disambig_editintro' );
        }
    } );
    $( function () {
        var cats = mw.config.get('wgCategories');
        if ( !cats ) {
            return;
        }
        if ( $.inArray( 'Living people', cats ) !== -1 || $.inArray( 'Possibly living people', cats ) !== -1 ) {
            addEditIntro( 'Template:BLP_editintro' );
        }
    } );
}
/* End of mw.loader.using callback */
} );
/* DO NOT ADD CODE BELOW THIS LINE */