Advertisement
designbymerovingi

myCRED: Custom Transfer AJAX Script

Jan 13th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * myCRED Transfer jQuery
  3.  * Handles transfer requests and autocomplete of user login names.
  4.  * Adjusted to also include a user message.
  5.  * @requires jQuery
  6.  * @requires jQuery UI
  7.  * @requires jQuery Autocomplete
  8.  * @version 1.1
  9.  */
  10. jQuery(function($){
  11.     // Transfer function
  12.     // Mod: Add transfer message to ajax call
  13.     var transfer_creds = function( to, creds, label, message ) {
  14.         $.ajax({
  15.             type : "POST",
  16.             data : {
  17.                 action    : 'mycred-transfer-creds',
  18.                 sender    : myCRED.user_id,
  19.                 recipient : to,
  20.                 amount    : creds,
  21.                 token     : myCRED.token,
  22.                 tmessage  : message
  23.             },
  24.             dataType : "JSON",
  25.             url : myCRED.ajaxurl,
  26.             // Before we start
  27.             beforeSend : function() {
  28.                 // Prevent users from clicking multiple times
  29.                 $('.mycred-click').attr( 'value', myCRED.working );
  30.                 $('.mycred-click').attr( 'disabled', 'disabled' );
  31.             },
  32.             // On Successful Communication
  33.             success    : function( data ) {
  34.                 // Debug - uncomment to use
  35.                 //console.log( data );
  36.                 // Remove disable
  37.                 $('.mycred-click').attr( 'value', label );
  38.                 $('.mycred-click').removeAttr( 'disabled' );
  39.                 // Security token could not be verified.
  40.                 if ( data == 'error_1' ) {
  41.                     alert( myCRED.error_1 );
  42.                 }
  43.                 // Communications error.
  44.                 else if ( data == 'error_2' ) {
  45.                     alert( myCRED.error_2 );
  46.                 }
  47.                 // Recipient not found.
  48.                 else if ( data == 'error_3' ) {
  49.                     alert( myCRED.error_3 );
  50.                 }
  51.                 // Trying to send to excluded user.
  52.                 else if ( data == 'error_4' ) {
  53.                     alert( myCRED.error_4 );
  54.                 }
  55.                 // Incorrect amount.
  56.                 else if ( data == 'error_5' ) {
  57.                     alert( myCRED.error_5 );
  58.                 }
  59.                 // This myCRED Add-on has not yet been setup!
  60.                 else if ( data == 'error_6' ) {
  61.                     alert( myCRED.error_6 );
  62.                 }
  63.                 // Insufficient funds.
  64.                 else if ( data == 'error_7' ) {
  65.                     alert( myCRED.error_7 );
  66.                 }
  67.                 // Transfer Limit exceeded.
  68.                 else if ( data == 'error_8' ) {
  69.                     alert( myCRED.error_8 );
  70.                 }
  71.                 // Transfer Completed.
  72.                 else if ( data == 'ok' ) {
  73.                     alert( myCRED.completed );
  74.  
  75.                     if ( myCRED.reload == '1' )
  76.                         location.reload();
  77.                 }
  78.                 else {
  79.                     $('.mycred-click').attr( 'value', data );
  80.                     if ( myCRED.reload == '1' )
  81.                         location.reload();
  82.                 }
  83.             },
  84.             // Error (sent to console)
  85.             error      : function( jqXHR, textStatus, errorThrown ) {
  86.                 alert( myCRED.error_2 );
  87.                 location.reload();
  88.                 // Debug - uncomment to use
  89.                 //console.log( jqXHR );
  90.             }
  91.         });
  92.     };
  93.    
  94.     // Autocomplete
  95.     // @api http://api.jqueryui.com/autocomplete/
  96.     var cache = {};
  97.     $('input.mycred-autofill').autocomplete({
  98.         minLength: 2,
  99.         source: function( request, response ) {
  100.             var term = request.term;
  101.             if ( term in cache ) {
  102.                 response( cache[ term ] );
  103.                 return;
  104.             }
  105.            
  106.             var send = {
  107.                 action : "mycred-autocomplete",
  108.                 token  : myCRED.atoken,
  109.                 string : request
  110.             };
  111.             $.getJSON( myCRED.ajaxurl, send, function( data, status, xhr ) {
  112.                 cache[ term ] = data;
  113.                 // Debug - uncomment to use
  114.                 //console.log( data );
  115.                 //console.log( status );
  116.                 response( data );
  117.             });
  118.         },
  119.         messages: {
  120.             noResults: '',
  121.             results: function() {}
  122.         },
  123.         appendTo : 'div.transfer-to'
  124.     });
  125.    
  126.     // Attempt Transfer
  127.     $('.mycred-click').click(function(){
  128.         // To:
  129.         var receipient = $(this).parent().prev().children( 'div' ).children( 'input' );
  130.         var to = $(receipient).val();
  131.         // Debug - uncomment to use
  132.         //console.log( to );
  133.  
  134.         // Amount:
  135.         var amount = $(this).prev().children( 'input' );
  136.         var creds = $(amount).val();
  137.         // Debug - uncomment to use
  138.         //console.log( creds );
  139.  
  140.         // Mod: Get the message fields value
  141.         var message = $( '#mycred-transfer-message' ).val();
  142.  
  143.         // If elements are not emepty attempt transfer
  144.         if ( to != '' && creds != '' ) {
  145.             transfer_creds( to, creds, $(this).attr( 'value' ), message );
  146.         }
  147.     });
  148. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement