User:Ragesoss/mentormessages.js

// if (( wgCanonicalNamespace == 'User_talk' && wgAction == 'view' && skin == 'vector' )) { // Script depends on jQuery dialog widget mw.loader.using( 'jquery.ui', function {   // Construct object (to prevent namespace conflicts)    wikiLove = {

displayProgress: function( form, message ) { $('#'+form+' div').hide; // remove everything else from the dialog box $('#'+form).append ( $(' ').html( message+' ' ) ); },     displayError: function( form, error ) { $('#'+form+' div').hide; // remove everything else from the dialog box $('#'+form).append ( $(' ').html( 'Error: '+error ) ); },     getEditToken: function { $.ajax({         url: wgScriptPath + '/api.php?',          data: 'action=query&prop=info&intoken=edit&titles=Main%20Page&indexpageids&format=json',          dataType: 'json',          success: function( data ) {            if ( data.query.pages && data.query.pageids ) {              var pageid = data.query.pageids[0];              wikiLove.editToken = data.query.pages[pageid].edittoken;            }          }        }); },     getEmailable: function { // Test to see if the 'E-mail this user' link exists wikiLove.emailable = $('#t-emailuser').length ? true : false; },     editPage: function( form, summary, template ) { $.ajax({         url: wgScriptPath + '/api.php?',          data: 'action=edit&title='+encodeURIComponent(mw.config.get('wgPageName'))+'&section=new&summary='+encodeURIComponent(summary)+'&text='+encodeURIComponent(template)+'&format=json&token='+encodeURIComponent(wikiLove.editToken),          dataType: 'json',          type: 'POST',          success: function( data ) {            if ( data.edit.result == "Success" ) {              window.location.reload;            } else {              wikiLove.displayError( form, 'Unknown result from API.' );            }          },          error: function( xhr ) {            wikiLove.displayError( form, 'Edit failed.' );            //console.debug( xhr.responseText );          }        }); },     sendEmail: function( form, subject, text ) { $.ajax({         url: wgScriptPath + '/api.php?',          data: 'action=emailuser&target='+encodeURIComponent(wgTitle)+'&subject='+encodeURIComponent(subject)+'&text='+encodeURIComponent(text)+'&format=json&token='+encodeURIComponent(wikiLove.editToken),          dataType: 'json',          type: 'POST'        }); },

launchDialog: function( interface, emailOption ) { // If user is emailable, add email checkbox to forms that request it       if ( emailOption && typeof wikiLove.emailable !== 'undefined' && wikiLove.emailable ) { if ( typeof interface.data('emailCheckboxAppended') == 'undefined' ) { interface.append( $(' ').html(' Notify user by email') ); interface.data('emailCheckboxAppended', true); }       }        // Open the dialog box interface.dialog('open'); // Retrieve the edit token so it will be available when the form is submitted wikiLove.getEditToken; },     initialize: function {

// Function to make a list of images selectable $.fn.selectItem = function { return this.each(function {           var containerId = this.id;            var customId = 0;            $('#'+containerId+' img').css('border', '8px solid transparent');            $('#'+containerId+' img').each(function { if (this.id == '') { this.id = 'select_img_' + customId; customId++; }             $('#'+this.id).click(function {                  $('#'+containerId+'Selected').val(this.title);                  $('#'+containerId+' img').css('border', '8px solid transparent');                  $('#'+this.id).css('border', '8px solid #AED0EA');              }) });         });        };        // List of all available templates var $barnstarList = [ 'Message' ];       // Define barnstar interface $wlBarnstars = $(' ') .append( $(' ').html( 'Select message or template type: ' ).append( $(' ') ) ) .append( $(' ').html( 'Enter a message (without a signature): ' ).append( $(' ') ) ) .dialog({           width: 500,            autoOpen: false,            title: 'Leave a message',            modal: true,            buttons: { "Leave the message": function {               $(this).dialog({buttons:{}});              if ( typeof wikiLove.editToken !== 'undefined' ) {                if ( $('#barnstarForm input[name="notify"]').attr('checked') ) {                  // Send email                  wikiLove.sendEmail( 'barnstarForm', 'You\'ve got a message from your Wikipedia mentor!', 'Hello '+wgTitle+'!\n\nI just left you a message on your English Wikipedia talk page.' );                }                // Perform edit to page                wikiLove.displayProgress( 'barnstarForm', 'Leaving the message for your mentee...' );                var template = $('#barnstarMessage').val +' ~';                wikiLove.editPage( 'barnstarForm', 'a message from your mentor', template ); } else { wikiLove.displayError( 'barnstarForm', 'Could not retrieve edit token.' ); }           }}          });        // Populate barnstar select list        $.each( $barnstarList, function(index, value) { $('#barnstarSelect').append( $('  ').val(value).html(value) ); });

// List of all available foods var $foodList = [ 'brownie', 'cookie', 'cupcake', 'pie' ];

// Define food interface $wlFood = $(' ') .append( $(' ').html( 'Select food: ' ).append( $(' ') ) ) .append( $(' ').html( 'Enter a personalized message (optional): ' ).append( $(' ') ) ) .dialog({           width: 500,            autoOpen: false,            title: 'Give some food',            modal: true,            buttons: { "Give food": function {               $(this).dialog({buttons:{}});              if ( typeof wikiLove.editToken !== 'undefined' ) {                $selectedFood = $('#foodSelect').val;                // Perform edit to page                wikiLove.displayProgress( 'foodForm', 'Adding '+$selectedFood+' to page...' );                if ( $('#foodMessage').val !=  ) {                  var template = +$('#foodMessage').val+;                } else {                  var template = ;                }                wikiLove.editPage( 'foodForm', 'A '+$selectedFood+' for you!', template );              } else {                wikiLove.displayError( 'foodForm', 'Could not retrieve edit token.' );              }            }}          });

// Populate food select list $.each( $foodList, function(index, value) { $('#foodSelect').append( $(' ').val(value).html(value) ); });

// Define kitten interface $wlKittens = $(' ') .html( '' ) .append( $(' ').html( 'Select which kitten you want to give:' ) ) .append( $(' ')           .html( '' )          ) .dialog({           width: 400,            autoOpen: false,            title: 'Give a kitten',            modal: true,            buttons: { "Give kitten": function {              $(this).dialog({buttons:{}});              if ( typeof wikiLove.editToken !== 'undefined' ) {                if ( $('#kittenForm input[name="notify"]').attr('checked') ) {                  // Send email                  wikiLove.sendEmail( 'kittenForm', 'You\'ve been given a kitten!', 'Hello '+wgTitle+'!\n\nI just left a kitten for you on your English Wikipedia talk page.' );                }                // Perform edit to page                wikiLove.displayProgress( 'kittenForm', 'Adding kitten to page...' );                var template = '';                wikiLove.editPage( 'kittenForm', 'A kitten for you!', template );              } else {                wikiLove.displayError( 'kittenForm', 'Could not retrieve edit token.' ); }           }}          });        // Apply special image selection interface for choosing kittens        $('#kitten').selectItem;        // Insert new tab into page        var menuBarnstar = ' Barnstar ';        var menuFood = ' Food</a> </li>';        var menuKitten = '<li id="wl-kitten"> <a onclick="wikiLove.launchDialog( $wlKittens, false ); return false;" href="#">Kitten</a> </li>';        var wikiLoveMenu = ' WikiLove <a href="#" style="padding:0 0.4em;width:auto;"><span style="display:inline-block;height:2.5em;width:16px;margin-right:14px;background-position:0 60%;background-image:url(http://upload.wikimedia.org/wikipedia/commons/b/bd/Silk_heart_icon.png);background-repeat:no-repeat;"> </a> <ul>'+menuBarnstar+menuFood+menuKitten+'</ul>  '; $('#p-cactions').after(wikiLoveMenu);

} // close initialize function

} // close wikiLove object wikiLove.initialize; wikiLove.getEmailable; }) // close mw.loader } // close if //