Zum Inhalt springen

Benutzer:TMg/advancedSearch.js/namespaceFilters.js

aus Wikipedia, der freien Enzyklopädie
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 9. März 2017 um 17:09 Uhr durch TMg (Diskussion | Beiträge) (Create default namespace filters even if not in advanced mode). Sie kann sich erheblich von der aktuellen Version unterscheiden.

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
( function ( mw, $ ) {
	'use strict';

	function isLoaded() {
		if ( !mw.libs ) {
			mw.libs = {};
		}
		if ( !mw.libs.advancedSearch ) {
			mw.libs.advancedSearch = {};
		}

		if ( mw.libs.advancedSearch.namespaceFiltersLoaded ) {
			return true;
		}
		mw.libs.advancedSearch.namespaceFiltersLoaded = true;

		return false;
	}

	if ( isLoaded() ) {
		return;
	}

	if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'Search' ) {
		return;
	}

	/* var $search = $( 'form#powersearch' );
	if ( !$search.length ) {
		return;
	} */
	$( 'form#search input[name=profile][value=default]' ).val( 'advanced' );

	var presets = [
		{
			id: 'articles',
			filter: '#mw-search-ns0'
		},
		{
			id: 'images',
			filter: '#mw-search-ns6'
		},
		{
			id: 'project',
			filter: '#mw-search-ns4, #mw-search-ns10, #mw-search-ns12, #mw-search-ns100, #mw-search-ns828'
		},
		{
			id: 'talk',
			filter: function ( index, checkbox ) {
				return /[13579]$/.test( checkbox.id );
			}
		},
		{
			id: 'all',
			filter: '*'
		}
	];

	var i18n = {
		'de': {
			articles: 'Artikel',
			images: 'Dateien',
			project: 'Projektnamensräume',
			talk: 'Diskussionsseiten',
			all: 'Alle',
			'powersearch-ns': 'Suche in Namensräumen:',
			'individual-namespaces': 'Individuelle Namensräume:',
			'ns:0': '(Artikel)',
			'ns:1': 'Diskussion',
			'ns:2': 'Benutzer',
			'ns:4': 'Wikipedia',
			'ns:6': 'Datei',
			'ns:8': 'MediaWiki',
			'ns:10': 'Vorlage',
			'ns:12': 'Hilfe',
			'ns:14': 'Kategorie',
			'ns:100': 'Portal',
			'ns:828': 'Modul',
		},
		'en': {
			articles: 'Articles',
			images: 'Files',
			project: 'Project namespaces',
			talk: 'Talk pages',
			all: 'All',
			'powersearch-ns': 'Search in namespaces:',
			'individual-namespaces': 'Individual namespaces:'
		}
	};

	/**
	 * @param {string} key
	 * @return {string}
	 */
	function msg( key ) {
		var lang = mw.config.get( 'wgUserLanguage' );

		return i18n[lang] && i18n[lang][key] || i18n.en[key] || '<' + key + '>';
	}

	function updatePresetStates() {
		presets.forEach( function ( preset ) {
			var $checkboxGroup = $( '#mw-searchoptions input[id^=mw-search-ns]' )
					.filter( preset.filter ),
				allChecked = true;

			if ( !$checkboxGroup.length ) {
				return;
			}

			$checkboxGroup.each( function ( index, checkbox ) {
				if ( !checkbox.checked ) {
					allChecked = false;
					return false;
				}
			} );

			$( '#' + preset.id ).prop( 'checked', allChecked );
		} );
	}

	/**
	 * @param {int} ns
	 * @return {jQuery}
	 */
	function createNamespaceTableCell( ns ) {
		var label = msg( 'ns:' + ns );
		if ( label[0] === '<' ) {
			label = msg( 'ns:' + ( ns - 1 ) ) + ' ' + msg( 'ns:1' );
		}
		return $( '<td>' ).append(
			$( '<input>' ).prop( {
				id: 'mw-search-ns' + ns,
				name: 'ns' + ns,
				type: 'checkbox',
				value: 1
			} ),
			' ',
			$( '<label>' ).prop( {
				for: 'mw-search-ns' + ns
			} ).text( label )
		);
	}

	mw.loader.using( [ 'oojs-ui' ], function () {
		var $presetsBar = $( '.mw-search-profile-tabs' ),
			$individualNamespaces = $( '#mw-searchoptions' ),
			$headline = $( '#mw-searchoptions h4' );

		if ( !$individualNamespaces.length ) {
			var $table = $( '<table>' );
			[ 0, 2, 4, 6, 8, 10, 12, 14, 100, 828 ].forEach( function ( ns ) {
				$table.append( $( '<tr>' ).append(
					createNamespaceTableCell( ns ),
					createNamespaceTableCell( ns + 1 )
				) );
			} );
			$individualNamespaces = $( '<fieldset>' ).prop( { id: 'mw-searchoptions' } );
			$headline = $( '<h4>' ).text( msg( 'powersearch-ns' ) );
			$presetsBar.after( $individualNamespaces.append( $headline, $table ) );
		}

		$headline.css( {
				color: '#666',
				float: 'none',
				'font-weight': 'normal',
				'margin-bottom': '0.3em'
			} );
		$individualNamespaces.css( {
				background: 'linear-gradient(rgba(0, 0, 0, 0.1), #fff 0.5em)',
				'box-sizing': 'border-box',
				'margin-top': '-1px',
				'max-width': '50em',
				'padding-top': '1em'
			} )
			.hide();
		$presetsBar.css( {
				'background-image': 'url(//de.wikipedia.org/w/load.php?modules=oojs-ui.styles.indicators&image=down)',
				'background-position': '99%',
				'background-repeat': 'no-repeat',
				'background-size': '18px',
				'border-color': '#a2a9b1',
				'border-radius': '2px',
				'box-sizing': 'border-box',
				cursor: 'pointer',
				'margin-top': '0.3em',
				'max-width': '50em',
				padding: '0.4em 0.7em',
				position: 'relative'
			} )
			.append( $( '<b>' ).append( $headline.text() ), '\n' )
			.click( function ( e ) {
				$individualNamespaces.toggle();
			} );

		$headline.text( msg( 'individual-namespaces' ) );

		$individualNamespaces.find( 'table' ).css( { float: 'none', margin: 0 } );
		$individualNamespaces.find( 'tr:first-child td:first-child' ).css( { 'min-width': '12em' } );

		presets.forEach( function ( preset ) {
			var $checkbox = $( '<input>' )
					.attr( { id: preset.id, type: 'checkbox' } )
					.css( { 'vertical-align': 'middle' } )
					.click( function ( e ) {
						e.stopPropagation();

						if ( !e.originalEvent ) {
							return;
						}

						$( '#mw-searchoptions input[id^=mw-search-ns]' )
							.filter( preset.filter )
							.prop( 'checked', e.target.checked );

						updatePresetStates();
					} ),
				$label = $( '<label>' )
					.attr( { for: preset.id } )
					.css( {
						cursor: 'pointer',
						padding: '0.4em 0.2em',
						'white-space': 'nowrap'
					} )
					.click( function ( e ) {
						e.stopPropagation();
					} )
					.append( $checkbox, '\n', msg( preset.id ) );

			$presetsBar.append( $label, '\n' );
		} );

		// $presetsBar.append( $( '<span>▼</span>' ).css( { float: 'right' } ) );

		$( '#mw-searchoptions input[id^=mw-search-ns]' ).click( function ( e ) {
			if ( !e.originalEvent ) {
				return;
			}

			updatePresetStates();
		} );

		updatePresetStates();

		$( '.search-types, #mw-search-togglebox, #mw-search-togglebox + .divider' )
			.hide();
	} );
} )( mediaWiki, jQuery );