MediaWiki:Gadget-Preview.js

//

// - // Shows a "preview" button next to each linked article to get a preview of the article right next to the table. // Much thanks to User:Denny/articlePreview.js, in Wikidata from which I got the idea. // // Usage: Enable the gadget Preview in your preferences. // -

( function ( mw, $ ) {	"use strict";

if ( !mw.config.get( 'wbEntityId' ) || mw.config.get( 'wbEntityId' ).substr( 0, 1 ) !== 'q' ) { return; }

switch ( mw.config.get( 'wgUserLanguage' ) ) { case 'de': case 'de-at': case 'de-ch': case 'de-formal': mw.messages.set( {			'title' :	'Artikelvorschau',			'preview' :	'Vorschau',			'noarticle' :	'Du hast noch keinen Artikel ausgewählt.',			'readmore' :	'Weiterlesen',			'hide' :	'verstecken',			'show' :	'zeigen'		} ); break; case 'fa': mw.messages.set( {			'title' :	'پیش‌نمایش مقاله',			'preview' :	'پیش‌نمایش',			'noarticle' :	'شما هنوز مقاله‌ای را انتخاب نکرده‌اید.',			'readmore' :	'مطالعهٔ بیشتر',			'hide' :	'پنهان',			'show' :	'نمایش'		} ); break; case 'fi': mw.messages.set( {			'title' :	'Artikkelin esikatselu',			'preview' :	'esikatselu',			'noarticle' :	'Et ole vielä valinnut artikkelia.',			'readmore' :	'Lue lisää',			'hide' :	'piilota',			'show' :	'näytä'		} ); break; case 'hu': mw.messages.set( {			'title' :	'Cikk előnézete',			'preview' :	'előnézet',			'noarticle' :	'Nem választottál cikket.',			'readmore' :	'Tovább',			'hide' :	'elrejt',			'show' :	'mutat'		} ); break; case 'nl': mw.messages.set( {			'title' :	'Voorbeeld van artikel',			'preview' :	'voorbeeld',			'noarticle' :	'Je hebt nog geen artikel geselecteerd.',			'readmore' :	'Meer lezen',			'hide' :	'Verbergen',			'show' :	'Tonen'		} ); break; default: case 'en': mw.messages.set( {			'title' :	'Article preview',			'preview' :	'preview',			'noarticle' :	'You have not selected an article yet.',			'readmore' :	'Read more',			'hide' :	'hide',			'show' :	'show'		} ); }

/**	 * The cached previews. */	var cache = {}; /**	 * The html elements. */	var $articlepreview, $title, $content, $container, $toggler;

/**	 * Initialising the links. */	function init { var lang = mw.config.get( 'wgUserLanguage' ); $( '.wb-widget-container' ).html(			$( ' ' )			.attr( 'id', 'x-articlepreview' )			.css( { 'width': '35em', 'margin-left': '30px' } )			.append( $( ' ' )				.text( mw.msg( 'title' ) ) )			.append( $( ' ' )				.attr( 'id', 'x-articlepreview-content' ) .append(					$( ' ' )					.css( 'font-style', 'italic' )					.text( mw.msg( 'noarticle' ) )				) )		);

if ( $( '.wb-sitelinks-link-' + lang + ' a' ).text ) { $( '#x-articlepreview-content p' ) .append( ' &rarr; ' ) .append(				$( '' )				.attr( 'href', '#' )				.text( $( '.wb-sitelinks-link-' + lang + ' a' ).text )				.click( function { preview( lang, $( '.wb-sitelinks-link-' + lang ).attr( 'dir' ) ); return false; } ) // 			); }

$( '.wb-sitelinks tbody tr' ).each( function {			var $link, lang, dir;			$link = $( this ).find( '.wb-sitelinks-link' );			lang = $link.attr( 'lang' );			dir = $link.attr( 'dir' );			$link.hover( function  { $( this ).append(					$( ' ' )					.append( $( '' ) .attr( 'href', '#x-articlepreview' ) .css( 'float', $( this ).attr( 'dir' ) === 'ltr' ? 'right' : 'left' ) .text( mw.msg( 'preview' ) ) .click( function {							preview( lang, dir );							//return false;						} ) )				);			}, function { $( this ).find( 'span' ).remove; } );		} );	}

/**	 * Adds the html to the preview widget. *	 * @param wiki the wiki to show */	function showPreview( site ) { var data = cache[ site.getId ];

$title .attr( 'dir', data.dir ) .attr( 'lang', data.lang ) .html( ( data.displaytitle || mw.html.escape( data.title ) ) + ' — ' + site.getName + ' ' ) .append(			$( ' ' )			.attr( 'dir', 'ltr' )			.attr( 'lang', 'en' )			.text( '(' + site.getId + ')' )		);

$content .attr( {			'class': data.dir ? 'mw-content-' + data.dir : undefined,			'dir': data.dir,			'lang': data.lang		} ) .html( data.content ) .append(			$( ' ' )			.append( $( '' ) .attr( {					'href': site.getUrlTo( data.title ),					'title': site.getId + ':' + data.title				} ) .text( mw.msg( 'readmore' ) ) ) // 		); //

if ( data.image ) { $content.prepend(				$( '' )				.attr( 'href', data.image.url )				.append( $( ' ' )					.attr( {						'src': data.image.source,						'width': data.image.width,						'height': data.image.height,						'class': 'thumbnail',						'title': data.image.name,						'alt': data.image.name					} ) )			);		}	}

/**	 * Showing the preview got by an http request. Requests are cached. *	 * @param lang the article's language * @param dir the article's language direction */	function preview( lang, dir ) { var title = $( '.wb-sitelinks-link-' + lang + ' a:eq(0)' ).text; if ( !title ) { return; }		if ( cache.hasOwnProperty( lang ) ) { showPreview( cache[lang] ); }		else { mw.loader.using( ['jquery.spinner'], function {				$( '#x-articlepreview-content' ).html( '' );				$.createSpinner( { size: 'small', type: 'block' } ).appendTo( '#x-articlepreview-content' );				$.getJSON( '//' + lang + '.miraheze.org/w/api.php?callback=?', {						action: 'query', prop: 'extracts', exintro: true, exchars: 1000, titles: title, format: 'json' },					function ( data ) { var $preview = $( ' ' ) .css( 'float', dir === 'ltr' ? 'right' : 'left' ) .append( '[' ) .append(							$( '' )							.attr( { href: '#', id: 'x-articlepreview-toggle' } )							.text( mw.msg( 'hide' ) ) // 						) .append( ']' ) .after(							$( ' ' )							.attr( 'dir', dir )							.attr( 'lang', lang )							.text( $( '.wb-sitelinks-sitename-' + lang ).text + ' ' )							.append( $( ' ' )								.attr( 'dir', 'ltr' ) .attr( 'lang', 'en' ) .text( '(' + lang + ')' ) )						)						.after(							$( ' ' )							.attr( { id: 'x-articlepreview-article', 'class': 'mw-content-' + dir, dir: dir, lang: lang } )							.html( data.query.pages[Object.keys( data.query.pages )[0]].extract.replace( /<\/p>(…|...)/, '… ' ) )							.append( $( ' ' )								.append(									$( '' )									.attr( 'href', '//' + lang + '.miraheze.org/wiki/' + mw.util.wikiUrlencode( title ) )									.text( mw.msg( 'readmore' ) )								) //  ) // 						); // 						cache[lang] = $preview; showPreview( $preview ); }				);			} );		}	}

/**	 * Adds the preview to the document. *	 * @param preview the preview in html format */	function showPreview( preview ) { $( '#x-articlepreview-content' ).html( preview ); // toggle link $( '#x-articlepreview-toggle' ).toggle( function {			$( '#x-articlepreview-article' ).slideUp;			$( this ).text( mw.msg( 'show' ) );		}, function  {			$( '#x-articlepreview-article' ).slideDown;			$( this ).text( mw.msg( 'hide' ) );		} ); }

$( document ).ready( init ); } )( mediaWiki, jQuery );

/**	 * The miraheze commons site. */	var commons = wb.sites.getSite( 'commonswiki' );

function updatePosition { if ( $( window ).scrollTop < 90 ) { $articlepreview.removeClass( 'fixed' ); } else { $articlepreview.addClass( 'fixed' ); }	}

function show { if ( $articlepreview.hasClass( 'open' ) ) { return; }		$toggler.attr( 'title', mw.msg( 'title' ) + ' — ' + mw.msg( 'hide' ) ); var width = Math.max( $( window ).width - 1300, 400 ); $articlepreview.addClass( 'open' ).animate( {			width: width + 'px'		} ); $container.width( ( width - 40 ) + 'px' ); }

function hide { $articlepreview.animate( {			width: '0px'		}, function {			$articlepreview.removeClass( 'open' );			$toggler.attr( 'title', mw.msg( 'title' ) + ' — ' + mw.msg( 'show' ) );		} ); } //