MediaWiki:Gadget-SeparateSpecialCategories.js: Porovnání verzí

Z Wikipedie, otevřené encyklopedie
Smazaný obsah Přidaný obsah
Danny B. (diskuse | příspěvky)
optimalizace kódu; refaktorizace
Danny B. (diskuse | příspěvky)
optimalizace kódu; kompatibilita s novou verzí po upgradu
Řádek 151: Řádek 151:
.click(
.click(
makeClickHandler( categoryFullPrefix )
makeClickHandler( categoryFullPrefix )
)
),
.after(
' ',
$( '<label>' )
$( '<label>' )
.attr({
.attr({
'for': elementId
'for': elementId
})
})
.text( categoryPrefix )
.text( categoryPrefix )
)
);
);
Řádek 169: Řádek 167:
'id': elementId
'id': elementId
})
})
.append(
.html( '<a href="' + categoryType.infoPage.getUrl() + '" title="' + mw.html.escape( categoryType.infoPage.getPrefixedText() ) + '">' + categoryType.infoText + '</a>: <ul></ul>' )
$( '<a>' )
.attr({
href: categoryType.infoPage.getUrl(),
title: categoryType.infoPage.getPrefixedText()
})
.text( categoryType.infoText )
,
': ',
$( '<ul>' )
)
.insertAfter( $( '.catlinks' ).last() )
.insertAfter( $( '.catlinks' ).last() )
.children( 'ul' )
.children( 'ul' )

Verze z 21. 5. 2014, 19:16

/**
 * @brief
 *  Separace speciálních typů kategorií
 * 
 * @details
 *  Oddělí speciální typy kategorií do vlastních boxů pod hlavním kategorizačním boxem.
 *  Na Special:WantedCategories umožní odfiltrování speciálních typů kategorií.
 * 
 * @remark
 *  Relies on:
 *  * mediawiki.Title
 *  * mediawiki.Uri
 * 
 * @author
 *  [[meta:User:Danny B.]]
 */

/*global mediaWiki, jQuery */
/*jslint browser: true, plusplus: true, white: true */
/*jshint browser:true, laxbreak:false, plusplus:false, white:false, undef:true, unused:true */


( function ( mw, $ ) {
	
	'use strict';
	
	
	var
		i,
		categoryTypes,
		categoryTypesLength,
		categoryPrefixes,
		rgxCategoryPrefixes;
	
	
	categoryTypes = [
		{
			id: 'MaintenanceCats',
			prefix: 'Údržba:',
			infoPage: new mw.Title( 'Project:Údržba' ),
			infoText: 'Údržba',
			removePrefix: true,
			treatAsExisting: false
		}, {
			id: 'MonitoringCats',
			prefix: 'Monitoring:',
			infoPage: new mw.Title( 'Project:Monitoring' ),
			infoText: 'Monitoring',
			removePrefix: true,
			treatAsExisting: true
		}
	];
	
	
	categoryTypesLength = categoryTypes.length;
	categoryPrefixes = categoryTypes[0].prefix;
	for ( i = 1; i < categoryTypesLength; i++ ) {
		categoryPrefixes += '|' + categoryTypes[i].prefix;
	}
	rgxCategoryPrefixes = new RegExp( '^(' + categoryPrefixes + ')' );
	
	
	if (
		(
			!mw.config.get( 'wgIsArticle' )
			|| mw.config.get( 'wgIsRedirect' )
			|| ( mw.config.get( 'wgNamespaceNumber' ) === 14 && rgxCategoryPrefixes.test( mw.config.get( 'wgTitle' ) ) )
			|| !$.grep( mw.config.get( 'wgCategories' ), function ( item ) { return rgxCategoryPrefixes.test( item ); } ).length
		)
		&& !( mw.config.get( 'wgNamespaceNumber' ) === -1 && mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Wantedcategories' )
	) {
		
		return;
		
	}
	
	
	function makeClickHandler( categoryFullPrefix ) {
		
		return function () {
			
			$( 'body.mw-special-Wantedcategories div.mw-spcontent ol li a[title^="' + categoryFullPrefix + '"]' )
				.parent()
				.toggle( $( this ).prop( 'checked' ) )
			;
			
		};
		
	}
	
	
	function makeSpecialCategoryLink( categoryType ) {
		
		return function () {
			
			$( this )
				.find( '> a' )
				.each( function () {
					if ( categoryType.removePrefix ) {
						$( this ).text( $( this ).text().replace( categoryType.prefix, '' ) );
					}
					if ( categoryType.treatAsExisting ) {
						$( this )
							.removeClass( 'new' )
							.attr( 'href', function ( index, value ) {
								return new mw.Title( new mw.Uri( value ).query.title ).getUrl();
							})
						;
					}
				})
			;
			
		};
		
	}
	
	
	$( document ).ready( function () {
		
		var
			categoryNamespaceName,
			categoryType,
			elementId,
			categoryPrefix,
			categoryFullPrefix;
		
		
		categoryNamespaceName = mw.config.get( 'wgFormattedNamespaces' )[14];
		
		
		for ( i = 0; i < categoryTypesLength; i++ ) {
			
			categoryType = categoryTypes[i];
			elementId = 'mwg-' + categoryType.id;
			categoryPrefix = categoryType.prefix;
			categoryFullPrefix = categoryNamespaceName + ':' + categoryPrefix;
			
			if (
				mw.config.get( 'wgNamespaceNumber' ) === -1
				&& mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Wantedcategories'
				&& $( 'div.mw-spcontent ol.special a[title^="' + categoryFullPrefix + '"]' ).length
			) {
				
				$( 'div.mw-spcontent ol.special' ).before(
					$( '<input>' )
						.attr({
							id: elementId,
							type: 'checkbox',
							checked: 'checked'
						})
						.click(
							makeClickHandler( categoryFullPrefix )
						),
					' ',
					$( '<label>' )
						.attr({
							'for': elementId
						})
						.text( categoryPrefix )
				);
				
			} else if ( $( '#catlinks ul a[title^="' + categoryFullPrefix + '"]' ).length ) {
				
				$( '<div>' )
					.attr({
						'class': 'catlinks mwg-specialcatlinks',
						'id': elementId
					})
					.append(
						$( '<a>' )
							.attr({
								href: categoryType.infoPage.getUrl(),
								title: categoryType.infoPage.getPrefixedText()
							})
							.text( categoryType.infoText )
						,
						': ',
						$( '<ul>' )
					)
					.insertAfter( $( '.catlinks' ).last() )
					.children( 'ul' )
					.append(
						$( '#catlinks ul li' )
							.has( 'a[title^="' + categoryFullPrefix + '"]' )
							.each( makeSpecialCategoryLink( categoryType ) )
					)
				;
				
			}
			
		}
		
		$( '#catlinks' )
			.has( 'ul:empty' )
			.remove()
		;
		
	});
	
	
}( mediaWiki, jQuery ) );