Jump to content

MediaWiki:Common.js: Difference between revisions

From Kanzenshuu Dragon Ball Wiki
Hujio (talk | contribs)
No edit summary
Hujio (talk | contribs)
No edit summary
Line 1: Line 1:
/*global mw, $, importScript, jsMsg */
// Add "mainpage" class to the body element
if (
mw.config.get( 'wgMainPageTitle' ) === mw.config.get( 'wgPageName' ) &&
mw.config.get( 'wgAction' ) === 'view'
) {
$( document ).ready( function() {
document.body.className += ' mainpage';
});
}
// Editpage scripts
if ( mw.config.get( 'wgAction' ) == 'edit' || mw.config.get( 'wgAction' ) == 'submit' ) {
importScript( 'MediaWiki:Editpage.js' );
}
/* End of extra pages */
/**
/**
  * Test if an element has a certain class
  * Collapsible tables *********************************************************
*
* Based on script from en.wikipedia.org, 2008-09-15.
* Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
  *
  *
  * @deprecated since MediaWiki 1.17: Use jQuery(element).hasClass instead
  * Description: Allows tables to be collapsed, showing only the header. See
*              [[Wikipedia:NavFrame]].
* Maintainers: [[User:R. Koot]]
  */
  */
var hasClass = ( function () {
 
var reCache = {};
var autoCollapse = 2;
return function ( element, className ) {
var collapseCaption = 'hide';
return ( reCache[className] ? reCache[className] : ( reCache[className] = new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ) ) ).test( element.className );
var expandCaption = 'show';
};
 
})();
window.collapseTable = function ( tableIndex ) {
    var Button = document.getElementById( 'collapseButton' + tableIndex );
/**
    var Table = document.getElementById( 'collapsibleTable' + tableIndex );
* Hide prefix in category
 
*
    if ( !Table || !Button ) {
* @source: www.mediawiki.org/wiki/Snippets/Hide_prefix_in_category
        return false;
* @rev: 3
    }
* @author Krinkle
 
*/
    var Rows = Table.rows;
mw.loader.using( 'jquery.mwExtension', function () {
    var i;
var $tplHideCategoryPrefix = $( '#mw-cat-hideprefix' );
 
if ( $tplHideCategoryPrefix.length ) {
    if ( Button.firstChild.data === collapseCaption ) {
var prefix = $tplHideCategoryPrefix.text();
        for ( i = 1; i < Rows.length; i++ ) {
if ( $.trim( prefix ) === '' ) {
            Rows[i].style.display = 'none';
prefix = mw.config.get( 'wgTitle' ) + '/';
        }
}
        Button.firstChild.data = expandCaption;
$( '#mw-pages' ).find( 'a' ).text( function( i, val ){
    } else {
return val.replace( new RegExp( '^' + $.escapeRE( prefix ) ), '' );
        for ( i = 1; i < Rows.length; i++ ) {
});
            Rows[i].style.display = Rows[0].style.display;
}
        }
});
        Button.firstChild.data = collapseCaption;
    }
/**
};
* Load the CodeReview 'tooltips' gadget on [[MediaWiki roadmap]] subpages,
 
* so it's available for the revision reports where it's particularly useful
function createCollapseButtons() {
* @author: Happy-melon
    var tableIndex = 0;
*/
    var NavigationBoxes = {};
if ( mw.config.get( 'wgPageName' ).match( /^MediaWiki_roadmap/ ) ) {
    var Tables = document.getElementsByTagName( 'table' );
mw.loader.load( 'ext.codereview.tooltips' );
    var i;
}
 
   
    function handleButtonLink( index, e ) {
/**
        window.collapseTable( index );
* Load withJS and withCSS
        e.preventDefault();
*
    }
* @source: www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL#Load_withJS_and_withCSS
 
* @rev: 1
    for ( i = 0; i < Tables.length; i++ ) {
* @author: Krinkle
        if ( $( Tables[i] ).hasClass( 'collapsible' ) ) {
*/
 
/* withJS */
            /* only add button and increment count if there is a header row to work with */
var extraJS = mw.util.getParamValue( 'withJS' );
            var HeaderRow = Tables[i].getElementsByTagName( 'tr' )[0];
if ( extraJS ) {
            if ( !HeaderRow ) continue;
// Disallow some characters in file name
            var Header = HeaderRow.getElementsByTagName( 'th' )[0];
if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
            if ( !Header ) continue;
importScript( extraJS );
 
// Don't use alert but the jsMsg system. Run jsMsg only once the DOM is ready.
            NavigationBoxes[ tableIndex ] = Tables[i];
} else {
            Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );
$( document ).ready( function() {
 
jsMsg( extraJS + ' script not allowed to be loaded.', 'error' );
            var Button    = document.createElement( 'span' );
} );
            var ButtonLink = document.createElement( 'a' );
}
            var ButtonText = document.createTextNode( collapseCaption );
}
 
/* withCSS */
            Button.className = 'collapseButton'; /* Styles are declared in Common.css */
var extraCSS = mw.util.getParamValue( 'withCSS' );
 
if ( extraCSS ) {
            ButtonLink.style.color = Header.style.color;
// Disallow some characters in file name
            ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
            ButtonLink.setAttribute( 'href', '#' );
importStylesheet( extraCSS );
            $( ButtonLink ).on( 'click', $.proxy( handleButtonLink, ButtonLink, tableIndex ) );
// Don't use alert but the jsMsg system. Run jsMsg only once the DOM is ready.
            ButtonLink.appendChild( ButtonText );
} else {
 
$( document ).ready( function() {
            Button.appendChild( document.createTextNode( '[' ) );
jsMsg( extraCSS + ' stylesheet not allowed to be loaded.', 'error' );
            Button.appendChild( ButtonLink );
} );
            Button.appendChild( document.createTextNode( ']' ) );
}
 
            Header.insertBefore( Button, Header.firstChild );
            tableIndex++;
        }
    }
 
    for ( i = 0;  i < tableIndex; i++ ) {
        if ( $( NavigationBoxes[i] ).hasClass( 'collapsed' ) || ( tableIndex >= autoCollapse && $( NavigationBoxes[i] ).hasClass( 'autocollapse' ) ) ) {
            window.collapseTable( i );
        }
        else if ( $( NavigationBoxes[i] ).hasClass ( 'innercollapse' ) ) {
            var element = NavigationBoxes[i];
            while ((element = element.parentNode)) {
                if ( $( element ).hasClass( 'outercollapse' ) ) {
                    window.collapseTable ( i );
                    break;
                }
            }
        }
    }
}
}
mw.hook( 'wikipage.content' ).add( createCollapseButtons );

Revision as of 01:23, 15 November 2013

/**
 * Collapsible tables *********************************************************
 *
 * Description: Allows tables to be collapsed, showing only the header. See
 *              [[Wikipedia:NavFrame]].
 * Maintainers: [[User:R. Koot]]
 */

var autoCollapse = 2;
var collapseCaption = 'hide';
var expandCaption = 'show';

window.collapseTable = function ( tableIndex ) {
    var Button = document.getElementById( 'collapseButton' + tableIndex );
    var Table = document.getElementById( 'collapsibleTable' + tableIndex );

    if ( !Table || !Button ) {
        return false;
    }

    var Rows = Table.rows;
    var i;

    if ( Button.firstChild.data === collapseCaption ) {
        for ( i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = 'none';
        }
        Button.firstChild.data = expandCaption;
    } else {
        for ( 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' );
    var i;

    function handleButtonLink( index, e ) {
        window.collapseTable( index );
        e.preventDefault();
    }

    for ( i = 0; i < Tables.length; i++ ) {
        if ( $( Tables[i] ).hasClass( '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', '#' );
            $( ButtonLink ).on( 'click', $.proxy( handleButtonLink, ButtonLink, tableIndex ) );
            ButtonLink.appendChild( ButtonText );

            Button.appendChild( document.createTextNode( '[' ) );
            Button.appendChild( ButtonLink );
            Button.appendChild( document.createTextNode( ']' ) );

            Header.insertBefore( Button, Header.firstChild );
            tableIndex++;
        }
    }

    for ( i = 0;  i < tableIndex; i++ ) {
        if ( $( NavigationBoxes[i] ).hasClass( 'collapsed' ) || ( tableIndex >= autoCollapse && $( NavigationBoxes[i] ).hasClass( 'autocollapse' ) ) ) {
            window.collapseTable( i );
        } 
        else if ( $( NavigationBoxes[i] ).hasClass ( 'innercollapse' ) ) {
            var element = NavigationBoxes[i];
            while ((element = element.parentNode)) {
                if ( $( element ).hasClass( 'outercollapse' ) ) {
                    window.collapseTable ( i );
                    break;
                }
            }
        }
    }
}

mw.hook( 'wikipage.content' ).add( createCollapseButtons );