trpdownloadApiFileBoxFeedback.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Updates the file status box based on the same JSON used to
  3. * update the progress bar.
  4. *
  5. * To set the URL of the JSON callback add the following code to the
  6. * preprocess hook for your download template. This is already taken
  7. * care of for you if you use the generic_download_page.tpl.php
  8. * @code
  9. drupal_add_js(
  10. array(
  11. 'trpdownloadApiProgressBar' => array(
  12. 'progressPath' => url('/tripal/progress/job/'.$trpdownload_key.'/'.$variables['job_id'])
  13. )
  14. ),
  15. 'setting'
  16. );
  17. * @endcode
  18. */
  19. (function ($) {
  20. Drupal.behaviors.trpdownloadApiFileBoxFeedback = {
  21. attach: function (context, settings) {
  22. if (typeof Drupal.settings.trpdownloadApiProgressBar === 'undefined') {
  23. return;
  24. }
  25. setTimeout(trpdownloadapiUpdateStatus, 1000);
  26. function trpdownloadapiUpdateStatus() {
  27. var btn = $('#download-btn');
  28. $.ajax({
  29. type: 'GET',
  30. url: Drupal.settings.trpdownloadApiProgressBar.progressPath,
  31. data: '',
  32. dataType: 'json',
  33. success: function (progress) {
  34. // Display errors.
  35. if (progress.status == 0) {
  36. return;
  37. }
  38. // Update display.
  39. /*pane.removeClass('file-not-ready');
  40. pane.removeClass('file-ready');
  41. pane.removeClass('file-error');
  42. pane.addClass(progress.file_class);*/
  43. // If our progress is complete then stop checking.
  44. if (progress.percentage == 100) {
  45. btn.removeAttr("disabled").removeClass('disabled');
  46. return;
  47. }
  48. // If we encountered an error, stop checking
  49. if (progress.file_class == 'file-error') {
  50. $('.progress-wrapper .message').addClass('text-danger');
  51. btn.parent().prepend('<p class="text-danger">'
  52. + 'An error occurred while processing your request. '
  53. + 'Please <a href="/contact">contact us</a> to resolve this issue. '
  54. + 'We apologize for any inconvenience this may have caused.</p>');
  55. btn.remove()
  56. return;
  57. }
  58. // Only if our progress is not complete, disable link
  59. // and, of course, schedule when to check again.
  60. else {
  61. btn.attr("disabled", true);
  62. setTimeout(trpdownloadapiUpdateStatus, 1000);
  63. }
  64. },
  65. error: function (xmlhttp) {
  66. pb.displayError(Drupal.ajaxError(xmlhttp, pb.uri));
  67. }
  68. });
  69. };
  70. }
  71. };
  72. }(jQuery));