sandbox_csv.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. $(document).ready(
  2. function ()
  3. {
  4. var readyStateStr = {
  5. 0: "Request not initialized",
  6. 1: "Server connection established",
  7. 2: "Response headers received",
  8. 3: "Processing request",
  9. 4: "Finished and response is ready"
  10. } ;
  11. function imapsync( cFunction ) {
  12. var xhr ;
  13. xhr = new XMLHttpRequest( ) ;
  14. var timerRefreshLog = setInterval( function() { refreshLog( xhr ) }, 6000 ) ;
  15. xhr.onreadystatechange = function( ) {
  16. cFunction( this, timerRefreshLog ) ;
  17. } ;
  18. var form_querystring = $("#form").serialize() ;
  19. $("#form_querystring").text( form_querystring ) ;
  20. xhr.open( "POST", "/cgi-bin/imapsync_csv_wrapper", true ) ;
  21. xhr.setRequestHeader( "Content-type",
  22. "application/x-www-form-urlencoded" ) ;
  23. xhr.send( form_querystring ) ;
  24. $("#output").text("Here comes the log!\n\n") ;
  25. }
  26. function handleRun( xhr, timerRefreshLog ) {
  27. $( "#console" ).text( "Status: " + xhr.status + " " + xhr.statusText + ".\n"
  28. + "State: " + readyStateStr[ xhr.readyState ] + "\n" ) ;
  29. if ( xhr.status == 200 && xhr.readyState == 4 ) {
  30. var headers = xhr.getAllResponseHeaders( ) ;
  31. clearInterval( timerRefreshLog ) ;
  32. refreshLog( xhr ) ; // a last time
  33. $("#bt-sync").prop("disabled", false) ;
  34. $("#csv_data").prop('readonly', false);
  35. }
  36. }
  37. function refreshLog( xhr ) {
  38. $( "#output" ).text( xhr.responseText ) ;
  39. }
  40. function abort()
  41. {
  42. var querystring = $("#form").serialize() + "&abort=on";
  43. var xhr;
  44. xhr = new XMLHttpRequest();
  45. xhr.onreadystatechange = function ()
  46. {
  47. handleAbort(xhr);
  48. };
  49. xhr.open("POST", "/cgi-bin/imapsync_csv_wrapper", true);
  50. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  51. xhr.send(querystring);
  52. }
  53. function handleAbort( xhr ) {
  54. $( "#abort" ).text( "Status: " + xhr.status + " " + xhr.statusText + ".\n"
  55. + "State: " + readyStateStr[ xhr.readyState ] + "\n" ) ;
  56. if ( xhr.status == 200 && xhr.readyState == 4 ) {
  57. var headers = xhr.getAllResponseHeaders( ) ;
  58. // $( "#abort" ).append( "\n" + headers + "\n" ) ;
  59. $( "#abort" ).append( xhr.responseText ) ;
  60. $("#csv_data").prop('readonly', false);
  61. $( "#bt-sync" ).prop("disabled", false);
  62. $( "#bt-abort" ).prop("disabled", false);
  63. }
  64. }
  65. $("#bt-sync").click(
  66. function ()
  67. {
  68. $("#bt-sync").prop("disabled", true) ;
  69. $("#csv_data").prop('readonly', true);
  70. $("#bt-abort").prop("disabled", false) ;
  71. $("#abort").text("") ;
  72. imapsync( handleRun ) ;
  73. }
  74. );
  75. $("#bt-abort").click(
  76. function ()
  77. {
  78. $("#bt-sync").prop("disabled", true);
  79. $("#csv_data").prop('readonly', true);
  80. $("#bt-abort").prop("disabled", true);
  81. abort();
  82. }
  83. );
  84. // in case of a manual refresh, start with
  85. $("#csv_data").prop('readonly', false);
  86. $("#bt-sync").prop("disabled", false);
  87. $("#bt-abort").prop("disabled", false);
  88. }
  89. ) ;