MediaWiki:Common.js

From Path of Exile Wiki
Revision as of 22:06, 24 September 2016 by >TheFrz (exception fix and clean up)
Jump to navigation Jump to search

Note: After saving, you have to bypass your browser's cache to see the changes. The simplest method that will work for most users is to hold down the ⇧ Shift key and click the Reload toolbar button. For details and other methods, see Help:Clear your browser cache.

/* 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 */


    /***
     * Redirect User:Name/skin.js and skin.css to the current skin's pages
     * (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 ) {
        var titleParts = mw.config.get( 'wgPageName' ).split( '/' );
        /* Make sure there was a part before and after the slash
         and that the latter is 'skin.js' or 'skin.css' */
        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' );
            }
        }
    }

    /**
     * @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
     * @rev 6
     */
    var extraCSS = mw.util.getParamValue( 'withCSS' ),
        extraJS = mw.util.getParamValue( 'withJS' );

    if ( extraCSS ) {
        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' } );
        }
    }

    if ( extraJS ) {
        if (extraJS.match(/^MediaWiki:[^&<>=%#]*\.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'});
        }
    }

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

    /* Hover Item Box */
    $(function(){"use strict";$(".itemhover").hover(function(){var e=$(this),t=e.next(".itemboxhover"),n=t.next(".itemboxhovericon"),r=$(window).width(),i=$(window).height(),s=$(window).scrollTop(),o=$(window).scrollLeft(),u=e.offset(),a=e.outerWidth(),f=e.outerHeight(),l=t.outerWidth(),c=t.outerHeight(),h=12,p={},d=n.outerWidth(),v=n.outerHeight(),m={},g;if(u.top-h-c-s>0){g="t";p.top=u.top-c-h;p.left=u.left+a/2-l/2}else if(r+o-u.left-a-h-l-d>0){g="r";p.top=u.top+f/2-c/2;p.left=u.left+a+h}else if(u.left-o-h-l-d>0){g="l";p.top=u.top+f/2-c/2;p.left=u.left-l-h}else{g="b";p.top=u.top+f+h;p.left=u.left+a/2-l/2}if((g==="t"||g==="b")&&p.left<o){p.left=o}else if((g==="t"||g==="b")&&r+o-l<p.left){p.left=r+o-l}else if((g==="r"||g==="l")&&p.top<s){p.top=s}m.top=p.top;m.left=p.left+l;if(g==="l"||r+o-d<m.left){m.left=p.left-d}if(i+s-v<m.top){m.top=i+s-v}t.offset({top:p.top,left:p.left}).removeClass("itemboxhoverhide");n.offset({top:m.top,left:m.left}).removeClass("itemboxhoverhide")},function(){var e=$(this),t=e.next(".itemboxhover"),n=t.next(".itemboxhovericon");t.addClass("itemboxhoverhide");n.addClass("itemboxhoverhide")}).children("a").removeAttr("title").end().nextAll(".itemboxhover, .itemboxhovericon").removeClass("itemboxhovernojs")});

    /* For adding expand/collapse all buttons for mw-collapsible */
    $(".mw-collapsible-collapse-all").on("click", function () {
        $('.mw-collapsible-toggle-expanded a').trigger('click')
    });

    $(".mw-collapsible-expand-all").on("click", function () {
        $('.mw-collapsible-toggle-collapsed a').trigger('click')
    });


    /* End of mw.loader.using callback */
});
/* DO NOT ADD CODE BELOW THIS LINE */