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 */
* Collapsible tables
*
* @version 2.0.1 (2013-03-26)
* @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
* @author [[User:R. Koot]]
* @author [[User:Krinkle]]
* @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
* is supported in MediaWiki core.
*/
var autoCollapse = 2;
var collapseCaption = 'hide';
var expandCaption = 'show';
function collapseTable( tableIndex ) {
        var Button = document.getElementById( 'collapseButton' + tableIndex );
        var Table = document.getElementById( 'collapsibleTable' + tableIndex );
   
   
        if ( !Table || !Button ) {
// Add "mainpage" class to the body element
                return false;
if (
        }
mw.config.get( 'wgMainPageTitle' ) === mw.config.get( 'wgPageName' ) &&
mw.config.get( 'wgAction' ) === 'view'
        var Rows = Table.rows;
) {
$( document ).ready( function() {
        if ( Button.firstChild.data == collapseCaption ) {
document.body.className += ' mainpage';
                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 createClickHandler( tableIndex ) {
// Editpage scripts
        return function ( e ) {
if ( mw.config.get( 'wgAction' ) == 'edit' || mw.config.get( 'wgAction' ) == 'submit' ) {
                e.preventDefault();
importScript( 'MediaWiki:Editpage.js' );
                collapseTable( tableIndex );
        }
}
}
   
   
function createCollapseButtons() {
/* End of extra pages */
        var tableIndex = 0;
        var NavigationBoxes = {};
        var Tables = document.getElementsByTagName( 'table' );
   
   
        for ( var i = 0; i < Tables.length; i++ ) {
/**
                if ( $( Tables[i] ).hasClass( 'collapsible' ) ) {
* Test if an element has a certain class
                        /* only add button and increment count if there is a header row to work with */
*
                        var HeaderRow = Tables[i].getElementsByTagName( 'tr' )[0];
* Based on script from en.wikipedia.org, 2008-09-15.
                        if ( !HeaderRow ) {
* Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
                                continue;
*
                        }
* @deprecated since MediaWiki 1.17: Use jQuery(element).hasClass instead
                        var Header = HeaderRow.getElementsByTagName( 'th' )[0];
*/
                        if ( !Header ) {
var hasClass = ( function () {
                                continue;
var reCache = {};
                        }
return function ( element, className ) {
return ( reCache[className] ? reCache[className] : ( reCache[className] = new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ) ) ).test( element.className );
};
})();
   
   
                        NavigationBoxes[tableIndex] = Tables[i];
/**
                        Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );
* Hide prefix in category
*
* @source: www.mediawiki.org/wiki/Snippets/Hide_prefix_in_category
* @rev: 3
* @author Krinkle
*/
mw.loader.using( 'jquery.mwExtension', function () {
var $tplHideCategoryPrefix = $( '#mw-cat-hideprefix' );
if ( $tplHideCategoryPrefix.length ) {
var prefix = $tplHideCategoryPrefix.text();
if ( $.trim( prefix ) === '' ) {
prefix = mw.config.get( 'wgTitle' ) + '/';
}
$( '#mw-pages' ).find( 'a' ).text( function( i, val ){
return val.replace( new RegExp( '^' + $.escapeRE( prefix ) ), '' );
});
}
});
   
   
                        var Button = document.createElement( 'span' );
/**
                        var ButtonLink = document.createElement( 'a' );
* Load the CodeReview 'tooltips' gadget on [[MediaWiki roadmap]] subpages,
                        var ButtonText = document.createTextNode( collapseCaption );
* so it's available for the revision reports where it's particularly useful
* @author: Happy-melon
*/
if ( mw.config.get( 'wgPageName' ).match( /^MediaWiki_roadmap/ ) ) {
mw.loader.load( 'ext.codereview.tooltips' );
}
   
   
                        Button.style.styleFloat = 'right';
/**
                        Button.style.cssFloat = 'right';
* Load withJS and withCSS
                        Button.style.fontWeight = 'normal';
*
                        Button.style.textAlign = 'right';
* @source: www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL#Load_withJS_and_withCSS
                        Button.style.width = '6em';
* @rev: 1
   
* @author: Krinkle
                        ButtonLink.style.color = Header.style.color;
  */
                        ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
/* withJS */
                        $( ButtonLink ).on( 'click', createClickHandler( tableIndex ) );
var extraJS = mw.util.getParamValue( 'withJS' );
                        ButtonLink.appendChild( ButtonText );
if ( extraJS ) {
// Disallow some characters in file name
                        Button.appendChild( document.createTextNode( '[' ) );
if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
                        Button.appendChild( ButtonLink );
importScript( extraJS );
                        Button.appendChild( document.createTextNode( ']' ) );
// Don't use alert but the jsMsg system. Run jsMsg only once the DOM is ready.
} else {
                        Header.insertBefore( Button, Header.childNodes[0] );
$( document ).ready( function() {
                        tableIndex++;
jsMsg( extraJS + ' script not allowed to be loaded.', 'error' );
                }
} );
        }
}
}
        for ( var i = 0; i < tableIndex; i++ ) {
/* withCSS */
                if ( $( NavigationBoxes[i] ).hasClass( 'collapsed' ) ||
var extraCSS = mw.util.getParamValue( 'withCSS' );
                        ( tableIndex >= autoCollapse && $( NavigationBoxes[i] ).hasClass( 'autocollapse' ) )
if ( extraCSS ) {
                ) {
// Disallow some characters in file name
                        collapseTable( i );
if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
                }
importStylesheet( extraCSS );
        }
// Don't use alert but the jsMsg system. Run jsMsg only once the DOM is ready.
} else {
$( document ).ready( function() {
jsMsg( extraCSS + ' stylesheet not allowed to be loaded.', 'error' );
} );
}
}
}
$( createCollapseButtons );

Revision as of 01:19, 15 November 2013

/*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
 *
 * 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
 */
var hasClass = ( function () {
	var reCache = {};
	return function ( element, className ) {
		return ( reCache[className] ? reCache[className] : ( reCache[className] = new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ) ) ).test( element.className );
	};
})();
 
/**
 * Hide prefix in category
 *
 * @source: www.mediawiki.org/wiki/Snippets/Hide_prefix_in_category
 * @rev: 3
 * @author Krinkle
 */
mw.loader.using( 'jquery.mwExtension', function () {
	var $tplHideCategoryPrefix = $( '#mw-cat-hideprefix' );
	if ( $tplHideCategoryPrefix.length ) {
		var prefix = $tplHideCategoryPrefix.text();
		if ( $.trim( prefix ) === '' ) {
			prefix = mw.config.get( 'wgTitle' ) + '/';
		}
		$( '#mw-pages' ).find( 'a' ).text( function( i, val ){
			return val.replace( new RegExp( '^' + $.escapeRE( prefix ) ), '' );
		});
	}
});
 
/**
 * Load the CodeReview 'tooltips' gadget on [[MediaWiki roadmap]] subpages,
 * so it's available for the revision reports where it's particularly useful
 * @author: Happy-melon
 */
if ( mw.config.get( 'wgPageName' ).match( /^MediaWiki_roadmap/ ) ) {
	mw.loader.load( 'ext.codereview.tooltips' );
}
 
/**
 * Load withJS and withCSS
 *
 * @source: www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL#Load_withJS_and_withCSS
 * @rev: 1
 * @author: Krinkle
 */
/* withJS */
var extraJS = mw.util.getParamValue( 'withJS' );
if ( extraJS ) {
	// Disallow some characters in file name
	if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
		importScript( extraJS );
	// Don't use alert but the jsMsg system. Run jsMsg only once the DOM is ready.
	} else {
		$( document ).ready( function() {
			jsMsg( extraJS + ' script not allowed to be loaded.', 'error' );
		} );
	}
}
/* withCSS */
var extraCSS = mw.util.getParamValue( 'withCSS' );
if ( extraCSS ) {
	// Disallow some characters in file name
	if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
		importStylesheet( extraCSS );
	// Don't use alert but the jsMsg system. Run jsMsg only once the DOM is ready.
	} else {
		$( document ).ready( function() {
			jsMsg( extraCSS + ' stylesheet not allowed to be loaded.', 'error' );
		} );
	}
}