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