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>' + MSG_CHECKOUTS + '</h3>');
31 var items = data.checkouts.items;
33 if (items.length == 0) {
34 checkouts_list = MSG_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>' + MSG_HOLDS + '</h3>');
47 var items = data.holds.items;
49 if (items.length == 0) {
50 holds_list = MSG_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">')
92 $('<span id="waiting_'+item.isbn+'" style="display:none;"><img class="throbber" src="/opac-tmpl/lib/jquery/plugins/themes/classic/throbber.gif" /></span>').appendTo(line);
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 $("#action_"+isbn).hide();
160 $("#waiting_"+isbn).show();
161 svc_ajax('post', params, function(data) {
162 if (data.checkouts) {
163 details.checkouts = data.checkouts;
166 details.holds = data.holds;
168 display_actions(el, isbn);
169 $("#action_"+isbn).show();
170 $("#waiting_"+isbn).hide();
174 function item_is_checked_out (isbn) {
175 if ( !(details && details.checkouts) ) {
178 var isbn_uc = isbn.toUpperCase();
179 var items = details.checkouts.items;
180 for (var i = 0; i < items.length; i++) {
181 if ( items[i].isbn.toUpperCase() == isbn_uc ) {
188 function item_is_on_hold (isbn) {
189 if ( !(details && details.holds) ) {
192 var isbn_uc = isbn.toUpperCase();
193 var items = details.holds.items;
194 for (var i = 0; i < items.length; i++) {
195 if ( items[i].isbn.toUpperCase() == isbn_uc ) {
202 function display_actions(el, isbn) {
204 if (is_identified()) {
206 var item = item_is_checked_out(isbn);
208 var expires = new Date(item.expires);
209 $('<span class="recordedbooks-item-status">')
210 .text(MSG_CHECKED_OUT_UNTIL.format(expires.toLocaleString()))
215 var download = $('<a href="'+item.url+'">').appendTo(el);
216 decorate_button(download, MSG_DOWNLOAD);
220 $(el).append( ajax_button(MSG_CHECK_IN, function() {
221 if( confirm(MSG_CHECK_IN_CONFIRM) ) {
222 item_action({action: "return", isbn: isbn}, el);
229 item = item_is_on_hold(isbn);
231 $('<span class="recordedbooks-status">')
238 $(el).append( ajax_button(MSG_CHECK_OUT, function() {
239 if( confirm(MSG_CHECK_OUT_CONFIRM) ) {
240 $("#action_"+isbn).hide();
241 $("#waiting_"+isbn).show();
242 svc_ajax('post', {action: "checkout", isbn: isbn}, function(data) {
243 if (data.checkouts) {
244 details.checkouts = data.checkouts;
247 details.holds = data.holds;
249 item = display_actions(el, isbn);
250 $("#action_"+isbn).show();
251 $("#waiting_"+isbn).hide();
257 $(el).append( ajax_button(MSG_PLACE_HOLD, function() {
258 item_action({action: "place_hold", isbn: isbn}, el);
263 $(el).append( ajax_button(MSG_CANCEL_HOLD, function() {
264 if( confirm(MSG_CANCEL_HOLD_CONFIRM) ) {
265 item_action({action: "remove_hold", isbn: isbn}, el);
273 function ajax_button(label, on_click) {
274 var button = $('<a href="#">')
279 decorate_button(button, label);
283 function decorate_button(button, label) {
285 .addClass("btn btn-primary btn-mini")
286 .css("color","white")
290 this.with_account_details = function( el, callback ) {
291 $(el).append(error_div);
292 load_account_details( callback );
295 this.display_account_details = function( el ) {
296 $(el).empty().append(error_div);
297 load_account_details(function(data) {
298 display_account(el, data);
302 this.display_error = function( el, error ) {
303 $(el).empty().append(error_div);
304 display_error(error);
307 this.is_identified = is_identified;
309 this.add_actions = function(el, isbn) {
310 var actions = $('<span class="actions">');
311 display_actions(actions, isbn);
312 $('<div id="action_'+isbn+'" class="actions-menu">')
315 $("#action_"+isbn).before('<span id="waiting_'+isbn+'" style="display:none;"><img class="throbber" src="/opac-tmpl/lib/jquery/plugins/themes/classic/throbber.gif" /></span>');
318 this.search = function( q, page_size, page, callback ) {
319 svc_ajax('get', { action: "search", q: q, page_size: page_size, page: page }, function (data) {
322 results = data.results;
323 if (!results.total) {
324 var total = results.items.length;
325 if ( total == results.page_size ) total = total + "+";
326 results.total = total;
330 results.error = data.error;