Bug 10856: (Follow-up) improve behavior of the "close shelf browser" link
[koha.git] / koha-tmpl / opac-tmpl / prog / en / js / openlibrary.js
1 if (typeof KOHA == "undefined" || !KOHA) {
2     var KOHA = {};
3 }
4
5 /**
6  * A namespace for OpenLibrary related functions.
7  */
8 KOHA.OpenLibrary = {
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     GetCoverFromIsbn: function() {
21         var bibkeys = [];
22         $("[id^=openlibrary-thumbnail]").each(function(i) {
23             bibkeys.push("ISBN:" + $(this).attr("class")); // id=isbn
24         });
25         bibkeys = bibkeys.join(',');
26         var scriptElement = document.createElement("script");
27         scriptElement.setAttribute("id", "jsonScript");
28         scriptElement.setAttribute("src",
29             "http://openlibrary.org/api/books?bibkeys=" + escape(bibkeys) +
30             "&callback=KOHA.OpenLibrary.olCallBack&jscmd=data");
31         scriptElement.setAttribute("type", "text/javascript");
32         document.documentElement.firstChild.appendChild(scriptElement);
33     },
34
35     /**
36      * Add cover pages <div
37      * and link to preview if div id is gbs-thumbnail-preview
38      */
39     olCallBack: function(booksInfo) {
40         for (id in booksInfo) {
41             var book = booksInfo[id];
42             var isbn = id.substring(5);
43             $("[id^=openlibrary-thumbnail]."+isbn).each(function() {
44                 var is_opacdetail = /openlibrary-thumbnail-preview/.exec($(this).attr("id"));
45                 var a = document.createElement("a");
46                 a.href = booksInfo.url;
47                 if (book.cover) {
48                     var img = document.createElement("img");
49                     if (is_opacdetail) {
50                         img.src = book.cover.medium;
51                         $(this).append(img);
52                         $(this).append('<div class="results_summary">' + '<a href="' + book.url + '">Preview</a></div>');
53                     } else {
54                         img.src = book.cover.medium;
55                         img.height = '110';
56                         $(this).append(img);
57                     }
58                 } else {
59                     var message =  document.createElement("span");
60                     $(message).attr("class","no-image");
61                     $(message).html(NO_OL_JACKET);
62                     $(this).append(message);
63                 }
64             });
65         }
66     }
67 };