Usuario:Vivaelcelta/MediaWiki:Gadget-AncreTitres.js

/**
 * 
 * Engadir botón para poder copiar a URL e ligazóns internas das seccións.
 * Importado desde https://fr.wikipedia.org/w/index.php?title=MediaWiki:Gadget-AncreTitres.js&action=edit
 * 
 * Este accesorio proporciona dúas ligazóns nas seccións á dereita do título da sección,
 * para que clicando copie no portapapeis a [URL] ou [[ligazón interna]] da sección
 *
 *
 * Auteurs : Pabix, Phe, Bayo, Chphe, Arkanosis, Mah3110, Ash_Crow
 * {{Projet:JavaScript/Script|AncreTitres}}
 */
/* global $, mw */
/* eslint-env browser */
// <nowiki>
mw.loader.using( [ 'mediawiki.util', 'mediawiki.notification', 'user' ], function () {
	'use strict';

	$( function ( $ ) {
		var lang = mw.config.get( 'wgUserLanguage' ),
			messages = {
				en: {
					'ancretitres-anchor-name': '[URL]',
					'ancretitres-internal-link-name': '[[Link]]',
					'ancretitres-description': 'Get an URL to this section',
					'ancretitres-int-description': 'Get an internal link to this section',
					'ancretitres-notif-title': 'Text copied to clipboard',
					'ancretitres-notif-error': 'Could not copy to clipboard'
				},
				gl: {
					'ancretitres-anchor-name': '[URL]',
					'ancretitres-internal-link-name': '[[Ligazón]]',
					'ancretitres-description': 'Obter unha URL desta sección',
					'ancretitres-int-description': 'Obter unha ligazón interna desta sección',
					'ancretitres-notif-title': 'Texto copiado no portapapeis',
					'ancretitres-notif-error': 'Non se puido copiar no portapapeis'
				},
				fr: {
					'ancretitres-anchor-name': '[URL]',
					'ancretitres-internal-link-name': '[[Lien]]',
					'ancretitres-description': 'Obtenir une URL vers cette section',
					'ancretitres-int-description': 'Obtenir un lien interne vers cette section',
					'ancretitres-notif-title': 'Texte copié dans le presse-papiers',
					'ancretitres-notif-error': 'Impossible de copier dans le presse-papiers'
				}
			},
			options = {
				linkcolor: '',
				fontSize: 'x-small',
				fontWeight: 'normal',
				afficheE: true,
				afficheI: true
			};

		mw.messages.set( messages.en );
		if ( lang && lang !== 'en' && lang in messages ) {
			mw.messages.set( messages[ lang ] );
		}

		function copyTextToClipboard( text ) {
			var textArea = document.createElement( 'textarea' );

			textArea.style.position = 'fixed';
			textArea.style.top = 0;
			textArea.style.left = 0;

			textArea.style.width = '2em';
			textArea.style.height = '2em';
			textArea.style.padding = 0;
			textArea.style.border = 'none';
			textArea.style.outline = 'none';
			textArea.style.boxShadow = 'none';
			textArea.style.background = 'transparent';

			textArea.value = text;

			document.body.appendChild( textArea );
			textArea.focus();
			textArea.select();
			document.execCommand( 'copy' );

			document.body.removeChild( textArea );
		}

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

		if ( !options.afficheI && !options.afficheE ) {
			return;
		}

		$( 'span.mw-headline' ).each( function ( _, headline ) {
			var $linkE, $linkI, $span = $( '<span>' ).addClass( 'noprint ancretitres' );

			$span.css( {
				'font-size': options.fontSize,
				'font-weight': options.fontWeight,
				color: ( options.linkcolor !== '' ? options.linkcolor : '' ),
				'-moz-user-select': 'none',
				'-webkit-user-select': 'none',
				'-ms-user-select': 'none',
				'user-select': 'none'
			} );

			if ( options.afficheE ) {
				$linkE = $( '<a href="#" title="' + mw.msg( 'ancretitres-description' ) + '">' + mw.msg( 'ancretitres-anchor-name' ) + '</a>' ).click( function () {
					var outputText = 'https:' + mw.config.get( 'wgServer' ) + mw.util.getUrl() + '#' + headline.id;
					copyTextToClipboard( outputText );
					mw.notification.notify( outputText, { title: mw.msg( 'ancretitres-notif-title' ), tag: 'ancretitres', type: 'info', autoHide: true } );

					return false;
				} );
				$span.append( ' ', $linkE );
			}

			if ( options.afficheI ) {
				$linkI = $( '<a href="#" title="' + mw.msg( 'ancretitres-int-description' ) + '">' + mw.msg( 'ancretitres-internal-link-name' ) + '</a>' ).click( function () {
					var outputText = '[[' + ( mw.config.get( 'wgPageName' ) + '#' + headline.id ).replace( /_/g, ' ' ) + ']]';
					copyTextToClipboard( outputText );
					mw.notification.notify( outputText, { title: mw.msg( 'ancretitres-notif-title' ), tag: 'ancretitres', type: 'info', autoHide: true } );

					return false;
				} );
				$span.append( ' ', $linkI );
			}

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

	} );

} );