Wikipedista:Matěj Suchánek/userwarn.js

Poznámka: Po uložení musíte vyprázdnit mezipaměť vašeho prohlížeče, jinak změny neuvidíte.

/**
 * Adds a dialog to any user's talk page where you can select a template that should be added to the talk page.
 *
 * @author [[d:User:Bene*]]
 */

// <nowiki>
( function( mw, $ ) {
	'use strict';

	if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'Contributions' && mw.config.get( 'wgNamespaceNumber' ) !== 3 ) { // user talk and Special:Contributions only
		return;
	}

	var user;

	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',
		'watch':			'Přidat uživatelovu diskusní stránku do sledovaných',
		'error-nowarning':	'Musíte zvolit varování.'
	} );

	if ( mw.util.getParamValue( 'userwarn' ) === 'success' ) {
		mw.notify( mw.msg( 'success' ) );
	}

	function init() {
		// Add dialog
		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( 'Vítejte' )
						)
						.append(
							$( '<option>' )
							.text( 'Pískoviště' )
						)
						.append(
							$( '<option>' )
							.text( 'Experimenty' )
						)
						.append(
							$( '<option>' )
							.text( 'Experimenty2' )
						)
						.append(
							$( '<option>' )
							.text( 'Experimenty3' )
						)
						.append(
							$( '<option>' )
							.text( 'Experimenty0' )
						)
						.append(
							$( '<option>' )
							.text( 'Experimenty0B' )
						)
						.append(
							$( '<option>' )
							.text( 'Experimenty2o' )
						)
						.append(
							$( '<option>' )
							.text( 'Experimenty3o' )
						)
						.append(
							$( '<option>' )
							.text( 'Experimenty-u' )
						)
						.append(
							$( '<option>' )
							.text( 'Experimenty2u' )
						)
						.append(
							$( '<option>' )
							.text( 'Experimenty3u' )
						)
						.append(
							$( '<option>' )
							.text( 'KomentářPOV' )
						)
						.append(
							$( '<option>' )
							.text( 'KomentářPOV2' )
						)
						.change( function() {
							$( '#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' );
				}
			} ]
		} );

		mw.util.addCSS( '#userwarn a { color: #007EDF; } #userwarn-result { margin: 20px; }' );

		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.postWithEditToken( {
			'action': 'edit',
			'notminor': '',
			'title': 'User talk:' + user,
			'summary': warning,
			'appendtext': '\n\n{{subst:' + warning + '|' + furtherarguments + '}} ' + ( signature ? '--~~~~' : '' ) + '\n',
			'watchlist': ( watch ? 'watch' : 'preferences' ),
		} )
		.done( function( data ) {
			if ( data.error && data.error.info ) {
				showError( data.error.info );
			} else {
				success();
			}
		} );
	}

	$.when(
		$.ready,
		mw.loader.using( [
			'jquery.ui', 'jquery.spinner', 'mediawiki.api',
			'mediawiki.util'
		] )
		.done( function () {
			user = mw.util.getParamValue( 'target' ) || mw.config.get( 'wgRelevantUserName' );
		} )
	).done( init );

} )( mediaWiki, jQuery );
// </nowiki>