From aee9cee0db2240600433c822663d09bdc6b12455 Mon Sep 17 00:00:00 2001
From: Chris Hallberg <crhallberg@gmail.com>
Date: Tue, 10 Feb 2015 12:45:06 -0500
Subject: [PATCH] Tab Classes can now be configured to ignore AJAX loading. -
 Added a method to AbstractBase and adjusted themes and JS.

---
 module/VuFind/src/VuFind/RecordTab/AbstractBase.php | 11 +++++++++++
 module/VuFind/src/VuFind/RecordTab/Map.php          | 11 +++++++++++
 themes/bootstrap3/js/record.js                      |  6 ++++--
 themes/bootstrap3/templates/collection/view.phtml   |  9 ++++++---
 themes/bootstrap3/templates/record/view.phtml       | 10 +++++-----
 5 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/module/VuFind/src/VuFind/RecordTab/AbstractBase.php b/module/VuFind/src/VuFind/RecordTab/AbstractBase.php
index fad767f1ffe..a8867355000 100644
--- a/module/VuFind/src/VuFind/RecordTab/AbstractBase.php
+++ b/module/VuFind/src/VuFind/RecordTab/AbstractBase.php
@@ -74,6 +74,17 @@ abstract class AbstractBase implements TabInterface
         return true;
     }
 
+    /**
+     * Can this tab be loaded via AJAX?
+     *
+     * @return bool
+     */
+    public function supportsAjax()
+    {
+        // Assume we can load by AJAX; subclasses may add rules.
+        return true;
+    }
+
     /**
      * Set the record driver
      *
diff --git a/module/VuFind/src/VuFind/RecordTab/Map.php b/module/VuFind/src/VuFind/RecordTab/Map.php
index 7ba348de69a..6161004bd39 100644
--- a/module/VuFind/src/VuFind/RecordTab/Map.php
+++ b/module/VuFind/src/VuFind/RecordTab/Map.php
@@ -55,6 +55,17 @@ class Map extends AbstractBase
         $this->enabled = $enabled;
     }
 
+    /**
+     * Can this tab be loaded via AJAX?
+     *
+     * @return bool
+     */
+    public function supportsAjax()
+    {
+        // No, Google script magic required
+        return false;
+    }
+
     /**
      * Get the on-screen description for this tab.
      *
diff --git a/themes/bootstrap3/js/record.js b/themes/bootstrap3/js/record.js
index 7780b98786f..c6fbf98627d 100644
--- a/themes/bootstrap3/js/record.js
+++ b/themes/bootstrap3/js/record.js
@@ -150,9 +150,11 @@ function registerTabEvents() {
 
 function ajaxLoadTab(tabid) {
   var id = $('.hiddenId')[0].value;
-  // Grab the part of the url that is the Controller and Record ID
+  // Try to parse out the controller portion of the URL. If this fails, or if
+  // we're flagged to skip AJAX for this tab, just return true and let the
+  // browser handle it.
   var urlroot = document.URL.match(new RegExp('/[^/]+/'+id));
-  if(!urlroot) {
+  if(!urlroot || document.getElementById(tabid).parentNode.className.indexOf('noajax') > -1) {
     return true;
   }
   $.ajax({
diff --git a/themes/bootstrap3/templates/collection/view.phtml b/themes/bootstrap3/templates/collection/view.phtml
index 54f5b7bffca..2bd1d8491fb 100644
--- a/themes/bootstrap3/templates/collection/view.phtml
+++ b/themes/bootstrap3/templates/collection/view.phtml
@@ -61,13 +61,16 @@
         <? foreach ($this->tabs as $tab => $obj): ?>
         <? // add current tab to breadcrumbs if applicable:
           $desc = $obj->getDescription();
-          $isCurrent = (strtolower($this->activeTab) == strtolower($tab));
-          if ($isCurrent) {
+          $tab_classes = array();
+          if (0 === strcasecmp($this->activeTab, $tab)) {
+            $tab_classes[] = 'active';
             $this->layout()->breadcrumbs .= ' <li class="active">' . $this->transEsc($desc) . '</li>';
             $activeTabObj = $obj;
           }
+          if (!$obj->isVisible()) { $tab_classes = 'hidden'; }
+          if (!$obj->supportsAjax()) { $tab_classes = 'noajax'; }
         ?>
-        <li<?=$isCurrent ? ' class="active"' : ''?>>
+        <li<?=count($tab_classes) > 0 ? ' class="' . implode(' ', $tab_classes) . '"' : ''?>>
           <a id="<?=strtolower($tab) ?>" href="<?=$this->recordLink()->getTabUrl($this->driver, $tab)?>#tabnav"><?=$this->transEsc($desc)?></a>
         </li>
         <? endforeach; ?>
diff --git a/themes/bootstrap3/templates/record/view.phtml b/themes/bootstrap3/templates/record/view.phtml
index cf36cc3d14f..aca9e7e3981 100644
--- a/themes/bootstrap3/templates/record/view.phtml
+++ b/themes/bootstrap3/templates/record/view.phtml
@@ -55,14 +55,14 @@
         <? foreach ($this->tabs as $tab => $obj): ?>
         <? // add current tab to breadcrumbs if applicable:
           $desc = $obj->getDescription();
-          $isCurrent = (strtolower($this->activeTab) == strtolower($tab));
-          if ($isCurrent) {
+          $tab_classes = array();
+          if (0 === strcasecmp($this->activeTab, $tab)) {
+            $tab_classes[] = 'active';
             $this->layout()->breadcrumbs .= '<li class="active">' . $this->transEsc($desc) . '</li>';
             $activeTabObj = $obj;
           }
-          $tab_classes = array();
-          if ($isCurrent) $tab_classes[] = 'active';
-          if (!$obj->isVisible()) $tab_classes[] = 'hidden';
+          if (!$obj->isVisible()) { $tab_classes[] = 'hidden'; }
+          if (!$obj->supportsAjax()) { $tab_classes[] = 'noajax'; }
         ?>
         <li<?=count($tab_classes) > 0 ? ' class="' . implode(' ', $tab_classes) . '"' : ''?>>
           <a id="<?=strtolower($tab) ?>" href="<?=$this->recordLink()->getTabUrl($this->driver, $tab)?>#tabnav"><?=$this->transEsc($desc)?></a>
-- 
GitLab