custom.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. $(document).ready(function () {
  2. function cleanupStaleModalState() {
  3. // If no modal is visible, remove stale Bootstrap backdrop and body lock.
  4. if (!document.querySelector('.modal.show')) {
  5. document.querySelectorAll('.modal-backdrop').forEach(function (backdrop) {
  6. backdrop.remove();
  7. });
  8. document.body.classList.remove('modal-open');
  9. document.body.style.removeProperty('padding-right');
  10. document.body.style.removeProperty('overflow');
  11. }
  12. }
  13. cleanupStaleModalState();
  14. window.addEventListener('pageshow', cleanupStaleModalState);
  15. if ($('.main-alert').length) {
  16. setTimeout(function () {
  17. $('.main-alert').fadeTo(2000, 500).slideUp(500, function () {
  18. $(".main-alert").slideUp(500);
  19. })
  20. }, 3000);
  21. }
  22. let user = localStorage.getItem('user');
  23. if (user > 0) {
  24. let socket = new WebSocket(localStorage.getItem('socketAddress'));
  25. socket.onopen = function () {
  26. console.log("[WS] Connected. Listen messages for user " + user);
  27. };
  28. socket.onmessage = function (event) {
  29. let received = JSON.parse(event.data);
  30. if (parseInt(received.data.user_id) === parseInt(user)) {
  31. console.log(received);
  32. console.log(`[WS] Received data action: ${received.data.action}. Message: ${received.data.message}`);
  33. if(received.data.payload.download) {
  34. document.location.href = '/storage/export/' + received.data.payload.download;
  35. }
  36. if(received.data.payload.link) {
  37. document.location.href = received.data.payload.link;
  38. setTimeout(function () {
  39. document.location.reload();
  40. }, 2000);
  41. }
  42. setTimeout(function () {
  43. if (received.data.payload.error) {
  44. $('.alerts').append('<div class="main-alert2 alert alert-danger" role="alert">' + received.data.message + '</div>');
  45. } else {
  46. $('.alerts').append('<div class="main-alert2 alert alert-success" role="alert">' + received.data.message + '</div>');
  47. }
  48. setTimeout(function () {
  49. $('.main-alert2').fadeTo(2000, 500).slideUp(500, function () {
  50. $(".main-alert2").slideUp(500);
  51. })
  52. }, 3000);
  53. }, 1000
  54. );
  55. }
  56. };
  57. socket.onclose = function (event) {
  58. if (event.wasClean) {
  59. console.log(`[WS] Closed clear, code=${event.code} reason=${event.reason}`);
  60. } else {
  61. console.log('[WS] Connection lost', event);
  62. }
  63. };
  64. socket.onerror = function (error) {
  65. console.log(`[error] ${error}`);
  66. };
  67. }
  68. });