From e98f10fa9e2da040eb12a2519db0ddb59f43c0e7 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 22 Nov 2022 07:52:49 +0100 Subject: [PATCH] Bug 32307: Fix gallery when Coce is enabled Bug 28179 added the gallery in the staff interface, and bug 28180 in the OPAC. OPAC and staff have different code, because the cover image providers are different. On bug 28180 we noticed that the OPAC needed additional code to handle OpenLibrary, Google Jacket and Coce, to differ the creation of the gallery to prevent Chocolat to be instantiated before the links (img src) are effectively in the DOM. To acchieve that we introduced a wait_for_images function, and added a "done" flag to the 3 "JS modules". However we haven't ported this code for the staff interface, which is also using Coce. Note that there was a bug at the OPAC as well for COCE, the ajax jQuery function is async, and so 'done' was set even if the call was not finished. Test plan: 0. Do not apply this patch 1. Enable LocalCoverImages and IntranetCoce 2. Pick a bibliographic record with an image returned from Coce 3. Add a local cover image 4. Go to the detail page 5. Open the gallery => Notice that you can notice a JS error and a loading icon on the second image. This behaviour is not always broken, it depends on the speed of the Coce server. If you don't see the problem reload the page. 6. Apply the patch and repeat => Notice that the cover slider block is longer to display the icon, but that the error is gone. 7. Confirm that there is no regression at the OPAC We could improve the situation here, and we could display the images in the slider, especially if the first image is a local image. But that's for another bug... Signed-off-by: David Nind Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi (cherry picked from commit fd6cbf2273f71acd39bbc920400da50aed55030c) Signed-off-by: Martin Renvoize --- koha-tmpl/intranet-tmpl/js/coce.js | 6 ++-- .../prog/en/modules/catalogue/detail.tt | 33 +++++++++++++++++-- koha-tmpl/opac-tmpl/bootstrap/js/coce.js | 5 +-- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/js/coce.js b/koha-tmpl/intranet-tmpl/js/coce.js index a56ca61fef..c8829aeb98 100644 --- a/koha-tmpl/intranet-tmpl/js/coce.js +++ b/koha-tmpl/intranet-tmpl/js/coce.js @@ -20,7 +20,7 @@ KOHA.coce = { var id = $(this).attr("class"); // id=isbn if (id !== '') { ids.push(id); } }); - if (ids.length == 0) return; + if (ids.length == 0) { this.done = 1; return; } ids = ids.join(','); var coceURL = host + '/cover?id=' + ids + '&provider=' + provider; $.ajax({ @@ -44,7 +44,9 @@ KOHA.coce = { }); } }, - + }).then(function(){ + // Cannot access 'this' from here + KOHA.coce.done = 1; }); } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt index 714515ff59..c222d927c7 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt @@ -1604,9 +1604,36 @@ Note that permanent location is a code, and location may be an authval. }); }); - $(window).load(function() { - verify_cover_images(); - }); + + [% IF ( IntranetCoce && CoceProviders ) %] + let counter_wait = 0; + function wait_for_images(cb){ + + var loaded = 1; + counter_wait++; + + if ( loaded ) { + loaded = KOHA.coce.done; + } + + if (!loaded && counter_wait < 50) {// Do not wait more than 5 seconds + window.setTimeout(function(){wait_for_images(cb);}, 100); + } else { + if (counter_wait >= 50 ) { + console.log("Could not retrieve the images") + } + cb(); + } + } + + $(window).load(function() { + wait_for_images(verify_cover_images); + }); + [% ELSE %] + $(window).load(function() { + verify_cover_images; + }); + [% END %] [% IF ( Koha.Preference('NovelistSelectStaffEnabled') && Koha.Preference('NovelistSelectStaffProfile') && ( normalized_isbn || normalized_upc ) ) %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/js/coce.js b/koha-tmpl/opac-tmpl/bootstrap/js/coce.js index 090059ba92..c8829aeb98 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/js/coce.js +++ b/koha-tmpl/opac-tmpl/bootstrap/js/coce.js @@ -44,9 +44,10 @@ KOHA.coce = { }); } }, - + }).then(function(){ + // Cannot access 'this' from here + KOHA.coce.done = 1; }); - this.done = 1; } }; -- 2.39.5