MediaWiki:Gadget-ajaxWatchlist.js

De Wikipedia, la enciclopedia libre

Nota: Después de guardar, debes refrescar la caché de tu navegador para ver los cambios. Internet Explorer: mantén presionada Ctrl mientras pulsas Actualizar. Firefox: mientras presionas Mayús pulsas el botón Actualizar, (o presiona Ctrl-Shift-R). Los usuarios de Google Chrome y Safari pueden simplemente pulsar el botón Recargar. Para más detalles e instrucciones acerca de otros exploradores, véase Ayuda:Cómo limpiar la caché.

/**
 * ajaxWatchlist.js
 *
 * A watchlist that automatically updates, so you can
 * spend less time refreshing and more time... er,
 * doing whatever you do.
 * see original code <https://en.wikipedia.org/wiki/User:Theopolisme/Scripts/ajaxWatchlist.js>
 *
 * @author Theopolisme
 * @author He7d3r
 */
( function ( $, mw ) {
	"use strict";
	var jqxhr, interval, $ajaxWatchlist;

	function updateWatchlist () {
		var $loadingIndicator = $( '.watchlistLoadingIndicator' ),
			$content = $( '#mw-content-text' );

		$ajaxWatchlist = $( '#ajaxWatchlist' );

		// If this is the first time we run the script, wrap everything
		// after and including the watchlist form in a div with the id
		// "ajaxWatchlist".
		if ( $ajaxWatchlist.length === 0 ) {
			$content.find( '#mw-watchlist-form' ).nextAll().addBack().wrapAll( '<div id="ajaxWatchlist"></div>' );
			$ajaxWatchlist = $( '#ajaxWatchlist' )
				.hover( function () {
					jqxhr.abort();
					clearInterval( interval );
				}, function () {
					interval = setInterval( updateWatchlist, window.watchlistUpdateFrequency || 20000 );
				} );
		}

		// Create a new loading indicator if it doesn't already
		// exist. If it already exists, just show it.
		if ( $loadingIndicator.length === 0 ) {
			$loadingIndicator = $( '<span>' )
				.appendTo( '.firstHeading' )
				.addClass( 'watchlistLoadingIndicator' )
				.append(
					$.createSpinner( {
						size: 'medium',
						type: 'inline'
					} ),
					'Actualizando lista de seguimiento...'
				);
		} else {
			$loadingIndicator.show();
		}

		// Make the ajax request to actually update the watchlist
		jqxhr = $.ajax( {
			url: location.href,
			dataType: 'html'
		} )
		.done( function ( data ) {
			// If the watchlist contents have changed, update the page
			// to display the new contents.
			var $newContent = $( data ).find( '#mw-content-text #mw-watchlist-form' ).nextAll().addBack(); // Same selector as $ajaxWatchlist
			if ( $ajaxWatchlist.text() !== $newContent.text() ) {
				$ajaxWatchlist.empty().append( $newContent );
				mw.hook( 'wikipage.content' ).fire( $ajaxWatchlist ); // So scripts will run on the updated content
			}
			$loadingIndicator.hide();
		} )
		.fail( function ( jqXHR, textStatus ) {
			// Hide the indicator and then display an error notification
			$loadingIndicator.hide();
			if ( textStatus !== 'abort' ) {
					mw.notify( 'ajaxWatchlist: No se puede actualizar la lista de seguimiento.' );
			}
		} );
	}

	if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Watchlist' ) {
		// Add custom css for the loading indicator
		mw.util.addCSS( '.watchlistLoadingIndicator { float: right; font-size: 12px; } ' );
		// Run updateWatchlist() every 20 seconds by default (can be configured via window.watchlistUpdateFrequency)
		mw.loader.using( [ 'jquery.spinner' ], function () {
			interval = setInterval( updateWatchlist, window.watchlistUpdateFrequency || 20000 );
		} );
	}
}( jQuery, mediaWiki ) );