跳转到内容

User:PhiLiP/c.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/*
 
== 增加摺疊功能 ==
*/
/** 摺疊 div table *****************************
 *  Description: 实现div.NavFrame和table.collapsible的可折叠性。
 *  JSConfig的collapseText、expandText、autoCollapse属性定义默认文字和默认最少自动折叠块
 *  Maintainers: [[User:fdcn]], [[User:PhiLiP]]
 */
$.fn.extend( {
	'toggleButton' : function( state ) {
		var parent = head = $( this );
		if ( head.attr( 'tagName' ).toLowerCase() == 'tr' ) { // 对表格特别处理
			if ( $( 'th', head ).length ) {
				parent = $( '> :last', head ); // 表格的按钮附在最后一个子节点里
			} else {
				return;
			}
		}
		var show, hide, button = $( 'span.NavToggle:first', head );
		if ( button.length ) {
			parent = button.parent();
		} else {
			// 没有按钮的话,就创建按钮
			button = $( '<span/>' ).addClass( 'NavToggle' ).css( 'width', '6em' );
			show = $( '<span/>' ).addClass( 'toggleShow' ).text( JSConfig.expandText ).appendTo( button );
			hide = $( '<span/>' ).addClass( 'toggleHide' ).text( JSConfig.collapseText ).appendTo( button );
		}
		button.css( 'display', 'inline' );
		parent.prepend( button );
		show.css( 'display', state ? show.attr( 'showStyle' ) || '' : 'none' );
		hide.css( 'display', state ? 'none' : show.attr( 'showStyle' ) || '' );
		
		$( 'a', this ).click( function( e ) {
			e = e || window.event;
    		if ( e.stopPropagation ) {
    			e.stopPropagation();
    		} else {
    			e.cancelBubble = true;
    		}
    	} );
    	
    	head.addClass( 'uncollapse toggleHotspot' )
    		.data( 'show', show )
    		.data( 'hide', hide )
    		.data( 'state', !state )
    		.css( 'cursor', 'pointer' )
    		.attr( 'tabindex', '0' );
    	
		return this;
	},
	'collapse' : function() {
		var el = $( this ).css( 'overflow', 'hidden' );
		var state = el.hasClass( 'collapsed' )
            || ( wgCollapseCount >= JSConfig.autoCollapse && el.hasClass( 'autocollapse' ) );
		
		var navhead = $( '> .NavHead:first,> tr:first', this ).toggleButton( state )
			.keydown( function( e ) {
    			if ( event.which == 13 ) { // Enter
    				_toggle(); // TODO: 用animate实现
    			}
    		} )
    		.click( _toggle );
		
		if ( state ) {
			el.css( 'height', navhead.outerHeight() );
		}
		
		function _toggle() {
			navhead.data( 'hide' ).toggle();
			navhead.data( 'show' ).toggle();
			var state = navhead.data( 'state' );
			if ( state ) {
				el.stop().animate( { 'height' : navhead.outerHeight() }, 600 );
			} else {
				el.stop().animate( { 'height' : '' }, 600 );
			}
			navhead.data( 'state', !state );
		}
		
		return this;
	}
} );

$( function() {
	var navFrame = $( 'div.NavFrame,table.collapsible' );
	window.wgCollapseCount = navFrame.length;

	navFrame.each( function() {
		$( this ).collapse();
	} );
} );

//修正摺疊後定位變化
hookEvent("load",function(){if(location.hash){location.href=location.hash;}});