Koha/koha-tmpl/opac-tmpl/bootstrap/js/google-jackets.js
Jonathan Druart 819095aa21 Bug 28180: Correctly guess if images have been loaded
window.load won't assure us that the images loaded using JS code are
correctly loaded.

This patch adds a flag to the different providers when the images are
fully loaded.

Note that the img.naturalHeight check is not working for google jackets
and is actually not needed (test with a bib record with and without
google jacket and confirm that it's working as expected).

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2021-10-21 12:24:04 +02:00

84 lines
3.3 KiB
JavaScript

if (typeof KOHA == "undefined" || !KOHA) {
var KOHA = {};
}
/**
* A namespace for Google related functions.
*/
KOHA.Google = {
/**
* Search all:
* <div title="biblionumber" id="isbn" class="gbs-thumbnail"></div>
* or
* <div title="biblionumber" id="isbn" class="gbs-thumbnail-preview"></div>
* and run a search with all collected isbns to Google Book Search.
* The result is asynchronously returned by Google and catched by
* gbsCallBack().
*/
GetCoverFromIsbn: function(newWindow) {
var bibkeys = [];
$("[id^=gbs-thumbnail]").each(function(i) {
bibkeys.push($(this).attr("class")); // id=isbn
});
bibkeys = bibkeys.join(',');
var scriptElement = document.createElement("script");
this.openInNewWindow=newWindow;
scriptElement.setAttribute("id", "jsonScript");
scriptElement.setAttribute("src",
"https://books.google.com/books?bibkeys=" + escape(bibkeys) +
"&jscmd=viewapi&callback=KOHA.Google.gbsCallBack");
scriptElement.setAttribute("type", "text/javascript");
document.documentElement.firstChild.appendChild(scriptElement);
},
/**
* Add cover pages <div
* and link to preview if div id is gbs-thumbnail-preview
*/
gbsCallBack: function(booksInfo) {
var target = '';
if (this.openInNewWindow) {
target = 'target="_blank" rel="noreferrer" ';
}
for (id in booksInfo) {
var book = booksInfo[id];
$("[id^=gbs-thumbnail]."+book.bib_key).each(function() {
if (typeof(book.thumbnail_url) != "undefined") {
if ( $(this).data('use-data-link') ) {
var a = document.createElement("a");
a.href = book.thumbnail_url;
var img = document.createElement("img");
img.src = book.thumbnail_url;
img.setAttribute('data-link', book.info_url);
a.append(img)
$(this).empty().append(a);
} else {
var img = document.createElement("img");
img.src = book.thumbnail_url;
$(this).empty().append(img);
var re = /^gbs-thumbnail-preview/;
if ( re.exec($(this).attr("id")) ) {
$(this).append(
'<div class="google-books-preview">' +
'<a '+target+'href="' +
book.info_url +
'"><img src="' +
'https://books.google.com/intl/en/googlebooks/images/gbs_preview_sticker1.gif' +
'"></a></div>'
);
}
}
} else {
var message = document.createElement("span");
$(message).attr("class","no-image");
$(message).html(NO_GOOGLE_JACKET);
$(this).empty().append(message);
}
});
}
this.done = 1;
}
};