Bug 25031: Improve handling of multiple covers on the biblio detail page in the staff...
[koha.git] / koha-tmpl / intranet-tmpl / js / coce.js
1 if (KOHA === undefined || !KOHA) { var KOHA = {}; }
2
3
4 /**
5  * A namespace for Coce cover images cache
6  */
7 KOHA.coce = {
8
9     /**
10      * Search all:
11      *    <div title="biblionumber" id="isbn" class="coce-thumbnail"></div>
12      * or
13      *    <div title="biblionumber" id="isbn" class="coce-thumbnail-preview"></div>
14      * and run a search with all collected isbns to coce cover service.
15      * The result is asynchronously returned, and used to append <img>.
16      */
17     getURL: function(host, provider, newWindow) {
18         var ids = [];
19         $("[id^=coce-thumbnail]").each(function(i) {
20             var id = $(this).attr("class"); // id=isbn
21             if (id !== '') { ids.push(id); }
22         });
23         if (ids.length == 0) return;
24         ids = ids.join(',');
25         var coceURL = host + '/cover?id=' + ids + '&provider=' + provider;
26         $.ajax({
27             url: coceURL,
28             dataType: 'jsonp',
29             success: function(urlPerID) {
30                 for (var id in urlPerID) {
31                     var url = urlPerID[id];
32                     $("[id^=coce-thumbnail]." + id).each(function() {
33                         var img = document.createElement("img");
34                         img.src = url;
35                         img.alt = "Cover image";
36                         img.onload = function() {
37                             // image dimensions can't be known until image has loaded
38                             if (img.height == 1 && img.width == 1) {
39                                 $(this).remove();
40                             }
41                         };
42                         $(this).html(img);
43                     });
44                 }
45             },
46
47         });
48     }
49
50 };