From dbaf9c9e079fd0386791a52f5170015989e71d9b Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 12 May 2014 15:03:32 -0500
Subject: [PATCH] pjax: Fix state corruption

If a new PJAX request is started before the current PJAX request finishes,
leave the user interface in a consistent state.

  * Finish and re-start the loadingbar animation
  * Ensure the overlay is hidden
  * Cancel all current animations (without using clearTimeout)

This also fixes other nuances such as the help tips not working after the
PJAX ui state was corrupted.
---
 scp/js/scp.js | 47 ++++++++++++++++++++++++-----------------------
 1 file changed, 24 insertions(+), 23 deletions(-)

diff --git a/scp/js/scp.js b/scp/js/scp.js
index b489006e1..ec2f74fec 100644
--- a/scp/js/scp.js
+++ b/scp/js/scp.js
@@ -631,42 +631,43 @@ getConfig = (function() {
     }
 })();
 
-$(document).on('pjax:start', function(event) {
-    // Don't show the spinner on back button
-    if (event instanceof PopStateEvent)
-        return;
-
-    clearInterval(window.ticket_refresh);
-    // Clear all timeouts
-    var id = window.setTimeout(function() {}, 0);
-    while (id--) {
-      window.clearTimeout(id);
-    }
-
-    if ($("#loadingbar").length === 0) {
-      $("body").append("<div id='loadingbar'></div>");
-      $("#loadingbar").addClass("waiting").append($("<dt/><dd/>"));
-
-      // right
-      $("#loadingbar").width((50 + Math.random() * 30) + "%");
-      $('#overlay').css('background-color','white').fadeIn();
-    }
-
+$(document).on('pjax:click', function(options) {
+    if (window.ticket_refresh !== undefined)
+        clearInterval(window.ticket_refresh);
+    // Stop all animations
+    $(document).stop(false, true);
     // Cancel save-changes warning banner
     $(document).unbind('pjax:beforeSend.changed');
     $(window).unbind('beforeunload');
 });
-$(document).on('pjax:end', function() {
+
+$(document).on('pjax:send', function(event) {
+    if ($('#loadingbar').length !== 0) {
+        $('#loadingbar').remove();
+    }
+
+    $("body").append("<div id='loadingbar'></div>");
+    $("#loadingbar").addClass("waiting").append($("<dt/><dd/>"));
+
+    // right
+    $('#loadingbar').stop(false, true).width((50 + Math.random() * 30) + "%");
+    $('#overlay').css('background-color','white').fadeIn();
+});
+
+$(document).on('pjax:complete', function() {
     // right
     $("#loadingbar").width("101%").delay(200).fadeOut(400, function() {
         $(this).remove();
     });
-    $('#overlay').hide().removeAttr('style');
+    $('#overlay').stop(false, true).hide().removeAttr('style');
+});
 
+$(document).on('pjax:end', function() {
     // Close popups
     // Close tooltips
     $('.tip_box').empty().hide();
     $('.dialog .body').empty().parent().hide();
+    $('#overlay').hide();
 });
 
 // Quick note interface
-- 
GitLab