1 /* global MSG_OVERDRIVE_LOGIN MSG_OVERDRIVE_LINK MSG_OVERDRIVE_LOGOUT MSG_OVERDRIVE_CHECKEDOUT_UNTIL MSG_DOWNLOAD MSG_OVERDRIVE_ACCESS_ONLINE MSG_OVERDRIVE_DOWNLOAD_AS MSG_CHECK_IN MSG_CHECK_OUT MSG_CHECKOUTS MSG_HOLDS MSG_ON_HOLD MSG_PLACE_HOLD MSG_CANCEL_HOLD MSG_OVERDRIVE_CANNOT_CHECKOUT MSG_CANCEL_HOLD_CONFIRM MSG_CHECK_IN_CONFIRM MSG_CHECK_OUT_CONFIRM OD_password_required */
3 if ( typeof KOHA == "undefined" || !KOHA ) {
7 KOHA.OverDrive = ( function() {
8 var proxy_base_url = '/cgi-bin/koha/svc/overdrive_proxy';
9 var library_base_url = 'http://api.overdrive.com/v1/libraries/';
11 Get: function( url, params, callback ) {
14 url: url.replace( /https?:\/\/api.overdrive.com\/v1/, proxy_base_url ),
17 error: function( xhr, error ) {
19 callback( JSON.parse( xhr.responseText ));
21 callback( {error: xhr.responseText || true} );
27 GetCollectionURL: function( library_id, callback ) {
28 if ( KOHA.OverDrive.collection_url ) {
29 callback( KOHA.OverDrive.collection_url );
34 library_base_url + library_id,
42 KOHA.OverDrive.collection_url = data.links.products.href;
44 callback( data.links.products.href );
48 Search: function( library_id, q, limit, offset, callback ) {
49 KOHA.OverDrive.GetCollectionURL( library_id, function( data ) {
57 {q: q, limit: limit, offset: offset},
65 KOHA.OverDriveCirculation = new function() {
66 var svc_url = '/cgi-bin/koha/svc/overdrive';
68 var error_div = $('<div class="overdrive-error">');
69 function display_error ( error ) {
70 error_div.text(error);
73 var login_link = $('<a href="#">')
76 if( OD_password_required ) { $("#overdrive-login").modal('show'); }
79 .text( MSG_OVERDRIVE_LOGIN );
81 var login_div = $('<div class="overdrive-login">').append(login_link);
85 function is_logged_in() {
86 return details ? details.is_logged_in : false;
89 var checkout_popup = null;
90 $( document ).ready(function() {
91 checkout_popup = $("#overdrive-checkout");
92 $("#overdrive-login-form").submit(function(e){
94 $("#overdrive-login").modal('hide');
95 var ODpassword = $("input[name='ODpassword']").val();
100 $( document ).ready(function() {
101 var p = window.opener;
104 try { cb = p.refresh_overdrive_account_details;}
105 catch(err){ return; } //Catch error if opener is not accessible
115 function display_account (container, data) {
116 if (!data.is_logged_in) {
117 $(container).append(login_div);
121 var overdrive_link = $('<a href="https://www.overdrive.com/account/" target="overdrive-account" class="overdrive-link" style="float:right">')
122 .text( MSG_OVERDRIVE_LINK );
123 $(container).append(overdrive_link);
125 var logout_link = $('<a href="#logout" class="overdrive-logout" style="float:left">')
128 $(container).empty().append(error_div);
129 logout(function(data) {
130 display_account(container, data);
132 }).text( MSG_OVERDRIVE_LOGOUT );
133 $(container).append(logout_link);
134 $(container).append('<br style="clear:both;"/>');
136 if (data.checkouts) {
137 var checkouts_div = $('<div class="overdrive-div">').html('<h3>' + MSG_CHECKOUTS + '</h3>');
138 var checkouts_list = $('<ul class="overdrive-list">');
139 data.checkouts.items.forEach(function(item) {
140 item_line(checkouts_list, item);
142 checkouts_div.append(checkouts_list);
143 $(container).append(checkouts_div);
147 var holds_div = $('<div class="overdrive-div">').html('<h3>' + MSG_HOLDS + '</h3>');
148 var holds_list = $('<ul class="overdrive-list">');
149 data.holds.items.forEach(function(item) {
150 item_line(holds_list, item);
152 holds_div.append(holds_list);
153 $(container).append(holds_div);
157 function item_line(ul_el, item) {
158 var line = $('<li class="overdrive-item">');
160 var thumb_url = item.images.thumbnail;
162 $('<img class="overdrive-item-thumbnail">')
163 .attr("src", thumb_url)
167 $('<div class="overdrive-item-title">')
170 $('<div class="overdrive-item-subtitle">')
173 $('<div class="overdrive-item-author">')
176 var actions = $('<span class="actions">');
177 display_actions(actions, item.id);
178 $('<div id="action_'+item.id+'" class="actions-menu">')
182 $(ul_el).append(line);
185 function svc_ajax ( method, params, success_callback ) {
191 success: function (data) {
193 display_error(data.error);
195 success_callback(data);
197 error: function(jqXHR, textStatus, errorThrown) {
198 display_error(errorThrown);
203 function load_account_details ( callback ) {
204 svc_ajax('get', { action: "account" }, function(data) {
211 svc_ajax('get', { action: "login", password: p }, function(data) {
213 if( data.login_success ){
214 $(login_div).detach();
215 if( $("#overdrive-results-page").length > 0 ){
218 KOHA.OverDriveCirculation.display_account_details( $("#opac-user-overdrive") );
224 function logout (callback) {
225 svc_ajax('post', { action: "logout" }, function(data) {
231 function item_action (params, el, copies_available) {
233 svc_ajax('post', params, function(data) {
234 if (data.checkouts) {
235 details.checkouts = data.checkouts;
238 details.holds = data.holds;
240 display_actions(el, id, copies_available);
244 function item_is_checked_out (id) {
245 if ( !(details && details.checkouts) ) {
248 var id_uc = id.toUpperCase();
249 var items = details.checkouts.items;
250 for (var i = 0; i < items.length; i++) {
251 if ( items[i].id.toUpperCase() == id_uc ) {
258 function item_is_on_hold (id) {
259 if ( !(details && details.holds) ) {
262 var id_uc = id.toUpperCase();
263 var items = details.holds.items;
264 for (var i = 0; i < items.length; i++) {
265 if ( items[i].id.toUpperCase() == id_uc ) {
272 function display_actions(el, id, copies_available) {
274 if (is_logged_in()) {
276 var item = item_is_checked_out(id);
278 var expires = new Date(item.expires);
279 $('<span class="overdrive-item-status">')
280 .text( MSG_OVERDRIVE_CHECKEDOUT_UNTIL + " " + expires.toLocaleString())
285 var download = $('<a href="#">').appendTo(el);
286 decorate_button(download, MSG_DOWNLOAD + " " + item.format);
287 svc_ajax('get', {action: "download-url", id: id, format: item.format}, function(data) {
288 download.attr("href", data.action);
294 var lockable_formats = [];
295 for (var f in item.formats) {
296 if (f == item.format) continue;
298 if (item.formats[f]) {
299 var access = $('<a target="_blank">').appendTo(el);
300 decorate_button(access, MSG_OVERDRIVE_ACCESS_ONLINE + " " + f);
301 svc_ajax('get', {action: "download-url", id: id, format: f}, function(data) {
302 access.attr("href", data.action);
307 lockable_formats.push(f);
310 if (lockable_formats.length > 0 && checkout_popup) {
311 $(el).append( ajax_button( MSG_OVERDRIVE_DOWNLOAD_AS, function() {
312 checkout_format(el, id, lockable_formats, copies_available);
317 if (item.format) return item;
319 $(el).append( ajax_button( MSG_CHECK_IN, function() {
320 if( confirm( MSG_CHECK_IN_CONFIRM ) ) {
321 item_action({action: "return", id: id}, el, copies_available + 1);
328 item = item_is_on_hold(id);
330 $('<span class="overdrive-status">')
336 if(copies_available && checkout_popup) {
337 $(el).append( ajax_button( MSG_CHECK_OUT , function() {
338 if( confirm( MSG_CHECK_OUT_CONFIRM ) ) {
339 svc_ajax('post', {action: "checkout", id: id}, function(data) {
340 if (data.checkouts) {
341 details.checkouts = data.checkouts;
344 details.holds = data.holds;
346 item = display_actions(el, id, copies_available - 1);
347 if (item && item.formats && !item.format) {
348 var has_available_formats = false;
349 var lockable_formats = [];
350 for (var f in item.formats) {
351 if (item.formats[f]) {
352 has_available_formats = true;
355 lockable_formats.push(f);
358 if (!has_available_formats) {
359 checkout_format(el, id, lockable_formats, copies_available - 1);
367 $(el).append( ajax_button( MSG_PLACE_HOLD, function() {
368 item_action({action: "place-hold", id: id}, el, copies_available);
373 $(el).append( ajax_button( MSG_CANCEL_HOLD, function() {
374 if( confirm( MSG_CANCEL_HOLD_CONFIRM ) ) {
375 item_action({action: "remove-hold", id: id}, el, copies_available);
383 function ajax_button(label, on_click) {
384 var button = $('<a href="#">')
389 decorate_button(button, label);
393 function decorate_button(button, label) {
395 .addClass("btn btn-primary btn-mini")
396 .css("color","white")
400 function checkout_format(el, id, formats, copies_available) {
401 if (formats.length == 0) {
402 alert( MSG_OVERDRIVE_CANNOT_CHECKOUT );
406 var checkout_format_list = checkout_popup.find("ul.overdrive-format-list").empty();
407 formats.forEach(function (item) {
408 var li = $('<li>').appendTo(checkout_format_list);
409 $('<input name="checkout-format" type="radio">')
414 checkout_popup.modal("show");
415 checkout_popup.find(".overdrive-checkout-submit").click(function(e) {
417 var format = checkout_format_list.find("input[type='radio'][name='checkout-format']:checked").val();
418 item_action({action: "checkout-format", id: id, format: format}, el, copies_available);
420 checkout_popup.modal("hide");
424 this.with_account_details = function( el, callback ) {
425 $(el).append(error_div);
426 load_account_details(function(data) {
427 if (!data.is_logged_in) {
428 $(el).append(login_div);
434 this.display_account_details = function( el ) {
435 window.refresh_overdrive_account_details = function () {
436 KOHA.OverDriveCirculation.display_account_details( el );
438 $(el).empty().append(error_div);
439 load_account_details(function(data) {
440 display_account(el, data);
444 this.display_error = function( el, error ) {
445 $(el).empty().append(error_div);
446 display_error(error);
449 this.is_logged_in = is_logged_in;
451 this.add_actions = function(el, id, copies_available) {
452 var actions = $('<span class="actions">');
453 display_actions(actions, id, copies_available);
454 $('<div id="action_'+id+'" class="actions-menu">')