1 if (typeof KOHA == "undefined" || !KOHA) {
6 * A namespace for OpenLibrary related functions.
8 KOHA.OpenLibrary = new function() {
12 * <div title="biblionumber" id="isbn" class="openlibrary-thumbnail"></div>
14 * <div title="biblionumber" id="isbn" class="openlibrary-thumbnail-preview"></div>
15 * and run a search with all collected isbns to Open Library Book Search.
16 * The result is asynchronously returned by OpenLibrary and catched by
19 this.GetCoverFromIsbn = function() {
21 $("[id^=openlibrary-thumbnail]").each(function(i) {
22 bibkeys.push("ISBN:" + $(this).attr("class")); // id=isbn
24 bibkeys = bibkeys.join(',');
25 var scriptElement = document.createElement("script");
26 scriptElement.setAttribute("id", "jsonScript");
27 scriptElement.setAttribute("src",
28 "https://openlibrary.org/api/books?bibkeys=" + escape(bibkeys) +
29 "&callback=KOHA.OpenLibrary.olCallBack&jscmd=data");
30 scriptElement.setAttribute("type", "text/javascript");
31 document.documentElement.firstChild.appendChild(scriptElement);
35 * Add cover pages <div
36 * and link to preview if div id is gbs-thumbnail-preview
38 this.olCallBack = function(booksInfo) {
39 for (id in booksInfo) {
40 var book = booksInfo[id];
41 var isbn = id.substring(5);
42 $("[id^=openlibrary-thumbnail]."+isbn).each(function() {
43 var is_opacdetail = /openlibrary-thumbnail-preview/.exec($(this).attr("id"));
44 var a = document.createElement("a");
45 a.href = booksInfo.url;
47 var img = document.createElement("img");
49 img.src = book.cover.medium;
50 $(this).empty().append(img);
51 $(this).append('<div class="results_summary">' + '<a href="' + book.url + '">' + OL_PREVIEW + '</a></div>');
53 img.src = book.cover.medium;
58 var message = document.createElement("span");
59 $(message).attr("class","no-image");
60 $(message).html(NO_OL_JACKET);
61 $(this).append(message);
67 var search_url = 'https://openlibrary.org/search?';
68 this.searchUrl = function( q ) {
70 return search_url + $.param(params);
73 var search_url_json = 'https://openlibrary.org/search.json';
74 this.search = function( q, page_no, callback ) {
77 params.page = page_no;
84 error: function( xhr, error ) {
86 callback( JSON.parse( xhr.responseText ));
88 callback( {error: xhr.responseText || true} );
95 /* readapi_automator.js */
98 This script helps to put readable links to Open Library books into
100 When loaded, it searches the DOM for <div> elements with class
101 "ol_readapi_book", extracts book identifiers from them (e.g. isbn,
102 lccn, etc.) and puts those into an asynchronous call to the Read API.
103 When the call returns, the results are used to add clickable links
104 to the "ol_readapi_book" elements found earlier.
105 A demonstration use of this script is available here:
106 http://internetarchive.github.com/read_api_extras/readapi_demo.html
109 var ol_readapi_automator =
110 (function () { // open anonymous scope for tidiness
113 var readapi_bibids = ['isbn', 'lccn', 'oclc', 'olid', 'iaid', 'bibkeys'];
114 var magic_classname = 'ol_readapi_book';
116 // added to book divs to correlate with API results
117 var magic_bookid = 'ol_bookid';
118 var ol_button_classname = 'ol_readapi_button';
120 // Find all book divs and concatenate ids from them to create a read
122 function create_query() {
123 var q = 'https://openlibrary.org/api/volumes/brief/json/';
125 function add_el(i, el) {
126 // tag with number found so it's easy to discover later
127 // (necessary? just go by index?)
128 // (choose better name?)
129 $(el).attr(magic_bookid, i);
136 for (bi in readapi_bibids) {
137 bibid = readapi_bibids[bi];
138 if ($(el).attr(bibid)) {
139 q += ';' + bibid + ':' + $(el).attr(bibid);
144 $('.' + magic_classname).each(add_el);
148 function make_read_button(bookdata) {
151 "http://openlibrary.org/images/button-read-open-library.png",
153 "http://openlibrary.org/images/button-borrow-open-library.png",
155 "http://openlibrary.org/images/button-checked-out-open-library.png"
157 if (bookdata.items.length == 0) {
160 first = bookdata.items[0];
161 if (!(first.status in buttons)) {
164 result = '<a href="' + first.itemURL + '">' +
165 '<img class="' + ol_button_classname +
166 '" src="' + buttons[first.status] + '"/></a>';
170 // Default function for decorating document elements with read API data
171 function default_decorate_el_fn(el, bookdata) {
172 // Note that 'bookdata' may be undefined, if the Read API call
173 // didn't return results for this book
175 decoration = 'Not found';
177 decoration = make_read_button(bookdata);
180 el.innerHTML += decoration;
182 el.style.visibility = 'hidden';
186 function do_query(q, decorate_el_fn) {
187 if (!decorate_el_fn) {
188 decorate_el_fn = default_decorate_el_fn;
190 var starttime = (new Date()).getTime();
192 // Call a function on each <div class="ol_readapi_book"> element
193 // with the target element and the data found for that element.
194 // Use decorate_el_fn if supplied, falling back to
195 // default_decorate_el_fn, above.
196 function query_callback(data, textStatus, jqXHR) {
197 var endtime = (new Date()).getTime();
198 var duration = (endtime - starttime) / 1000;
199 // console.log('took ' + duration + ' seconds');
201 $('.' + magic_classname).each(function(i, el) {
202 var bookid = $(el).attr(magic_bookid);
203 if (bookid && bookid in data) {
204 decorate_el_fn(el, data[bookid]);
211 // console.log('calling ' + q);
213 data: { 'show_all_items': 'true' },
215 success: query_callback
220 var q = create_query();
225 create_query: create_query,
226 make_read_button: make_read_button
230 })(); // close anonymous scope
234 * Support alternate query targets, e.g. Hathi
237 * ezproxy prefix (implies show_inlibrary?)
238 * console debug output? (check all console.log)