User:The Herald/userwarn.js

From Wikidata
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * Adds a dialog to any user's talk page where you can select a template that should be added to the talk page.
 *
 * @author [[User:Bene*]]
 */
 
// <nowiki>
( function( mw, $ ) {
	'use strict';
 
	if( mw.config.get( 'wgPageName' ).indexOf( 'Special:Contributions' ) === -1 && mw.config.get( 'wgNamespaceNumber' ) !== 3 ) { // user talk and Special:Contributions only
		return;
	}
 
	$( '<style type="text/css"> #userwarn a { color: #007EDF; } #userwarn-result { margin: 20px } </style>' ).appendTo( 'head' );
 
	var user;
	if ( mw.config.get( 'wgNamespaceNumber' ) === 3 ) {
		user = mw.config.get( 'wgTitle' );
	} else {
		user = mw.util.getParamValue( 'target' ) || mw.config.get( 'wgTitle' ).substr( 14 );
	}
 
	switch ( mw.config.get( 'wgUserLanguage' ) ) {
	default:
	case 'en':
		mw.messages.set( {
			'userwarn':		'Warn this user',
			'close':		'Close',
			'intro':		'This tool will add one of the following templates to the bottom of this user\'s talk page.',
			'warning':		'warning',
			'selectwarning':	'Please select a warning template',
			'success':		'The user was successfully warned.',
			'options':		'Show more options',
			'furtherarguments':	'Enter further arguments (seperated by <code>|</code>) that will be added to the template:',
			'signature':		'Automatically add the signature after the template',
			'watch':			'Add the user\'s talk page to your watchlist',
			'error-nowarning':	'You have to select a warning.'
		} );
		break;
	case 'cs':
		mw.messages.set( {
			'userwarn':		'Upozornit uživatele',
			'close':		'Zavřít',
			'intro':		'Tento nástroj vloží na konec uživatelovy diskusní stránky jednu z následujících šablon.',
			'warning':		'varování',
			'selectwarning':	'Prosím, vyberte šablonu s varováním',
			'success':		'Uživatel byl úspěšně upozorněn.',
			'options':		'Ukaž další nastavení',
			'furtherarguments':	'Vložte další parametry (oddělené <code>|</code>), které se zahrnou do šablony:',
			'signature':		'Za šablonu automaticky vložit podpis',
			'error-nowarning':	'Musíte zvolit varování.'
		} );
		break;
	case 'de':
		mw.messages.set( {
			'userwarn':		'Warne diesen Benutzer',
			'close':		'Schließen',
			'intro':		'Dieses Werkzeug wird eine der folgenden Vorlagen am Ende der Diskussionsseite von diesem Benutzer hinzufügen.',
			'warning':		'Warnung',
			'selectwarning':	'Bitte wähle eine Warnungsvorlage aus',
			'success':		'Der Benutzer wurde erfolgreich verwarnt.',
			'options':		'Zeige mehr Einstellungen',
			'furtherarguments':	'Gib weitere Parameter (getrennt durch <code>|</code>) ein, die der Vorlage hinzugefügt werden:',
			'signature':		'Füge automatisch die Signatur hinter der Vorlage ein',
			'watch':			'Beobachte die Diskussionsseite dieses Benutzers',
			'error-nowarning':	'Du musst eine Warnung auswählen.'
		} );
		break;
	case 'id':
		mw.messages.set( {
			'userwarn':             'Peringatkan pengguna ini',
			'close':                'Tutup',
			'intro':                'Ini akan menambah salah satu templat berikut ke bagian bawah halaman pembicaraan pengguna ini.',
			'warning':              'Peringatan',
			'selectwarning':        'Silakan pilih templat',
			'success':              'Pesan berhasil dikirimkan.',
			'options':              'Lihat pilihan lain',
			'furtherarguments':     'Berikan keterangan tambahan (pisahkan dengan <code>|</code>) yg akan ditambahkan pada templat:',
			'signature':            'Otomatis cantumkan tanda tangan setelah templat',
			'error-nowarning':      'Anda harus memilih templat.'
		} );
		break;
	case 'min':
		mw.messages.set( {
			'userwarn':             'Ingekkan pangguno ko',
			'close':                'Tutuik',
			'intro':                'Iko akan manambah salah satu templat barikuik ka bagian bawah laman rundiang pangguno ko.',
			'warning':              'Paringatan',
			'selectwarning':        'Silakan piliah templat',
			'success':              'Pasan bahasil dikirim.',
			'options':              'Lihek piliahan lain',
			'furtherarguments':     'Agiah katarangan tambahan (pisahan jo tando <code>|</code>) nan ka ditambahan pado templat:',
			'signature':            'Otomatih malatakkan tando tangan sasudah templat',
			'error-nowarning':      'Sanak haruih mamiliah templat.'
		} );
		break;
	}
 
	if( mw.util.getParamValue( 'userwarn' ) === 'success' ) {
		mw.notify( mw.msg( 'success' ) );
	}
 
	function init() {
		// Add dialog
		mw.loader.using( [ 'jquery.ui' ], function() {
			var $form = $( '<div>' )
			.attr( 'id', 'userwarn' )
			.append(
				$( '<form>' )
				.attr( 'action', 'javascript:;' )
				.submit( douserwarn )
				.append(
					$( '<fieldset>' )
					.attr( 'id', 'userwarn-form' )
					.append(
						$( '<legend>' )
						.text( mw.msg( 'userwarn' ) )
					) // </legend>
					.append(
						$( '<p>' )
						.attr( 'id', 'userwarn-intro' )
						.text( mw.msg( 'intro' ) )
					) // </p>
					.append(
						$( '<p>' )
						.append(
							$( '<label>' )
							.attr( {
								'for'	: 'userwarn-warning',
								'class'	: 'userwarn-label'
							} )
							.text( mw.msg( 'selectwarning' ) + ': ' )
						) // </label>
						.append(
							$( '<select>' )
							.attr( {
								'id'	: 'userwarn-warning',
								'class'	: 'userwarn-input'
							} )
							.append(
								$( '<option>' )
								.attr( 'value', 'none' )
								.text( '(' + mw.msg( 'warning' ) + ')' )
							)
							.append(
								$( '<option>' )
								.text( 'Uw-link-removal1' )
							)
							.append(
								$( '<option>' )
								.text( 'Uw-link-removal2' )
							)
							.append(
								$( '<option>' )
								.text( 'Uw-link-removal3' )
							)
							.append(
								$( '<option>' )
								.text( 'Uw-link-removal4' )
							)
							.append(
								$( '<option>' )
								.text( 'Uw-vandalism1' )
							)
							.append(
								$( '<option>' )
								.text( 'Uw-vandalism2' )
							)
							.append(
								$( '<option>' )
								.text( 'Uw-vandalism3' )
							)
							.append(
								$( '<option>' )
								.text( 'Uw-vandalism4' )
							)
							.append(
								$( '<option>' )
								.text( 'No-articles-description' )
							)
							.append(
								$( '<option>' )
								.text( 'Uw-lang as desc' )
							)
							.append(
								$( '<option>' )
								.text( 'welcome' )
							)
							.change( function() {
								//$( '#userwarn-result' ).html( mw.msg( 'doc-' + $( '#userwarn-warning' ).val() ) );
								$( '#userwarn-result' )
								.html(
									$( '#userwarn-warning' ).val() === 'none' ? '' : $( '<a>' )
									.attr( 'href', mw.util.getUrl( 'Template:' + $( '#userwarn-warning' ).val() ) )
									.text( ' →‎ Template:' + $( '#userwarn-warning' ).val() )
								);
							} )
						) // </select>
					) // </p>
					.append(
						$( '<input>' )
						.attr( {
							'type': 'checkbox',
							'id': 'userwarn-options-toggle'
						} )
						.change( function() {
							if( $( '#userwarn-options-toggle' ).prop( 'checked' ) === true ) {
								$( '#userwarn-options' ).slideDown();
							}
							else {
								$( '#userwarn-options' ).slideUp();
							}
						} )
					) // </input>
					.append(
						$( '<label>' )
						.attr( 'for', 'userwarn-options-toggle' )
						.text( mw.msg( 'options' ) )
					) // </label>
					.append(
						$( '<div>' )
						.attr( 'id', 'userwarn-options' )
						.hide()
						.append(
							$( '<p>' )
							.append(
								$( '<label>' )
								.attr( 'for', 'userwarn-furtherarguments' )
								.html( mw.msg( 'furtherarguments' ) )
							)
							.append(
								$( '<input>' )
								.attr( {
									'id': 'userwarn-furtherarguments',
									'type': 'text'
								} )
								.css( 'width', '100%' )
							)
						) // </p>
						.append(
							$( '<p>' )
							.append(
								$( '<input>' )
								.attr( {
									'id': 'userwarn-signature',
									'type': 'checkbox',
									'checked': 'checked'
								} )
							)
							.append(
								$( '<label>' )
								.attr( 'for', 'userwarn-signature' )
								.text( mw.msg( 'signature' ) )
							)
						) // </p>
						.append(
							$( '<p>' )
							.append(
								$( '<input>' )
								.attr( {
									'id': 'userwarn-watch',
									'type': 'checkbox'
								} )
							)
							.append(
								$( '<label>' )
								.attr( 'for', 'userwarn-watch' )
								.text( mw.msg( 'watch' ) )
							)
						) // </p>
					) // </div>
				) // </fieldset>
			) // </form>
			.append(
				$( '<p>' )
				.attr( 'id', 'userwarn-result' )
			); // </div>
 
			var contentForm = $( $form ).appendTo( '#content' );
			contentForm.dialog( {
				title: '<img src="//upload.wikimedia.org/wikipedia/commons/thumb/b/ba/Symbol_opinion_vote.svg/200px-Symbol_opinion_vote.svg.png" width="20" alt="!" title="!" style="margin-right: 1.5em;">' + mw.msg( 'userwarn' ),
				autoOpen: false,
				modal: true,
				width: 500,
				buttons: [ {
					id: 'userwarn-button-userwarn',
					text: mw.msg( 'userwarn' ),
					click: douserwarn
				}, {
					id: 'userwarn-button-close',
					text: mw.msg( 'close' ),
					click: function() {
						$( '#userwarn' ).dialog( 'close' );
					}
				} ]
			} );
 
			var portletLink = mw.util.addPortletLink( 'p-tb', '#', mw.msg( 'userwarn' ), 't-userwarn', mw.msg( 'userwarn' ) );
			$( portletLink ).click( function() {
				$( '#userwarn' ).dialog( 'open' );
				return false;
			} );
		} );
	}
 
	function douserwarn() {
		createSpinner();
		var warning = $( '#userwarn-warning' ).val();
		var furtherarguments = $( '#userwarn-furtherarguments' ).val();
		var signature = $( '#userwarn-signature' ).prop( 'checked' ) === true;
		var watch = $( '#userwarn-watch' ).prop( 'checked' ) === true;
		if( warning === 'none' ) {
			showError( mw.msg( 'error-nowarning' ) );
		}
		else {
			userwarn( user, warning, furtherarguments, signature, watch );
		}
	}
 
	function createSpinner() {
		$( '#userwarn-result' ).html( '<div id="userwarn-spinner" />' );
		$.createSpinner( {
			size: 'large',
			type: 'block',
			id: 'userwarn-spinner'
		} ).replaceAll( '#userwarn-spinner' );
	}
 
	function success() {
		window.location.href = mw.util.getUrl( 'User talk:' + user ) + '?userwarn=success';
	}
 
	function showError( error ) {
		$( '#userwarn-result' ).html(
			$( '<p>' )
			.attr( 'class', 'error' )
			.html( error )
		);
	}
 
	function userwarn( user, warning, furtherarguments, signature, watch ) {
		var api = new mw.Api();
		api.post( {
			'format': 'json',
			'action': 'edit',
			'title': 'User talk:' + user,
			'summary': warning,
			'appendtext': '\n{{subst:' + warning + '|' + furtherarguments + '}} ' + ( signature ? '--~~~~' : '' ) + '\n',
			'watchlist': ( watch ? 'watch' : 'preferences' ),
			'token': mw.user.tokens.get( 'csrfToken' )
		} )
		.done( function( data ) {
			if( data.error && data.error.info ) {
				showError( data.error.info );
			} else {
				success();
			}
		} );
	}
 
	$( document ).ready( init );
 
} )( mediaWiki, jQuery );
// </nowiki>