Diferencia entre revisiones de «MediaWiki:Gadget-CitationTemplatesExtracts.js»

Contenido eliminado Contenido añadido
(Sin diferencias)

Revisión del 14:18 16 jul 2016

var config = mw.config.get( [
		'wgNamespaceNumber',
		'wgNamespaceIds',
		'wgTitle'
	] ),
	TARGET_TITLE = 'Plantillas de fuentes y autorizaciones';

if (
	config.wgNamespaceNumber === config.wgNamespaceIds.category &&
	config.wgTitle.indexOf( TARGET_TITLE ) === config.wgTitle.length - TARGET_TITLE.length
) {
	mw.loader.using( [
		'mediawiki.api.parse',
		'jquery.tipsy'
	] ).done( function () {
		var cache = {},
			defaultTipsyOptions = {
				gravity: $.fn.tipsy.autoNS,
				html: true
			},
			API_DELAY = 100,
			api = new mw.Api();
		
		function makeRequest( template ) {
			return api.parse( mw.format( '{' + '{$1}}', template ), {
				title: 'título',
				prop: 'text',
				disablelimitreport: true
			} );
		}
		
		function bindTipsyActions( $el, cacheKey ) {
			$el.tipsy( $.extend( {}, defaultTipsyOptions, {
				title: function () {
					return cache[ cacheKey ];
				}
			} ) );
		}
		
		mw.hook( 'wikipage.content' ).add( function ( $content ) {
			$content.find( '#mw-pages' ).find( 'li > a[title^="Plantilla:"]' ).each( function () {
				var $el = $( this ),
					cacheKey = $el.attr( 'title' );
				
				$el.on( 'mouseenter.tipsy-ref', function ( evt ) {
					var timerID;
					
					if ( cache.hasOwnProperty( cacheKey ) && cache[ cacheKey ] !== null ) {
						$el.off( 'mouseenter.tipsy-ref mouseleave.tipsy-ref' );
						bindTipsyActions( $el, cacheKey );
						$el.trigger( 'mouseenter' );
						return;
					}
					
					timerID = setTimeout( function () {
						var template = $el.attr( 'title' ).replace( /^Plantilla:/, '' ),
							request = makeRequest( template );
						
						$el.data( 'request', request ).removeData( 'timer-id' );
						
						request.done( function ( wikitext ) {
							var pageText, parsed;
						
							$el.off( 'mouseenter.tipsy-ref mouseleave.tipsy-ref' );
							cache[ cacheKey ] = wikitext.toString();
							bindTipsyActions( $el, cacheKey );
							
							if ( $el.is( ':hover' ) ) {
								$el.trigger( 'mouseenter' );
							}
						} );
					}, API_DELAY );
					
					$el.data( 'timer-id', timerID );
				} );
				
				$el.on( 'mouseleave.tipsy-ref', function ( evt ) {
					var timerID = $el.data( 'timer-id' ),
						request = $el.data( 'request' );
					
					if ( timerID !== undefined ) {
						clearTimeout( timerID );
					}
					
					if ( request !== undefined ) {
						request.abort();
					}
					
					$el.removeData( [ 'timer-id', 'request' ] );
				} );
			} );
		} );
	} );	
}