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

Z Wikipedie, otevřené encyklopedie
Smazaný obsah Přidaný obsah
Danny B. (diskuse | příspěvky)
Separace speciálních typů kategorií
 
Danny B. (diskuse | příspěvky)
+ umožnění odfiltrování speciálních typů kategorií na Special:WantedCategories; optimalizace kódu; refaktorizace
Řádek 5: Řádek 5:
* @details
* @details
* Oddělí speciální typy kategorií do vlastních boxů pod hlavním kategorizačním boxem.
* 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
* @remark
Řádek 25: Řádek 26:
var
var categoryTypes = [
i,
categoryTypes,
categoryPrefixes,
rgxCategoryPrefixes;
categoryTypes = [
{
{
boxId: 'mwg-MaintenanceCats',
id: 'MaintenanceCats',
prefix: 'Údržba:',
prefix: 'Údržba:',
infoPage: new mw.Title( 'Project:Údržba' ),
infoPage: new mw.Title( 'Project:Údržba' ),
Řádek 34: Řádek 42:
treatAsExisting: false
treatAsExisting: false
}, {
}, {
boxId: 'mwg-MonitoringCats',
id: 'MonitoringCats',
prefix: 'Monitoring:',
prefix: 'Monitoring:',
infoPage: new mw.Title( 'Project:Monitoring' ),
infoPage: new mw.Title( 'Project:Monitoring' ),
Řádek 44: Řádek 52:
var prefixes = categoryTypes[0].prefix;
categoryPrefixes = categoryTypes[0].prefix;
for ( var i = 1; i < categoryTypes.length; i++ ) {
for ( i = 1; i < categoryTypes.length; i++ ) {
prefixes += '|' + categoryTypes[i].prefix;
categoryPrefixes += '|' + categoryTypes[i].prefix;
}
}
var rgxPrefixes = new RegExp( '^(' + prefixes + ')' );
rgxCategoryPrefixes = new RegExp( '^(' + categoryPrefixes + ')' );
if (
if (
(
!mw.config.get( 'wgIsArticle' )
!mw.config.get( 'wgIsArticle' )
||
mw.config.get( 'wgIsRedirect' )
|| mw.config.get( 'wgIsRedirect' )
|| ( mw.config.get( 'wgNamespaceNumber' ) === 14 && rgxCategoryPrefixes.test( mw.config.get( 'wgTitle' ) ) )
||
!$.grep( mw.config.get( 'wgCategories' ), function ( item ) { return rgxPrefixes.test( item ); } ).length
|| !$.grep( mw.config.get( 'wgCategories' ), function ( item ) { return rgxCategoryPrefixes.test( item ); } ).length
) {
)
&& !( mw.config.get( 'wgNamespaceNumber' ) === -1 && mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Wantedcategories' )
) {
return;
return;
}
function makeClickHandler( title ) {
return function () {
$( 'body.mw-special-Wantedcategories div.mw-spcontent ol li a[title^="' + title + '"]' )
.parent()
.toggle( $( this ).prop( 'checked' ) )
;
};
}
}
Řádek 66: Řádek 90:
$( document ).ready( function () {
$( document ).ready( function () {
var categoryPrefix;
var
elementId,
categoryPrefix,
categoryFullPrefix;
for ( var i = 0; i < categoryTypes.length; i++ ) {
for ( i = 0; i < categoryTypes.length; i++ ) {
categoryPrefix = mw.config.get( 'wgFormattedNamespaces' )['14'] + ':' + categoryTypes[i].prefix;
elementId = 'mwg-' + categoryTypes[i].id;
categoryPrefix = categoryTypes[i].prefix;
categoryFullPrefix = mw.config.get( 'wgFormattedNamespaces' )[14] + ':' + categoryPrefix;
if (
if ( $( '#catlinks ul a[title^="' + categoryPrefix + '"]' ).length ) {
mw.config.get( 'wgNamespaceNumber' ) === -1
&& mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Wantedcategories'
&& $( 'div.mw-spcontent ol.special a[title^="' + categoryFullPrefix + '"]' ).length
) {
$( 'body.mw-special-Wantedcategories div.mw-spcontent ol' ).before(
$( '<input>' )
.attr({
id: elementId,
type: 'checkbox',
checked: 'checked'
})
.click(
makeClickHandler( categoryFullPrefix )
)
.after(
$( '<label>' )
.attr({
'for': elementId
})
.text( categoryPrefix )
)
);
} else if ( $( '#catlinks ul a[title^="' + categoryFullPrefix + '"]' ).length ) {
$( '<div>' )
$( '<div>' )
.attr({
.attr({
'class': 'catlinks mwg-specialcatlinks',
'class': 'catlinks mwg-specialcatlinks',
'id': categoryTypes[i].boxId
'id': elementId
})
})
.html( '<a href="' + categoryTypes[i].infoPage.getUrl() + '" title="' + mw.html.escape( categoryTypes[i].infoPage.getPrefixedText() ) + '">' + categoryTypes[i].infoText + '</a>: <ul></ul>' )
.html( '<a href="' + categoryTypes[i].infoPage.getUrl() + '" title="' + mw.html.escape( categoryTypes[i].infoPage.getPrefixedText() ) + '">' + categoryTypes[i].infoText + '</a>: <ul></ul>' )
Řádek 84: Řádek 140:
.append(
.append(
$( '#catlinks ul li' )
$( '#catlinks ul li' )
.has( 'a[title^="' + categoryPrefix + '"]' )
.has( 'a[title^="' + categoryFullPrefix + '"]' )
.each( function () {
.each( function () {
$( this )
$( this )
.find( '> a' )
.find( '> a' )
.each( function () {
.each( function () {
if (categoryTypes[i].removePrefix ) {
if ( categoryTypes[i].removePrefix ) {
$( this ).text( $( this ).text().replace( categoryTypes[i].prefix, '' ) );
$( this ).text( $( this ).text().replace( categoryTypes[i].prefix, '' ) );
}
}
if (categoryTypes[i].treatAsExisting ) {
if ( categoryTypes[i].treatAsExisting ) {
$( this )
$( this )
.removeClass( 'new' )
.removeClass( 'new' )

Verze z 26. 4. 2014, 13:41

/**
 * @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,
		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
		}
	];
	
	
	categoryPrefixes = categoryTypes[0].prefix;
	for ( i = 1; i < categoryTypes.length; 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( title ) {
		
		return function () {
			
			$( 'body.mw-special-Wantedcategories div.mw-spcontent ol li a[title^="' + title + '"]' )
				.parent()
				.toggle( $( this ).prop( 'checked' ) )
				;
			
		};
		
	}
	
	
	$( document ).ready( function () {
		
		var
			elementId,
			categoryPrefix,
			categoryFullPrefix;
		
		
		for ( i = 0; i < categoryTypes.length; i++ ) {
			
			elementId = 'mwg-' + categoryTypes[i].id;
			categoryPrefix = categoryTypes[i].prefix;
			categoryFullPrefix = mw.config.get( 'wgFormattedNamespaces' )[14] + ':' + categoryPrefix;
			
			if (
				mw.config.get( 'wgNamespaceNumber' ) === -1
				&& mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Wantedcategories'
				&& $( 'div.mw-spcontent ol.special a[title^="' + categoryFullPrefix + '"]' ).length
			) {
				
				$( 'body.mw-special-Wantedcategories div.mw-spcontent ol' ).before(
					$( '<input>' )
						.attr({
							id: elementId,
							type: 'checkbox',
							checked: 'checked'
						})
						.click(
							makeClickHandler( categoryFullPrefix )
						)
						.after(
							$( '<label>' )
								.attr({
									'for': elementId
								})
								.text( categoryPrefix )
						)
						
				);
				
			} else if ( $( '#catlinks ul a[title^="' + categoryFullPrefix + '"]' ).length ) {
				
				$( '<div>' )
					.attr({
						'class': 'catlinks mwg-specialcatlinks',
						'id': elementId
					})
					.html( '<a href="' + categoryTypes[i].infoPage.getUrl() + '" title="' + mw.html.escape( categoryTypes[i].infoPage.getPrefixedText() ) + '">' + categoryTypes[i].infoText + '</a>: <ul></ul>' )
					.insertAfter( $( '.catlinks' ).last() )
					.children( 'ul' )
					.append(
						$( '#catlinks ul li' )
							.has( 'a[title^="' + categoryFullPrefix + '"]' )
							.each( function () {
								$( this )
									.find( '> a' )
									.each( function () {
										if ( categoryTypes[i].removePrefix ) {
											$( this ).text( $( this ).text().replace( categoryTypes[i].prefix, '' ) );
										}
										if ( categoryTypes[i].treatAsExisting ) {
											$( this )
												.removeClass( 'new' )
												.attr( 'href', function ( index, value ) {
													return new mw.Title( new mw.Uri( value ).query.title ).getUrl();
												})
											;
										}
									});
							})
					)
				;
				
			}
			
		}
		
		$( '#catlinks' )
			.has( 'ul:empty' )
			.remove()
		;
		
	});
	
	
}( mediaWiki, jQuery ) );