User:Sross (Public Policy)/helpme.js

//

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

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];              helpMe.editToken = data.query.pages[pageid].edittoken;            }          }        }); },     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(helpMe.editToken),          dataType: 'json',          type: 'POST',          success: function( data ) {            if ( data.edit.result == "Success" ) {              window.location.reload;            } else {              helpMe.displayError( form, 'Unknown result from API.' );            }          },          error: function( xhr ) {            helpMe.displayError( form, 'Edit failed.' );            //console.debug( xhr.responseText );          }        }); },

launchDialog: function( interface ) {

// Open the dialog box interface.dialog('open'); // Retrieve the edit token so it will be available when the form is submitted helpMe.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 help templates var $awardList = [ 'Help me', 'Just a message' ];       // Define help interface $wlAwards = $(' ') .append( $(' ').html( 'What kind of help do you need? ' ).append( $(' ') ) ) .append( $(' ').html( 'Ask a question or describe your problem: ' ).append( $(' ') ) ) .dialog({           width: 500,            autoOpen: false,            title: 'Request help',            modal: true,            buttons: { "Get help": function {               $(this).dialog({buttons:{}});              if ( typeof helpMe.editToken !== 'undefined' ) {                // Perform edit to page                helpMe.displayProgress( 'awardForm', 'Adding award to page...' );                var template = ''+ $('#awardMessage').val +' ~';                helpMe.editPage( 'awardForm', 'Award', template );              } else {                helpMe.displayError( 'awardForm', 'Could not retrieve edit token.' );              }            }}          }); // Populate award selection list $.each( $awardList, function(index, value) { $('#awardSelect').append( $(' ').val(value).html(value) ); });

// List of message types var $messageList = [ 'Just a message', 'Ambassador welcome', ];

// Define message interface $wlMessage = $(' ') .append( $(' ').html( 'Select message or template: ' ).append( $(' ') ) ) .append( $(' ').html( 'Enter a message (without a signature), or leave blank for welcome template: ' ).append( $(' ') ) ) .dialog({           width: 500,            autoOpen: false,            title: 'Leave a message',            modal: true,            buttons: { "Leave message": function {               $(this).dialog({buttons:{}});              if ( typeof helpMe.editToken !== 'undefined' ) {                // Perform edit to page                helpMe.displayProgress( 'messageForm', 'Leaving your message...' );                var template = ''+$('#messageMessage').val +' ~';                helpMe.editPage( 'messageForm', 'Message', template );              } else {                helpMe.displayError( 'messageForm', 'Could not retrieve edit token.' );              }            }}          }); // Populate award selection list $.each( $messageList, function(index, value) { $('#messageSelect').append( $(' ').val(value).html(value) ); }); // Insert new tab into page var menuMessage = ' Message '; var menuAward = ' Help '; var helpMeMenu = ' helpMe Get help  <ul>'+menuMessage+menuAward+'</ul> '; $('#p-cactions').after(helpMeMenu);

} // close initialize function

} // close helpMe object helpMe.initialize; }) // close mw.loader } // close if //