MediaWiki:Gadget-CategoryCompare.js
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.
if ( mw.config.get( 'wgNamespaceNumber' ) === 14 ) {
$( function() {
'use strict';
var linktext = 'Compare';
var linktitle = 'Find pages in the corresponding category on another wiki';
var starttext = 'Go';
var stoptext = 'Stop';
var stoppedtext = 'Stopped!';
var donetext = 'Done!';
var closetext = 'Close';
var selecttext = 'Select';
var api = new mw.Api();
var esc = mw.html.escape;
var langs = $( '.interlanguage-link-target' ).map( function( i, e ) {
return $( e ).attr( 'lang' );
} ).get();
var options = langs.map( function( lang ) {
var option = '<option';
if ( lang === 'en' ) {
option += ' selected';
}
option += '>' + lang + '</option>';
return option;
} );
var optionsstr = options.join( '' );
var link;
if ( mw.config.get( 'wgUserLanguage' ) === 'sv' ) {
linktext = 'Jämför';
linktitle = 'Hitta sidor som ligger i motsvarande kategori på en annan wiki';
starttext = 'Kör';
stoptext = 'Stopp';
stoppedtext = 'Stoppad!';
donetext = 'Klart!';
closetext = 'Stäng';
selecttext = 'Markera';
}
if ( langs.length ) {
link = mw.util.addPortletLink(
'p-cactions',
'#',
linktext,
'ca-categorycompare',
linktitle
);
$( link ).find( 'a' ).click( function( e ) {
e.preventDefault();
var shouldContinue = true;
var str = '<div>' +
'<select id="gadget-categorycompare-select">' + optionsstr + '</select>' +
'<button type="button" id="gadget-categorycompare-start">' + starttext + '</button>' +
'<button type="button" id="gadget-categorycompare-stop">' + stoptext + '</button>' +
'<button type="button" id="gadget-categorycompare-close">' + closetext + '</button>' +
'<ol id="gadget-categorycompare-list"></ol>' +
'<textarea id="gadget-categorycompare-textarea" readonly></textarea>' +
'<button type="button" id="gadget-categorycompare-selectbutton">' + selecttext + '</button>' +
'</div>';
// 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 );
}
var myDialog;
var windowManager;
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 = 'gadgetcategorycomparedialog';
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 () {
// 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 } );
this.content.$element.append( str );
this.$body.append( this.content.$element );
var uniques = new Set();
function init() {
var lang = $( '#gadget-categorycompare-select option:selected' ).text();
var iwapi = new mw.ForeignApi( 'https://' + lang + '.wikipedia.org/w/api.php' );
var langlink = $( '.interlanguage-link-target' ).filter( function( i, e ) {
return $( e ).attr( 'lang' ) === lang;
} );
var cathref = langlink.attr( 'href' );
var cat = cathref.split( '/wiki/' )[ 1 ];
cat = decodeURIComponent( cat );
$( '#gadget-categorycompare-select' ).prop( 'disabled', true );
$( '#gadget-categorycompare-start' ).prop( 'disabled', true );
function getPages( start ) {
var obj = {
action: 'query',
list: 'categorymembers',
cmtitle: cat,
cmtype: 'page',
cmlimit: 50,
formatversion: 2
};
if ( start ) {
obj.cmcontinue = start;
}
iwapi.get( obj ).done( function( categorymembersres ) {
var pages = categorymembersres.query.categorymembers.map( function( categorymember ) {
return categorymember.title;
} );
var i = 0;
function getLangLinks() {
if ( shouldContinue ) {
setTimeout( function() {
iwapi.get( {
action: 'query',
titles: pages[ i ],
prop: 'langlinks',
lllang: mw.config.get( 'wgContentLanguage' ),
formatversion: 2
} ).done( function( langlinksres ) {
var langlinks = langlinksres.query.pages[ 0 ].langlinks;
var page;
if ( langlinks ) {
page = langlinks[ 0 ].title;
api.get( {
action: 'query',
titles: page,
prop: 'categories',
cllimit: 50,
formatversion: 2
} ).done( function( categoriesres ) {
var categories = categoriesres.query.pages[ 0 ].categories.map( function( categoryobj ) {
return categoryobj.title;
} );
if ( !categories.includes( mw.config.get( 'wgPageName' ).replace( /_/g, ' ' ) ) ) {
if ( !uniques.has( page ) ) {
$( '#gadget-categorycompare-list' ).append( '<li><a href="/wiki/' + esc( page ) + '">' + esc( page ) + '</a></li>' );
$( '#gadget-categorycompare-textarea' ).val( function( i, v ) {
return v + '#[[' + page + ']]\n';
} );
uniques.add( page );
}
}
if ( i === pages.length -1 ) {
if ( categorymembersres[ 'continue' ] ) {
if ( categorymembersres[ 'continue' ].cmcontinue ) {
getPages( categorymembersres[ 'continue' ].cmcontinue );
}
} else {
$( '#gadget-categorycompare-stop' ).text( donetext );
$( '#gadget-categorycompare-stop' ).prop( 'disabled', true );
}
} else {
i = i + 1;
getLangLinks();
}
} ).fail( function() {
getPages();
} );
} else if ( i === pages.length -1 ) {
if ( categorymembersres[ 'continue' ] ) {
if ( categorymembersres[ 'continue' ].cmcontinue ) {
getPages( categorymembersres[ 'continue' ].cmcontinue );
}
} else {
$( '#gadget-categorycompare-stop' ).text( donetext );
$( '#gadget-categorycompare-stop' ).prop( 'disabled', true );
}
} else {
i = i + 1;
getLangLinks();
}
} ).fail( function() {
getPages();
} );
}, 1000 );
}
}
getLangLinks();
} ).fail( function() {
getPages();
} );
}
getPages();
}
$( '#gadget-categorycompare-start' ).click( function() {
init();
} );
$( '#gadget-categorycompare-stop' ).click( function( ev ) {
shouldContinue = false;
$( '#gadget-categorycompare-stop' ).text( stoppedtext );
$( ev.currentTarget ).prop( 'disabled', true );
} );
$( '#gadget-categorycompare-close' ).click( function() {
shouldContinue = false;
myDialog.close();
} );
$( '#gadget-categorycompare-selectbutton' ).click( function() {
$( '#gadget-categorycompare-textarea' ).select();
} );
};
// 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.
myDialog = new MyDialog( {
classes: [
'gadget-categorycompare-dialog'
]
} );
// Create and append a window manager, which will open and close the window.
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 );
}
} );
}
} );
}