wc-orders.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* global wc_orders_params */
  2. jQuery( function( $ ) {
  3. if ( typeof wc_orders_params === 'undefined' ) {
  4. return false;
  5. }
  6. /**
  7. * WCOrdersTable class.
  8. */
  9. var WCOrdersTable = function() {
  10. $( document )
  11. .on( 'click', '.post-type-shop_order .wp-list-table tbody td', this.onRowClick )
  12. .on( 'click', '.order-preview:not(.disabled)', this.onPreview );
  13. };
  14. /**
  15. * Click a row.
  16. */
  17. WCOrdersTable.prototype.onRowClick = function( e ) {
  18. if ( $( e.target ).filter( 'a, a *, .no-link, .no-link *' ).length ) {
  19. return true;
  20. }
  21. if ( window.getSelection && window.getSelection().toString().length ) {
  22. return true;
  23. }
  24. var $row = $( this ).closest( 'tr' ),
  25. href = $row.find( 'a.order-view' ).attr( 'href' );
  26. if ( href && href.length ) {
  27. e.preventDefault();
  28. if ( e.metaKey || e.ctrlKey ) {
  29. window.open( href, '_blank' );
  30. } else {
  31. window.location = href;
  32. }
  33. }
  34. };
  35. /**
  36. * Preview an order.
  37. */
  38. WCOrdersTable.prototype.onPreview = function() {
  39. var $previewButton = $( this ),
  40. $order_id = $previewButton.data( 'order-id' );
  41. if ( $previewButton.data( 'order-data' ) ) {
  42. $( this ).WCBackboneModal({
  43. template: 'wc-modal-view-order',
  44. variable : $previewButton.data( 'order-data' )
  45. });
  46. } else {
  47. $previewButton.addClass( 'disabled' );
  48. $.ajax({
  49. url: wc_orders_params.ajax_url,
  50. data: {
  51. order_id: $order_id,
  52. action : 'woocommerce_get_order_details',
  53. security: wc_orders_params.preview_nonce
  54. },
  55. type: 'GET',
  56. success: function( response ) {
  57. $( '.order-preview' ).removeClass( 'disabled' );
  58. if ( response.success ) {
  59. $previewButton.data( 'order-data', response.data );
  60. $( this ).WCBackboneModal({
  61. template: 'wc-modal-view-order',
  62. variable : response.data
  63. });
  64. }
  65. }
  66. });
  67. }
  68. return false;
  69. };
  70. /**
  71. * Init WCOrdersTable.
  72. */
  73. new WCOrdersTable();
  74. } );