Χρήστης:Geraki/warn.js

Από τη Βικιπαίδεια, την ελεύθερη εγκυκλοπαίδεια

Μετά την αποθήκευση πρέπει να καθαρίσετε την κρυφή μνήμη (cache) του browser σας για να δείτε τις αλλαγές: Σε Chrome, Firefox, Safari, Internet Explorer και Edge: Κρατήστε πατημένο το Shift και κάντε κλικ στο κουμπί Ανανέωση στην μπάρα εργαλείων.

/**
 * Adds a dialog to any user's talk page where you can select a template that should be added to the talk page.
 */

// <nowiki>
( function( mw, $ ) {
	
	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',
			'error-nowarning':	'You have to select a warning.',
			'doc-none':		'', // DO NOT TRANSLATE
			'doc-Uw-link-removal1':	'This template is used to politely inform a new user that they have removed a sitelink without adding it to another item.', // DO NOT TRANSLATE
			'doc-Uw-link-removal2':	'doc-link-removal2', // DO NOT TRANSLATE
			'doc-Uw-link-removal3':	'doc-link-removal3', // DO NOT TRANSLATE
			'doc-Uw-link-removal4':	'doc-link-removal4', // DO NOT TRANSLATE
			'doc-Uw-vandalism1':	'doc-vandalism1', // DO NOT TRANSLATE
			'doc-Uw-vandalism2':	'doc-vandalism2', // DO NOT TRANSLATE
			'doc-Uw-vandalism3':	'doc-vandalism3', // DO NOT TRANSLATE
			'doc-Uw-vandalism4':	'doc-vandalism4', // DO NOT TRANSLATE
			'doc-welcome':		'Adds the welcome template to the user\'s talk page.' // DO NOT TRANSLATE
		} );
		break;
        case 'el':
                mw.messages.set( {
                        'userwarn':             'Ειδοποίηση χρήστη',
                        'close':                'Κλείσιμο',
                        'intro':                'Αυτό το εργαλείο θα προσθέσει κάποιο από τα ακολουθα πρότυπα στον πάτο της σελίδας συζήτησης αυτού του χρήστη.',
                        'warning':              'Ειδοποίηση',
                        'selectwarning':        'Διάλεξε πρότυπο ειδοποίησης',
                        'success':              'Ο χρήστης ειδοποιήθηκε με επιτυχία.',
                        'options':              'Εμφάνιση περισσότερων επιλογών',
                        'furtherarguments':     'Εισάγετε επιπλέον παραμέτρους (χωρισμένες με <code>|</code>) που θα προστεθούν στο πρότυπο:',
                        'signature':            'Αυτόματη προσθήκη υπογραφής μετά το πρότυπο',
                        'error-nowarning':      'Πρέπει να επιλέξετε μια ειδοποίηση.',
                        'doc-none':             '', // DO NOT TRANSLATE
                        'doc-Uw-link-removal1': 'This template is used to politely inform a new user that they have removed a sitelink without adding it to another item.', // DO NOT TRANSLATE
                        'doc-Uw-link-removal2': 'doc-link-removal2', // DO NOT TRANSLATE
                        'doc-Uw-link-removal3': 'doc-link-removal3', // DO NOT TRANSLATE
                        'doc-Uw-link-removal4': 'doc-link-removal4', // DO NOT TRANSLATE
                        'doc-Uw-vandalism1':    'doc-vandalism1', // DO NOT TRANSLATE
                        'doc-Uw-vandalism2':    'doc-vandalism2', // DO NOT TRANSLATE
                        'doc-Uw-vandalism3':    'doc-vandalism3', // DO NOT TRANSLATE
                        'doc-Uw-vandalism4':    'doc-vandalism4', // DO NOT TRANSLATE
                        'doc-welcome':          'Adds the welcome template to the user\'s talk page.' // DO NOT TRANSLATE
                } );
		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( '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>
					) // </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;
		if( warning === 'none' ) {
			showError( mw.msg( 'error-nowarning' ) );
		}
		else {
			userwarn( user, warning, furtherarguments, signature );
		}
	}
 
	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 ) {
		$.ajax( {
			type: 'POST',
  			url: mw.util.wikiScript( 'api' ),
			data: {
				'format': 'json',
				'action': 'edit',
				'title': 'User talk:' + user,
				'summary': warning,
				'appendtext': '\n{{subst:' + warning + '|' + furtherarguments + '}} ' + ( signature ? '--~~~~' : '' ) + '\n',
				'token': mw.user.tokens.get( 'csrfToken' )
			},
			success: function( data ) {
				if( data.error && data.error.info ) {
					showError( data.error.info );
				} else {
					success();
				}
			}
		} );
	}
	
	$( document ).ready( init );
} )( mediaWiki, jQuery );
// </nowiki>