network-orders.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*global woocommerce_network_orders */
  2. (function( $, _, undefined ) {
  3. if ( 'undefined' === typeof woocommerce_network_orders ) {
  4. return;
  5. }
  6. var orders = [],
  7. promises = [], // Track completion (pass or fail) of ajax requests.
  8. deferred = [], // Tracks the ajax deferreds.
  9. $tbody = $( document.getElementById( 'network-orders-tbody' ) ),
  10. template = _.template( $( document.getElementById( 'network-orders-row-template') ).text() ),
  11. $loadingIndicator = $( document.getElementById( 'woocommerce-network-order-table-loading' ) ),
  12. $orderTable = $( document.getElementById( 'woocommerce-network-order-table' ) ),
  13. $noneFound = $( document.getElementById( 'woocommerce-network-orders-no-orders' ) );
  14. // No sites, so bail.
  15. if ( ! woocommerce_network_orders.sites.length ) {
  16. $loadingIndicator.removeClass( 'is-active' );
  17. $orderTable.removeClass( 'is-active' );
  18. $noneFound.addClass( 'is-active' );
  19. return;
  20. }
  21. $.each( woocommerce_network_orders.sites, function( index, value ) {
  22. promises[ index ] = $.Deferred();
  23. deferred.push( $.ajax( {
  24. url : woocommerce_network_orders.order_endpoint,
  25. data: {
  26. _wpnonce: woocommerce_network_orders.nonce,
  27. network_orders: true,
  28. blog_id: value
  29. },
  30. type: 'GET'
  31. } ).success(function( response ) {
  32. var orderindex;
  33. for ( orderindex in response ) {
  34. orders.push( response[ orderindex ] );
  35. }
  36. promises[ index ].resolve();
  37. }).fail(function (){
  38. promises[ index ].resolve();
  39. }) );
  40. } );
  41. if ( promises.length > 0 ) {
  42. $.when.apply( $, promises ).done( function() {
  43. var orderindex,
  44. currentOrder;
  45. // Sort orders, newest first
  46. orders.sort(function( a, b ) {
  47. var adate, bdate;
  48. adate = Date.parse( a.date_created_gmt );
  49. bdate = Date.parse( b.date_created_gmt );
  50. if ( adate === bdate ) {
  51. return 0;
  52. }
  53. if ( adate < bdate ) {
  54. return 1;
  55. } else {
  56. return -1;
  57. }
  58. });
  59. if ( orders.length > 0 ) {
  60. for ( orderindex in orders ) {
  61. currentOrder = orders[ orderindex ];
  62. $tbody.append( template( currentOrder ) );
  63. }
  64. $noneFound.removeClass( 'is-active' );
  65. $loadingIndicator.removeClass( 'is-active' );
  66. $orderTable.addClass( 'is-active' );
  67. } else {
  68. $noneFound.addClass( 'is-active' );
  69. $loadingIndicator.removeClass( 'is-active' );
  70. $orderTable.removeClass( 'is-active' );
  71. }
  72. } );
  73. }
  74. })( jQuery, _ );