Lines 1-3
Link Here
|
|
|
1 |
/* global __ */ |
1 |
if (typeof KOHA == "undefined" || !KOHA) { |
2 |
if (typeof KOHA == "undefined" || !KOHA) { |
2 |
var KOHA = {}; |
3 |
var KOHA = {}; |
3 |
} |
4 |
} |
Lines 18-24
KOHA.OpenLibrary = new function() {
Link Here
|
18 |
*/ |
19 |
*/ |
19 |
this.GetCoverFromIsbn = function() { |
20 |
this.GetCoverFromIsbn = function() { |
20 |
var bibkeys = []; |
21 |
var bibkeys = []; |
21 |
$("[id^=openlibrary-thumbnail]").each(function(i) { |
22 |
$("[id^=openlibrary-thumbnail]").each(function() { |
22 |
bibkeys.push("ISBN:" + $(this).attr("class")); // id=isbn |
23 |
bibkeys.push("ISBN:" + $(this).attr("class")); // id=isbn |
23 |
}); |
24 |
}); |
24 |
bibkeys = bibkeys.join(','); |
25 |
bibkeys = bibkeys.join(','); |
Lines 29-58
KOHA.OpenLibrary = new function() {
Link Here
|
29 |
"&callback=KOHA.OpenLibrary.olCallBack&jscmd=data"); |
30 |
"&callback=KOHA.OpenLibrary.olCallBack&jscmd=data"); |
30 |
scriptElement.setAttribute("type", "text/javascript"); |
31 |
scriptElement.setAttribute("type", "text/javascript"); |
31 |
document.documentElement.firstChild.appendChild(scriptElement); |
32 |
document.documentElement.firstChild.appendChild(scriptElement); |
32 |
} |
33 |
}; |
33 |
|
34 |
|
34 |
/** |
35 |
/** |
35 |
* Add cover pages <div |
36 |
* Add cover pages <div |
36 |
* and link to preview if div id is gbs-thumbnail-preview |
37 |
* and link to preview if div id is gbs-thumbnail-preview |
37 |
*/ |
38 |
*/ |
38 |
this.olCallBack = function(booksInfo) { |
39 |
this.olCallBack = function(booksInfo) { |
39 |
for (id in booksInfo) { |
40 |
for (var id in booksInfo) { |
40 |
var book = booksInfo[id]; |
41 |
var book = booksInfo[id]; |
41 |
var isbn = id.substring(5); |
42 |
var isbn = id.substring(5); |
|
|
43 |
var a; |
42 |
$("[id^=openlibrary-thumbnail]."+isbn).each(function() { |
44 |
$("[id^=openlibrary-thumbnail]."+isbn).each(function() { |
43 |
var a = document.createElement("a"); |
45 |
a = document.createElement("a"); |
44 |
a.href = booksInfo.url; |
46 |
a.href = booksInfo.url; |
45 |
if (book.cover) { |
47 |
if (book.cover) { |
|
|
48 |
var img; |
46 |
if ( $(this).data('use-data-link') ) { |
49 |
if ( $(this).data('use-data-link') ) { |
47 |
var a = document.createElement("a"); |
50 |
a = document.createElement("a"); |
48 |
a.href = book.cover.large; |
51 |
a.href = book.cover.large; |
49 |
var img = document.createElement("img"); |
52 |
img = document.createElement("img"); |
50 |
img.src = book.cover.medium; |
53 |
img.src = book.cover.medium; |
51 |
img.setAttribute('data-link', book.cover.large); |
54 |
img.setAttribute('data-link', book.cover.large); |
52 |
a.append(img) |
55 |
a.append(img); |
53 |
$(this).empty().append(a); |
56 |
$(this).empty().append(a); |
54 |
} else { |
57 |
} else { |
55 |
var img = document.createElement("img"); |
58 |
img = document.createElement("img"); |
56 |
img.src = book.cover.medium; |
59 |
img.src = book.cover.medium; |
57 |
img.height = '110'; |
60 |
img.height = '110'; |
58 |
$(this).append(img); |
61 |
$(this).append(img); |
Lines 66-72
KOHA.OpenLibrary = new function() {
Link Here
|
66 |
}); |
69 |
}); |
67 |
} |
70 |
} |
68 |
this.done = 1; |
71 |
this.done = 1; |
69 |
} |
72 |
}; |
70 |
|
73 |
|
71 |
var search_url = 'https://openlibrary.org/search?'; |
74 |
var search_url = 'https://openlibrary.org/search?'; |
72 |
this.searchUrl = function( q ) { |
75 |
this.searchUrl = function( q ) { |
Lines 85-91
KOHA.OpenLibrary = new function() {
Link Here
|
85 |
url: search_url_json, |
88 |
url: search_url_json, |
86 |
dataType: 'json', |
89 |
dataType: 'json', |
87 |
data: params, |
90 |
data: params, |
88 |
error: function( xhr, error ) { |
91 |
error: function( xhr ) { |
89 |
try { |
92 |
try { |
90 |
callback( JSON.parse( xhr.responseText )); |
93 |
callback( JSON.parse( xhr.responseText )); |
91 |
} catch ( e ) { |
94 |
} catch ( e ) { |
Lines 110-211
A demonstration use of this script is available here:
Link Here
|
110 |
http://internetarchive.github.com/read_api_extras/readapi_demo.html |
113 |
http://internetarchive.github.com/read_api_extras/readapi_demo.html |
111 |
*/ |
114 |
*/ |
112 |
|
115 |
|
113 |
var ol_readapi_automator = |
|
|
114 |
(function () { // open anonymous scope for tidiness |
116 |
(function () { // open anonymous scope for tidiness |
115 |
|
117 |
|
116 |
// 'constants' |
118 |
// 'constants' |
117 |
var readapi_bibids = ['isbn', 'lccn', 'oclc', 'olid', 'iaid', 'bibkeys']; |
119 |
var readapi_bibids = ['isbn', 'lccn', 'oclc', 'olid', 'iaid', 'bibkeys']; |
118 |
var magic_classname = 'ol_readapi_book'; |
120 |
var magic_classname = 'ol_readapi_book'; |
119 |
var ol_readapi_books = $("." + magic_classname ); |
121 |
var ol_readapi_books = $("." + magic_classname ); |
120 |
var result; |
122 |
var result; |
121 |
|
123 |
|
122 |
// added to book divs to correlate with API results |
124 |
// added to book divs to correlate with API results |
123 |
var magic_bookid = 'ol_bookid'; |
125 |
var magic_bookid = 'ol_bookid'; |
124 |
var ol_button_classname = 'ol_readapi_button'; |
126 |
var ol_button_classname = 'ol_readapi_button'; |
125 |
|
127 |
|
126 |
// Find all book divs and concatenate ids from them to create a read |
128 |
// Find all book divs and concatenate ids from them to create a read |
127 |
// API query url |
129 |
// API query url |
128 |
function create_query() { |
130 |
function create_query() { |
129 |
var q = 'https://openlibrary.org/api/volumes/brief/json/'; |
131 |
var q = 'https://openlibrary.org/api/volumes/brief/json/'; |
130 |
|
132 |
|
131 |
function add_el(i, el) { |
133 |
function add_el(i, el) { |
132 |
// tag with number found so it's easy to discover later |
134 |
// tag with number found so it's easy to discover later |
133 |
// (necessary? just go by index?) |
135 |
// (necessary? just go by index?) |
134 |
// (choose better name?) |
136 |
// (choose better name?) |
135 |
$(el).attr(magic_bookid, i); |
137 |
$(el).attr(magic_bookid, i); |
136 |
|
138 |
|
137 |
if (i > 0) { |
139 |
if (i > 0) { |
138 |
q += '|'; |
140 |
q += '|'; |
139 |
} |
141 |
} |
140 |
q += 'id:' + i; |
142 |
q += 'id:' + i; |
141 |
|
143 |
|
142 |
for (bi in readapi_bibids) { |
144 |
for (var bi in readapi_bibids) { |
143 |
bibid = readapi_bibids[bi]; |
145 |
var bibid = readapi_bibids[bi]; |
144 |
if ($(el).attr(bibid)) { |
146 |
if ($(el).attr(bibid)) { |
145 |
q += ';' + bibid + ':' + $(el).attr(bibid); |
147 |
q += ';' + bibid + ':' + $(el).attr(bibid); |
|
|
148 |
} |
146 |
} |
149 |
} |
147 |
} |
150 |
} |
148 |
} |
|
|
149 |
|
150 |
$('.' + magic_classname).each(add_el); |
151 |
return q; |
152 |
} |
153 |
|
151 |
|
154 |
function make_read_button(bookdata) { |
152 |
$('.' + magic_classname).each(add_el); |
155 |
buttons = { |
153 |
return q; |
156 |
'full access': |
|
|
157 |
"https://openlibrary.org/images/button-read-open-library.png", |
158 |
'lendable': |
159 |
"https://openlibrary.org/images/button-borrow-open-library.png", |
160 |
'checked out': |
161 |
"https://openlibrary.org/images/button-checked-out-open-library.png" |
162 |
}; |
163 |
if (bookdata.items.length == 0) { |
164 |
return false; |
165 |
} |
154 |
} |
166 |
first = bookdata.items[0]; |
|
|
167 |
if (!(first.status in buttons)) { |
168 |
return false; |
169 |
} |
170 |
result = '<a target="_blank" href="' + first.itemURL + '">' + |
171 |
'<img class="' + ol_button_classname + |
172 |
'" src="' + buttons[first.status] + '"/></a>'; |
173 |
console.log( result ); |
174 |
return result; |
175 |
} |
176 |
|
155 |
|
177 |
// Default function for decorating document elements with read API data |
156 |
function make_read_button(bookdata) { |
178 |
function default_decorate_el_fn(el, bookdata) { |
157 |
var buttons = { |
179 |
// Note that 'bookdata' may be undefined, if the Read API call |
158 |
'full access': |
180 |
// didn't return results for this book |
159 |
"https://openlibrary.org/images/button-read-open-library.png", |
181 |
var decoration; |
160 |
'lendable': |
182 |
if (bookdata) { |
161 |
"https://openlibrary.org/images/button-borrow-open-library.png", |
183 |
decoration = make_read_button(bookdata); |
162 |
'checked out': |
184 |
} |
163 |
"https://openlibrary.org/images/button-checked-out-open-library.png" |
185 |
if (decoration) { |
164 |
}; |
186 |
el.innerHTML += decoration; |
165 |
if (bookdata.items.length == 0) { |
187 |
el.style.display = 'block' |
166 |
return false; |
188 |
} else { |
167 |
} |
189 |
el.style.display = 'none'; |
168 |
var first = bookdata.items[0]; |
|
|
169 |
if (!(first.status in buttons)) { |
170 |
return false; |
171 |
} |
172 |
result = '<a target="_blank" href="' + first.itemURL + '">' + |
173 |
'<img class="' + ol_button_classname + |
174 |
'" src="' + buttons[first.status] + '"/></a>'; |
175 |
return result; |
190 |
} |
176 |
} |
191 |
} |
|
|
192 |
|
177 |
|
193 |
function do_query(q, decorate_el_fn) { |
178 |
// Default function for decorating document elements with read API data |
194 |
if (!decorate_el_fn) { |
179 |
function default_decorate_el_fn(el, bookdata) { |
195 |
decorate_el_fn = default_decorate_el_fn; |
180 |
// Note that 'bookdata' may be undefined, if the Read API call |
|
|
181 |
// didn't return results for this book |
182 |
var decoration; |
183 |
if (bookdata) { |
184 |
decoration = make_read_button(bookdata); |
185 |
} |
186 |
if (decoration) { |
187 |
el.innerHTML += decoration; |
188 |
el.style.display = 'block'; |
189 |
} else { |
190 |
el.style.display = 'none'; |
191 |
} |
196 |
} |
192 |
} |
197 |
var starttime = (new Date()).getTime(); |
193 |
|
198 |
|
194 |
function do_query(q, decorate_el_fn) { |
199 |
// Call a function on each <div class="ol_readapi_book"> element |
195 |
if (!decorate_el_fn) { |
200 |
// with the target element and the data found for that element. |
196 |
decorate_el_fn = default_decorate_el_fn; |
201 |
// Use decorate_el_fn if supplied, falling back to |
197 |
} |
202 |
// default_decorate_el_fn, above. |
198 |
// Call a function on each <div class="ol_readapi_book"> element |
203 |
function query_callback(data, textStatus, jqXHR) { |
199 |
// with the target element and the data found for that element. |
204 |
var endtime = (new Date()).getTime(); |
200 |
// Use decorate_el_fn if supplied, falling back to |
205 |
var duration = (endtime - starttime) / 1000; |
201 |
// default_decorate_el_fn, above. |
206 |
// console.log('took ' + duration + ' seconds'); |
202 |
function query_callback(data) { |
207 |
|
203 |
$('.' + magic_classname).each(function(i, el) { |
208 |
$('.' + magic_classname).each(function(i, el) { |
|
|
209 |
var bookid = $(el).attr(magic_bookid); |
204 |
var bookid = $(el).attr(magic_bookid); |
210 |
if (bookid && bookid in data) { |
205 |
if (bookid && bookid in data) { |
211 |
decorate_el_fn(el, data[bookid]); |
206 |
decorate_el_fn(el, data[bookid]); |
Lines 213-241
function do_query(q, decorate_el_fn) {
Link Here
|
213 |
decorate_el_fn(el); |
208 |
decorate_el_fn(el); |
214 |
} |
209 |
} |
215 |
}); |
210 |
}); |
216 |
} |
211 |
} |
217 |
|
212 |
|
218 |
// console.log('calling ' + q); |
213 |
// console.log('calling ' + q); |
219 |
$.ajax({ url: q, |
214 |
$.ajax({ url: q, |
220 |
data: { 'show_all_items': 'true' }, |
215 |
data: { 'show_all_items': 'true' }, |
221 |
dataType: 'jsonp', |
216 |
dataType: 'jsonp', |
222 |
success: query_callback |
217 |
success: query_callback |
223 |
}); |
218 |
}); |
224 |
} |
219 |
} |
225 |
|
220 |
|
226 |
if( ol_readapi_books.length > 0 ){ |
221 |
if( ol_readapi_books.length > 0 ){ |
227 |
// Do stuff |
222 |
// Do stuff |
228 |
var q = create_query(); |
223 |
var q = create_query(); |
229 |
do_query(q); |
224 |
do_query(q); |
230 |
|
225 |
|
231 |
result = { |
226 |
result = { |
232 |
do_query: do_query, |
227 |
do_query: do_query, |
233 |
create_query: create_query, |
228 |
create_query: create_query, |
234 |
make_read_button: make_read_button |
229 |
make_read_button: make_read_button |
235 |
}; |
230 |
}; |
236 |
} |
231 |
} |
237 |
|
232 |
|
238 |
return result; |
233 |
return result; |
239 |
})(); // close anonymous scope |
234 |
})(); // close anonymous scope |
240 |
|
235 |
|
241 |
/* |
236 |
/* |
242 |
- |
|
|