1 if ( typeof KOHA == "undefined" || !KOHA ) {
5 KOHA.RecordedBooks = new function() {
6 var svc_url = '/cgi-bin/koha/svc/recordedbooks';
8 var error_div = $('<div class="recordedbooks-error">');
9 function display_error ( error ) {
10 error_div.text(error);
15 function is_identified() {
16 return details ? details.is_identified : false;
19 var checkout_popup = null;
20 $( document ).ready(function() {
21 checkout_popup = $("#recordedbooks-checkout");
24 function display_account (container, data) {
25 if (!data.is_identified) {
30 var checkouts_div = $('<div class="recordedbooks-div">').html('<h3>' + _("Checkouts") + '</h3>');
31 var items = data.checkouts.items;
33 if (items.length == 0) {
34 checkouts_list = _("No checkouts");
36 checkouts_list = $('<ul class="recordedbooks-list">');
37 data.checkouts.items.forEach(function(item) {
38 item_line(checkouts_list, item);
41 checkouts_div.append(checkouts_list);
42 $(container).append(checkouts_div);
46 var holds_div = $('<div class="recordedbooks-div">').html('<h3>' + _("Holds") + '</h3>');
47 var items = data.holds.items;
49 if (items.length == 0) {
50 holds_list = _("No holds");
52 holds_list = $('<ul class="recordedbooks-list">');
53 data.holds.items.forEach(function(item) {
54 item_line(holds_list, item);
57 holds_div.append(holds_list);
58 $(container).append(holds_div);
62 function item_line(ul_el, item) {
63 var line = $('<li class="recordedbooks-item">');
65 var thumb_url = item.images.small;
67 $('<img class="recordedbooks-item-thumbnail">')
68 .attr("src", thumb_url)
72 $('<div class="recordedbooks-item-title">')
75 $('<div class="recordedbooks-item-subtitle">')
78 $('<div class="recordedbooks-item-author">')
81 if (item.files && item.files.length > 0) {
82 downloads = $('<div class="recordedbooks-item-author">')
85 render_downloads(downloads, item.files);
87 var actions = $('<span class="actions">');
88 display_actions(actions, item.isbn);
89 $('<div id="action_'+item.isbn+'" class="actions-menu">')
93 $(ul_el).append(line);
96 function render_downloads(el, files) {
97 if (files.length == 0) return;
98 var file_spec = files.shift();
99 if (/^https?:\/\/api\./.test(file_spec.url)) {
103 success: function (data) {
104 append_download_link(el, data.url, data.id);
105 render_downloads(el, files);
107 error: function(jqXHR, textStatus, errorThrown) {
108 display_error(errorThrown);
112 append_download_link(el, file_spec.url, file_spec.filename);
113 render_downloads(el, files);
116 function append_download_link(el, url, text) {
118 $( '<a href="' + url + '" target="recordedbooks">' )
124 function svc_ajax ( method, params, success_callback, callback_for_error_too ) {
125 // remove when jquery is upgraded
126 for (var key in params) {
127 if (params[key] === null) delete params[key];
134 success: function (data) {
135 if (data.error && !callback_for_error_too) {
136 display_error(data.error);
138 success_callback(data);
140 error: function(jqXHR, textStatus, errorThrown) {
141 if (callback_for_error_too) {
142 success_callback({error: errorThrown});
145 display_error(errorThrown);
150 function load_account_details ( callback ) {
151 svc_ajax('get', { action: "account" }, function(data) {
157 function item_action (params, el) {
158 var isbn = params.isbn;
159 svc_ajax('post', params, function(data) {
160 if (data.checkouts) {
161 details.checkouts = data.checkouts;
164 details.holds = data.holds;
166 display_actions(el, isbn);
170 function item_is_checked_out (isbn) {
171 if ( !(details && details.checkouts) ) {
174 var isbn_uc = isbn.toUpperCase();
175 var items = details.checkouts.items;
176 for (var i = 0; i < items.length; i++) {
177 if ( items[i].isbn.toUpperCase() == isbn_uc ) {
184 function item_is_on_hold (isbn) {
185 if ( !(details && details.holds) ) {
188 var isbn_uc = isbn.toUpperCase();
189 var items = details.holds.items;
190 for (var i = 0; i < items.length; i++) {
191 if ( items[i].isbn.toUpperCase() == isbn_uc ) {
198 function display_actions(el, isbn) {
200 if (is_identified()) {
202 var item = item_is_checked_out(isbn);
204 var expires = new Date(item.expires);
205 $('<span class="recordedbooks-item-status">')
206 .text(_("Checked out until") + " " + expires.toLocaleString())
211 var download = $('<a href="'+item.url+'">').appendTo(el);
212 decorate_button(download, _("Download"));
216 $(el).append( ajax_button(_("Check in"), function() {
217 if( confirm(_("Are you sure you want to return this item?")) ) {
218 item_action({action: "return", isbn: isbn}, el);
225 item = item_is_on_hold(isbn);
227 $('<span class="recordedbooks-status">')
234 $(el).append( ajax_button(_("Check out"), function() {
235 if( confirm(_("Are you sure you want to checkout this item?")) ) {
236 svc_ajax('post', {action: "checkout", isbn: isbn}, function(data) {
237 if (data.checkouts) {
238 details.checkouts = data.checkouts;
241 details.holds = data.holds;
243 item = display_actions(el, isbn);
249 $(el).append( ajax_button(_("Place hold"), function() {
250 item_action({action: "place_hold", isbn: isbn}, el);
255 $(el).append( ajax_button(_("Cancel"), function() {
256 if( confirm(_("Are you sure you want to cancel this hold?")) ) {
257 item_action({action: "remove_hold", isbn: isbn}, el);
265 function ajax_button(label, on_click) {
266 var button = $('<a href="#">')
271 decorate_button(button, label);
275 function decorate_button(button, label) {
277 .addClass("btn btn-primary btn-mini")
278 .css("color","white")
282 this.with_account_details = function( el, callback ) {
283 $(el).append(error_div);
284 load_account_details( callback );
287 this.display_account_details = function( el ) {
288 $(el).empty().append(error_div);
289 load_account_details(function(data) {
290 display_account(el, data);
294 this.display_error = function( el, error ) {
295 $(el).empty().append(error_div);
296 display_error(error);
299 this.is_identified = is_identified;
301 this.add_actions = function(el, isbn) {
302 var actions = $('<span class="actions">');
303 display_actions(actions, isbn);
304 $('<div id="action_'+isbn+'" class="actions-menu">')
309 this.search = function( q, page_size, page, callback ) {
310 svc_ajax('get', { action: "search", q: q, page_size: page_size, page: page }, function (data) {
313 results = data.results;
314 if (!results.total) {
315 var total = results.items.length;
316 if ( total == results.page_size ) total = total + "+";
317 results.total = total;
321 results.error = data.error;