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

Материал из Янтарной ноосферы, свободной интернет-энциклопедии
(всяческая хрень с возможностью сворачивания)
 
м (1 версия импортирована: попробуем)
(нет различий)

Версия от 18:04, 10 февраля 2023

/* Размещённый здесь код JavaScript будет загружаться пользователям при обращении к каждой странице */
// 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>' ).addClass('collapseButton');
			$a = $( '<a>' )
				.attr( 'id', 'collapseButton' + tblIdx )
				.attr( 'href', 'javascript:collapseTable(' + tblIdx + ');' )
				.css( 'color', // изменяем цвет ссылки, только если цвет текста в навбоксе нестандартный
					($cell.css( 'color' ) == $('.mw-body').css('color'))
					? 'auto'
					: $cell.css( 'color' ) )
				.text( collapseCaption );
			$btn
				.append( '[' )
				.append( $a )
				.append( ']' );
			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() === collapseCaption ),
		cssDisplay = isShown ? 'none' : $rows.first().css( 'display' );

	$btn.text( isShown ? expandCaption : collapseCaption );
	$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' );
	} );
}