MediaWiki:Gadget-ValValidering.js

Från Wikipedia

OBS: Efter du har publicerat sidan kan du behöva tömma din webbläsares cache för att se ändringarna.

  • Firefox / Safari: Håll ned Skift och klicka på Uppdatera sidan eller tryck Ctrl-F5 eller Ctrl-R (⌘-R på Mac)
  • Google Chrome: Tryck Ctrl-Skift-R (⌘-Skift-R på Mac)
  • Internet Explorer / Edge: Håll ned Ctrl och klicka på Uppdatera eller tryck Ctrl-F5
  • Opera: Tryck Ctrl-F5.
$( function () {
	'use strict';
	var text = 'Validate';
	var tooltip = 'Look for double votes';
	var close = 'Close';
	var nofound = 'No double votes found.';
	var found = 'Double votes found!';
	if ( mw.config.get( 'wgUserLanguage' ) === 'sv' ) {
		text = 'Validera';
		tooltip = 'Leta efter dubbelröster';
		close = 'Stäng';
		nofound = 'Inga dubbelröster hittades.';
		found = 'Dubbelröster hittades!';
	}
	if ( String.prototype.startsWith ) {
		if ( mw.config.get( 'wgPageName' ).startsWith( 'Wikipedia:Ansökan_om_administrativ_behörighet/' ) && $( '#wpTextbox1' ).length ) {
			mw.util.addPortletLink(
				'p-cactions',
				'#',
				text,
				'ca-valvalidering',
				tooltip
			);
			$( '#ca-valvalidering' ).click( function ( e ) {
				e.preventDefault();
				var wikitext = $( '#wpTextbox1' ).val();
				var lines;
				var msg = nofound;
				var list = [];
				var names = [];
				var names2 = [];
				var dupes = [];
				var htmllist;
				lines = wikitext.split( /\n/ );
				lines.forEach( function ( line ) {
					if ( line.startsWith( '#' ) && !line.startsWith( '#*' ) && !line.startsWith( '#:' ) ) {
						list.push( line );
					}
				} );
				list.forEach( function ( elem ) {
					elem = elem.replace( /^#[^\[\]\n]*\[\[(?:[Aa]nvändare|[Uu]ser):([^\[\]\|:\n]+)(?:[^\n]+)/, '$1' );
					names.push( elem );
				} );
				names.forEach( function( name ) {
					if ( names2.includes( name ) ) {
						dupes.push( name );
					}
					names2.push( name );
				} );
				if ( dupes.length ) {
					htmllist = dupes.map( function( dupe ) {
						return '<li>' + dupe + '</li>';
					} );
					msg = found;
					msg += '<ol>';
					msg += htmllist.join( '' );
					msg += '</ol>';
				}
				msg += '<div><button id="gadget-valvalidering-close">' + close + '</button></div>';

				// Creating and opening a simple dialog window.

				// Subclass Dialog class. Note that the OOjs inheritClass() method extends the parent constructor's prototype and static methods and properties to the child constructor.

				function MyDialog( config ) {
					MyDialog.super.call( this, config );
				}

				// Declare early for use in MyDialog.prototype methods
				var myDialog;
				var windowManager;

				OO.inheritClass( MyDialog, OO.ui.Dialog );

				// Specify a title statically (or, alternatively, with data passed to the opening() method).
				MyDialog.static.name = 'gadgetvalvalideringdialog';
				MyDialog.static.title = 'Simple dialog';

				// Customize the initialize() function: This is where to add content to the dialog body and set up event handlers.
				MyDialog.prototype.initialize = function () {
					// Call the parent method
					MyDialog.super.prototype.initialize.call( this );
					// Create and append a layout and some content.
					this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } );
					this.content.$element.append( msg );
					this.$body.append( this.content.$element );
					$( '#gadget-valvalidering-close' ).click( function() {
						myDialog.close();
					} );
				};

				// Use the getTeardownProcess() method to perform actions whenever the dialog is closed.
				// This method provides access to data passed into the window's close() method
				// or the window manager's closeWindow() method.
				MyDialog.prototype.getTeardownProcess = function ( data ) {
					return MyDialog.super.prototype.getTeardownProcess.call( this, data )
					.first( function () {
						// Perform any cleanup as needed
						$( '.oo-ui-windowManager' ).remove();
					}, this );
				};

				// Make the window.
				myDialog = new MyDialog();

				// Create and append a window manager, which will open and close the window.
				windowManager = new OO.ui.WindowManager();
				$( 'body' ).append( windowManager.$element );

				// Add the window to the window manager using the addWindows() method.
				windowManager.addWindows( [ myDialog ] );

				// Open the window!
				windowManager.openWindow( myDialog );
			} );
		}
	}
} );