r88965 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88964‎ | r88965 | r88966 >
Date:11:15, 27 May 2011
Author:janpaul123
Status:resolved (Comments)
Tags:
Comment:
Removed the old gallery function and moved some functionality to the current gallery function. It's now possible to show a random selection of images.
Modified paths:
  • /trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.core.js (modified) (history)
  • /trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.defaultOptions.js (modified) (history)

Diff [purge]

Index: trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.defaultOptions.js
@@ -271,11 +271,9 @@
272272 header: 'A beer for you!',
273273 text: '[[$3|left|150px]]\n$1\n\n~~~~\n<br style="clear: both"/>', // custom text
274274 gallery: {
275 - // right now we can only query the local wiki (not e.g. commons)
276 - category: 'Category:Beer',
277 - total: 100, // total number of pictures to retrieve, and to randomise
278 - num: 3, // number of pictures to show from the randomised set
279 - width: 145 // width of each picture in pixels in the interface (not in the template)
 275+ imageList: [ 'Cruzcampo.jpg', 'Glass_of_la_trappe_quadrupel.jpg', 'Hefeweizen.jpg', 'Krušovice_Mušketýr_in_glass.JPG', 'NCI_Visuals_Food_Beer.jpg', 'PintJug.jpg' ],
 276+ width: 145,
 277+ number: 3
280278 },
281279 icon: mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/extensions/WikiLove/modules/ext.wikiLove/images/icons/wikilove-icon-beer.png' // icon for left-side menu
282280 },
@@ -285,8 +283,9 @@
286284 header: 'A kitten for you!',
287285 text: '[[$3|left|150px]]\n$1\n\n~~~~\n<br style="clear: both"/>', // $3 is the image filename
288286 gallery: {
289 - imageList: ['File:Cucciolo gatto Bibo.jpg','File:Kitten (06) by Ron.jpg','File:Kitten-stare.jpg'],
290 - width: 145
 287+ imageList: [ 'Cucciolo gatto Bibo.jpg','Kitten (06) by Ron.jpg','Kitten-stare.jpg', 'Cat03.jpg', 'Kot_Leon.JPG', 'Greycat.jpg' ],
 288+ width: 145,
 289+ number: 3
291290 },
292291 icon: mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/extensions/WikiLove/modules/ext.wikiLove/images/icons/wikilove-icon-kitten.png' // icon for left-side menu
293292 },
Index: trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.core.js
@@ -440,60 +440,80 @@
441441 $.wikiLove.gallery = {};
442442 $( '#mw-wikilove-gallery-spinner' ).fadeIn( 200 );
443443
444 - $.each( $.wikiLove.currentTypeOrSubtype.gallery.imageList, function(index, value) {
 444+ if( typeof $.wikiLove.currentTypeOrSubtype.gallery.number == 'undefined'
 445+ || $.wikiLove.currentTypeOrSubtype.gallery.number <= 0
 446+ ) {
 447+ $.wikiLove.currentTypeOrSubtype.gallery.number = $.wikiLove.currentTypeOrSubtype.gallery.imageList.length;
 448+ }
445449
446 - $.ajax({
447 - url: mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api.php',
448 - data: {
449 - 'action' : 'query',
450 - 'format' : 'json',
451 - 'prop' : 'imageinfo',
452 - 'iiprop' : 'mime|url',
453 - 'titles' : value,
454 - 'iiurlwidth' : $.wikiLove.currentTypeOrSubtype.gallery.width
455 - },
456 - dataType: 'json',
457 - type: 'POST',
458 - success: function( data ) {
459 - $( '#mw-wikilove-gallery-spinner' ).fadeOut( 200 );
460 -
461 - if ( !data || !data.query || !data.query.pages ) {
462 - return;
 450+ var titles = '';
 451+ var imageList = $.wikiLove.currentTypeOrSubtype.gallery.imageList;
 452+ for( var i=0; i<$.wikiLove.currentTypeOrSubtype.gallery.number; i++ ) {
 453+ // get a randomimage
 454+ var id = Math.floor( Math.random() * imageList.length );
 455+ titles = titles + 'File:' + imageList[id] + '|';
 456+
 457+ // remove the random page from the keys array
 458+ imageList.splice(id, 1);
 459+ }
 460+
 461+ var index = 0;
 462+ $.ajax({
 463+ url: mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api.php',
 464+ data: {
 465+ 'action' : 'query',
 466+ 'format' : 'json',
 467+ 'prop' : 'imageinfo',
 468+ 'iiprop' : 'mime|url',
 469+ 'titles' : titles,
 470+ 'iiurlwidth' : $.wikiLove.currentTypeOrSubtype.gallery.width
 471+ },
 472+ dataType: 'json',
 473+ type: 'POST',
 474+ success: function( data ) {
 475+ $( '#mw-wikilove-gallery-spinner' ).fadeOut( 200 );
 476+
 477+ if ( !data || !data.query || !data.query.pages ) {
 478+ return;
 479+ }
 480+ $.each( data.query.pages, function( id, page ) {
 481+ if ( page.imageinfo && page.imageinfo.length ) {
 482+ // build an image tag with the correct url and width
 483+ $img = $( '<img/>' )
 484+ .attr( 'src', page.imageinfo[0].thumburl )
 485+ .attr( 'width', $.wikiLove.currentTypeOrSubtype.gallery.width )
 486+ .hide()
 487+ .load( function() { $( this ).css( 'display', 'inline-block' ); } );
 488+ $( '#mw-wikilove-gallery-content' ).append(
 489+ $( '<a href="#"></a>' )
 490+ .attr( 'id', 'mw-wikilove-gallery-img-' + index )
 491+ .append( $img )
 492+ .click( function( e ) {
 493+ e.preventDefault();
 494+ $( '#mw-wikilove-gallery a' ).removeClass( 'selected' );
 495+ $( this ).addClass( 'selected' );
 496+ $( '#mw-wikilove-image' ).val( $.wikiLove.gallery[$( this ).attr( 'id' )] );
 497+ })
 498+ );
 499+ $.wikiLove.gallery['mw-wikilove-gallery-img-' + index] = page.title;
 500+ index++;
463501 }
464 - $.each( data.query.pages, function( id, page ) {
465 - if ( page.imageinfo && page.imageinfo.length ) {
466 - // build an image tag with the correct url and width
467 - $img = $( '<img/>' )
468 - .attr( 'src', page.imageinfo[0].thumburl )
469 - .attr( 'width', $.wikiLove.currentTypeOrSubtype.gallery.width )
470 - .hide()
471 - .load( function() { $( this ).css( 'display', 'inline-block' ); } );
472 - $( '#mw-wikilove-gallery-content' ).append(
473 - $( '<a href="#"></a>' )
474 - .attr( 'id', 'mw-wikilove-gallery-img-' + index )
475 - .append( $img )
476 - .click( function( e ) {
477 - e.preventDefault();
478 - $( '#mw-wikilove-gallery a' ).removeClass( 'selected' );
479 - $( this ).addClass( 'selected' );
480 - $( '#mw-wikilove-image' ).val( $.wikiLove.gallery[$( this ).attr( 'id' )] );
481 - })
482 - );
483 - $.wikiLove.gallery['mw-wikilove-gallery-img-' + index] = page.title;
484 - }
485 - } );
486 - }
487 - });
488 -
 502+ } );
 503+ }
489504 });
490 - },
 505+ }
491506
492507 /*
493508 * This is a bit of a hack to show some random images. A predefined set of image infos are
494509 * retrieved using the API. Then we randomise this set ourselves and select some images to
495510 * show. Eventually we probably want to make a custom API call that does this properly and
496511 * also allows for using remote galleries such as Commons, which is now prohibited by JS.
 512+ *
 513+ * For now this function is disabled. It also shares code with the current gallery function,
 514+ * so when enabling it again it should be implemented cleaner with a custom API call, and
 515+ * without duplicate code between functions
497516 */
 517+ /*
498518 makeGallery: function() {
499519 $( '#mw-wikilove-gallery-content' ).html( '' );
500520 $.wikiLove.gallery = {};
@@ -575,6 +595,7 @@
576596 }
577597 });
578598 },
 599+ */
579600 };
580601 } ) ( jQuery );
581602

Comments

#Comment by Catrope (talk | contribs)   20:50, 2 June 2011
+						$img = $( '<img/>' )

$img is leaked to the global scope. This is still the case in trunk.

OK otherwise.

Status & tagging log