MediaWiki:Gadget-GratisdataDescription.js

$( function {	'use strict';	var iwapi = new mw.ForeignApi( 'https://gratisdata.miraheze.org/w/api.php' );	var v = mw.config.get;	var blockmsg = 'The user or IP address is blocked.';	var title = 'Description from Gratisdata';	var buttontextadd = 'Add Gratisdata description';	var buttonchangeImage = new Image(12, 12);	buttonchangeImage.src = 'https://upload.wikimedia.org/wikipedia/commons/6/63/Arbcom_ru_editing.svg';	var buttontitlechange = 'Change Gratisdata description';	var label = 'Gratisdata description';	var submitbuttontext = 'Save';	var recentchangestext = 'The latest descriptions changed with this gadget can be viewed ';	var recentchangeslinktext = 'here';	var tagName = v.wgDBname + '-DescriptionGadget';	if ( v.wgUserLanguage === 'sv' ) {		blockmsg = 'Användaren eller IP-adressen är blockerad.';		title = 'Beskrivning från Gratisdata';		buttontextadd = 'Lägg till Gratisdata-beskrivning'; buttontitlechange = 'Ändra Gratisdata-beskrivning'; label = 'Gratisdata-beskrivning'; submitbuttontext = 'Spara'; recentchangestext = 'De senaste beskrivningarna ändrade med den här finessen kan visas '; recentchangeslinktext = 'här'; }	function getDescription { function openDialog( prefill ) { var dialogstr = ' '; dialogstr += ' ' + label + ' '; dialogstr += ''; dialogstr += '' + submitbuttontext + ' '; dialogstr += ' ' + recentchangestext + '' + recentchangeslinktext + '. ';			dialogstr += ' ';

// 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 = 'gadgetgratisdatadescriptiondialog'; 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 ); if ( prefill ) { $( '#gadget-gratisdatadescription-input' ).val( prefill ); }					$( '#gadget-gratisdatadescription-submit' ).click( function {						var editobj = {							action: 'wbsetdescription',							id: v.wgWikibaseItemId,							language: v.wgContentLanguage,							value: $( '#gadget-gratisdatadescription-input' ).val,							formatversion: 2							};						function edit {							( new mw.Api ).get( { list: 'blocks', bkusers: mw.config.get( 'wgUserName' ), formatversion: 2 } ).done( function( blockdata ) { if ( blockdata && blockdata.query && blockdata.query.blocks && blockdata.query.blocks.length ) { mw.notify( blockmsg, {										type: 'error',										tag: 'gadget-gratisdatadescription-blocked'									} ); } else { iwapi.postWithToken( 'csrf', editobj ).done( function {										getDescription;										myDialog.close;									} ).fail( function( code, editres ) {										mw.notify( code, { type: 'error', tag: 'gadget-gratisdatadescription' } );									} );								}							} );						}						iwapi.get( { action: 'query', list: 'tags', tglimit: 'max', tgprop: [ 'active', 'defined', 'source' ],							formatversion: 2 } ).done( function( tagsres ) { var query = tagsres.query; var tags; function hasTag( tagobj ) { return tagobj.name === tagName && tagobj.defined && tagobj.active && tagobj.source.indexOf( 'manual' ) !== -1; }							if ( query ) { tags = query.tags; if ( tags && Array.isArray( tags ) ) { if ( tags.some( hasTag ) ) { editobj.tags = tagName; }								}							}							edit; } ).fail( function { edit; } );					} );					$( '#gadget-gratisdatadescription-input' ).keyup( function( e ) {						if ( e.which === 13 ) {							$( '#gadget-gratisdatadescription-submit' ).trigger( 'click' );						}					} ); $( '.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-gratisdatadescription-input' ).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;					}, this ); };

// Make the window. myDialog = new MyDialog( {					classes: [						'gadget-gratisdatadescription-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 ); }		}		iwapi.get( {			action: 'wbgetentities',			ids: v.wgWikibaseItemId,			props: 'descriptions',			languages: v.wgContentLanguage,			formatversion: 2		} ).done( function( data ) {			var entitiesobj = data.entities;			var entityobj;			var descriptionsobj;			var descriptionobj;			var text;			$( '#gadget-gratisdatadescription-container' ).remove;			if ( entitiesobj ) {				entityobj = entitiesobj[ v.wgWikibaseItemId ];				if ( entityobj ) {					descriptionsobj = entityobj.descriptions;					if ( descriptionsobj ) {						descriptionobj = descriptionsobj[ v.wgContentLanguage ];						if ( descriptionobj ) {							text = descriptionobj.value;							if ( text ) {								$( '#contentSub' ).append( $( ' ' ).attr( 'id', 'gadget-gratisdatadescription-container' ) );								$( '#gadget-gratisdatadescription-container' ).append( $( ' ' ).attr( {										id: 'gadget-gratisdatadescription',										title: title									} ).text( text ) );								$( '#gadget-gratisdatadescription-container' ).append( $( ' ' ).attr( 'id', 'gadget-gratisdatadescription-buttoncontainer' ) );								$( '#gadget-gratisdatadescription-buttoncontainer' ).append( $( '' ).attr( {										href: 'https://gratisdata.miraheze.org/wiki/' + v.wgWikibaseItemId,										id: 'gadget-gratisdatadescription-button',										title: buttontitlechange									} ).append( buttonchangeImage ) );								$( '#gadget-gratisdatadescription-button' ).click( function( e ) { e.preventDefault; openDialog( text ); } );							}						} else {							$( '#contentSub' ).append( $( ' ' ).attr( 'id', 'gadget-gratisdatadescription-container' ) );							$( '#gadget-gratisdatadescription-container' ).append( $( '' ).attr( {									href: 'https://gratisdata.miraheze.org/wiki/' + v.wgWikibaseItemId,									id: 'gadget-gratisdatadescription-button'								} ).text( buttontextadd ) );							$( '#gadget-gratisdatadescription-button' ).click( function( e ) { e.preventDefault; openDialog; } );						}					}				}			}		} );	}	if ( v.wgWikibaseItemId ) { getDescription; } } );