From b4ef3df833d09e24d4babee9ab304563692fe721 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Tue, 8 Apr 2014 23:24:36 -0500
Subject: [PATCH] pjax: Fix help tip fetching

---
 include/class.osticket.php   |  7 ++++++-
 include/staff/footer.inc.php |  2 +-
 include/staff/header.inc.php |  6 +++++-
 scp/forms.php                |  3 ++-
 scp/js/tips.js               | 30 ++++++++++++++++++------------
 scp/settings.php             |  3 ++-
 6 files changed, 34 insertions(+), 17 deletions(-)

diff --git a/include/class.osticket.php b/include/class.osticket.php
index abd563839..ce5d63544 100644
--- a/include/class.osticket.php
+++ b/include/class.osticket.php
@@ -42,6 +42,7 @@ class osTicket {
 
     var $title; //Custom title. html > head > title.
     var $headers;
+    var $pjax_extra;
 
     var $config;
     var $session;
@@ -163,13 +164,17 @@ class osTicket {
         return $replacer->replaceVars($input);
     }
 
-    function addExtraHeader($header) {
+    function addExtraHeader($header, $pjax_script=false) {
         $this->headers[md5($header)] = $header;
+        $this->pjax_extra[md5($header)] = $pjax_script;
     }
 
     function getExtraHeaders() {
         return $this->headers;
     }
+    function getExtraPjax() {
+        return $this->pjax_extra;
+    }
 
     function setPageTitle($title) {
         $this->title = $title;
diff --git a/include/staff/footer.inc.php b/include/staff/footer.inc.php
index 858874e07..f7f2d5024 100644
--- a/include/staff/footer.inc.php
+++ b/include/staff/footer.inc.php
@@ -25,7 +25,7 @@ if(is_object($thisstaff) && $thisstaff->isStaff()) { ?>
 if ($.support.pjax) {
   $(document).on('click', 'a', function(event) {
     if (!$(this).hasClass('no-pjax'))
-        $.pjax.click(event, {container: $('#content')})
+        $.pjax.click(event, {container: $('#content'), timeout: 2000})
   })
 }
 </script>
diff --git a/include/staff/header.inc.php b/include/staff/header.inc.php
index ac0e62594..9de588c43 100644
--- a/include/staff/header.inc.php
+++ b/include/staff/header.inc.php
@@ -113,7 +113,11 @@
         }
         ?>
     </ul>
-    <div id="content" data-pjax-container>
+    <div id="content">
+<?php } elseif ($pjax = $ost->getExtraPjax()) { # endif X_PJAX ?>
+    <script type="text/javascript">
+    <?php foreach ($pjax as $s) echo $s.";"; ?>
+    </script>
 <?php } # endif X_PJAX ?>
         <?php if($errors['err']) { ?>
             <div id="msg_error"><?php echo $errors['err']; ?></div>
diff --git a/scp/forms.php b/scp/forms.php
index 43e66242a..18303449e 100644
--- a/scp/forms.php
+++ b/scp/forms.php
@@ -126,7 +126,8 @@ $page='dynamic-forms.inc.php';
 if($form || ($_REQUEST['a'] && !strcasecmp($_REQUEST['a'],'add')))
     $page='dynamic-form.inc.php';
 
-$ost->addExtraHeader('<meta name="tip-namespace" content="forms" />');
+$ost->addExtraHeader('<meta name="tip-namespace" content="forms" />',
+    "$('#content').data('tipNamespace', 'forms');");
 $nav->setTabActive('manage');
 require(STAFFINC_DIR.'header.inc.php');
 require(STAFFINC_DIR.$page);
diff --git a/scp/js/tips.js b/scp/js/tips.js
index 9fdd22617..a4a9e8b4c 100644
--- a/scp/js/tips.js
+++ b/scp/js/tips.js
@@ -24,20 +24,26 @@ jQuery(function() {
             });
     },
     getHelpTips = (function() {
-        var dfd = $.Deferred(),
-            requested = false,
-            namespace = $('meta[name=tip-namespace]').attr('content');
-        return function() {
-            if (namespace && dfd.state() != 'resolved' && !requested)
-                requested = $.ajax({
+        var dfd, cache = {};
+        return function(namespace) {
+            var namespace = namespace
+                || $('#content').data('tipNamespace')
+                || $('meta[name=tip-namespace]').attr('content');
+            if (!namespace)
+                return false;
+            else if (!cache[namespace])
+                cache[namespace] = {
+                  dfd: dfd = $.Deferred(),
+                  ajax: $.ajax({
                     url: "ajax.php/help/tips/" + namespace,
                     dataType: 'json',
-                    success: function (json_config) {
-                        dfd.resolve(json_config);
-                    }
-                });
-            return dfd;
-        }
+                    success: $.proxy(function (json_config) {
+                        this.resolve(json_config);
+                    }, dfd)
+                  })
+                }
+            return cache[namespace].dfd;
+        };
     })();
 
     //Generic tip.
diff --git a/scp/settings.php b/scp/settings.php
index a4243fd95..96ee4b97c 100644
--- a/scp/settings.php
+++ b/scp/settings.php
@@ -48,7 +48,8 @@ if($page && $_POST && !$errors) {
 }
 
 $config=($errors && $_POST)?Format::input($_POST):Format::htmlchars($cfg->getConfigInfo());
-$ost->addExtraHeader('<meta name="tip-namespace" content="'.$page[1].'" />');
+$ost->addExtraHeader('<meta name="tip-namespace" content="'.$page[1].'" />',
+    "$('#content').data('tipNamespace', '".$page[1]."');");
 
 $nav->setTabActive('settings', ('settings.php?t='.$target));
 require_once(STAFFINC_DIR.'header.inc.php');
-- 
GitLab