SHARE
TWEET

Untitled

helgatheviki Dec 11th, 2019 110 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* global wc_orders_params */
  2. jQuery( function( $ ) {
  3.  
  4.     if ( typeof WC_SHIPPING_TRACKING === 'undefined' ) {
  5.         return false;
  6.     }
  7.  
  8.     const KEYCODE_ENTER = 13;
  9.     const KEYCODE_ESC = 27;
  10.  
  11.     /**
  12.      * WCOrdersTable class.
  13.      */
  14.     var WC_Shipping_Tracking = function() {
  15.         $( document )
  16.             .on( 'blur',     '.wc_shipping_tracking_input', this.onInputBlur )
  17.             .on( 'keyup',    '.wc_shipping_tracking_input', this.onInputKeyUp )
  18.             .on( 'keypress', '.wc_shipping_tracking_input', this.onInputKeyPress )
  19.             .on( 'click',    '.edit_wc_shipping_tracking',  this.onEditClick );
  20.     };
  21.  
  22.     /**
  23.      * Blur an input.
  24.      */
  25.     WC_Shipping_Tracking.prototype.onInputBlur = function( e ) {
  26.         console.log('blur');
  27.         if( $.trim( $( e.target ).val() ) != $( e.target ).data('original' ) ) {
  28.             WC_Shipping_Tracking.handle_save( $( e.target ) );
  29.         } else {
  30.             WC_Shipping_Tracking.handle_cancel( $( e.target ) );
  31.         }
  32.     };
  33.  
  34.     /**
  35.      * Clear on ESC
  36.      */
  37.     WC_Shipping_Tracking.prototype.onInputKeyUp = function( e ) {
  38.         if( e.which == KEYCODE_ESC ){
  39.             WC_Shipping_Tracking.handle_cancel( $( e.target ) );
  40.         }
  41.     };
  42.  
  43.     /**
  44.      * Save on enter.
  45.      */
  46.     WC_Shipping_Tracking.prototype.onInputKeyPress = function( e ) {
  47.         // Recommended to use e.which, it's normalized across browsers.
  48.         if( e.which == KEYCODE_ENTER ){
  49.             e.preventDefault();
  50.             WC_Shipping_Tracking.handle_save( $( e.target ) );
  51.         }
  52.     };
  53.  
  54.     /**
  55.      * Show the input when edit is clicked.
  56.      */
  57.     WC_Shipping_Tracking.prototype.onEditClick = function( e ) {
  58.         $( e.target ).hide().prev( '.tracking_number_input' ).show().focus().prev( '.wc_shipping_tracking_value' ).hide();
  59.     };
  60.  
  61.     /**
  62.      * Cancel an input.
  63.      */
  64.     WC_Shipping_Tracking.prototype.handle_cancel = function( $input ) {
  65.         if( typeof($input) === 'undefined' )
  66.             return false;
  67.  
  68.         $input.hide().val($input.data('original'));
  69.         $input.prev( '.wc_shipping_tracking_value' ).show();
  70.         $input.next( '.edit_wc_shipping_tracking' ).show();
  71.     };
  72.  
  73.     /**
  74.      * Save an input.
  75.      */
  76.     WC_Shipping_Tracking.prototype.handle_save = function( $input ) { console.log($input)
  77.         if( typeof($input) === 'undefined' )
  78.             return false;
  79.  
  80.         // Related objects.
  81.         var $display = $input.prev( '.wc_shipping_tracking_value' );
  82.         var $edit = $input.next( '.edit_wc_shipping_tracking' );
  83.         var $loader = $edit.next( '.tracking_number_loader' );
  84.  
  85.         // Show loader and disable input.
  86.         $loader.show().css( 'display', 'inline-block' );
  87.         $input.prop( 'disabled', true);
  88.  
  89.         // Get some data.
  90.         var order_id = $input.data( 'order_id' );
  91.         var tracking_number = $input.val();
  92.  
  93.         // Send via ajax.
  94.         $.ajax({
  95.             url: ajaxurl,
  96.             type: 'POST',
  97.             data: {
  98.                 'action' : 'save_tracking_number',
  99.                 'order_id' : order_id,
  100.                 'tracking_number' : tracking_number,
  101.                 'security' : WC_TRACKING_NUMBER.security
  102.              },
  103.         })
  104.  
  105.         .done( function( msg ){  console.log(msg);
  106.        
  107.             // Re-enable the input, show value, hide input.
  108.             $input.val( msg ).prop( 'disabled', false ).data( 'original', msg ).hide();
  109.             $loader.fadeOut( 'fast' );
  110.             $display.html( msg ).show();
  111.             $edit.show();
  112.  
  113.         });
  114.     }
  115.  
  116.     /*
  117.      * Initialize script.
  118.      */
  119.     new WC_Shipping_Tracking();
  120.  
  121. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top