From ad7e886c26ad79fdfecfac1d8fb55062711475d4 Mon Sep 17 00:00:00 2001
From: Owen Leonard <oleonard@myacpl.org>
Date: Thu, 21 Nov 2019 18:08:48 +0000
Subject: [PATCH] Bug 24084: PlainMARC view broken on OPAC if other $.ajax
 calls produce errors

This patch removes the use of jQuery's ajaxSetup() and load() to get the
"plain" MARC view and replaces it with $.get(). This allows for
error-handling on this specific AJAX request rather than all on the
page.

To test, apply the patch and view a bibliographic record in the OPAC.

 - Click the "MARC view" tab.
 - Click the "view plain" link.
  - The plain-text MARC view should load.
  - Clicking "view labeled" should return you to the original view.

To test error handling, edit opac-MARCdetail.tt line 185 and add a typo
to the URL, e.g. "opac-showmark.pl." Repeat the above steps. Clicking
the "view plain" link should trigger an error message: "Sorry, plain
view is temporarily unavailable."

Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
---
 .../bootstrap/en/modules/opac-MARCdetail.tt        | 62 ++++++++++------------
 1 file changed, 29 insertions(+), 33 deletions(-)

diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-MARCdetail.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-MARCdetail.tt
index 319a5aceb6..5954259186 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-MARCdetail.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-MARCdetail.tt
@@ -172,42 +172,38 @@
 [% BLOCK jsinclude %]
 <script>
 [% IF ( OPACXSLTDetailsDisplay ) %]
-$(document).ready(function(){
-    $.ajaxSetup({
-        error:function(x,e){
-            switch (x.status) {
-                case 200: break;
-                default:
-                $('#switchview').parent().html("<div class=\"dialog alert\">"+_("Sorry, plain view is temporarily unavailable")+".</div>");
-                $("#plainmarc").hide();
-                $("#labeledmarc").show();
-                break;
-            }
-        }
-    });
-
-    var loaded = 0;
-    var toggle = 0;
-    $("#switchview").on("click",function(e){
-        e.preventDefault();
-        if( toggle == 0){
-            $(this).text(_("view labeled"));
-            $("#labeledmarc").hide();
-            if(!loaded){
-                $("#plainmarc").show().html("<div style=\"margin:1em;padding:1em;border:1px solid #EEE;font-size:150%;\"><img src=\"[% interface | html %]/[% theme | html %]/images/loading.gif\" /> "+_("Loading")+"...</div>").load("/cgi-bin/koha/opac-showmarc.pl","id=[% biblio.biblionumber | html %]&viewas=html");
-                loaded = 1;
+    $(document).ready(function(){
+        var loaded = 0;
+        var toggle = 0;
+        $("#switchview").on("click",function(e){
+            e.preventDefault();
+            if( toggle == 0){
+                $(this).text(_("view labeled"));
+                $("#labeledmarc").hide();
+                if(!loaded){
+                    $("#plainmarc").show().html("<div style=\"margin:1em;padding:1em;border:1px solid #EEE;font-size:150%;\"><img src=\"[% interface | html %]/[% theme | html %]/images/loading.gif\" /> "+_("Loading")+"...</div>");
+                    var plain_marc = $.get( "/cgi-bin/koha/opac-showmarc.pl", { id: "[% biblio.biblionumber | html %]", viewas: "html" })
+                    .done(function( data ) {
+                        $("#plainmarc").html( data );
+                        loaded = 1;
+                    })
+                    .fail(function() {
+                        $('#switchview').parent().html("<div class=\"dialog alert\">"+_("Sorry, plain view is temporarily unavailable")+".</div>");
+                        $("#plainmarc").hide();
+                        $("#labeledmarc").show();
+                    });
+                } else {
+                    $("#plainmarc").show();
+                }
+                toggle = 1;
             } else {
-                $("#plainmarc").show();
+                $(this).text(_("view plain"));
+                $("#labeledmarc").show();
+                $("#plainmarc").hide();
+                toggle = 0;
             }
-            toggle = 1;
-        } else {
-            $(this).text(_("view plain"));
-            $("#labeledmarc").show();
-            $("#plainmarc").hide();
-            toggle = 0;
-        }
+        });
     });
-});
 [% END %]
 </script>
 [% END %]
-- 
2.11.0