step-processing.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. jQuery(document).ready(function($) {
  2. var progressbar = $( "#progressbar" ),
  3. progressLabel = $( ".progress-label" );
  4. progressbar.progressbar({
  5. value: false,
  6. change: function() {
  7. var value = parseInt( progressbar.progressbar( "value" ) );
  8. if ( value == 90 ) {
  9. nfProgressBar.currentLabel = 1;
  10. } else if ( value % 10 == 0 ) {
  11. nfProgressBar.changeTextLabel();
  12. }
  13. var text = nfProgressBar.getTextLabel();
  14. progressLabel.text( text + " " + progressbar.progressbar( "value" ) + "%" );
  15. },
  16. complete: function() {
  17. progressLabel.text( "Complete!" );
  18. }
  19. });
  20. if ( nfProcessingAction != 'none' ) {
  21. var nfProgressBar = {
  22. labels: nf_processing.step_labels,
  23. currentLabel: 0,
  24. getTextLabel: function() {
  25. var label = this.labels[ this.currentLabel ];
  26. return label;
  27. },
  28. changeTextLabel: function() {
  29. var max = Object.keys( this.labels ).length;
  30. if ( max == 1 ) {
  31. max = 0;
  32. }
  33. var labelNum = Math.floor( Math.random() * ( max - 2 + 1 ) ) + 1;
  34. this.currentLabel = labelNum;
  35. }
  36. };
  37. var nfProcessing = {
  38. setup: function() {
  39. // Figure out when we're going to change the size of the bar.
  40. this.interval = Math.floor( 100 / parseInt( this.totalSteps ) );
  41. },
  42. process: function() {
  43. $.post( ajaxurl, { step: this.step, total_steps: nfProcessing.totalSteps, args: this.args, action: nfProcessingAction }, function( response ) {
  44. response = $.parseJSON( response );
  45. nfProcessing.step = response.step;
  46. nfProcessing.totalSteps = response.total_steps;
  47. nfProcessing.args = response.args;
  48. nfProcessing.errors = response.errors;
  49. if ( nfProcessing.errors ) {
  50. $( "#nf-upgrade-errors").removeClass('hidden');
  51. $.each( nfProcessing.errors, function( index, error ) {
  52. $(".nf-upgrade-errors-list").append('<li>ERROR: ' + error + '</li>');
  53. });
  54. }
  55. if ( nfProcessing.runSetup == 1 ) {
  56. nfProcessing.setup();
  57. nfProcessing.runSetup = 0;
  58. }
  59. if ( ! response.complete ) {
  60. nfProcessing.progress();
  61. nfProcessing.process();
  62. } else {
  63. progressbar.progressbar( "value", 100 );
  64. if ( typeof response.redirect != 'undefined' && response.redirect != '' ) {
  65. document.location.href = response.redirect;
  66. }
  67. }
  68. });
  69. },
  70. progress: function() {
  71. var val = progressbar.progressbar( "value" ) || 0;
  72. progressbar.progressbar( "value", val + this.interval );
  73. },
  74. step: 'loading',
  75. totalSteps: 0,
  76. runSetup: 1,
  77. interval: 0,
  78. args: nfProcessingArgs,
  79. }
  80. nfProcessing.process();
  81. }
  82. });