MediaWiki:Gadget-generations.js

De Poképédia
Aller à la navigation Aller à la recherche

Note : après avoir publié vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

  • Firefox / Safari : maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou appuyez sur Ctrl + F5 ou Ctrl + R (⌘ + R sur un Mac).
  • Google Chrome : appuyez sur Ctrl + Maj + R (⌘ + Shift + R sur un Mac).
  • Internet Explorer / Edge : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl + F5.
  • Opera : appuyez sur Ctrl + F5.
if ( $.inArray( 'Pokémon', mw.config.get( 'wgCategories' ) ) != -1 || $.inArray( 'Capacité', mw.config.get( 'wgCategories' ) ) != -1 ) {

	// Config : la génération indiquée et la précédente seront considérées comme les seules encore présentes sur la page principale
	// Lors du passage à une nouvelle génération, seules les mises à jour de cette variable et du Modèle:Générations sont nécessaires
	var genActuelle = 9;

	/**
	 * Fonction permettant de créer un sommaire
	 *
	 * Créé en avril 2017 par XFusio
	 */
	var createToc = function( toc ) {
		var headings = $( '#mw-content-text' ).find( 'h2, h3, h4, h5, h6' ).not( '#toc h2' );
		var i, x, y = [1, 1, 1, 1, 1, 1], last_y = [1, 1, 1, 1, 1, 1];
		var parent;

		for ( i = 0 ; i < headings.length ; i++ ) {
			headings[i].level = headings[i].tagName.replace( "H", "" );
			headings[i].name = $( headings[i] ).find( '> .mw-headline' ).text();
			headings[i].link = $( headings[i] ).find( '> a' ).attr( 'id' );
			if ( headings[i].link === undefined ) headings[i].link = $( headings[i] ).find( '> .mw-headline' ).attr( 'id' );

			if ( headings[i].level == 2 ) {
				headings[i].number = y[1];
				y[1]++;
				toc.append( '<li class="toclevel-1 tocsection-' + i + '"><a href="#' + headings[i].link + '"><span class="tocnumber">' + headings[i].number + '</span> <span class="toctext">' + headings[i].name + '</span></a></li>' );
			}
			else {
				for ( x = i - 1 ; x >= 0 ; x-- ) {
					if ( headings[x].level < headings[i].level ) {
						if ( last_y[headings[x].level] != x ) {
							y[headings[x].level] = 1;
							last_y[headings[x].level] = x;
						}
						headings[i].number = headings[x].number + "." + y[headings[x].level];
						y[headings[x].level]++;

						parent = toc.find( '.tocsection-' + x );
						if ( parent.find( '> ul' ).length < 1 ) parent.append( $( '<ul>' ) );
						parent.find( '> ul' ).append( '<li class="toclevel-' + headings[i].level + ' tocsection-' + i + '"><a href="#' + headings[i].link + '"><span class="tocnumber">' + headings[i].number + '</span> <span class="toctext">' + headings[i].name + '</span></a></li>' );
						break;
					}
				}
			}
		}
	};


	/**
	 * Affichage des informations d'une génération spécifique dans les articles de Pokémon et de capacité
	 *
	 * Idée de base en 2013 par Misdre : http://www.pokepedia.fr/Utilisateur:Misdre/common.js?oldid=539301
	 * Créé en mars 2017 par XFusio
	 * Modif de mai 2018 par XFusio : supporte désormais les pages de capacité & détection automatique des sections à modifier
	 */

	var generation = function( gen ) {
		var page = mw.config.get( 'wgPageName' ).replace( new RegExp('/.+', 'i'), "" );
		if ( gen != genActuelle && gen != genActuelle - 1 ) page += '/Génération ' + gen;
    
		if ( $( '.permuter:first' ).data( 'apparition' ) > gen ) {
			console.log( 'Le Pokémon ou la capacité demandée n\'existe pas dans la génération demandée, et le changement de génération n\'a donc pas pu être effectué.' );
			$( '.permuter a' ).click( function( e ) {
				e.preventDefault();
				var gen = $( this ).text();
				$.cookie( 'defaultGeneration', gen );
				if ( gen != genActuelle && gen != genActuelle - 1 ) generation( gen );
			} );
		}
		else {
			$(".permuter").css({ "background-image": "url(\"/images/d/de/Ajax-loader.gif\")", "background-repeat": "no-repeat", "background-size": "100% 8px" });

			new mw.Api().get({
				'format': 'json',
				'action': 'parse',
				'page': page,
				'prop': 'text',
				'redirects': ''
			}).done( function( data ) {
				var permuter, position, sections = [];
				var parser = new DOMParser();
				var contents = parser.parseFromString(data.parse.text['*'], 'text/html');
        
				$('.permuter').each(function(){
					if ( $( contents ).find( '.permuter[data-section="' + $( this ).data('section') + '"]' ).length > 0 ) sections.push( $( this ).data('section') );
				});

				sections.forEach( function( element ) {
					if ( element == "Infobox" ) {
						$( '.permuter[data-section="Infobox"]' ).html( $( contents ).find( '.permuter[data-section="Infobox"]' ).html() );
						$( '.ficheinfo:first' ).html( $( contents ).find( '.ficheinfo:first' ).html() );
					}
					else {
						permuter = $( '.permuter[data-section="' + element +'"]' );
						position = permuter.prev( 'h2, h3, h4, h5, h6' ).prev();
						permuter.prev( 'h2, h3, h4, h5, h6' ).nextUntil( 'h2' ).addBack().remove();
						$( contents ).find( '.permuter[data-section="' + element +'"]' ).prev( 'h2, h3, h4, h5, h6' ).nextUntil( 'h2' ).addBack().insertAfter( position );
					}
				} );

				if ( $.inArray( 'Pokémon', mw.config.get( 'wgCategories' ) ) != -1 ) {
					var title = $( '.permuter[data-section=Descriptions]' ).prev( 'h2, h3, h4, h5, h6' );
					var prev = title.prev();
					var contenu = title.html();
					title.remove();
					$( '<h3>' ).html( contenu ).insertAfter( prev );

					$( '#content .ruban-pokemon:not(:first):not(:last)' ).remove();
				}

				if ( typeof boutonsAfficherMasquer == "function" ) {
					boutonsAfficherMasquer();
				}

				$( '#toc > ul' ).html( "" );
				createToc( $( '#toc > ul' ) );

				// Conserve en mémoire le dernier changement de génération effectué, pour le pas le refaire une autre fois d'affilée
				$( '.permuter' ).data( 'precedent', gen );
				$( '.permuter a ').click( function( e ) {
					e.preventDefault();
					var gen = $( this ).text();
					if ( gen != $.cookie( 'defaultGeneration' ) ) $.cookie( 'defaultGeneration', gen );
					genApparente = $( this ).closest( '.permuter' ).data( 'precedent' );
					if ( genApparente == genActuelle && gen != genApparente && gen != genApparente - 1 ) generation( gen );
					else if ( genApparente == genActuelle - 1 && gen != genApparente && gen != genActuelle ) generation( gen );
					else if ( gen != genApparente ) generation( gen );
				} );
			
			} ).fail( function( error ) {
				console.error( 'Gadget generations : ' + error );
				
			} ).always( function() {
				$(".permuter").css("background-image",  "none");
			} );
		}
	};

	( function() {
		var defaultGeneration = $.cookie( 'defaultGeneration' );
		if ( !defaultGeneration ) {
			defaultGeneration = genActuelle;
			$.cookie( 'defaultGeneration', genActuelle );
		}
		if ( defaultGeneration != genActuelle && defaultGeneration != genActuelle - 1 ) {
			generation( defaultGeneration );
		}
		else {
			$( '.permuter a' ).click( function( e ) {
				e.preventDefault();
				var gen = $( this ).text();
				if ( gen != defaultGeneration ) $.cookie( 'defaultGeneration', gen );
				if ( gen != genActuelle && gen != genActuelle - 1 ) generation( gen );
			} );
		}
	} )();

}