Användare:Matma Rex/test 20170308.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.
mw.loader.using( [ 'mediawiki.api', 'oojs-ui' ], function() {
	'use strict';
	if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Search' ) {
		$( function() {
			var buttontext = 'Category search';
			var desc = 'This form can be used to search in categories. If two or more categories are given, the <a href="//en.wikipedia.org/wiki/Intersection_(set_theory)">intersection</a> is returned. Please note that pages in subcategories to the given categories cannot be found.';
			var createinputtext = 'Create another input field';
			var searchtext = 'Search';
			var closetext = 'Close';
			if ( mw.config.get( 'wgUserLanguage' ) === 'sv' ) {
				buttontext = 'Kategorisökning';
				desc = 'Det här formuläret kan användas för att söka i kategorier. Om två eller fler kategorier anges visas <a href="//sv.wikipedia.org/wiki/Snitt">snittet</a>. Notera att sidor i underkategorier till de valda kategorierna inte hittas.';
				createinputtext = 'Skapa ett till inmatningsfält';
				searchtext = 'Sök';
				closetext = 'Stäng';
			}
			$( '.search-types ul' ).append( '<li><a href="#" id="gadget-incategory">' + buttontext + '</a></li>' );
			$( '#gadget-incategory' ).click( function( e ) {
				e.preventDefault();
				// 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 );
				}
				function focusLastInput() {
					$( '#gadget-incategory-dialog input' ).last().focus();
				}
				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 = 'gadgetincategorydialog';
					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 that = this;
						// 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 } );
						var dialogstr = '<div id="gadget-incategory-dialog">';
						var n = 0;
						var iw;
						function createinput() {
							iw.append( '<input list="gadget-incategory-datalist' + n + '"><datalist id="gadget-incategory-datalist' + n + '"></datalist>' );
							focusLastInput();
						}
						var s;
						dialogstr += '<p>' + desc + '</p>';
						dialogstr += '<div id="inputwrapper"></div>';
						dialogstr += '<button id="gadget-incategory-createinput">' + createinputtext + '</button>';
						dialogstr += '<button id="gadget-incategory-search">' + searchtext + '</button>';
						dialogstr += '<button id="gadget-incategory-close">' + closetext + '</button>';
						dialogstr += '</div>';
						this.content.$element.append( dialogstr );
						this.$body.append( this.content.$element );
						$( '#gadget-incategory-close' ).click( function() {
							that.close();
						} );
						iw = $( '#inputwrapper' );
						createinput();
						$( '#gadget-incategory-createinput' ).click( function() {
							n += 1;
							createinput();
						} );
						s = $( '#gadget-incategory-search' );
						iw.on( 'input', 'input', function( e ) {
							var api = new mw.Api();
							var text = $( this ).val();
							if ( !/^\s*$/.test( text ) ) {
								api.get( {
									action: 'query',
									list: 'allpages',
									apfrom: text,
									apnamespace: 14,
									aplimit: 5
								} ).done( function( data ) {
									var arr = data.query.allpages;
									var pages = [];
									var stripped = [];
									var datalist = [];
									var str;
									arr.forEach( function( elem ) {
										pages.push( elem.title );
									} );
									pages.forEach( function( elem ) {
										stripped.push( elem.replace( /^Kategori:/, '' ) );
									} );
									stripped.forEach( function( elem ) {
										datalist.push( '<option value="' + elem + '">' );
									} );
									str = datalist.join( '' );
									$( e.target ).next().empty().append( str );
								} );
							}
						} );
						s.click( function() {
							var arr = [];
							var trimmed = [];
							var prefixed = [];
							var str;
							var encodedstr;
							$( '#gadget-incategory-dialog input' ).each( function() {
								arr.push( $( this ).val() );
							} );
							arr.forEach( function( elem ) {
								if ( !/^\s*$/.test( elem ) ) {
									trimmed.push( elem );
								}
							} );
							trimmed.forEach( function( elem ) {
								prefixed.push( 'incategory:"' + elem + '"' );
							} );
							str = prefixed.join( ' ' );
							encodedstr = encodeURIComponent( str );
							window.location = '/w/index.php?title=Special%3ASök&search=' + encodedstr;
						} );
						iw.on( 'keyup', 'input', function( e ) {
							if ( e.which === 13 ) {
								s.trigger( 'click' );
							}
						} );
					};

					// Set up the ready mode of the window. 
					MyDialog.prototype.getReadyProcess = function ( data ) {
						return MyDialog.super.prototype.getReadyProcess.call( this, data )
						.next( function () {
							focusLastInput();
						}, 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.
					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 );
				}
			} );
		} );
	}
} );