MediaWiki:Common.js: различия между версиями

Материал из GIS-Lab
Перейти к навигации Перейти к поиску
Нет описания правки
Нет описания правки
 
Строка 1: Строка 1:
/* Размещённый здесь JavaScript код будет загружаться всем пользователям при обращении к каждой странице */
importMW = function ( name ) {
 
importScript( 'MediaWiki:' + name + '.js' );
/** Collapsible tables *********************************************************
};
*
*  Description: Allows tables to be collapsed, showing only the header. See
*                        http://www.mediawiki.org/wiki/Manual:Collapsible_tables.
*  Maintainers: [[en:User:R. Koot]]
*/
   
   
var autoCollapse = 2;
importScript_ = importScript;
var collapseCaption = 'скрыть';
importScript = function ( page, proj ) {
var expandCaption = 'показать';
if ( !proj ) {
importScript_( page );
} else {
if ( proj.indexOf( '.' ) === -1 ) {
proj += '.wikipedia.org';
}
importScriptURI( '//' + proj + '/w/index.php?action=raw&ctype=text/javascript&title=' + mw.util.wikiUrlencode( page ) );
}
};
   
   
function collapseTable( tableIndex ) {
        var Button = document.getElementById( 'collapseButton' + tableIndex );
        var Table = document.getElementById( 'collapsibleTable' + tableIndex );
   
   
        if ( !Table || !Button ) {
mw.config.set( 'tableSorterCollation', { 'ё': 'е' } );
                return false;
        }
   
   
        var Rows = Table.rows;
   
   
        if ( Button.firstChild.data == collapseCaption ) {
// Messages
                for ( var i = 1; i < Rows.length; i++ ) {
var zeroSectionTip = 'Править введение';
                        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 createCollapseButtons() {
var NavigationBarHide = '[скрыть]',
        var tableIndex = 0;
NavigationBarShow = '[показать]',
        var NavigationBoxes = new Object();
NavigationBarShowDefault = 2;
        var Tables = document.getElementsByTagName( 'table' );
   
   
        for ( var i = 0; i < Tables.length; i++ ) {
if ( /^en$/.test( mw.config.get( 'wgUserLanguage' ) ) ) {
                if ( hasClass( Tables[i], 'collapsible' ) ) {
importMW( 'Common-' + mw.config.get( 'wgUserLanguage' ) );
}
   
   
                        /* only add button and increment count if there is a header row to work with */
function editZeroSection() {
                        var HeaderRow = Tables[i].getElementsByTagName( 'tr' )[0];
if ( !mw.config.get( 'wgArticleId' ) ) {
                        if ( !HeaderRow ) {
return;
                                continue;
}
                        }
mw.util.$content.find( 'h2' )
                        var Header = HeaderRow.getElementsByTagName( 'th' )[0];
.children( '.mw-editsection:first' )
                        if ( !Header ) {
.clone().prependTo( '#bodyContent' )
                                continue;
.css( 'float', 'right' )
                        }
.find( 'a' )
.each( function( index ) {
                        NavigationBoxes[tableIndex] = Tables[i];
$( this ).attr( 'title', zeroSectionTip );
                        Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );
if ( $( this ).attr( 'class' ) === 'mw-editsection-visualeditor' ) {
$( this ).attr( 'href', mw.config.get( 'wgScript' ) + '?title=' + mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) ) + '&veaction=edit&vesection=0' );
                        var Button = document.createElement( 'span' );
} else {
                        var ButtonLink = document.createElement( 'a' );
$( this ).attr( 'href', mw.config.get( 'wgScript' ) + '?title=' + mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) ) + '&action=edit&section=0' );
                        var ButtonText = document.createTextNode( collapseCaption );
}
} );
                        Button.className = 'collapseButton'; // Styles are declared in [[MediaWiki:Common.css]]
}  
                        ButtonLink.style.color = Header.style.color;
                        ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
                        ButtonLink.setAttribute( 'href', "javascript:collapseTable(" + tableIndex + ");" );
                        ButtonLink.appendChild( ButtonText );
                        Button.appendChild( document.createTextNode( '[' ) );
                        Button.appendChild( ButtonLink );
                        Button.appendChild( document.createTextNode( ']' ) );
                        Header.insertBefore( Button, Header.childNodes[0] );
                        tableIndex++;
                }
        }
   
   
        for ( var i = 0;  i < tableIndex; i++ ) {
                if ( hasClass( NavigationBoxes[i], 'collapsed' ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], 'autocollapse' ) ) ) {
                        collapseTable( i );
                } else if ( hasClass( NavigationBoxes[i], 'innercollapse' ) ) {
                        var element = NavigationBoxes[i];
                        while ( element = element.parentNode ) {
                                if ( hasClass( element, 'outercollapse' ) ) {
                                        collapseTable( i );
                                        break;
                                }
                        }
                }
        }
}
   
   
addOnloadHook( createCollapseButtons );
/** Test if an element has a certain class **************************************
*
* Description: Uses regular expressions and caching for better performance.
* Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
*/
// Collapsiblе: [[ВП:СБ]]
// Collapsiblе: [[ВП:СБ]]
   
   
Строка 211: Строка 162:
}
}
   
   
var hasClass = ( function() {
        var reCache = {};
        return function( element, className ) {
                return ( reCache[className] ? reCache[className] : ( reCache[className] = new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ) ) ).test( element.className );
        };
})();
//Collapsiblе: [[ВП:СБ]]
   
   
var hasClass = (function (){
// Execution
  var reCache = {}
mw.loader.using( 'mediawiki.util', function() {
  return function (element, className){
   
  return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className)
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:
$( editZeroSection );
$( collapsibleDivs );
$( collapsibleTables );
$( checkEmptyCreatebox );
mw.loader.load( '//meta.wikimedia.org/w/index.php?title=MediaWiki:Wikiminiatlas.js&action=raw&ctype=text/javascript&smaxage=21600&maxage=86400' );
if ( document.location && document.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' );
if ( !mw.config.get( 'wgUserName' ) ) {
mw.util.addCSS( '#mw-fr-revisiontag { display:none }' ); // hide FlaggedRevs
}
// 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' );
}
// Generate interpuncts and parens for IE < 8
if ( $.client.profile().versionBase < '8' ) {
$( '.hlist' ).find( 'dt + dd, dt + dt' ).prev()
.append( '<b>:</b> ' );
$( '.hlist' ).find( 'dd + dd, dd + dt, li + li' ).prev()
.append( '<b>•</b> ' );
$( '.hlist' ).find( 'dl dl, ol ol, ul ul' )
.prepend( '( ' ).append( ') ' );
}
}
// ВП:СО, кроме статей  В Контакте, Одноклассники и Facebook
if ( mw.config.get( 'wgArticleId' ) !== 639373 &&
mw.config.get( 'wgArticleId' ) !== 932117 &&
mw.config.get( 'wgArticleId' ) !== 1297302 &&
mw.config.get( 'wgArticleId' ) !== 25133866
) {
importMW( 'Wikibugs' );
}
// Iwiki sorting
if ( !mw.config.get( 'wgUserName' ) ||
( mw.config.get( 'wgUserName' ) &&
( ( mw.config.get( 'wgLangPrefs' ) === null ? false : true ) ||
( mw.config.get( 'wgAddLangHints' ) === null ? false : mw.config.get( 'wgAddLangHints' ) ) ||
( mw.config.get( 'wgUseUserLanguage' ) === null ? false : mw.config.get( 'wgUseUserLanguage' ) ) ) )
) {
importMW( 'Interwiki-links' );
}
} );
   
   
function collapsibleTables(){
var Table, HRow,  HCell, btn, a, tblIdx = 0, colTables = []
var allTables = document.getElementsByTagName('table')
for (var i=0; Table = allTables[i]; i++){
  if (!hasClass(Table, 'collapsible')) continue
  if (!(HRow=Table.rows[0])) continue
  if (!(HCell=HRow.getElementsByTagName('th')[0])) continue
  Table.id = 'collapsibleTable' + tblIdx
  btn = document.createElement('span')
  btn.style.cssText = 'float:right; font-weight:normal; font-size:smaller'
  a = document.createElement('a')
  a.id = 'collapseButton' + tblIdx
  a.href = 'javascript:collapseTable(' + tblIdx + ');'
  a.style.color = HCell.style.color
  a.appendChild(document.createTextNode(NavigationBarHide))
  btn.appendChild(a)
  HCell.insertBefore(btn, HCell.childNodes[0])
  colTables[tblIdx++] = Table
}
for (var i=0; i < tblIdx; i++)
  if ((tblIdx > NavigationBarShowDefault && hasClass(colTables[i], 'autocollapse')) || hasClass(colTables[i], 'collapsed'))
    collapseTable(i)
}
   
   
function collapseTable (idx){
// Extra scripts
var Table = document.getElementById('collapsibleTable' + idx)
var btn = document.getElementById('collapseButton' + idx)
if (!Table || !btn) return false
var Rows = Table.rows
var isShown = (btn.firstChild.data == NavigationBarHide)
btn.firstChild.data = isShown ?  NavigationBarShow : NavigationBarHide
var disp = isShown ? 'none' : Rows[0].style.display
for (var i=1; i < Rows.length; i++)
    Rows[i].style.display = disp
}
   
   
function collapsibleDivs(){
var withJS = document.URL.match( /[&?]withjs=((mediawiki:)?([^&#]+))/i );
var navIdx = 0, colNavs = [], i, NavFrame
if ( withJS ) {
var divs = document.getElementById('content').getElementsByTagName('div')
importScript_( 'MediaWiki:' + withJS[3] );
for (i=0; NavFrame = divs[i]; i++) {
  if (!hasClass(NavFrame, 'NavFrame')) continue
  NavFrame.id = 'NavFrame' + navIdx
  var a = document.createElement('a')
  a.className = 'NavToggle'
  a.id = 'NavToggle' + navIdx
  a.href = 'javascript:collapseDiv(' + navIdx + ');'
  a.appendChild(document.createTextNode(NavigationBarHide))
  for (var j=0; j < NavFrame.childNodes.length; j++)
    if (hasClass(NavFrame.childNodes[j], 'NavHead'))
      NavFrame.childNodes[j].appendChild(a)
  colNavs[navIdx++] = NavFrame
}
for (i=0; i < navIdx; i++)
  if ((navIdx > NavigationBarShowDefault && !hasClass(colNavs[i], 'expanded')) || hasClass(colNavs[i], 'collapsed'))
    collapseDiv(i)
}
}
   
   
function collapseDiv(idx) {
var execJS = document.getElementById( 'executeJS' );
var div = document.getElementById('NavFrame' + idx)
if ( execJS ) {
var btn = document.getElementById('NavToggle' + idx)
$.each( execJS.className.split( ' ' ), function ( i, sc ) {
if (!div || !btn) return false
sc = $.trim( sc.replace( /[^\w ]/g, '' ) );
var isShown = (btn.firstChild.data == NavigationBarHide)
if ( sc ) {
btn.firstChild.data = isShown ? NavigationBarShow : NavigationBarHide
importMW( 'Script/' + sc );
var disp = isShown ? 'none' : 'block'
}
for (var child = div.firstChild; child != null;  child = child.nextSibling)
} );
  if (hasClass(child, 'NavPic') || hasClass(child, 'NavContent'))
      child.style.display = disp
}
}
var NavigationBarHide = '[скрыть]';
var NavigationBarShow = '[показать]';
var NavigationBarShowDefault = 2;
addOnloadHook( collapsibleDivs );

Текущая версия от 08:21, 24 марта 2015

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';
		}
		importScriptURI( '//' + proj + '/w/index.php?action=raw&ctype=text/javascript&title=' + mw.util.wikiUrlencode( page ) );
	}
};
 
 
mw.config.set( 'tableSorterCollation', { 'ё': 'е' } );
 
 
// Messages
var zeroSectionTip = 'Править введение';
 
var NavigationBarHide = '[скрыть]',
	NavigationBarShow = '[показать]',
	NavigationBarShowDefault = 2;
 
if ( /^en$/.test( mw.config.get( 'wgUserLanguage' ) ) ) {
	importMW( 'Common-' + mw.config.get( 'wgUserLanguage' ) );
}
 
function editZeroSection() {
	if ( !mw.config.get( 'wgArticleId' ) ) {
		return;
	}
	mw.util.$content.find( 'h2' )
		.children( '.mw-editsection:first' )
		.clone().prependTo( '#bodyContent' )
		.css( 'float', 'right' )
		.find( 'a' )
		.each( function( index ) { 
			$( this ).attr( 'title', zeroSectionTip );
			if ( $( this ).attr( 'class' ) === 'mw-editsection-visualeditor' ) { 
				$( this ).attr( 'href', mw.config.get( 'wgScript' ) + '?title=' + mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) ) + '&veaction=edit&vesection=0' );
			} else { 
				$( this ).attr( 'href', mw.config.get( 'wgScript' ) + '?title=' + mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) ) + '&action=edit&section=0' );
			} 
		} );
} 
 
 
// Collapsiblе: [[ВП:СБ]]
 
function collapsibleTables() {
	var $btn,
		$a,
		tblIdx = 0,
		colTables = [];
 
	$( 'table.collapsible' ).each( function() {
		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 );
		}
	}
}
 
function collapseTable ( idx ) {
	var $table = $( '#collapsibleTable' + idx ),
		$rows = $table.find( '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() {
	var navIdx = 0,
		colNavs = [],
		i;
	$( '#mw-content-text div.NavFrame' ).each( function() {
		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 );
		}
	}
}
 
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:
				$( editZeroSection );
				$( collapsibleDivs );
				$( collapsibleTables );
				$( checkEmptyCreatebox );
				mw.loader.load( '//meta.wikimedia.org/w/index.php?title=MediaWiki:Wikiminiatlas.js&action=raw&ctype=text/javascript&smaxage=21600&maxage=86400' );
				if ( document.location && document.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' );
 
	if ( !mw.config.get( 'wgUserName' ) ) {
		mw.util.addCSS( '#mw-fr-revisiontag { display:none }' ); // hide FlaggedRevs
	}
 
 
	// 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' );
		}
		// Generate interpuncts and parens for IE < 8
		if ( $.client.profile().versionBase < '8' ) {
			$( '.hlist' ).find( 'dt + dd, dt + dt' ).prev()
				.append( '<b>:</b> ' );
			$( '.hlist' ).find( 'dd + dd, dd + dt, li + li' ).prev()
				.append( '<b>•</b> ' );
			$( '.hlist' ).find( 'dl dl, ol ol, ul ul' )
				.prepend( '( ' ).append( ') ' );
		}
	}
 
	// ВП:СО, кроме статей  В Контакте, Одноклассники и Facebook
	if ( mw.config.get( 'wgArticleId' ) !== 639373 &&
		mw.config.get( 'wgArticleId' ) !== 932117 &&
		mw.config.get( 'wgArticleId' ) !== 1297302 &&
		mw.config.get( 'wgArticleId' ) !== 25133866
	) {
		importMW( 'Wikibugs' );
	}
 
 
	// Iwiki sorting
	if ( !mw.config.get( 'wgUserName' ) ||
		( mw.config.get( 'wgUserName' ) &&
			( ( mw.config.get( 'wgLangPrefs' ) === null ? false : true ) ||
				( mw.config.get( 'wgAddLangHints' ) === null ? false : mw.config.get( 'wgAddLangHints' ) ) ||
				( mw.config.get( 'wgUseUserLanguage' ) === null ? false : mw.config.get( 'wgUseUserLanguage' ) ) ) )
	) {
		importMW( 'Interwiki-links' );
	}
 
} );
 
 
// Extra scripts
 
var withJS = document.URL.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 );
		}
	} );
}