Hoppa till innehållet

MediaWiki:Gadget-rcexcludeuser.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)
  • Edge Håll ned Ctrl och klicka på Uppdatera eller tryck Ctrl-F5.
  • Opera: Tryck Ctrl-F5.
( function() {
	'use strict';
	var key = 'gadget-rcexcludeuser-excludedusers';
	var v = mw.config.get();
	var hideClass = 'gadget-rcexcludeuser-hidden';
	function removeUnsafeCharacters( str ) {
		var unsafeCharacters = /[\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/;
		return str.replace( unsafeCharacters, '' );
	}
	function getExcludedUsers() {
		var output = mw.storage.get( key );
		if ( output ) {
			output = output.trim();
			if ( output ) {
				output = removeUnsafeCharacters( output );
				return output.split( '\n' );
			}
		}
		return [];
	}
	function hideExcludedUsers() {
		var excludedusers = getExcludedUsers();
		$( '.mw-changeslist' ).children( '.special' ).children( 'li' ).each( function( i, e ) {
			$( e ).removeClass( hideClass );
			var user = $( e ).find( '.mw-userlink' ).first().text();
			if ( excludedusers.includes( user ) ) {
				$( e ).addClass( hideClass );
			}
		} );
	}
	$( function() {
		var buttontitle = 'Hide actions from users you trust';
		var buttontext = 'Hide users';
		var label = 'Exclude users (separate with newline)';
		if ( v.wgUserLanguage === 'sv' ) {
			buttontitle = 'Dölj handlingar från användare du litar på';
			buttontext = 'Dölj användare';
			label = 'Exkludera användare (separera med radbrytning)';
		}
		if ( $( '.mw-changeslist' ).length ) {
			$( '#firstHeading' ).append( '<button type="button" title="' + buttontitle + '" id="gadget-rcexcludeuser-button">' + buttontext + '</button>' );
			$( '#gadget-rcexcludeuser-button' ).click( function() {
				var settingsstr = '<div>';
				settingsstr += '<p><label for="gadget-rcexcludeuser-textarea">' + label + '</label></p>';
				settingsstr += '<textarea id="gadget-rcexcludeuser-textarea"></textarea>';
				settingsstr += '</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 );
				}
				var myDialog;
				var windowManager;
				if ( $( '.oo-ui-window-active' ).length === 0 ) {
					OO.inheritClass( MyDialog, OO.ui.Dialog );

					// Specify a title statically (or, alternatively, with data passed to the opening() method).
					MyDialog.static.name = 'gadgetrcexcludeuserdialog';
					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( settingsstr );
						this.$body.append( this.content.$element );
						$( '#gadget-rcexcludeuser-textarea' ).val( ( getExcludedUsers() ).join( '\n' ) );
						$( '#gadget-rcexcludeuser-textarea' ).on( 'input', function( e ) {
							var input = $( e.currentTarget ).val().trim();
							if ( input ) {
								input = removeUnsafeCharacters( input );
								mw.storage.set( key, input );
							} else {
								mw.storage.remove( key );
							}
						} );
						$( '.oo-ui-windowManager' ).click( function( e ) {
							if ( !$( e.target ).parents().filter( '.oo-ui-window' ).length ) {
								myDialog.close();
							}
						} );
					};

					// Set up the ready mode of the window. 
					MyDialog.prototype.getReadyProcess = function ( data ) {
						return MyDialog.super.prototype.getReadyProcess.call( this, data )
						.next( function () {
							$( '#gadget-rcexcludeuser-textarea' ).focus();
						}, this );
					};

					// 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();
							hideExcludedUsers();
						}, this );
					};

					// Make the window.
					myDialog = new MyDialog( {
						classes: [
							'gadget-rcexcludeuser-dialog'
						]
					} );

					// 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 );
				}
			} );
			hideExcludedUsers();
		}
	} );
	mw.hook( 'wikipage.content' ).add( function( elements ) {
		if ( elements.hasClass( 'mw-changeslist' ) ) {
			hideExcludedUsers();
		}
	} );
}() );