Bug 14242: (follow-up) Hide autofil when syspref disabled, clear ISBN
[koha.git] / koha-tmpl / opac-tmpl / bootstrap / js / google-jackets.js
1 if (typeof KOHA == "undefined" || !KOHA) {
2     var KOHA = {};
3 }
4
5 /**
6  * A namespace for Google related functions.
7  */
8 KOHA.Google = {
9
10
11     /**
12      * Search all:
13      *    <div title="biblionumber" id="isbn" class="gbs-thumbnail"></div>
14      * or
15      *    <div title="biblionumber" id="isbn" class="gbs-thumbnail-preview"></div>
16      * and run a search with all collected isbns to Google Book Search.
17      * The result is asynchronously returned by Google and catched by
18      * gbsCallBack().
19      */
20     GetCoverFromIsbn: function(newWindow) {
21         var bibkeys = [];
22         $("[id^=gbs-thumbnail]").each(function(i) {
23             bibkeys.push($(this).attr("class")); // id=isbn
24         });
25         bibkeys = bibkeys.join(',');
26         var scriptElement = document.createElement("script");
27         this.openInNewWindow=newWindow;
28         scriptElement.setAttribute("id", "jsonScript");
29         scriptElement.setAttribute("src",
30             "https://books.google.com/books?bibkeys=" + escape(bibkeys) +
31             "&jscmd=viewapi&callback=KOHA.Google.gbsCallBack");
32         scriptElement.setAttribute("type", "text/javascript");
33         document.documentElement.firstChild.appendChild(scriptElement);
34
35     },
36
37     /**
38      * Add cover pages <div
39      * and link to preview if div id is gbs-thumbnail-preview
40      */
41     gbsCallBack: function(booksInfo) {
42          var target = '';
43          if (this.openInNewWindow) {
44             target = 'target="_blank" rel="noreferrer" ';
45          }
46          for (id in booksInfo) {
47              var book = booksInfo[id];
48              $("[id^=gbs-thumbnail]."+book.bib_key).each(function() {
49                  if (typeof(book.thumbnail_url) != "undefined") {
50                      if ( $(this).data('use-data-link') ) {
51                          var a = document.createElement("a");
52                          a.href = book.thumbnail_url;
53                          var img = document.createElement("img");
54                          img.src = book.thumbnail_url;
55                          img.setAttribute('data-link', book.info_url);
56                          a.append(img)
57                          $(this).empty().append(a);
58                      } else {
59                          var img = document.createElement("img");
60                          img.src = book.thumbnail_url;
61                          $(this).empty().append(img);
62                          var re = /^gbs-thumbnail-preview/;
63                          if ( re.exec($(this).attr("id")) ) {
64                              $(this).append(
65                                  '<div class="google-books-preview">' +
66                                  '<a '+target+'href="' +
67                                  book.info_url +
68                                  '"><img src="' +
69                                  'https://books.google.com/intl/en/googlebooks/images/gbs_preview_sticker1.gif' +
70                                  '"></a></div>'
71                                  );
72                          }
73                      }
74                  } else {
75                      var message = document.createElement("span");
76                      $(message).attr("class","no-image");
77                      $(message).html(NO_GOOGLE_JACKET);
78                      $(this).empty().append(message);
79                  }
80              });
81         }
82         this.done = 1;
83     }
84 };