Bug 25031: Improve handling of multiple covers on the biblio detail page in the staff...
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / localcovers.js
1 /* global _ */
2
3 if (typeof KOHA == "undefined" || !KOHA) {
4     var KOHA = {};
5 }
6
7 /**
8  * A namespace for local cover related functions.
9  */
10 KOHA.LocalCover = {
11
12
13     /**
14      * Search all:
15      *    <div title="biblionumber" id="isbn" class="openlibrary-thumbnail"></div>
16      * or
17      *    <div title="biblionumber" id="isbn" class="openlibrary-thumbnail-preview"></div>
18      * and run a search with all collected isbns to Open Library Book Search.
19      * The result is asynchronously returned by OpenLibrary and catched by
20      * olCallBack().
21      */
22     GetCoverFromBibnumber: function(uselink) {
23         var mydiv = $("#local-thumbnail-preview");
24         var biblionumber = mydiv.data("biblionumber");
25         var img = document.createElement("img");
26         img.src = "/cgi-bin/koha/catalogue/image.pl?thumbnail=1&biblionumber=" + biblionumber;
27         img.onload = function() {
28             // image dimensions can't be known until image has loaded
29             if ( (img.complete != null) && (!img.complete) ) {
30                 mydiv.remove();
31             }
32         };
33         if (uselink) {
34             var a = $("<a />").attr('href', '/cgi-bin/koha/catalogue/imageviewer.pl?biblionumber=' + $(mydiv).attr("class"));
35             $(a).append(img);
36             mydiv.append(a);
37         } else {
38             mydiv.append(img);
39         }
40     },
41     LoadResultsCovers: function(){
42         $("div [id^=local-thumbnail]").each(function(i) {
43             var mydiv = this;
44             var message = document.createElement("span");
45             $(message).attr("class","no-image thumbnail");
46             $(message).html( _("No cover image available") );
47             $(mydiv).append(message);
48             var img = $("<img />");
49             img.attr('src','/cgi-bin/koha/catalogue/image.pl?thumbnail=1&biblionumber=' + $(mydiv).attr("class"))
50                 .addClass("thumbnail")
51                 .load(function () {
52                     if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth <= 1) {
53                         //IE HACK
54                         try {
55                             var otherCovers = $(mydiv).closest('td').find('img');
56                             var nbCovers = otherCovers.length;
57                             if(nbCovers > 0){
58                                 var badCovers = 0;
59                                 otherCovers.each(function(){
60                                     if(this.naturalWidth <= 1){
61                                         $(this).parent().remove();
62                                         badCovers++;
63                                     }
64                                 });
65                                 if(badCovers < nbCovers){
66                                     $(mydiv).parent().remove();
67                                 }
68                             }
69                         }
70                         catch(err){
71                         }
72                     } else {
73                         $(mydiv).append(img);
74                         $(mydiv).children('.no-image').remove();
75                     }
76                 });
77         });
78     }
79 };