Hoppa till innehållet

MediaWiki:Gadget-ReportProblem.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.
//<nowiki>
$( function() {
	'use strict';
	var v = mw.config.get();
	var pagetitle = v.wgPageName.replace( /_/g, ' ' );
	var enc = mw.util.wikiUrlencode;
	var esc = mw.html.escape;
	var tagName = 'reportproblem';
	var javascripterrors = [];
	window.onerror = function( msg ) {
		javascripterrors.push( msg );
	};
	var articlepage = 'Wikipedia:Anmäl ett fel';
	var technicalpage = 'Wikipedia:Wikipediafrågor';
	var buttontext = 'Report a problem';
	var buttontooltip = 'Report a factual error or a technical error';
	var typeofproblem = 'What type of problem would you like to report?';
	var articletext = 'Factual error or problem in article';
	var technicaltext = 'Bug or technical problem';
	var closetext = 'Close';
	var closetitle = 'Close the dialog window';
	var reportarticleproblem = 'Report a problem in an article';
	var articlelabel = 'Describe the problem in this article as closely as possible';
	var technicallabel = 'Describe the technical problem as closely as possible';
	var technicalbrowserlabel = 'What web browser are you using?';
	var submitbuttontext = 'Submit';
	var warning = 'The error report is public and any reply from volunteers will be posted on this web site (' + v.wgServerName + '). Do not include your name or e-mail address.';
	var ipwarning = 'Because you are not logged in, your IP address will be used in place of a user name.';
	var errorreportsentto = 'Error report sent to ';
	var emptydescription = 'The report is missing a description of the problem';
	var technicalwarning = 'By submitting a technical error report, you agree to publicizing skin, action, namespace as well as any special page and JavaScript errors.';
	var emptydescriptionorbrowser = 'The report needs to contain information about which web browser you are using, as well as a description of the problem';
	var skinheading = 'Skin: ';
	var actionheading = 'Action: ';
	var namespaceheading = 'Namespace: ';
	var specialpageheading = 'Special page: ';
	var javascripterrorheading = 'JavaScript error: ';
	var technicalproblemheading = 'Technical problem ';
	var browserheading = 'Browser: ';
	var descriptionheading = 'Description: ';
	var tagsobj = {
		action: 'query',
		list: 'tags',
		tglimit: 'max',
		tgprop: [
			'active',
			'defined',
			'source'
		],
		formatversion: 2
	};
	function hasTag( tagobj ) {
		return tagobj.name === tagName && tagobj.defined && tagobj.active && tagobj.source.includes( 'manual' );
	}
	function incompletearticlereport() {
		$( '#gadget-reportproblem-statuscontainer' ).addClass( 'gadget-reportproblem-fail' ).append( emptydescription );
	}
	function incompletetechnicalreport() {
		$( '#gadget-reportproblem-statuscontainer' ).addClass( 'gadget-reportproblem-fail' ).append( emptydescriptionorbrowser );
	}
	if ( String.prototype.endsWith && Array.prototype.includes ) {
		if ( v.wgUserLanguage === 'sv' ) {
			buttontext = 'Rapportera problem';
			buttontooltip = 'Rapportera ett faktafel eller tekniskt fel';
			typeofproblem = 'Vilken typ av problem vill du rapportera?';
			articletext = 'Faktafel eller problem i artikel';
			technicaltext = 'Bugg eller tekniskt problem';
			closetext = 'Stäng';
			closetitle = 'Stäng dialogrutan';
			reportarticleproblem = 'Rapportera problem i artikel';
			articlelabel = 'Beskriv problemet i den här artikeln så noggrant du kan';
			technicallabel = 'Beskriv det tekniska problemet så noggrant du kan';
			technicalbrowserlabel = 'Vilken webbläsare använder du?';
			submitbuttontext = 'Skicka in';
			warning = 'Felrapporten är offentlig och eventuellt svar från frivilliga bidragsgivare kommer att ske på den här webbplatsen (' + v.wgServerName + '). Inkludera inte ditt namn eller din e-postadress.';
			ipwarning = 'Eftersom du inte är inloggad kommer din IP-adress användas i stället för ett användarnamn.';
			errorreportsentto = 'Felrapport skickad till ';
			emptydescription = 'Rapporten saknar en beskrivning av problemet';
			technicalwarning = 'Genom att skicka in en teknisk felrapport godkänner du att skin, handling, namnrymd samt eventuell specialsida och JavaScript-fel offentliggörs.';
			emptydescriptionorbrowser = 'Rapporten måste innehålla information om vilken webbläsare du använder, samt en beskrivning av problemet';
		}
		if ( v.wgContentLanguage === 'sv' ) {
			skinheading = 'Skin: ';
			actionheading = 'Handling: ';
			namespaceheading = 'Namnrymd: ';
			specialpageheading = 'Specialsida: ';
			javascripterrorheading = 'JavaScript-fel: ';
			technicalproblemheading = 'Tekniskt problem ';
			browserheading = 'Webbläsare: ';
			descriptionheading = 'Beskrivning: ';
		}
		mw.util.addPortletLink(
			'p-tb',
			'#',
			buttontext,
			'gadget-reportproblem',
			buttontooltip
		);
		$( '#gadget-reportproblem' ).find( 'a' ).click( function( e ) {
			e.preventDefault();
			var dialog;
			// 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 );
			}
			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 = 'gadgetreportproblemdialog';
				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 () {
					var str = '<div id="gadget-reportproblem-dialog">';
					str += '<p id="gadget-reportproblem-typeofproblem">' + typeofproblem + '</p>';
					if ( v.wgNamespaceNumber === 0 ) {
						str += '<button id="gadget-reportproblem-article">' + articletext + '</button>';
					}
					str += '<button id="gadget-reportproblem-technical">' + technicaltext + '</button>';
					str += '<button id="gadget-reportproblem-close" title="' + closetitle + '">' + closetext + '</button>';
					str += '</div>';
					// 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( str );
					this.$body.append( this.content.$element );
					dialog = $( '#gadget-reportproblem-dialog' );
				};

				// 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.
				var 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 );
				$( '#gadget-reportproblem-article' ).click( function() {
					dialog.html(
						'<h2>' + pagetitle + '</h2>' +
						'<p><label id="gadget-reportproblem-article-label" for="gadget-reportproblem-article-textarea">' + articlelabel + '</label></p>' +
						'<textarea id="gadget-reportproblem-article-textarea"></textarea>' +
						'<p id="gadget-reportproblem-warning">' + warning + '</p>'
					);
					$( '#gadget-reportproblem-article-textarea' ).focus();
					if ( v.wgUserName === null ) {
						dialog.append(
							'<p id="gadget-reportproblem-ipwarning">' + ipwarning + '</p>'
						);
					}
					dialog.append(
						'<p id="gadget-reportproblem-statuscontainer"></p>' +
						'<button id="gadget-reportproblem-article-submit">' + submitbuttontext + '</button>' +
						'<button id="gadget-reportproblem-close" title="' + closetitle + '">' + closetext + '</button>'
					);
					myDialog.updateSize();
					$( '#gadget-reportproblem-article-submit' ).click( function( ev ) {
						$( '#gadget-reportproblem-statuscontainer' ).empty().removeClass();
						var userinput = $( '#gadget-reportproblem-article-textarea' ).val();
						var editobj = {
							action: 'edit',
							title: articlepage,
							section: 'new',
							sectiontitle: '[[' + pagetitle + ']]',
							formatversion: 2	
						};
						function edit() {
							( new mw.Api() ).postWithToken( 'csrf', editobj ).done( function( result, jqXHR ) {
								$( '#gadget-reportproblem-statuscontainer' ).addClass( 'gadget-reportproblem-success' ).append( errorreportsentto + '<a href="' + v.wgServer + '/wiki/' + esc( enc( articlepage ) ) + '">' + articlepage + '</a>' );
								$( ev.currentTarget ).prop( 'disabled', true );
								myDialog.updateSize();
							} ).fail( function( code, editres ) {
								$( '#gadget-reportproblem-statuscontainer' ).addClass( 'gadget-reportproblem-fail' ).append( code );
								myDialog.updateSize();
							} );
						}
						if ( userinput ) {
							userinput = userinput.trim();
							if ( userinput ) {
								if ( !userinput.endsWith( '~~~~' ) ) {
									userinput += ' ~~~~';
								}
								editobj.appendtext = userinput;
								( new mw.Api() ).get( tagsobj ).done( function( tagsres ) {
									var query = tagsres.query;
									var tags;
									if ( query ) {
										tags = query.tags;
										if ( tags && Array.isArray( tags ) ) {
											if ( tags.some( hasTag ) ) {
												editobj.tags = tagName;
											}
										}
									}
									edit();
								} ).fail( function() {
									edit();
								} );
							} else {
								incompletearticlereport();
							}
						} else {
							incompletearticlereport();
						}
					} );
				} );
				$( '#gadget-reportproblem-technical' ).click( function() {
					dialog.html(
						'<p><label id="gadget-reportproblem-technical-browser-label" for="gadget-reportproblem-technical-input">' + technicalbrowserlabel + '</label></p>' +
						'<input id="gadget-reportproblem-technical-input" list="gadget-reportproblem-datalist">' +
						'<datalist id="gadget-reportproblem-datalist"><option value="Chrome"><option value="Edge"><option value="Firefox"><option value="Opera"><option value="Safari"></datalist>' +
						'<p><label id="gadget-reportproblem-technical-label" for="gadget-reportproblem-technical-textarea">' + technicallabel + '</label></p>' +
						'<textarea id="gadget-reportproblem-technical-textarea"></textarea>' +
						'<p id="gadget-reportproblem-warning">' + warning + '</p>' +
						'<p id="gadget-reportproblem-technicalwarning">' + technicalwarning + '</p>'
					);
					$( '#gadget-reportproblem-technical-input' ).focus();
					if ( v.wgUserName === null ) {
						dialog.append(
							'<p id="gadget-reportproblem-ipwarning">' + ipwarning + '</p>'
						);
					}
					dialog.append(
						'<p id="gadget-reportproblem-statuscontainer"></p>' +
						'<button id="gadget-reportproblem-technical-submit">' + submitbuttontext + '</button>' +
						'<button id="gadget-reportproblem-close" title="' + closetitle + '">' + closetext + '</button>'
					);
					myDialog.updateSize();
					$( '#gadget-reportproblem-technical-submit' ).click( function( ev ) {
						$( '#gadget-reportproblem-statuscontainer' ).empty().removeClass();
						var reporttext = skinheading + v.skin + '\n\n' + actionheading + v.wgAction + '\n\n' + namespaceheading + v.wgNamespaceNumber;
						if ( v.wgNamespaceNumber === -1 ) {
							reporttext += '\n\n' + specialpageheading + v.wgCanonicalSpecialPageName;
						}
						javascripterrors.forEach( function( msg ) {
							reporttext += '\n\n' + javascripterrorheading + msg;
						} );
						var userinputbrowser = $( '#gadget-reportproblem-technical-input' ).val();
						var userinputdescription = $( '#gadget-reportproblem-technical-textarea' ).val();
						var editobj = {
							action: 'edit',
							title: technicalpage,
							section: 'new',
							sectiontitle: technicalproblemheading,
							formatversion: 2	
						};
						function edit() {
							( new mw.Api() ).postWithToken( 'csrf', editobj ).done( function( result, jqXHR ) {
								$( '#gadget-reportproblem-statuscontainer' ).addClass( 'gadget-reportproblem-success' ).append( errorreportsentto + '<a href="' + v.wgServer + '/wiki/' + esc( enc( technicalpage ) ) + '">' + technicalpage + '</a>' );
								$( ev.currentTarget ).prop( 'disabled', true );
								myDialog.updateSize();
							} ).fail( function( code, editres ) {
								$( '#gadget-reportproblem-statuscontainer' ).addClass( 'gadget-reportproblem-fail' ).append( code );
								myDialog.updateSize();
							} );
						}
						function getTime() {
							( new mw.Api() ).get( {
								action: 'parse',
								text: '{{#timel:Y-m-d H:i}}',
								prop: [
									'text',
									'parsewarnings'
								],
								contentmodel: 'wikitext',
								formatversion: 2
							} ).done( function( parsedata ) {
								if (
									parsedata &&
									parsedata.parse &&
									parsedata.parse.text &&
									$( parsedata.parse.text ).find( 'p' ).text().trim()
								) {
									editobj.sectiontitle += $( parsedata.parse.text ).find( 'p' ).text().trim();
								}
								edit();
							} ).fail( function( code, parseres ) {
								editobj.sectiontitle += '{{subst:#timel:Y-m-d H:i}}';
								edit();
							} );
						}
						if ( userinputbrowser && userinputdescription ) {
							userinputbrowser = userinputbrowser.trim();
							userinputdescription = userinputdescription.trim();
							if ( userinputbrowser && userinputdescription ) {
								reporttext += '\n\n' + browserheading + userinputbrowser;
								if ( !userinputdescription.endsWith( '~~~~' ) ) {
									userinputdescription += ' ~~~~';
								}
								reporttext += '\n\n' + descriptionheading + userinputdescription;
								editobj.appendtext = reporttext;
								( new mw.Api() ).get( tagsobj ).done( function( tagsres ) {
									var query = tagsres.query;
									var tags;
									if ( query ) {
										tags = query.tags;
										if ( tags && Array.isArray( tags ) ) {
											if ( tags.some( hasTag ) ) {
												editobj.tags = tagName;
											}
										}
									}
									getTime();
								} ).fail( function() {
									getTime();
								} );
							} else {
								incompletetechnicalreport();
							}
						} else {
							incompletetechnicalreport();
						}
					} );
				} );
				dialog.on( 'click', '#gadget-reportproblem-close', function() {
					myDialog.close();
				} );
			}
		} );
	}
} );
//</nowiki>