MediaWiki:Gadget-EditsByUser.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.
if ( mw.config.get( 'wgAction' ) === 'history' ) {
	$( function() {
		'use strict';
		var text = 'By user';
		var title = 'Find edits by user';
		var labeluser = 'User';
		var titlelimit = 'Upper limit for how many edits should be fetched';
		var labellimit = 'Limit';
		var contbutton;
		var cont = 'Continue';
		var titlecont = 'Fetch more results';
		var search = 'Search';
		var error = 'No results were found';
		var URLParam = mw.util.getParamValue( 'editsbyuser' );
		if ( mw.config.get( 'wgUserLanguage' ) === 'sv' ) {
			text = 'Av användare';
			title = 'Hitta redigeringar av användare';
			labeluser = 'Användare';
			titlelimit = 'Övre gräns för hur många redigeringar som ska hämtas';
			labellimit = 'Gräns';
			cont = 'Fortsätt';
			titlecont = 'Hämta fler resultat';
			search = 'Sök';
			error = 'Inga resultat hittades';
		}
		contbutton = '<button type="button" id="gadget-editsbyuser-cont" title="' + titlecont + '">' + cont + '</button>';
		function openDialog( prefill ) {
			// 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 dialogstr = '<div id="gadget-editsbyuser-dialog">';
			dialogstr += '<label for="gadget-editsbyuser-user">' + labeluser + ':</label>';
			dialogstr += '<input id="gadget-editsbyuser-user" list="gadget-editsbyuser-users">';
			dialogstr += '<datalist id="gadget-editsbyuser-users"></datalist>';
			dialogstr += '<label for="gadget-editsbyuser-limit" title="' + titlelimit + '">' + labellimit + ':</label>';
			dialogstr += '<input id="gadget-editsbyuser-limit">';
			dialogstr += '<table id="gadget-editsbyuser-res" class="wikitable"><tbody id="gadget-editsbyuser-tbody"></tbody></table>';
			dialogstr += '<div id="gadget-editsbyuser-errorcontainer"></div>';
			dialogstr += '<div id="gadget-editsbyuser-contcontainer"></div>';
			dialogstr += '<button type="button" id="gadget-editsbyuser-search">' + search + '</button>';
			dialogstr += '</div>';
			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 = 'gadgeteditsbyuserdialog';
				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( dialogstr );
					this.$body.append( this.content.$element );
					var n = 0;
					function getEdits( start ) {
						var user = $( '#gadget-editsbyuser-user' ).val();
						var limit = $( '#gadget-editsbyuser-limit' ).val();
						var obj = {
							action: 'query',
							prop: 'revisions',
							titles: mw.config.get( 'wgPageName' ),
							rvprop: [
								'ids',
								'timestamp',
								'parsedcomment',
								'user'
							],
							formatversion: 2
						};
						var URLParams;
						if ( limit ) {
							limit = limit.trim();
							if ( limit ) {
								obj.rvlimit = limit;
							}
						}
						if ( start ) {
							obj.rvcontinue = start;
						} else {
							n = 0;
						}
						if ( user ) {
							user = user.trim();
							if ( user ) {
								obj.rvuser = user;
								if ( window.URLSearchParams ) {
									URLParams = ( new URLSearchParams( location.search ) );
									URLParams.set( 'editsbyuser', user );
									history.replaceState( null, null, '?' + URLParams + location.hash );
								}
								( new mw.Api() ).get( obj ).done( function( data ) {
									var query;
									var pages;
									var page;
									var revs;
									var trarr = [];
									var trstr;
									if ( data.query ) {
										query = data.query;
										if ( query.pages ) {
											pages = query.pages;
											if ( pages.length ) {
												page = pages[ 0 ];
												if ( page ) {
													revs = page.revisions;
													if ( revs ) {
														revs.forEach( function( rev ) {
															var tr;
															if ( rev.user === user ) {
																n += 1;
																tr = '<tr>';
																tr += '<td>';
																tr += n;
																tr += '</td>';
																tr += '<td>';
																if ( rev.revid ) {
																	tr += '<a href="/wiki/Special:Diff/' + rev.revid + '">' + rev.revid + '</a>';
																}
																tr += '</td>';
																tr += '<td>';
																if ( rev.timestamp ) {
																	tr += rev.timestamp;
																}
																tr += '</td>';
																tr += '<td>';
																if ( rev.parsedcomment ) {
																	tr += rev.parsedcomment;
																}
																tr += '</td>';
																tr += '</tr>';
																trarr.push( tr );
															}
														} );
														trstr = trarr.join( '' );
														$( '#gadget-editsbyuser-tbody' ).empty();
														$( '#gadget-editsbyuser-errorcontainer' ).empty();
														$( '#gadget-editsbyuser-tbody' ).append( trstr );
														$( '#gadget-editsbyuser-contcontainer' ).empty();
														if ( data.continue ) {
															if ( data.continue.rvcontinue ) {
																$( '#gadget-editsbyuser-contcontainer' ).append( contbutton );
																$( '#gadget-editsbyuser-cont' ).click( function() {
																	getEdits( data.continue.rvcontinue );
																} );
															}
														}
													} else {
														$( '#gadget-editsbyuser-tbody' ).empty();
														$( '#gadget-editsbyuser-errorcontainer' ).text( error );
													}
												}
											}
										}
									}
									myDialog.updateSize();
								} );
							}
						}
					}
					$( '#gadget-editsbyuser-search' ).click( function() {
						getEdits();
					} );
					if ( prefill ) {
						$( '#gadget-editsbyuser-user' ).val( prefill );
						$( '#gadget-editsbyuser-search' ).click();
					}
					$( '#gadget-editsbyuser-dialog input' ).keyup( function( e ) {
						if ( e.which === 13 ) {
							$( '#gadget-editsbyuser-search' ).trigger( 'click' );
						}
					} );
					$( '#gadget-editsbyuser-user' ).on( 'input', function( e ) {
						$( '#gadget-editsbyuser-users' ).empty();
						var user = $( e.currentTarget ).val();
						var obj;
						if ( user ) {
							user = user.trim();
							if ( user ) {
								obj = {
									action: 'query',
									list: 'allusers',
									aufrom: user,
									formatversion: 2
								};
								( new mw.Api() ).get( obj ).done( function( data ) {
									var query;
									var userobjs;
									var users;
									var options;
									var optionsstr;
									if ( data.query ) {
										query = data.query;
										if ( query.allusers ) {
											userobjs = query.allusers;
											if ( userobjs.length ) {
												users = userobjs.map( function( userobj ) {
													return userobj.name;
												} );
												options = users.map( function( user ) {
													return '<option value="' + mw.html.escape( user ) + '">';
												} );
												optionsstr = options.join( '' );
												$( '#gadget-editsbyuser-users' ).empty();
												$( '#gadget-editsbyuser-users' ).append( optionsstr );
											}
										}
									}
								} );
							}
						}
					} );
					$( '.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-editsbyuser-user' ).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();
						$( 'body' ).attr( 'style', '' );
						var URLParams;
						if ( window.URLSearchParams ) {
							URLParams = ( new URLSearchParams( location.search ) );
							URLParams.delete( 'editsbyuser' );
							history.replaceState( null, null, '?' + URLParams + location.hash );
						}
					}, this );
				};

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

				// Create and append a window manager, which will open and close the window.
				var 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 );
			}
		}
		if ( URLParam ) {
			openDialog( URLParam );
		}
		$( '#contentSub' ).prepend( '<button type="button" title="' + title + '" id="gadget-editsbyuser">' + text + '</button>' );
		$( '#gadget-editsbyuser' ).click( function() {
			openDialog();
		} );
	} );
}