MediaWiki:Common.js
Revision as of 23:27, 24 October 2013 by Mastergalen (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
/**
* Element animator (used in [[Template:Grid]])
*
* Will cycle the active class on any child elements within an element with the animated class.
*/
if ( $( '.animated' ).length ) {
setInterval( function() {
$( '.animated' ).each( function() {
var current = $( this ).find( '.active' ).removeClass( 'active' ), next = current.next();
if ( !current.next().length ) {
next = $( this ).children().eq( 0 );
}
next.addClass( 'active' );
} );
}, 2000 );
}
/**
* Pause grid templates with lots of cells in them (e.g. [[Template:Grid/Crafting Table]]) on mouseover
*
* This is so people have a chance to look at each image on the cell
* and click on pages they want to view.
*/
function pauseGrid( grid ) {
$( grid ).hover( function() {
$( this ).find( '.grid .animated' ).removeClass( 'animated' ).addClass( 'paused' );
}, function() {
$( this ).find( '.grid .paused' ).removeClass( 'paused' ).addClass( 'animated' );
} );
}
pauseGrid( '.grid-Crafting_Table' );
pauseGrid( '.grid-Furnace' );
pauseGrid( '.grid-Brewing_Stand' );
/**
* Frame parser (for [[Template:Grid]])
*
* Requests the urls for all the animated grids on a page in 2
* API requests (due to a bug, 1 API request when it is fixed)
* and appends them to the correct location.
*/
var baseURL = '/', wikiURL = '/wiki/', $grids = $( '.grid' ), imgString = '';
if ( $grids.length ) {
$grids.each( function() {
var imgs = $( this ).data( 'imgs' ), mod = $( this ).data( 'mod' );
if ( !imgs ) {
return true;
}
imgs = imgs.split( ';' );
imgs.shift();
$.each( imgs, function() {
if ( !this.trim() ) {
return true;
}
if ( this.indexOf( ':' ) > -1 ) {
this.replace( /([^:]*):?([^,]*)/, function( $, mod, name ) {
if ( mod.trim().toLowerCase() === 'v' || mod.trim().toLowerCase() === 'vanilla' ) {
if ( imgString.indexOf( 'File:Grid ' + name.trim() + '.png' ) === -1 ) {
imgString += 'File:Grid ' + name.trim() + '.png|';
}
} else {
if ( imgString.indexOf( 'File:Grid ' + name.trim() + ' (' + mod.trim() + ').png' ) === -1 ) {
imgString += 'File:Grid ' + name.trim() + ' (' + mod.trim() + ').png|';
}
}
} );
} else {
this.replace( /([^,]*)/, function( $, name ) {
if ( !mod || mod.trim().toLowerCase() === 'v' || mod.trim().toLowerCase() === 'vanilla' ) {
if ( imgString.indexOf( 'File:Grid ' + name.trim() + '.png' ) === -1 ) {
imgString += 'File:Grid ' + name.trim() + '.png|';
}
} else {
if ( imgString.indexOf( 'File:Grid ' + name.trim() + ' (' + mod.trim() + ').png' ) === -1 ) {
imgString += 'File:Grid ' + name.trim() + ' (' + mod.trim() + ').png|';
}
}
} );
}
} );
} );
imgString = imgString.slice( 0, -1 );
/* Thanks to an API bug, &redirects doesn't work properly with prop=imageinfo
* Some of the images will return without any imageinfo, even though they are valid
* So the redirects have to be resolved in a separate request...
*/
if ( imgString ) {
$.ajax( {
type: 'POST',
url: baseURL + 'w/api.php?action=query&format=json&redirects',
data: { titles: imgString },
timeout: 20000
} ).done( function( data ) {
var redirects = {};
if ( data.query.redirects ) {
$.each( data.query.redirects, function() {
redirects[this.to] = this.from;
imgString = imgString.replace( this.from, this.to );
} );
}
$.ajax( {
type: 'POST',
url: baseURL + 'w/api.php?action=query&format=json&prop=imageinfo&iiprop=url&iiurlwidth=32&iiurlheight=32',
data: { titles: imgString },
timeout: 20000
} ).done( function( data ) {
var urls = {};
$.each( data.query.pages, function( index ) {
if ( index < 0 ) {
return true;
}
if ( redirects.hasOwnProperty( this.title ) ) {
urls[redirects[this.title].replace( /File:Grid (.*).png/, '$1' )] = this.imageinfo[0].thumburl;
} else {
urls[this.title.replace( /File:Grid (.*).png/, '$1' )] = this.imageinfo[0].thumburl;
}
} );
$grids.each( function() {
var $grid = $( this ), imgs = $grid.data( 'imgs' ), mod = $( this ).data( 'mod' ), html = '';
if ( !imgs ) {
return true;
}
imgs = imgs.split( ';' );
imgs.shift();
$.each( imgs, function() {
if ( !this.trim() ) {
html += gridFormat();
return true;
}
if ( this.indexOf( ':' ) > -1 ) {
this.replace( /([^:]*):?([^,]*),?(\d*)/, function( $, mod, name, num ) {
if ( mod.trim().toLowerCase() === 'v' || mod.trim().toLowerCase() === 'vanilla' ) {
html += gridFormat( name.trim(), name.trim(), urls[name.trim()], num );
} else {
var img = name.trim() + ' (' + mod.trim() + ')';
html += gridFormat( img, 'Mods/' + mod.trim() + '/' + name.trim(), urls[img], num );
}
} );
} else {
this.replace( /([^,]*),?(\d*)/, function( $, name, num ) {
if ( !mod || mod.trim().toLowerCase() === 'v' || mod.trim().toLowerCase() === 'vanilla' ) {
html += gridFormat( name.trim(), name.trim(), urls[name.trim()], num );
} else {
var img = name.trim() + ' (' + mod.trim() + ')';
html += gridFormat( img, 'Mods/' + mod.trim() + '/' + name.trim(), urls[img], num );
}
} );
}
} );
$grid.find( '> .border > span > .animated' ).append( html );
} );
} ).fail( function( error ) {
console.error( error );
} );
} ).fail( function( error ) {
console.error( error );
} );
}
}
function gridFormat( name, link, url, num ) {
var html = '<span class="image">';
if ( name ) {
if ( url ) {
html += '<a title="' + link + '" href="' + wikiURL + link.replace( / /g, '_' ) + '"><img width="32" height="32" src="' + url + '" alt="' + name + '"></a>';
if ( num ) {
html += '<span class="number"><a title="' + link + '" href="' + wikiURL + link.replace( / /g, '_' ) + '">' + num + '</a></span>';
}
} else {
html += '<a class="new" title="File:Grid ' + name + '.png" href="' + baseURL + 'w/index.php?title=Special:Upload&wpDestFile=Grid_' + name.replace( / /g, '_' ) + '.png"></a>';
}
} else {
html += ' ';
}
return html += '</span>';
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.