MediaWiki:Gadget-AncreTitres.js

Un article de Wikipèdia, l'enciclopèdia liura.

Nòta : Aprèp aver publicat la pagina, vos cal forçar son recargament complet tot ignorant lo contengut actual de l'amagatal de vòstre navigador per veire los cambiaments : Mozilla / Firefox / Konqueror / Safari : mantenètz la tòca Majuscula (Shift) en clicant lo boton Actualizar (Reload,) o quichatz Maj-Ctrl-R (Maj-Cmd-R sus Apple Mac) ; Internet Explorer / Opera : mantenètz la tòca Ctrl en clicant lo boton Actualizar o quichatz Ctrl-F5.

/**
 * AncreTitres
 *
 * Cette fonction fournit un lien vers une section de page en cliquant
 * sur le lien [URL] ou [[lien]] à droite du titre de section.
 *
 * Auteurs : Pabix, Phe, Bayo, Chphe et Arkanosis
 * {{Projet:JavaScript/Script|AncreTitres}}
 */

/*global alert, jQuery, mediaWiki, window */
/*jslint vars: true, plusplus: true */

// <nowiki>

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

	$( function() {

		var _option = {
			nom_ancre : '[URL]',
			nom_lien_interne : '[[ligam]]',
			description : 'Obténer una URL cap a aquesta seccion',
			descinterne : 'Obténer un [[Ligam#intèrne]]',
			linkcolor : '',
			fontSize : 'xx-small',
			fontWeight : 'normal',
			afficheE : true,
			afficheI : true
		};

		if ( typeof window.AncreTitres !== 'undefined' ) {
			$.extend( _option, window.AncreTitres );
		}

		if ( !$( '#content' ).length ||
		     ( !_option.afficheI && !_option.afficheE ) ) {
			return;
		}

		$( 'span.mw-headline' ).each( function( _, headline ) {
			var anchor = headline.getAttribute( 'id' );

			var $span = $( '<span class="noprint ancretitres" style="' +
				'font-size: ' + _option.fontSize + '; ' +
				'font-weight: ' + _option.fontWeight + '; ' +
				 (_option.linkcolor !== '' ? 'color: ' + _option.linkcolor + ';' : '') +
				'-moz-user-select:none; -webkit-user-select:none; -ms-user-select:none; user-select:none;' +
			'"></span>' );

			if ( _option.afficheE ) {
				$span.append( ' ' );
				$( '<a href="#" title="' + _option.description + '">' + _option.nom_ancre + '</a>' ).click( function() {
					window.prompt(
						'Lien :',
						'https:' + mw.config.get( 'wgServer' ) + mw.util.getUrl() + '#' + anchor
					);
					return false;
				} ).appendTo( $span );
			}

			if ( _option.afficheI ) {
				$span.append( ' ' );
				$( '<a href="#" title="' + _option.descinterne+ '">' + _option.nom_lien_interne + '</a>' ).click( function() {
					var decodedAnchor = decodeURIComponent(
						anchor
						.replace( /\.([0-9A-F]{2})/g, '%$1' )
						.replace( /%C2%A0/g, '.C2.A0' ) // espace insécable
						.replace( /%E2%80%89/g, '.E2.80.89' ) // espace fine
						.replace( /%7B%7B/g, '.7B.7B' ) // "{{"
						.replace( /%7D%7D/g, '.7D.7D' ) // "}}"
					);
					window.prompt(
						'Ligam :',
						'[[' + ( mw.config.get( 'wgPageName' ) + '#' + decodedAnchor ).replace( /_/g, ' ' ) + ']]'
					);
					return false;
				} ).appendTo( $span );
			}

			$( headline ).parent().append( $span );
		} );

	} );

} ( mediaWiki, jQuery ) );

// </nowiki>