http://www.mediawiki.org/wiki/Special:Code/MediaWiki/89030
Revision: 89030 Author: janpaul123 Date: 2011-05-28 10:49:36 +0000 (Sat, 28 May 2011) Log Message: ----------- Restructured the core file per http://www.mediawiki.org/wiki/User:Krinkle/Extension_review/WikiLove (IIFE) Modified Paths: -------------- trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.core.js trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.init.js Modified: trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.core.js =================================================================== --- trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.core.js 2011-05-28 09:58:43 UTC (rev 89029) +++ trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.core.js 2011-05-28 10:49:36 UTC (rev 89030) @@ -1,38 +1,41 @@ -( function( $ ) { $.wikiLove = { +( function( $ ) { +$.wikiLove = (function(){ + +var options = {}, // options modifiable by the user + $dialog = null, // dialog jQuery object + currentTypeId = null, // id of the currently selected type (e.g. 'barnstar' or 'makeyourown') + currentSubtypeId = null, // id of the currently selected subtype (e.g. 'original' or 'special') + currentTypeOrSubtype = null, // content of the current (sub)type (i.e. an object with title, descr, text, etc.) + previewData = null, // data of the currently previewed thing is set here + emailable = false, + gallery = {}; - options: {}, // options modifiable by the user +return { optionsHook: function() { return {}; }, // hook that can be overridden by the user to modify options - $dialog: null, // dialog jQuery object - currentTypeId: null, // id of the currently selected type (e.g. 'barnstar' or 'makeyourown') - currentSubtypeId: null, // id of the currently selected subtype (e.g. 'original' or 'special') - currentTypeOrSubtype: null, // content of the current (sub)type (i.e. an object with title, descr, text, etc.) - previewData: null, // data of the currently previewed thing is set here - emailable: false, - gallery: {}, /* * Opens the dialog and builds it if necessary. */ openDialog: function() { - if ( $.wikiLove.$dialog === null ) { + if ( $dialog === null ) { // Test to see if the 'E-mail this user' link exists - $.wikiLove.emailable = $( '#t-emailuser' ).length ? true : false; + emailable = $( '#t-emailuser' ).length ? true : false; // Build a type list like this: var $typeList = $( '<ul id="mw-wikilove-types"></ul>' ); - for( var typeId in $.wikiLove.options.types ) { + for( var typeId in options.types ) { var $button = $( '<a href="#"></a>' ); var $buttonInside = $( '<div class="mw-wikilove-inside"></div>' ); - if( typeof $.wikiLove.options.types[typeId].icon == 'string' ) { + if( typeof options.types[typeId].icon == 'string' ) { $buttonInside.append( '<div class="mw-wikilove-icon-box"><img src="' - + mw.html.escape( $.wikiLove.options.types[typeId].icon ) + '"/></div>' ); + + mw.html.escape( options.types[typeId].icon ) + '"/></div>' ); } else { $buttonInside.addClass( 'mw-wikilove-no-icon' ); } - $buttonInside.append( '<div class="mw-wikilove-link-text">' + $.wikiLove.options.types[typeId].name + '</div>' ); + $buttonInside.append( '<div class="mw-wikilove-link-text">' + options.types[typeId].name + '</div>' ); $button.append( '<div class="mw-wikilove-left-cap"></div>'); $button.append( $buttonInside ); @@ -41,7 +44,7 @@ $typeList.append( $( '<li tabindex="0"></li>' ).append( $button ) ); } - $.wikiLove.$dialog = $( '\ + $dialog = $( '\ <div id="mw-wikilove-dialog">\ <div id="mw-wikilove-select-type">\ <span class="mw-wikilove-number">1</span>\ @@ -95,9 +98,9 @@ </form>\ </div>\ </div>' ); - $.wikiLove.$dialog.localize(); + $dialog.localize(); - $.wikiLove.$dialog.dialog({ + $dialog.dialog({ width: 800, position: ['center', 80], autoOpen: false, @@ -119,7 +122,7 @@ $( '#mw-wikilove-message' ).elastic(); // have the message textarea grow automatically } - $.wikiLove.$dialog.dialog( 'open' ); + $dialog.dialog( 'open' ); }, /* @@ -131,21 +134,21 @@ $( '#mw-wikilove-get-started' ).hide(); // always hide the get started section var newTypeId = $( this ).data( 'typeId' ); - if( $.wikiLove.currentTypeId != newTypeId ) { // only do stuff when a different type is selected - $.wikiLove.currentTypeId = newTypeId; - $.wikiLove.currentSubtypeId = null; // reset the subtype id + if( currentTypeId != newTypeId ) { // only do stuff when a different type is selected + currentTypeId = newTypeId; + currentSubtypeId = null; // reset the subtype id $( '#mw-wikilove-types a' ).removeClass( 'selected' ); $( this ).addClass( 'selected' ); // highlight the new type in the menu - if( typeof $.wikiLove.options.types[$.wikiLove.currentTypeId].subtypes == 'object' ) { + if( typeof options.types[currentTypeId].subtypes == 'object' ) { // we're dealing with subtypes here - $.wikiLove.currentTypeOrSubtype = null; // reset the (sub)type object until a subtype is selected + currentTypeOrSubtype = null; // reset the (sub)type object until a subtype is selected $( '#mw-wikilove-subtype' ).html( '' ); // clear the subtype menu - for( var subtypeId in $.wikiLove.options.types[$.wikiLove.currentTypeId].subtypes ) { + for( var subtypeId in options.types[currentTypeId].subtypes ) { // add all the subtypes to the menu while setting their subtype ids in jQuery data - var subtype = $.wikiLove.options.types[$.wikiLove.currentTypeId].subtypes[subtypeId]; + var subtype = options.types[currentTypeId].subtypes[subtypeId]; if ( typeof subtype.option != 'undefined' ) { $( '#mw-wikilove-subtype' ).append( $( '<option></option>' ).text( subtype.option ).data( 'subtypeId', subtypeId ) @@ -155,13 +158,13 @@ $( '#mw-wikilove-subtype' ).show(); // change and show the subtype label depending on the type - $( '#mw-wikilove-subtype-label' ).text( $.wikiLove.options.types[$.wikiLove.currentTypeId].select || mw.msg( 'wikilove-select-type' ) ); + $( '#mw-wikilove-subtype-label' ).text( options.types[currentTypeId].select || mw.msg( 'wikilove-select-type' ) ); $( '#mw-wikilove-subtype-label' ).show(); $.wikiLove.changeSubtype(); // update controls depending on the currently selected (i.e. first) subtype } else { // there are no subtypes, just use this type for the current (sub)type - $.wikiLove.currentTypeOrSubtype = $.wikiLove.options.types[$.wikiLove.currentTypeId]; + currentTypeOrSubtype = options.types[currentTypeId]; $( '#mw-wikilove-subtype' ).hide(); $( '#mw-wikilove-subtype-label' ).hide(); $.wikiLove.updateAllDetails(); // update controls depending on this type @@ -169,7 +172,7 @@ $( '#mw-wikilove-add-details' ).show(); $( '#mw-wikilove-preview' ).hide(); - $.wikiLove.previewData = null; + previewData = null; } }, @@ -179,14 +182,14 @@ changeSubtype: function() { // find out which subtype is selected var newSubtypeId = $( '#mw-wikilove-subtype option:selected' ).first().data( 'subtypeId' ); - if( $.wikiLove.currentSubtypeId != newSubtypeId ) { // only change stuff when a different subtype is selected - $.wikiLove.currentSubtypeId = newSubtypeId; - $.wikiLove.currentTypeOrSubtype = $.wikiLove.options.types[$.wikiLove.currentTypeId] - .subtypes[$.wikiLove.currentSubtypeId]; - $( '#mw-wikilove-subtype-description' ).html( $.wikiLove.currentTypeOrSubtype.descr ); + if( currentSubtypeId != newSubtypeId ) { // only change stuff when a different subtype is selected + currentSubtypeId = newSubtypeId; + currentTypeOrSubtype = options.types[currentTypeId] + .subtypes[currentSubtypeId]; + $( '#mw-wikilove-subtype-description' ).html( currentTypeOrSubtype.descr ); $.wikiLove.updateAllDetails(); $( '#mw-wikilove-preview' ).hide(); - $.wikiLove.previewData = null; + previewData = null; } }, @@ -197,44 +200,44 @@ $( '#mw-wikilove-dialog' ).find( '.mw-wikilove-error' ).remove(); // only show the description if it exists for this type or subtype - if( typeof $.wikiLove.currentTypeOrSubtype.descr == 'string' ) { + if( typeof currentTypeOrSubtype.descr == 'string' ) { $( '#mw-wikilove-subtype-description').show(); } else { $( '#mw-wikilove-subtype-description').hide(); } // show or hide header label and textbox depending on whether a predefined header exists - if( $.inArray( 'header', $.wikiLove.currentTypeOrSubtype.fields ) >= 0 ) { + if( $.inArray( 'header', currentTypeOrSubtype.fields ) >= 0 ) { $( '#mw-wikilove-header-label').show(); $( '#mw-wikilove-header' ).show(); } else { $( '#mw-wikilove-header-label').hide(); $( '#mw-wikilove-header' ).hide(); } - $( '#mw-wikilove-header' ).val( $.wikiLove.currentTypeOrSubtype.header || '' ); + $( '#mw-wikilove-header' ).val( currentTypeOrSubtype.header || '' ); // show or hide title label and textbox depending on whether a predefined title exists - if( $.inArray( 'title', $.wikiLove.currentTypeOrSubtype.fields ) >= 0 ) { + if( $.inArray( 'title', currentTypeOrSubtype.fields ) >= 0 ) { $( '#mw-wikilove-title-label').show(); $( '#mw-wikilove-title' ).show(); } else { $( '#mw-wikilove-title-label').hide(); $( '#mw-wikilove-title' ).hide(); } - $( '#mw-wikilove-title' ).val( $.wikiLove.currentTypeOrSubtype.title || '' ); + $( '#mw-wikilove-title' ).val( currentTypeOrSubtype.title || '' ); // show or hide image label and textbox depending on whether a predefined image exists - if( $.inArray( 'image', $.wikiLove.currentTypeOrSubtype.fields ) >= 0 ) { + if( $.inArray( 'image', currentTypeOrSubtype.fields ) >= 0 ) { $( '#mw-wikilove-image-label').show(); $( '#mw-wikilove-image' ).show(); } else { $( '#mw-wikilove-image-label').hide(); $( '#mw-wikilove-image' ).hide(); } - $( '#mw-wikilove-image' ).val( $.wikiLove.currentTypeOrSubtype.image || '' ); + $( '#mw-wikilove-image' ).val( currentTypeOrSubtype.image || '' ); - if( typeof $.wikiLove.currentTypeOrSubtype.gallery == 'object' ) { - if( $.wikiLove.currentTypeOrSubtype.gallery.imageList instanceof Array) { + if( typeof currentTypeOrSubtype.gallery == 'object' ) { + if( currentTypeOrSubtype.gallery.imageList instanceof Array) { $( '#mw-wikilove-gallery-label' ).show(); $( '#mw-wikilove-gallery' ).show(); $.wikiLove.showGallery(); // build gallery from array of images @@ -251,7 +254,7 @@ $( '#mw-wikilove-gallery' ).hide(); } - if( $.inArray( 'notify', $.wikiLove.currentTypeOrSubtype.fields ) >= 0 && $.wikiLove.emailable ) { + if( $.inArray( 'notify', currentTypeOrSubtype.fields ) >= 0 && emailable ) { $( '#mw-wikilove-notify' ).show(); } else { $( '#mw-wikilove-notify' ).hide(); @@ -269,11 +272,11 @@ if ( $( '#mw-wikilove-image' ).val().length <= 0 ) { - if( typeof $.wikiLove.currentTypeOrSubtype.gallery == 'object' ) { + if( typeof currentTypeOrSubtype.gallery == 'object' ) { $.wikiLove.showError( 'wikilove-err-image' ); return false; } else { - $( '#mw-wikilove-image' ).val( $.wikiLove.options.defaultImage ); + $( '#mw-wikilove-image' ).val( options.defaultImage ); } } if( $( '#mw-wikilove-header' ).val().length <= 0 ) { @@ -295,24 +298,24 @@ $.wikiLove.showError( 'wikilove-err-sig' ); return false; } - var text = $.wikiLove.prepareMsg( - $.wikiLove.currentTypeOrSubtype.text || $.wikiLove.options.defaultText, - $.wikiLove.currentTypeOrSubtype.imageSize, - $.wikiLove.currentTypeOrSubtype.backgroundColor, - $.wikiLove.currentTypeOrSubtype.borderColor + var text = prepareMsg( + currentTypeOrSubtype.text || options.defaultText, + currentTypeOrSubtype.imageSize, + currentTypeOrSubtype.backgroundColor, + currentTypeOrSubtype.borderColor ); - $.wikiLove.doPreview( '==' + $( '#mw-wikilove-header' ).val() + "==\n" + text ); - $.wikiLove.previewData = { + doPreview( '==' + $( '#mw-wikilove-header' ).val() + "==\n" + text ); + previewData = { 'header': $( '#mw-wikilove-header' ).val(), 'text': text, 'message': $( '#mw-wikilove-message' ).val(), - 'type': $.wikiLove.currentTypeId - + ($.wikiLove.currentSubtypeId !== null ? '-' + $.wikiLove.currentSubtypeId : '') + 'type': currentTypeId + + (currentSubtypeId !== null ? '-' + currentSubtypeId : '') }; - if ( $( '#mw-wikilove-notify-checkbox:checked' ).val() && $.wikiLove.emailable ) { - $.wikiLove.previewData.email = $.wikiLove.prepareMsg( $.wikiLove.currentTypeOrSubtype.email ); + if ( $( '#mw-wikilove-notify-checkbox:checked' ).val() && emailable ) { + previewData.email = prepareMsg( currentTypeOrSubtype.email ); } }, @@ -336,9 +339,9 @@ msg = msg.replace( '$2', $( '#mw-wikilove-title' ).val() ); // replace the title msg = msg.replace( '$3', $( '#mw-wikilove-image' ).val() ); // replace the image - var myImageSize = imageSize || $.wikiLove.options.defaultImageSize; - var myBackgroundColor = backgroundColor || $.wikiLove.options.defaultBackgroundColor; - var myBorderColor = borderColor || $.wikiLove.options.defaultBorderColor; + var myImageSize = imageSize || options.defaultImageSize; + var myBackgroundColor = backgroundColor || options.defaultBackgroundColor; + var myBorderColor = borderColor || options.defaultBorderColor; msg = msg.replace( '$4', myImageSize ); // replace the image size msg = msg.replace( '$5', myBackgroundColor ); // replace the background color @@ -387,8 +390,8 @@ */ submitSend: function( e ) { e.preventDefault(); - $.wikiLove.doSend( $.wikiLove.previewData.header, $.wikiLove.previewData.text, - $.wikiLove.previewData.message, $.wikiLove.previewData.type, $.wikiLove.previewData.email ); + $.wikiLove.doSend( previewData.header, previewData.text, + previewData.message, previewData.type, previewData.email ); }, /* @@ -447,18 +450,18 @@ */ showGallery: function() { $( '#mw-wikilove-gallery-content' ).html( '' ); - $.wikiLove.gallery = {}; + gallery = {}; $( '#mw-wikilove-gallery-spinner' ).fadeIn( 200 ); - if( typeof $.wikiLove.currentTypeOrSubtype.gallery.number == 'undefined' - || $.wikiLove.currentTypeOrSubtype.gallery.number <= 0 + if( typeof currentTypeOrSubtype.gallery.number == 'undefined' + || currentTypeOrSubtype.gallery.number <= 0 ) { - $.wikiLove.currentTypeOrSubtype.gallery.number = $.wikiLove.currentTypeOrSubtype.gallery.imageList.length; + currentTypeOrSubtype.gallery.number = currentTypeOrSubtype.gallery.imageList.length; } var titles = ''; - var imageList = $.wikiLove.currentTypeOrSubtype.gallery.imageList; - for( var i=0; i<$.wikiLove.currentTypeOrSubtype.gallery.number; i++ ) { + var imageList = currentTypeOrSubtype.gallery.imageList; + for( var i=0; i<currentTypeOrSubtype.gallery.number; i++ ) { // get a randomimage var id = Math.floor( Math.random() * imageList.length ); titles = titles + 'File:' + imageList[id] + '|'; @@ -476,7 +479,7 @@ 'prop' : 'imageinfo', 'iiprop' : 'mime|url', 'titles' : titles, - 'iiurlwidth' : $.wikiLove.currentTypeOrSubtype.gallery.width + 'iiurlwidth' : currentTypeOrSubtype.gallery.width }, dataType: 'json', type: 'POST', @@ -491,7 +494,7 @@ // build an image tag with the correct url and width $img = $( '<img/>' ) .attr( 'src', page.imageinfo[0].thumburl ) - .attr( 'width', $.wikiLove.currentTypeOrSubtype.gallery.width ) + .attr( 'width', currentTypeOrSubtype.gallery.width ) .hide() .load( function() { $( this ).css( 'display', 'inline-block' ); } ); $( '#mw-wikilove-gallery-content' ).append( @@ -502,15 +505,26 @@ e.preventDefault(); $( '#mw-wikilove-gallery a' ).removeClass( 'selected' ); $( this ).addClass( 'selected' ); - $( '#mw-wikilove-image' ).val( $.wikiLove.gallery[$( this ).attr( 'id' )] ); + $( '#mw-wikilove-image' ).val( gallery[$( this ).attr( 'id' )] ); }) ); - $.wikiLove.gallery['mw-wikilove-gallery-img-' + index] = page.title; + gallery['mw-wikilove-gallery-img-' + index] = page.title; index++; } } ); } }); + }, + + /* + * Init function which is called upon page load. Binds the WikiLove icon to opening the dialog. + */ + init: function() { + options = $.wikiLove.optionsHook(); + $( '#ca-wikilove a' ).click( function( e ) { + $.wikiLove.openDialog(); + e.preventDefault(); + }); } /* @@ -526,7 +540,7 @@ /* makeGallery: function() { $( '#mw-wikilove-gallery-content' ).html( '' ); - $.wikiLove.gallery = {}; + gallery = {}; $( '#mw-wikilove-gallery-spinner' ).fadeIn( 200 ); $.ajax({ @@ -536,19 +550,19 @@ 'format' : 'json', 'prop' : 'imageinfo', 'iiprop' : 'mime|url', - 'iiurlwidth' : $.wikiLove.currentTypeOrSubtype.gallery.width, + 'iiurlwidth' : currentTypeOrSubtype.gallery.width, 'generator' : 'categorymembers', - 'gcmtitle' : $.wikiLove.currentTypeOrSubtype.gallery.category, + 'gcmtitle' : currentTypeOrSubtype.gallery.category, 'gcmnamespace': 6, 'gcmsort' : 'timestamp', - 'gcmlimit' : $.wikiLove.currentTypeOrSubtype.gallery.total + 'gcmlimit' : currentTypeOrSubtype.gallery.total }, dataType: 'json', type: 'POST', success: function( data ) { // clear $( '#mw-wikilove-gallery-content' ).html( '' ); - $.wikiLove.gallery = {}; + gallery = {}; // if we have any images at all if( data.query) { @@ -556,7 +570,7 @@ var keys = Object.keys( data.query.pages ); // try to find "num" images to show - for( var i=0; i<$.wikiLove.currentTypeOrSubtype.gallery.num; i++ ) { + for( var i=0; i<currentTypeOrSubtype.gallery.num; i++ ) { // continue looking for a new image until we have found one thats valid // or until we run out of images while( keys.length > 0 ) { @@ -572,7 +586,7 @@ // build an image tag with the correct url and width var $img = $( '<img/>' ) .attr( 'src', page.imageinfo[0].url ) - .attr( 'width', $.wikiLove.currentTypeOrSubtype.gallery.width ) + .attr( 'width', currentTypeOrSubtype.gallery.width ) .hide() .load( function() { $( this ).css( 'display', 'inline-block' ); } ); @@ -585,18 +599,18 @@ e.preventDefault(); $( '#mw-wikilove-gallery a' ).removeClass( 'selected' ); $( this ).addClass( 'selected' ); - $( '#mw-wikilove-image' ).val( $.wikiLove.gallery[$( this ).attr( 'id' )] ); + $( '#mw-wikilove-image' ).val( gallery[$( this ).attr( 'id' )] ); }) ); // save the page title into an array so we know which image id maps to which title - $.wikiLove.gallery['mw-wikilove-gallery-img-' + i] = page.title; + gallery['mw-wikilove-gallery-img-' + i] = page.title; break; } } } } - if( $.wikiLove.gallery.length <= 0 ) { + if( gallery.length <= 0 ) { $( '#mw-wikilove-gallery' ).hide(); $( '#mw-wikilove-gallery-label' ).hide(); } @@ -607,6 +621,6 @@ }, */ }; + +}()); } ) ( jQuery ); - -mw.log( 'core loaded' ); \ No newline at end of file Modified: trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.init.js =================================================================== --- trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.init.js 2011-05-28 09:58:43 UTC (rev 89029) +++ trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.init.js 2011-05-28 10:49:36 UTC (rev 89030) @@ -1,15 +1,3 @@ -/* - * Init function which is called upon page load. Binds the WikiLove icon to opening the dialog. - */ - ( function( $ ) { -$.wikiLove.init = function() { - $.wikiLove.options = $.wikiLove.optionsHook(); - $( '#ca-wikilove a' ).click( function( e ) { - $.wikiLove.openDialog(); - e.preventDefault(); - }); -} - $( document ).ready( $.wikiLove.init ); } )( jQuery ); _______________________________________________ MediaWiki-CVS mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs
