tntsearch.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ((function($) {
  2. $(document).ready(function() {
  3. var Request, Toastr = null;
  4. if (typeof Grav !== 'undefined' && Grav && Grav.default && Grav.default.Utils) {
  5. Request = Grav.default.Utils.request;
  6. Toastr = Grav.default.Utils.toastr;
  7. }
  8. var indexer = $('#tntsearch-index, #admin-nav-quick-tray .tntsearch-reindex'),
  9. current = null, currentTray = null;
  10. if (!indexer.length) { return; }
  11. indexer.on('click', function(e) {
  12. e.preventDefault();
  13. var target = $(e.target),
  14. isTray = target.closest('#admin-nav-quick-tray').length,
  15. status = indexer.siblings('.tntsearch-status'),
  16. errorDetails = indexer.siblings('.tntsearch-error-details');
  17. current = status.clone(true);
  18. console.log(isTray);
  19. if (isTray) {
  20. target = target.is('i') ? target.parent() : target;
  21. currentTray = target.find('i').attr('class');
  22. target.find('i').attr('class', 'fa fa-fw fa-circle-o-notch fa-spin');
  23. }
  24. errorDetails
  25. .hide()
  26. .empty();
  27. status
  28. .removeClass('error success')
  29. .empty()
  30. .html('<i class="fa fa-circle-o-notch fa-spin" />');
  31. $.ajax({
  32. type: 'POST',
  33. url: GravAdmin.config.base_url_relative + '.json/task' + GravAdmin.config.param_sep + 'reindexTNTSearch',
  34. data: { 'admin-nonce': GravAdmin.config.admin_nonce }
  35. }).done(function(done) {
  36. if (done.status === 'success') {
  37. indexer.removeClass('critical').addClass('reindex');
  38. status.removeClass('error').addClass('success');
  39. Toastr.success(done.message);
  40. } else {
  41. indexer.removeClass('reindex').addClass('critical');
  42. status.removeClass('success').addClass('error');
  43. var error = done.message;
  44. if (done.details) {
  45. error += '<br />' + done.details;
  46. errorDetails
  47. .text(done.details)
  48. .show();
  49. status.replaceWith(current);
  50. }
  51. Toastr.error(error);
  52. }
  53. status.html(done.message);
  54. }).fail(function(error) {
  55. if (error.responseJSON && error.responseJSON.error) {
  56. indexer.removeClass('reindex').addClass('critical');
  57. errorDetails
  58. .text(error.responseJSON.error.message)
  59. .show();
  60. status.replaceWith(current);
  61. }
  62. }).always(function() {
  63. target.find('i').attr('class', currentTray);
  64. current = null;
  65. currentTray = null;
  66. });
  67. })
  68. });
  69. })(jQuery));