Merge remote-tracking branch 'kc/new/bug_5995' into kcmaster
[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         $("div [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");
31         scriptElement.setAttribute("type", "text/javascript");
32         document.documentElement.firstChild.appendChild(scriptElement);
33
34     },
35
36     /**
37      * Add cover pages <div
38      * and link to preview if div id is gbs-thumbnail-preview
39      */
40     olCallBack: function(booksInfo) {
41        for (id in booksInfo) {
42           var book = booksInfo[id];
43           var isbn = book.bib_key.substring(5);
44           
45           $("."+isbn).each(function() {
46               var a = document.createElement("a");
47               a.href = book.info_url;
48                                       if (typeof(book.thumbnail_url) != "undefined") {
49                         var img = document.createElement("img");
50                         img.src = book.thumbnail_url;
51                                                 $(this).append(img);
52                   var re = /^openlibrary-thumbnail-preview/;
53                   if ( re.exec($(this).attr("id")) ) {
54                       $(this).append(
55                         '<div style="margin-bottom:5px; margin-top:-5px;font-size:9px">' +
56                         '<a href="' + 
57                         book.info_url + 
58                         '">Preview</a></div>' 
59                       );
60                   }
61                                 } else {
62                                         var message = document.createElement("span");
63                                             $(message).attr("class","no-image");
64                                             $(message).html(NO_OL_JACKET);
65                                             $(this).append(message);
66                                     }
67         });
68       }
69     }
70 };