1 if ( typeof KOHA
== "undefined" || !KOHA
) {
5 KOHA
.OverDrive
= ( function() {
6 var proxy_base_url
= '/cgi-bin/koha/svc/overdrive_proxy';
7 var library_base_url
= 'http://api.overdrive.com/v1/libraries/';
9 Get: function( url
, params
, callback
) {
12 url
: url
.replace( /https?:\/\/api.overdrive.com\/v1/, proxy_base_url
),
15 error: function( xhr
, error
) {
17 callback( JSON
.parse( xhr
.responseText
));
19 callback( {error
: xhr
.responseText
|| true} );
25 GetCollectionURL: function( library_id
, callback
) {
26 if ( KOHA
.OverDrive
.collection_url
) {
27 callback( KOHA
.OverDrive
.collection_url
);
32 library_base_url
+ library_id
,
40 KOHA
.OverDrive
.collection_url
= data
.links
.products
.href
;
42 callback( data
.links
.products
.href
);
46 Search: function( library_id
, q
, limit
, offset
, callback
) {
47 KOHA
.OverDrive
.GetCollectionURL( library_id
, function( data
) {
55 {q
: q
, limit
: limit
, offset
: offset
},
63 KOHA
.OverDriveCirculation
= new function() {
64 var svc_url
= '/cgi-bin/koha/svc/overdrive';
66 var error_div
= $('<div class="overdrive-error">');
67 function display_error ( error
) {
68 error_div
.text(error
);
71 var login_link
= $('<a href="#">')
74 if( OD_password_required
) { $("#overdrive-login").modal('show'); }
77 .text(_("Login to OverDrive account"));
79 var login_div
= $('<div class="overdrive-login">').append(login_link
);
83 function is_logged_in() {
84 return details
? details
.is_logged_in
: false;
87 var checkout_popup
= null;
88 $( document
).ready(function() {
89 checkout_popup
= $("#overdrive-checkout");
90 $("#overdrive-login-form").submit(function(e
){
92 $("#overdrive-login").modal('hide');
93 var ODpassword
= $("input[name='ODpassword']").val();
98 $( document
).ready(function() {
99 var p
= window
.opener
;
101 try { cb
= p
.refresh_overdrive_account_details
;}
102 catch(err
){ return; } //Catch error if opener is not accessible
112 function display_account (container
, data
) {
113 if (!data
.is_logged_in
) {
114 $(container
).append(login_div
);
118 var overdrive_link
= $('<a href="https://www.overdrive.com/account/" target="overdrive-account" class="overdrive-link" style="float:right">')
119 .text("OverDrive Account Page");
120 $(container
).append(overdrive_link
);
122 var logout_link
= $('<a href="#logout" class="overdrive-logout" style="float:left">')
125 $(container
).empty().append(error_div
);
126 logout(function(data
) {
127 display_account(container
, data
);
129 }).text(_("Logout from OverDrive account"));
130 $(container
).append(logout_link
);
131 $(container
).append('<br style="clear:both;"/>');
133 if (data
.checkouts
) {
134 var checkouts_div
= $('<div class="overdrive-div">').html('<h3>' + _("Checkouts") + '</h3>');
135 var checkouts_list
= $('<ul class="overdrive-list">');
136 data
.checkouts
.items
.forEach(function(item
) {
137 item_line(checkouts_list
, item
);
139 checkouts_div
.append(checkouts_list
);
140 $(container
).append(checkouts_div
);
144 var holds_div
= $('<div class="overdrive-div">').html('<h3>' + _("Holds") + '</h3>');
145 var holds_list
= $('<ul class="overdrive-list">');
146 data
.holds
.items
.forEach(function(item
) {
147 item_line(holds_list
, item
);
149 holds_div
.append(holds_list
);
150 $(container
).append(holds_div
);
154 function item_line(ul_el
, item
) {
155 var line
= $('<li class="overdrive-item">');
157 var thumb_url
= item
.images
.thumbnail
;
159 $('<img class="overdrive-item-thumbnail">')
160 .attr("src", thumb_url
)
164 $('<div class="overdrive-item-title">')
167 $('<div class="overdrive-item-subtitle">')
170 $('<div class="overdrive-item-author">')
173 var actions
= $('<span class="actions">');
174 display_actions(actions
, item
.id
);
175 $('<div id="action_'+item
.id
+'" class="actions-menu">')
179 $(ul_el
).append(line
);
182 function svc_ajax ( method
, params
, success_callback
) {
188 success: function (data
) {
190 display_error(data
.error
);
192 success_callback(data
);
194 error: function(jqXHR
, textStatus
, errorThrown
) {
195 display_error(errorThrown
);
200 function load_account_details ( callback
) {
201 svc_ajax('get', { action
: "account" }, function(data
) {
208 svc_ajax('get', { action
: "login", password
: p
}, function(data
) {
210 if( data
.login_success
){
211 $(login_div
).detach();
212 if( $("#overdrive-results-page").length
> 0 ){
215 KOHA
.OverDriveCirculation
.display_account_details( $("#opac-user-overdrive") );
221 function logout (callback
) {
222 svc_ajax('post', { action
: "logout" }, function(data
) {
228 function item_action (params
, el
, copies_available
) {
230 svc_ajax('post', params
, function(data
) {
231 if (data
.checkouts
) {
232 details
.checkouts
= data
.checkouts
;
235 details
.holds
= data
.holds
;
237 display_actions(el
, id
, copies_available
);
241 function item_is_checked_out (id
) {
242 if ( !(details
&& details
.checkouts
) ) {
245 var id_uc
= id
.toUpperCase();
246 var items
= details
.checkouts
.items
;
247 for (var i
= 0; i
< items
.length
; i
++) {
248 if ( items
[i
].id
.toUpperCase() == id_uc
) {
255 function item_is_on_hold (id
) {
256 if ( !(details
&& details
.holds
) ) {
259 var id_uc
= id
.toUpperCase();
260 var items
= details
.holds
.items
;
261 for (var i
= 0; i
< items
.length
; i
++) {
262 if ( items
[i
].id
.toUpperCase() == id_uc
) {
269 function display_actions(el
, id
, copies_available
) {
271 if (is_logged_in()) {
273 var item
= item_is_checked_out(id
);
275 var expires
= new Date(item
.expires
);
276 $('<span class="overdrive-item-status">')
277 .text(_("Checked out until") + " " + expires
.toLocaleString())
282 var download
= $('<a href="#">').appendTo(el
);
283 decorate_button(download
, _("Download") + " " + item
.format
);
284 svc_ajax('get', {action
: "download-url", id
: id
, format
: item
.format
}, function(data
) {
285 download
.attr("href", data
.action
);
291 var lockable_formats
= [];
292 for (var f
in item
.formats
) {
293 if (f
== item
.format
) continue;
295 if (item
.formats
[f
]) {
296 var access
= $('<a target="_blank">').appendTo(el
);
297 decorate_button(access
, _("Access online") + " " + f
);
298 svc_ajax('get', {action
: "download-url", id
: id
, format
: f
}, function(data
) {
299 access
.attr("href", data
.action
);
304 lockable_formats
.push(f
);
307 if (lockable_formats
.length
> 0 && checkout_popup
) {
308 $(el
).append( ajax_button(_("Download as"), function() {
309 checkout_format(el
, id
, lockable_formats
, copies_available
);
314 if (item
.format
) return item
;
316 $(el
).append( ajax_button(_("Check in"), function() {
317 if( confirm(_("Are you sure you want to return this item?")) ) {
318 item_action({action
: "return", id
: id
}, el
, copies_available
+ 1);
325 item
= item_is_on_hold(id
);
327 $('<span class="overdrive-status">')
333 if(copies_available
&& checkout_popup
) {
334 $(el
).append( ajax_button(_("Check out"), function() {
335 if( confirm(_("Are you sure you want to checkout this item?")) ) {
336 svc_ajax('post', {action
: "checkout", id
: id
}, function(data
) {
337 if (data
.checkouts
) {
338 details
.checkouts
= data
.checkouts
;
341 details
.holds
= data
.holds
;
343 item
= display_actions(el
, id
, copies_available
- 1);
344 if (item
&& item
.formats
&& !item
.format
) {
345 var has_available_formats
= false;
346 var lockable_formats
= [];
347 for (var f
in item
.formats
) {
348 if (item
.formats
[f
]) {
349 has_available_formats
= true;
352 lockable_formats
.push(f
);
355 if (!has_available_formats
) {
356 checkout_format(el
, id
, lockable_formats
, copies_available
- 1);
364 $(el
).append( ajax_button(_("Place hold"), function() {
365 item_action({action
: "place-hold", id
: id
}, el
, copies_available
);
370 $(el
).append( ajax_button(_("Cancel"), function() {
371 if( confirm(_("Are you sure you want to cancel this hold?")) ) {
372 item_action({action
: "remove-hold", id
: id
}, el
, copies_available
);
380 function ajax_button(label
, on_click
) {
381 var button
= $('<a href="#">')
386 decorate_button(button
, label
);
390 function decorate_button(button
, label
) {
392 .addClass("btn btn-primary btn-mini")
393 .css("color","white")
397 function checkout_format(el
, id
, formats
, copies_available
) {
398 if (formats
.length
== 0) {
399 alert(_("Item cannot be checked out - no available formats"));
403 var checkout_format_list
= checkout_popup
.find("ul.overdrive-format-list").empty();
404 formats
.forEach(function (item
) {
405 var li
= $('<li>').appendTo(checkout_format_list
);
406 $('<input name="checkout-format" type="radio">')
411 checkout_popup
.modal("show");
412 checkout_popup
.find(".overdrive-checkout-submit").click(function(e
) {
414 var format
= checkout_format_list
.find("input[type='radio'][name='checkout-format']:checked").val();
415 item_action({action
: "checkout-format", id
: id
, format
: format
}, el
, copies_available
);
417 checkout_popup
.modal("hide");
421 this.with_account_details = function( el
, callback
) {
422 $(el
).append(error_div
);
423 load_account_details(function(data
) {
424 if (!data
.is_logged_in
) {
425 $(el
).append(login_div
);
431 this.display_account_details = function( el
) {
432 window
.refresh_overdrive_account_details = function () {
433 KOHA
.OverDriveCirculation
.display_account_details( el
);
435 $(el
).empty().append(error_div
);
436 load_account_details(function(data
) {
437 display_account(el
, data
);
441 this.display_error = function( el
, error
) {
442 $(el
).empty().append(error_div
);
443 display_error(error
);
446 this.is_logged_in
= is_logged_in
;
448 this.add_actions = function(el
, id
, copies_available
) {
449 var actions
= $('<span class="actions">');
450 display_actions(actions
, id
, copies_available
);
451 $('<div id="action_'+id
+'" class="actions-menu">')