MediaWiki:Gadget-enhanced-categories.js
Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.
- Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
- Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
- Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
- Opera: Naciśnij klawisze Ctrl+F5.
/**
* Dodatkowe funkcje dla stron kategorii:
* - wyszukiwanie haseł na podstawie kombinacji znaków indeksu literowego
* - pobieranie danych poprzez AJAX
* - wizualne rozróżnienie wyników wyszukiwania
* Autor: Peter Bowman @ plwikt
*/
var gadget = mw.libs.enhancedCategories = {};
var config = mw.config.get( [
'wgUserLanguage',
'wgPageName'
] ),
L10n = {
qqx: [
'enhanced-categories-search',
'enhanced-categories-go',
'enhanced-categories-cancel',
'enhanced-categories-found',
'enhanced-categories-startswith',
'enhanced-categories-endswith'
],
en: [
'Entry search',
'Load results',
'Cancel',
'Found $1 {{PLURAL:$1|result|results}} $2 "$3".',
'starting with',
'ending with'
],
pl: [
'Wyszukiwanie haseł',
'Załaduj wyniki',
'Anuluj',
'Znaleziono $1 {{PLURAL:$1|wynik|wyniki|wyników}} $2 „$3”.',
'{{PLURAL:$1|zaczynający|zaczynające|zaczynających}} się na',
'{{PLURAL:$1|kończący|kończące|kończących}} się na'
]
};
var $catindex, $container, $mwpages;
var $searchIcon, $acceptIcon, $cancelIcon, $inputbox, $loadIcon;
function request( param, value ) {
var url = new mw.Uri( mw.util.getUrl() );
url.query[ param ] = value;
url.fragment = 'mw-pages';
loading();
$.get( url.toString() ).done( function ( data ) {
var $content = $( data ).find( '#mw-content-text' );
mw.hook( 'wikipage.content' ).fire( $content );
$loadIcon.hide();
$container.replaceWith( $container = $content.children( '.mw-category-generated' ) );
$mwpages = $container.children( '#mw-pages' );
$mwpages[ 0 ].scrollIntoView();
history.pushState( $container.html(), '', url.toString() );
gadget.lastSearch = {
param: param,
value: value
};
if ( param === 'from' && value.length > 1 ) {
delimit();
}
} )
.fail( function () {
location.href = url.toString();
} );
}
function delimit() {
var $links = $mwpages.children( 'div' ).find( 'a' ),
term = gadget.lastSearch.value.toLowerCase();
$links.each( function( count ) {
var first,
text = this.textContent.toLowerCase();
if ( gadget.aTergo
? ( reverseString( text ).indexOf( term ) !== 0 )
: ( text.indexOf( term ) !== 0 )
) {
if ( ( gadget.count = count ) > 0 ) {
first = $links.first().text().toLowerCase();
if ( gadget.aTergo
? ( first.charAt( first.length - 1 ) === text.charAt( text.length - 1 ) )
: ( first.charAt( 0 ) === text.charAt( 0 ) )
) {
$( this ).parent( 'li' ).addClass( 'catindex-ecsep' );
}
$mwpages.find( 'p' ).append( ' ' + mw.msg(
'enhanced-categories-found',
count,
gadget.aTergo
? mw.msg( 'enhanced-categories-endswith', count )
: mw.msg( 'enhanced-categories-startswith', count ),
gadget.aTergo
? reverseString( term )
: term
) );
}
return false;
}
} );
}
function loading() {
$loadIcon.show();
$mwpages.addClass( 'catlinks-mw-pages-loading' );
}
function processLinks( $links, callback ) {
$links
.off( 'click' )
.on( 'click', function ( evt ) {
evt.preventDefault();
if ( callback ) {
callback( this );
} else {
request( 'from', this.textContent );
}
} );
}
function processNavigationLinks( link ) {
var url = new mw.Uri( link.href );
if ( 'pageuntil' in url.query ) {
request( 'pageuntil', url.query.pageuntil );
} else if ( 'pagefrom' in url.query ) {
request( 'pagefrom', url.query.pagefrom );
}
}
// https://stackoverflow.com/a/1611449
function reverseString( s ) {
return s.split( '' ).reverse().join( '' );
}
function init() {
var $links, aTergo, ltr;
$container = $( '#mw-content-text' ).children( '.mw-category-generated' );
$mwpages = $container.children( '#mw-pages' );
$loadIcon = $.createSpinner( {
size: 'large',
type: 'block'
} )
.hide()
.insertBefore( $container );
mw.hook( 'wikipage.content' ).add( function ( $cnt ) {
processLinks( $cnt.find( '#mw-pages' ).children( 'a' ), processNavigationLinks );
} );
history.replaceState( $container.html(), '', location.href );
if ( !( $catindex = $( '#catindex' ).find( '.catindex-list' ) ).length ) {
return;
}
$links = $catindex.find( 'a' );
processLinks( $links );
aTergo = gadget.aTergo = ( config.wgPageName.indexOf( '_(indeks_a_tergo)' ) !== -1 );
ltr = !$( '.rtl-category-indicator' ).length;
$searchIcon = $( '<img>' ).attr( {
src: '//upload.wikimedia.org/wikipedia/commons/thumb/1/15/Viewmag.png/16px-Viewmag.png',
alt: mw.msg( 'enhanced-categories-search' ),
title: mw.msg( 'enhanced-categories-search' )
} );
$acceptIcon = $( '<img>' ).attr( {
src: '//upload.wikimedia.org/wikipedia/commons/thumb/0/0e/Forward.png/16px-Forward.png',
alt: mw.msg( 'enhanced-categories-go' ),
title: mw.msg( 'enhanced-categories-go' )
} );
$cancelIcon = $( '<img>' ).attr( {
src: '//upload.wikimedia.org/wikipedia/commons/thumb/f/f3/Nuvola_actions_cancel.png/16px-Nuvola_actions_cancel.png',
alt: mw.msg( 'enhanced-categories-cancel' ),
title: mw.msg( 'enhanced-categories-cancel' )
} );
$inputbox = $( '<input>' ).attr( {
type: 'text',
size: 30,
dir: ltr ? 'ltr' : 'rtl'
} );
$( '<div>' )
.attr( 'id', 'catindex-ecbox' )
.append( [
$searchIcon
.on( 'click', function () {
$searchIcon.hide();
$inputbox.show().focus();
$acceptIcon.show();
$cancelIcon.show();
$catindex.addClass( 'catindex-eclinks' );
processLinks( $links, function ( link ) {
$inputbox.val( $inputbox.val() + link.textContent );
} );
} ),
$inputbox
.hide()
.keypress( function ( evt ) {
if ( evt.key === 'Enter' ) {
$acceptIcon.trigger( 'click' );
}
} ),
$acceptIcon
.hide()
.on( 'click', function () {
if ( $inputbox.val() ) {
request( 'from', aTergo
? reverseString( $inputbox.val() )
: $inputbox.val()
);
}
} ),
$cancelIcon
.hide()
.on( 'click', function () {
$searchIcon.show();
$inputbox.val( '' ).hide();
$acceptIcon.hide();
$cancelIcon.hide();
$catindex.removeClass( 'catindex-eclinks' );
processLinks( $links );
} )
] )
.appendTo( $catindex.parent() );
}
$.each( L10n[ config.wgUserLanguage ] || L10n.en, function ( i, message ) {
mw.messages.set( L10n.qqx[ i ], message );
} );
window.onpopstate = function ( e ) {
if ( e.state ) {
$container.html( e.state );
$mwpages = $container.children( '#mw-pages' );
mw.hook( 'wikipage.content' ).fire( $( '#mw-content-text' ) );
}
};
$.when(
mw.loader.using( [
'jquery.spinner',
'mediawiki.util',
'mediawiki.jqueryMsg',
'mediawiki.Uri'
] ),
$.ready
).done( init );