MediaWiki:Common.js

Материал из Emuverse

Замечание: Возможно, после публикации вам придётся очистить кэш своего браузера, чтобы увидеть изменения.

  • Firefox / Safari: Удерживая клавишу Shift, нажмите на панели инструментов Обновить либо нажмите Ctrl+F5 или Ctrl+R (⌘+R на Mac)
  • Google Chrome: Нажмите Ctrl+Shift+R (⌘+Shift+R на Mac)
  • Internet Explorer / Edge: Удерживая Ctrl, нажмите Обновить либо нажмите Ctrl+F5
  • Opera: Нажмите Ctrl+F5.
importMW = function ( name ) {
	importScript( 'MediaWiki:' + name + '.js' );
};

importScript_ = importScript;
importScript = function ( page, proj ) {
	if ( !proj ) {
		importScript_( page );
	} else {
		if ( proj.indexOf( '.' ) === -1 ) {
			proj += '.wikipedia.org';
		}
		mw.loader.load( '//' + proj + '/w/index.php?title=' + mw.util.wikiUrlencode( page ) + '&action=raw&ctype=text/javascript' );
	}
};


mw.config.set( 'tableSorterCollation', { 'ё': 'е' } );


// Messages
var NavigationBarHide = '[скрыть]',
	NavigationBarShow = '[показать]',
	NavigationBarShowDefault = 2;

if ( /^en$/.test( mw.config.get( 'wgUserLanguage' ) ) ) {
	importMW( 'Common-' + mw.config.get( 'wgUserLanguage' ) );
}

// Collapsiblе: [[ВП:СБ]]

function collapsibleTables( $content ) {
	var $btn,
		$a,
		tblIdx = 0,
		colTables = [],
		$Tables = $content.find( 'table' );

    $Tables.each( function( i, table ) {
        if ( $(table).hasClass( 'collapsible' ) ) {
			var $table = $( this ),
				$row = $table.find( 'tr' ).first(),
				$cell = $row.find( 'th' ).first();
			if ( !$cell.length ) {
				return;
			}
			$table.attr( 'id', 'collapsibleTable' + tblIdx );
			$btn = $( '<span>' )
				.css( {
					'float': 'right',
					'font-weight': 'normal',
					'font-size': 'smaller'
				} );
			$a = $( '<a>' )
				.attr( 'id', 'collapseButton' + tblIdx )
				.attr( 'href', 'javascript:collapseTable(' + tblIdx + ');' )
				.css( 'color', $cell.css( 'color' ) )
				.text( NavigationBarHide )
				.appendTo( $btn );
			if ( $cell.contents().length ) {
				$btn.insertBefore( $cell.contents().first() );
			} else {
				$btn.appendTo( $cell );
			}
			colTables[tblIdx++] = $table;
        }
	} );
	for ( var i = 0; i < tblIdx; i++ ) {
		if ( colTables[i].hasClass( 'collapsed' ) ||
			( tblIdx > NavigationBarShowDefault &&
				colTables[i].hasClass( 'autocollapse' )
			)
		) {
			collapseTable( i );
		}
	}
}

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

function collapseTable ( idx ) {
	var $table = $( '#collapsibleTable' + idx ),
		$rows = $table.children().children( 'tr' ),
		$btn = $( '#collapseButton' + idx );
	if ( !$table.length || !$rows.length || !$btn.length ) {
		return false;
	}

	var isShown = ( $btn.text() === NavigationBarHide ),
		cssDisplay = isShown ? 'none' : $rows.first().css( 'display' );

	$btn.text( isShown ? NavigationBarShow : NavigationBarHide );
	$rows.slice( 1 ).each( function() {
		$( this ).css( 'display', cssDisplay );
	} );
}

function collapsibleDivs( $content ) {
	var navIdx = 0,
		colNavs = [],
		i,
		$Divs = $content.find( 'div' );
	
	$Divs.each( function( i, div ) {
        if ( $(div).hasClass( 'NavFrame' ) ) {	
			var $navFrame = $( this );
			$navFrame.attr( 'id', 'NavFrame' + navIdx );
			var $a = $( '<a>' )
				.addClass( 'NavToggle' )
				.attr( 'id', 'NavToggle' + navIdx )
				.attr( 'href', 'javascript:collapseDiv(' + navIdx + ');' )
				.text( NavigationBarHide );
			$navFrame.children( '.NavHead' ).append( $a );
			colNavs[navIdx++] = $navFrame;
        }
	} );
	for ( i = 0; i < navIdx; i++ ) {
		if ( colNavs[i].hasClass( 'collapsed' ) ||
			( navIdx > NavigationBarShowDefault &&
				!colNavs[i].hasClass( 'expanded' )
			)
		) {
			collapseDiv( i );
		}
	}
}

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

function collapseDiv ( idx ) {
	var $div = $( '#NavFrame' + idx ),
		$btn = $( '#NavToggle' + idx );
	if ( !$div.length || !$btn.length ) {
		return false;
	}
	var isShown = ( $btn.text() === NavigationBarHide );
	$btn.text( isShown ? NavigationBarShow : NavigationBarHide );
	$div.children( '.NavContent,.NavPic' ).each( function() {
		$( this ).css( 'display', isShown ? 'none' : 'block' );
	} );
}

function checkEmptyCreatebox() {
	$( 'form.createbox' ).on( 'submit', function() {
		if ( !$( this ).find( 'input.createboxInput' ).val() ) {
			window.alert( 'Не указано название новой статьи!' );
			return false;
		}
		return true;
	} );
}


// Execution
mw.loader.using( 'mediawiki.util', function() {

	if ( mw.config.get( 'wgCanonicalNamespace' ) === 'Special' ) {
		if ( /^(Uplo|Sear|Stat|Spec|Abus|Prefe|Move|Watch|Newp|Log|Block$)/i.test( mw.config.get( 'wgCanonicalSpecialPageName' ) ) ) {
			importMW( mw.config.get( 'wgCanonicalSpecialPageName' ) );
		}

	} else {
		switch ( mw.config.get( 'wgAction' ) ) {

			case 'history':
		 		importMW( 'History' );
		 		break;

			case 'delete':
				importMW( 'Deletepage' );
				break;

			case 'edit':
			case 'submit':
				importMW( 'Editpage' );
				// and continue with the default: view, purge

			default:
				$( checkEmptyCreatebox );
				mw.loader.load( '//meta.wikimedia.org/w/index.php?title=MediaWiki:Wikiminiatlas.js&action=raw&ctype=text/javascript' );
				if ( location.protocol === 'https:' ) {
					importMW( 'Secure' );
				}
				if ( navigator.platform.indexOf( 'Win' ) !== -1 ) {
					mw.util.addCSS( '.IPA, .Unicode { font-family: "Arial Unicode MS", "Lucida Sans Unicode"; }' );
				}

				switch ( mw.config.get( 'wgNamespaceNumber' ) ) {
					case 0:
					case 100:
						importMW( 'Osm' );
						if ( mw.config.get( 'wgIsMainPage' ) ) {
							importMW( 'Mainpage' );
						}
						break;
					case 6:
						importMW( 'Filepage' );
						break;
				}
		}
	}

	importMW( 'Sidebar-related' );

	// Helper script for .hlist class in common.css
	// Author: [[:en:User:Edokter]]
	if ( $.client.profile().name == 'msie' ) {
		// Add pseudo-selector class to last child list items in IE 8
		if ( $.client.profile().versionBase == '8' ) {
			$( '.hlist' ).find( 'dd:last-child, dt:last-child, li:last-child' )
				.addClass( 'hlist-last-child' );
		}
	}

} );


// Extra scripts

var withJS = location.href.match( /[&?]withjs=((mediawiki:)?([^&#]+))/i );
if ( withJS ) {
	importScript_( 'MediaWiki:' + withJS[3] );
}

var execJS = document.getElementById( 'executeJS' );
if ( execJS ) {
	$.each( execJS.className.split( ' ' ), function ( i, sc ) {
		sc = $.trim( sc.replace( /[^\w ]/g, '' ) );
		if ( sc ) {
			importMW( 'Script/' + sc );
		}
	} );
}