Bug 21257: (bug 13618 follow-up) Handle undef values in JS before escapeHtml call
[koha.git] / koha-tmpl / intranet-tmpl / prog / 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                 .addClass("thumbnail")
61                 .load(function () {
62                     if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth <= 1) {
63                         //IE HACK
64                         try {
65                             var otherCovers = $(mydiv).closest('td').find('img');
66                             var nbCovers = otherCovers.length;
67                             if(nbCovers > 0){
68                                 var badCovers = 0;
69                                 otherCovers.each(function(){
70                                     if(this.naturalWidth <= 1){
71                                         $(this).parent().remove();
72                                         badCovers++;
73                                     }
74                                 });
75                                 if(badCovers < nbCovers){
76                                     $(mydiv).parent().remove();
77                                 }
78                             }
79                         }
80                         catch(err){
81                         }
82                     } else {
83                         $(mydiv).append(img);
84                         $(mydiv).children('.no-image').remove();
85                     }
86                 });
87         });
88     }
89 };