跳转到内容

MediaWiki:Gadget-NavFrame.js

维基百科,自由的百科全书

这是本页的一个历史版本,由Fantasticfears留言 | 贡献2013年11月9日 (六) 04:28编辑。这可能和当前版本存在着巨大的差异。

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
 * Dynamic Navigation Bars. See [[Wikipedia:NavFrame]]
 * 
 * Based on script from en.wikipedia.org, 2008-09-15.
 *
 * @source www.mediawiki.org/wiki/MediaWiki:Gadget-NavFrame.js
 * @maintainer Helder.wiki, 2012–2013
 * @maintainer Krinkle, 2013
 * @maintainer Fantasticfears, 2013
 */
( function () {
 
// Set up the words in your language
var collapseCaption = wgULS('隐藏', '隱藏');
var expandCaption = wgULS('显示', '顯示');
 
var navigationBarHide = collapseCaption + '▲';
var navigationBarShow = expandCaption + '▼';
 
/**
 * Shows and hides content and picture (if available) of navigation bars.
 *
 * @param {number} indexNavigationBar The index of navigation bar to be toggled
 * @param {jQuery.Event} e Event object
 */
function toggleNavigationBar( indexNavigationBar, e ) {
	var toggle = $( '#NavToggle' + indexNavigationBar ),
		frame = $( '#NavFrame' + indexNavigationBar ),
		isFrameCollapsed = frame.hasClass( 'collapsed' );

	if ( !frame || !toggle ) {
		return false;
	}

	if ( isFrameCollapsed ) {
		frame.find( '.NavPic, .NavContent' ).each( function() {
			$( this ).css( 'display', 'block' );
		});
		frame.find( '.toggleShow' ).each( function() {
			$( this ).css( 'display', 'block' );
		});
		frame.find( '.toggleHide' ).each( function() {
			$( this ).css( 'display', 'none' );
		});
		toggle.text( navigationBarHide );
		frame.removeClass( 'collapsed' );
	} else {
		frame.find( '.NavPic, .NavContent' ).each( function() {
			$( this ).css( 'display', 'none' );
		});
		frame.find( '.toggleShow' ).each( function() {
			$( this ).css( 'display', 'none' );
		});
		frame.find( '.toggleHide' ).each( function() {
			$( this ).css( 'display', 'block' );
		});
		toggle.text( navigationBarShow );
		frame.addClass( 'collapsed' );
	}
}

/**
 * Adds show/hide-button to navigation bars.
 *
 * @param {jQuery} $content
 */
function createNavigationBarToggleButton( $content ) {
	// Iterate over all (new) nav frames
	$content.find( 'div.NavFrame' ).each( function( indexNavigationBar ) {
		var frame = $( this ).attr( 'id', 'NavFrame' + indexNavigationBar );
		// If found a navigation bar
		var navToggle = $( '<a href="#" class="NavToggle" id="NavToggle' + indexNavigationBar + '"></a>' );
		frame.find( '.NavHead' ).each( function() {
			$( this ).on( 'click', $.proxy( toggleNavigationBar, null, indexNavigationBar ) );
		});
        if ( frame.hasClass( 'collapsed' ) ) {
        	frame.find( '.NavPic, .NavContent' ).each( function() {
        		$( this ).css( 'display', 'none' );
			});
        }

		var showNavigationBarHide = true;
		frame.find( '.NavPic, .NavContent' ).each( function() {
			if ( $( this ).css( 'display' ) === 'none' ) {
				showNavigationBarHide = false;
				return false;
			}
		});

		navToggle.text( showNavigationBarHide? navigationBarHide: navigationBarShow );

		frame.find( '.NavHead' ).each( function() {
			$( this ).append( navToggle );
			return false;
		});
	});
}

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

}());