跳转到内容

User:SunAfterRain/js/internalLinkHelper-redtipsy.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
$.when(
	$.ready,
	mw.loader.using( [ 'ext.gadget.HanAssist' ] )
).then( function () {
	function i18nMsg( $1, $2 ) {
		return mw.format(
			mw.libs.HanAssist.vary( {
				hans: '条目“$1”尚未创建,可参考$2维基百科的对应页面:',
				hant: '條目「$1」尚未創建,可參考$2維基百科的對應頁面:'
			} ),
			$1, $2
		);
	}
	
	function makeTidy( $ele, getTitleElementFunction ) {
		var $tip = $( '<div class="tipsy tipsy-nw ilh-tipsy" role="tooltip">' );
		var $tipArrow = $( '<div class="tipsy-arrow">' );
		var $tipInner = $( '<div class="tipsy-inner">' );
		$tip.append( $tipArrow, $tipInner );

		return {
			$self: $ele,
			show: function () {
				$tipInner.append( getTitleElementFunction() );
	
				$tip
					.remove()
					.css( {
						top: 0,
						left: 0,
						visibility: 'hidden',
						display: 'block'
					} )
					.attr( 'aria-hidden', 'true' )
					.appendTo( document.body );

				var pos = $.extend(
					{},
					$ele.offset(),
					{
						width: $ele[ 0 ].offsetWidth,
						height: $ele[ 0 ].offsetHeight
					}
				);

				$tip.css( {
					top: pos.top + pos.height,
					left: pos.left + pos.width / 2 - 15
				} );

				$( document ).on( 'keydown', function ( e ) {
					if ( e.keyCode === 27 ) { // ESC
						$tip.hide();
					}
				} );

				$tip
					.stop()
					.css( {
						opacity: 0,
						display: 'block',
						visibility: 'visible'
					})
					.attr( 'aria-hidden', 'false' )
					.animate( {
						opacity: 1.0
					}, 100 );
			},
			tip: function () {
				return $tip;
			}
		};
	}
	
	mw.hook( 'wikipage.content' ).add( function( $content ) {
		$( '.ilh-all', $content ).not( '.ilh-blue' ).on( 'internalLinkHelper-close', function( event ) {
			var $this = $( this );
			if ( $this.data( 'internalLinkHelper-showing' ) ) {
				$( this ).removeClass( 'ilh-active' ).find( '.ilh-page a' ).tipsy( 'hide' ).end()
					.data( 'internalLinkHelper-showing', false );
			}
		} ).each( function() {
			var origTitle = $( this ).data( 'orig-title' ),
				$foreignSpan = $( '.ilh-link', this ),
				$linkAnchor = $( '.ilh-page a', this ),
				$langSpan = $( '.ilh-lang', this ),
				langName = $langSpan.text(),
				$that = $( this ).data( 'internalLinkHelper-showing', false );

			if ( !$linkAnchor.length ) {
				return;
			}

			var tidy = makeTidy(
				$linkAnchor,
				function () {
					return $( '<div>' ).append(
						$( '<span>' ).text( i18nMsg( origTitle, langName ) ),
						$foreignSpan.clone(),
						$( '<span>' ).text( '。' )
					);
				}
			);
			var timeout = null;
			
			var maybeClearTimeout = function() {
				if ( timeout !== null ) {
					clearTimeout( timeout );
				}
			}, autoSetTimeout = function() {
				maybeClearTimeout();
				timeout = setTimeout.apply( null, arguments );
			};
			
			var mouseleave = function() {
				autoSetTimeout( function() {
					$that.trigger( 'internalLinkHelper-close' );
				}, 500 );
			}, mouseenter = function() {
				if ( $that.data( 'internalLinkHelper-showing' ) ) {
					maybeClearTimeout();
				} else {
					$( '.ilh-all', $content ).not( $that ).trigger( 'internalLinkHelper-close' );
					$that.addClass( 'ilh-active' ).data( 'internalLinkHelper-showing', true );
					tidy.show();
					tidy.tip().mouseleave( mouseleave ).mouseenter( mouseenter );
				}
			};
			
			$linkAnchor.mouseleave( mouseleave ).mouseenter( mouseenter );
		} );
	} );
} );