Bug 13379: DBRev 3.19.00.005
[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             "http://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" ';
45          }
46          for (id in booksInfo) {
47              var book = booksInfo[id];
48              $("[id^=gbs-thumbnail]."+book.bib_key).each(function() {
49                  var a = document.createElement("a");
50                  a.href = book.info_url;
51                  if (typeof(book.thumbnail_url) != "undefined") {
52                      var img = document.createElement("img");
53                      img.src = book.thumbnail_url;
54                      $(this).empty().append(img);
55                      var re = /^gbs-thumbnail-preview/;
56                      if ( re.exec($(this).attr("id")) ) {
57                          $(this).append(
58                              '<div style="margin-bottom:5px; margin-top:-5px;font-size:9px">' +
59                              '<a '+target+'href="' +
60                              book.info_url +
61                              '"><img src="' +
62                              'http://books.google.com/intl/en/googlebooks/images/gbs_preview_sticker1.gif' +
63                              '"></a></div>'
64                              );
65                      }
66                  } else {
67                      var message = document.createElement("span");
68                      $(message).attr("class","no-image");
69                      $(message).html(NO_GOOGLE_JACKET);
70                      $(this).empty().append(message);
71                  }
72              });
73          }
74
75      }
76 };