User:Jalexander-WMF/WGtemplate.js

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

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];              WGtemplate.editToken = data.query.pages[pageid].edittoken;            }          }        }); },     getEmailable: function { // Test to see if the 'E-mail this user' link exists WGtemplate.emailable = $('#t-emailuser').length ? true : false; },     editPage: function( form, template ) { $.ajax({         url: wgScriptPath + '/api.php?',          data: 'action=edit&title='+encodeURIComponent(mw.config.get('wgPageName'))+'&section=new&text='+encodeURIComponent(template)+'&format=json&token='+encodeURIComponent(WGtemplate.editToken),          dataType: 'json',          type: 'POST',          success: function( data ) {            if ( data.edit.result == "Success" ) {              window.location.reload;            } else {              WGtemplate.displayError( form, 'Unknown result from API.' );            }          },          error: function( xhr ) {            WGtemplate.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(WGtemplate.editToken),          dataType: 'json',          type: 'POST'        }); },

launchDialog: function( interface, emailOption ) { //If user is emailable, add email checkbox to forms that request it       if ( emailOption && typeof WGtemplate.emailable !== 'undefined' && WGtemplate.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 WGtemplate.getEditToken; },     initialize: function {

// List of all available options var $optionList = [ 'welcome' ];       // Define message interface $wlBarnstars = $(' ') .append( $(' ').html( 'Select template: ' ).append( $(' ') ) ) .append( $(' ').html( 'Enter any additional message (without a signature): ' ).append( $(' ') ) ) .dialog({           width: 500,            autoOpen: false,            title: 'Leave your message',            modal: true,            buttons: { "Leave message": function {               $(this).dialog({buttons:{}});              if ( typeof WGtemplate.editToken !== 'undefined' ) {                 if ( $('#barnstarForm input[name="notify"]').attr('checked') ) {                  // Send email                  WGtemplate.sendEmail( 'barnstarForm', 'You have a message on Wikipedia!', 'Hello '+wgTitle+'!\n\n A wiki guide just left you a message on your English Wikipedia talk page. You can click below to go check. \n https://en.wikipedia.org/wiki/User_talk:'+wgTitle+ );               }                // Perform edit to page                WGtemplate.displayProgress( 'barnstarForm', 'Adding message to page...' );                var message = $('#barnstarMessage').val;                if ( message.indexOf(  ) == -1 ) {                  message += ' Again, welcome to Wikipedia! ~';                }                var template = ' \n:'+ message;                WGtemplate.editPage( 'barnstarForm', template );              } else {                WGtemplate.displayError( 'barnstarForm', 'Could not retrieve edit token.' );              }            }}          }); // Populate barnstar select list $.each( $optionList, function(index, value) { $('#optionSelect').append( $(' ').val(value).html(value) ); });

// Insert new tab into page var menuBarnstar = ' Messages '; var WGtemplateMenu = ' WGtemplate   '+menuBarnstar+' '; $('#p-cactions').after(WGtemplateMenu);

} // close initialize function

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