/* 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 */ if ( typeof KOHA == "undefined" || !KOHA ) { var KOHA = {}; } KOHA.OverDrive = ( function() { var proxy_base_url = '/cgi-bin/koha/svc/overdrive_proxy'; var library_base_url = 'http://api.overdrive.com/v1/libraries/'; return { Get: function( url, params, callback ) { $.ajax( { type: 'GET', url: url.replace( /https?:\/\/api.overdrive.com\/v1/, proxy_base_url ), dataType: 'json', data: params, error: function( xhr, error ) { try { callback( JSON.parse( xhr.responseText )); } catch ( e ) { callback( {error: xhr.responseText || true} ); } }, success: callback } ); }, GetCollectionURL: function( library_id, callback ) { if ( KOHA.OverDrive.collection_url ) { callback( KOHA.OverDrive.collection_url ); return; } KOHA.OverDrive.Get( library_base_url + library_id, {}, function ( data ) { if ( data.error ) { callback( data ); return; } KOHA.OverDrive.collection_url = data.links.products.href; callback( data.links.products.href ); } ); }, Search: function( library_id, q, limit, offset, callback ) { KOHA.OverDrive.GetCollectionURL( library_id, function( data ) { if ( data.error ) { callback( data ); return; } KOHA.OverDrive.Get( data, {q: q, limit: limit, offset: offset}, callback ); } ); } }; } )(); KOHA.OverDriveCirculation = new function() { var svc_url = '/cgi-bin/koha/svc/overdrive'; var error_div = $('
'); function display_error ( error ) { error_div.text(error); } var login_link = $('') .click(function(e) { e.preventDefault(); if( OD_password_required ) { $("#overdrive-login").modal('show'); } else { login(""); } }) .text( MSG_OVERDRIVE_LOGIN ); var login_div = $('
').append(login_link); var details = null; function is_logged_in() { return details ? details.is_logged_in : false; } var checkout_popup = null; $( document ).ready(function() { checkout_popup = $("#overdrive-checkout"); $("#overdrive-login-form").submit(function(e){ e.preventDefault(); $("#overdrive-login").modal('hide'); var ODpassword = $("input[name='ODpassword']").val(); login( ODpassword ); }); $("#overdrive-login").on("shown.bs.modal", function(){ $("#ODpassword").focus(); }); var p = window.opener; var cb; if (p) { try { cb = p.refresh_overdrive_account_details;} catch(err){ return; } //Catch error if opener is not accessible if (cb) { cb(); } else { p.location.reload(); } window.close(); } }); function display_account (container, data) { if (!data.is_logged_in) { $(container).append(login_div); return; } var overdrive_link = $('') .text( MSG_OVERDRIVE_LINK ); $(container).append(overdrive_link); var logout_link = $('') .click(function(e) { e.preventDefault(); $(container).empty().append(error_div); logout(function(data) { display_account(container, data); }); }).text( MSG_OVERDRIVE_LOGOUT ); $(container).append(logout_link); $(container).append('
'); if (data.checkouts) { var checkouts_div = $('
').html('

' + MSG_CHECKOUTS + '

'); var checkouts_list = $('