From a6d0623c73bef862fd440b4c0253cdeff1648303 Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Wed, 26 Aug 2015 10:19:57 -0400
Subject: [PATCH] Fixed OpenURL click handling when loading Combined results
 with ajax.

---
 .../src/VuFind/View/Helper/Root/OpenUrl.php   |  9 +------
 themes/bootstrap3/js/openurl.js               | 15 ++++++-----
 .../templates/Helpers/openurl.phtml           |  4 +--
 .../templates/combined/results-ajax.phtml     | 26 ++++++++++++++++---
 4 files changed, 34 insertions(+), 20 deletions(-)

diff --git a/module/VuFind/src/VuFind/View/Helper/Root/OpenUrl.php b/module/VuFind/src/VuFind/View/Helper/Root/OpenUrl.php
index 9031cf92448..f329d162ad7 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/OpenUrl.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/OpenUrl.php
@@ -111,9 +111,6 @@ class OpenUrl extends \Zend\View\Helper\AbstractHelper
      */
     public function renderTemplate()
     {
-        // Static counter to ensure that each OpenURL gets a unique ID.
-        static $counter = 0;
-
         if (null !== $this->config && isset($this->config->url)) {
             // Trim off any parameters (for legacy compatibility -- default config
             // used to include extraneous parameters):
@@ -123,9 +120,6 @@ class OpenUrl extends \Zend\View\Helper\AbstractHelper
         }
 
         $embed = (isset($this->config->embed) && !empty($this->config->embed));
-        if ($embed) {
-            $counter++;
-        }
 
         $embedAutoLoad = isset($this->config->embed_auto_load)
             ? $this->config->embed_auto_load : false;
@@ -161,8 +155,7 @@ class OpenUrl extends \Zend\View\Helper\AbstractHelper
             'openUrlGraphicHeight' => empty($this->config->graphic_height)
                 ? false : $this->config->graphic_height,
             'openUrlEmbed' => $embed,
-            'openUrlEmbedAutoLoad' => $embedAutoLoad,
-            'openUrlId' => $counter
+            'openUrlEmbedAutoLoad' => $embedAutoLoad
         ];
 
         // Render the subtemplate:
diff --git a/themes/bootstrap3/js/openurl.js b/themes/bootstrap3/js/openurl.js
index 22c40afd9aa..8fe90723f38 100644
--- a/themes/bootstrap3/js/openurl.js
+++ b/themes/bootstrap3/js/openurl.js
@@ -18,6 +18,12 @@ function loadResolverLinks($target, openUrl) {
     });
 }
 
+function embedOpenUrlLinks(element) {
+    var openUrl = element.children('span.openUrl:first').attr('title');
+    element.removeClass('openUrlEmbed').hide();
+    loadResolverLinks(element.next('div.resolver').removeClass('hidden'), openUrl);
+}
+
 $(document).ready(function() {
     // assign action to the openUrlWindow link class
     $('a.openUrlWindow').click(function(){
@@ -28,13 +34,10 @@ $(document).ready(function() {
     });
 
     // assign action to the openUrlEmbed link class
-    $('a.openUrlEmbed').click(function(){
-        var params = extractClassParams(this);
-        var openUrl = $(this).children('span.openUrl:first').attr('title');
-        $(this).hide();
-        loadResolverLinks($('#openUrlEmbed'+params.openurl_id).removeClass('hidden'), openUrl);
+    $('a.openUrlEmbed').click(function() {
+        embedOpenUrlLinks($(this));
         return false;
     });
 
     $('a.openUrlEmbed.openUrlEmbedAutoLoad').trigger("click");
-});
\ No newline at end of file
+});
diff --git a/themes/bootstrap3/templates/Helpers/openurl.phtml b/themes/bootstrap3/templates/Helpers/openurl.phtml
index 51afd8087b7..a39a2c6dcdc 100644
--- a/themes/bootstrap3/templates/Helpers/openurl.phtml
+++ b/themes/bootstrap3/templates/Helpers/openurl.phtml
@@ -1,7 +1,7 @@
 <?
   $this->headScript()->appendFile("openurl.js");
   if ($this->openUrlEmbed) {
-    $classes = "fulltext openUrlEmbed openurl_id:{$this->openUrlId}"
+    $classes = "fulltext openUrlEmbed"
       . ($this->openUrlEmbedAutoLoad ? ' openUrlEmbedAutoLoad' : '');
     $class = ' class="' . $classes . '"';
   } elseif ($this->openUrlWindow) {
@@ -29,5 +29,5 @@
   <? endif; ?>
 </a>
 <? if ($this->openUrlEmbed): ?>
-  <div id="openUrlEmbed<?=$this->openUrlId?>" class="resolver hidden"><?=$this->transEsc('Loading')?>...</div>
+  <div class="resolver hidden"><?=$this->transEsc('Loading')?>...</div>
 <? endif; ?>
diff --git a/themes/bootstrap3/templates/combined/results-ajax.phtml b/themes/bootstrap3/templates/combined/results-ajax.phtml
index c6b15d64c67..d2c9eb3217d 100644
--- a/themes/bootstrap3/templates/combined/results-ajax.phtml
+++ b/themes/bootstrap3/templates/combined/results-ajax.phtml
@@ -1,15 +1,33 @@
 <?
+    // Make sure OpenURL support is loaded
+    $this->headScript()->appendFile("openurl.js");
+
     $view = $currentSearch['view'];
     $results = $view->results;
     $params = $results->getParams();
     $lookfor = $params->getDisplayQuery();
 
     // Set up Javascript for use below:
-    $loadJs = 'var url = path + "/Combined/Result?id=' . urlencode($searchClassId)
-        . '&lookfor=' . urlencode($lookfor) . '";'
-        . "\$('#combined_" . $this->escapeHtml($searchClassId) . "').load(url, '', function(responseText) { if (responseText.length == 0) $('#combined_" . $this->escapeHtml($searchClassId) . "').hide(); });";
+    $searchClassIdEncoded = urlencode($searchClassId);
+    $searchClassIdHtmlEscaped = $this->escapeHtml($searchClassId);
+    $lookforEncoded = urlencode($lookfor);
+    $loadJs = <<<JS
+$(document).ready(function(){
+    var url = path + '/Combined/Result?id=$searchClassIdEncoded&lookfor=$lookforEncoded';
+    $('#combined_$searchClassIdHtmlEscaped').load(url, '', function(responseText) {
+        if (responseText.length == 0) {
+            $('#combined_$searchClassIdHtmlEscaped').hide();
+        }
+        $('a.openUrlEmbed').click(function() {
+            embedOpenUrlLinks($(this));
+            return false;
+        });
+    });
+});
+JS;
+
 ?>
 <h2><?=$this->transEsc($currentSearch['label'])?></h2>
 <p><i class="fa fa-spinner fa-spin"></i> <?=$this->transEsc("Loading")?>...</p>
-<?=$this->inlineScript(\Zend\View\Helper\HeadScript::SCRIPT, '$(document).ready(function(){' . $loadJs . '});', 'SET')?>
+<?=$this->inlineScript(\Zend\View\Helper\HeadScript::SCRIPT, $loadJs, 'SET')?>
 <noscript><?=$this->transEsc('Please enable JavaScript.')?></noscript>
-- 
GitLab