Lines 59-61
KOHA.OverDrive = ( function() {
Link Here
|
59 |
} |
59 |
} |
60 |
}; |
60 |
}; |
61 |
} )(); |
61 |
} )(); |
|
|
62 |
|
63 |
KOHA.OverDriveCirculation = new function() { |
64 |
var svc_url = '/cgi-bin/koha/svc/overdrive'; |
65 |
|
66 |
var error_div = $('<div class="overdrive-error">'); |
67 |
function display_error ( error ) { |
68 |
error_div.text(error); |
69 |
} |
70 |
|
71 |
var login_link = $('<a href="#">') |
72 |
.click(function(e) { |
73 |
e.preventDefault(); |
74 |
login(); |
75 |
}) |
76 |
.text(_("Login to OverDrive account")); |
77 |
var login_div = $('<div class="overdrive-login">').append(login_link); |
78 |
|
79 |
var details = null; |
80 |
|
81 |
function is_logged_in() { |
82 |
return details ? details.is_logged_in : false; |
83 |
} |
84 |
|
85 |
var checkout_popup = null; |
86 |
$( document ).ready(function() { |
87 |
checkout_popup = $("#overdrive-checkout"); |
88 |
}); |
89 |
|
90 |
function display_account (container, data) { |
91 |
if (!data.is_logged_in) { |
92 |
$(container).append(login_div); |
93 |
return; |
94 |
} |
95 |
|
96 |
var logout_link = $('<a href="#logout" class="overdrive-logout">') |
97 |
.click(function(e) { |
98 |
e.preventDefault(); |
99 |
$(container).empty().append(error_div); |
100 |
logout(function(data) { |
101 |
display_account(container, data); |
102 |
}); |
103 |
}).text(_("Logout from OverDrive account")); |
104 |
$(container).append(logout_link); |
105 |
|
106 |
if (data.checkouts) { |
107 |
var checkouts_div = $('<div class="overdrive-div">').html('<h3>' + _("Checkouts") + '</h3>'); |
108 |
var checkouts_list = $('<ul class="overdrive-list">'); |
109 |
data.checkouts.items.forEach(function(item) { |
110 |
item_line(checkouts_list, item); |
111 |
}); |
112 |
checkouts_div.append(checkouts_list); |
113 |
$(container).append(checkouts_div); |
114 |
} |
115 |
|
116 |
if (data.holds) { |
117 |
var holds_div = $('<div class="overdrive-div">').html('<h3>' + _("Holds") + '</h3>'); |
118 |
var holds_list = $('<ul class="overdrive-list">'); |
119 |
data.holds.items.forEach(function(item) { |
120 |
item_line(holds_list, item); |
121 |
}); |
122 |
holds_div.append(holds_list); |
123 |
$(container).append(holds_div); |
124 |
} |
125 |
} |
126 |
|
127 |
function item_line(ul_el, item) { |
128 |
var line = $('<li class="overdrive-item">'); |
129 |
if (item.images) { |
130 |
var thumb_url = item.images.thumbnail; |
131 |
if (thumb_url) { |
132 |
$('<img class="overdrive-item-thumbnail">') |
133 |
.attr("src", thumb_url) |
134 |
.appendTo(line); |
135 |
} |
136 |
} |
137 |
$('<div class="overdrive-item-title">') |
138 |
.text(item.title) |
139 |
.appendTo(line); |
140 |
$('<div class="overdrive-item-subtitle">') |
141 |
.text(item.subtitle) |
142 |
.appendTo(line); |
143 |
$('<div class="overdrive-item-author">') |
144 |
.text(item.author) |
145 |
.appendTo(line); |
146 |
var actions = $('<span class="actions">'); |
147 |
display_actions(actions, item.id); |
148 |
$('<div id="action_'+item.id+'" class="actions-menu">') |
149 |
.append(actions) |
150 |
.appendTo(line); |
151 |
|
152 |
$(ul_el).append(line); |
153 |
} |
154 |
|
155 |
function svc_ajax ( method, params, success_callback ) { |
156 |
return $.ajax({ |
157 |
method: method, |
158 |
dataType: "json", |
159 |
url: svc_url, |
160 |
data: params, |
161 |
success: function (data) { |
162 |
if (data.error) { |
163 |
display_error(data.error); |
164 |
} |
165 |
success_callback(data); |
166 |
}, |
167 |
error: function(jqXHR, textStatus, errorThrown) { |
168 |
display_error(errorThrown); |
169 |
} |
170 |
}); |
171 |
} |
172 |
|
173 |
function load_account_details ( callback ) { |
174 |
svc_ajax('get', { action: "account" }, function(data) { |
175 |
details = data; |
176 |
callback(data); |
177 |
}); |
178 |
} |
179 |
|
180 |
function login() { |
181 |
svc_ajax('get', { action: "login" }, function(data) { |
182 |
details = null; |
183 |
if (data.login_url) { |
184 |
window.location = data.login_url; |
185 |
} |
186 |
}); |
187 |
} |
188 |
|
189 |
function logout (callback) { |
190 |
svc_ajax('post', { action: "logout" }, function(data) { |
191 |
details = null; |
192 |
callback(data); |
193 |
}); |
194 |
} |
195 |
|
196 |
function item_action (params, el, copies_available) { |
197 |
var id = params.id; |
198 |
svc_ajax('post', params, function(data) { |
199 |
if (data.checkouts) { |
200 |
details.checkouts = data.checkouts; |
201 |
} |
202 |
if (data.holds) { |
203 |
details.holds = data.holds; |
204 |
} |
205 |
display_actions(el, id, copies_available); |
206 |
}); |
207 |
} |
208 |
|
209 |
function item_is_checked_out (id) { |
210 |
if ( !(details && details.checkouts) ) { |
211 |
return null; |
212 |
} |
213 |
var id_uc = id.toUpperCase(); |
214 |
var items = details.checkouts.items; |
215 |
for (var i = 0; i < items.length; i++) { |
216 |
if ( items[i].id.toUpperCase() == id_uc ) { |
217 |
return items[i]; |
218 |
} |
219 |
} |
220 |
return null; |
221 |
} |
222 |
|
223 |
function item_is_on_hold (id) { |
224 |
if ( !(details && details.holds) ) { |
225 |
return false; |
226 |
} |
227 |
var id_uc = id.toUpperCase(); |
228 |
var items = details.holds.items; |
229 |
for (var i = 0; i < items.length; i++) { |
230 |
if ( items[i].id.toUpperCase() == id_uc ) { |
231 |
return items[i]; |
232 |
} |
233 |
} |
234 |
return null; |
235 |
} |
236 |
|
237 |
function display_actions(el, id, copies_available) { |
238 |
$(el).empty(); |
239 |
if (is_logged_in()) { |
240 |
|
241 |
var item = item_is_checked_out(id); |
242 |
if (item) { |
243 |
var expires = new Date(item.expires); |
244 |
$('<span class="overdrive-item-status">') |
245 |
.text(_("Checked out until") + " " + expires.toLocaleString()) |
246 |
.appendTo(el); |
247 |
$(el).append(" "); |
248 |
|
249 |
if (item.format) { |
250 |
var download = $('<a href="#">').appendTo(el); |
251 |
decorate_button(download, _("Download") + " " + item.format); |
252 |
svc_ajax('get', {action: "download-url", id: id, format: item.format}, function(data) { |
253 |
download.attr("href", data.action); |
254 |
}); |
255 |
$(el).append(" "); |
256 |
} |
257 |
|
258 |
if (item.formats) { |
259 |
var lockable_formats = []; |
260 |
for (var f in item.formats) { |
261 |
if (f == item.format) continue; |
262 |
|
263 |
if (item.formats[f]) { |
264 |
var access = $('<a target="_blank">').appendTo(el); |
265 |
decorate_button(access, _("Access online") + " " + f); |
266 |
svc_ajax('get', {action: "download-url", id: id, format: f}, function(data) { |
267 |
access.attr("href", data.action); |
268 |
}); |
269 |
$(el).append(" "); |
270 |
} |
271 |
else { |
272 |
lockable_formats.push(f); |
273 |
} |
274 |
} |
275 |
if (lockable_formats.length > 0 && checkout_popup) { |
276 |
$(el).append( ajax_button(_("Download as"), function() { |
277 |
checkout_format(el, id, lockable_formats, copies_available); |
278 |
}) ).append(" "); |
279 |
} |
280 |
} |
281 |
|
282 |
if (item.format) return item; |
283 |
|
284 |
$(el).append( ajax_button(_("Check in"), function() { |
285 |
if( confirm(_("Are you sure you want to return this item?")) ) { |
286 |
item_action({action: "return", id: id}, el, copies_available + 1); |
287 |
} |
288 |
}) ); |
289 |
|
290 |
return item; |
291 |
} |
292 |
|
293 |
item = item_is_on_hold(id); |
294 |
if (item) { |
295 |
$('<span class="overdrive-status">') |
296 |
.text(_("On hold")) |
297 |
.appendTo(el); |
298 |
$(el).append(" "); |
299 |
} |
300 |
|
301 |
if(copies_available && checkout_popup) { |
302 |
$(el).append( ajax_button(_("Check out"), function() { |
303 |
if( confirm(_("Are you sure you want to checkout this item?")) ) { |
304 |
svc_ajax('post', {action: "checkout", id: id}, function(data) { |
305 |
if (data.checkouts) { |
306 |
details.checkouts = data.checkouts; |
307 |
} |
308 |
if (data.holds) { |
309 |
details.holds = data.holds; |
310 |
} |
311 |
item = display_actions(el, id, copies_available - 1); |
312 |
if (item && item.formats && !item.format) { |
313 |
var has_available_formats = false; |
314 |
var lockable_formats = []; |
315 |
for (var f in item.formats) { |
316 |
if (item.formats[f]) { |
317 |
has_available_formats = true; |
318 |
break; |
319 |
} |
320 |
lockable_formats.push(f); |
321 |
} |
322 |
|
323 |
if (!has_available_formats) { |
324 |
checkout_format(el, id, lockable_formats, copies_available - 1); |
325 |
} |
326 |
} |
327 |
}); |
328 |
} |
329 |
}) ); |
330 |
} |
331 |
else if (!item) { |
332 |
$(el).append( ajax_button(_("Place hold"), function() { |
333 |
item_action({action: "place_hold", id: id}, el, copies_available); |
334 |
}) ); |
335 |
} |
336 |
|
337 |
if (item) { |
338 |
$(el).append( ajax_button(_("Cancel"), function() { |
339 |
if( confirm(_("Are you sure you want to cancel this hold?")) ) { |
340 |
item_action({action: "return", id: id}, el, copies_available); |
341 |
} |
342 |
}) ); |
343 |
} |
344 |
return item; |
345 |
} |
346 |
} |
347 |
|
348 |
function ajax_button(label, on_click) { |
349 |
var button = $('<a href="#">') |
350 |
.click(function(e) { |
351 |
e.preventDefault(); |
352 |
on_click(); |
353 |
}); |
354 |
decorate_button(button, label); |
355 |
return button; |
356 |
} |
357 |
|
358 |
function decorate_button(button, label) { |
359 |
$(button) |
360 |
.addClass("btn btn-primary btn-mini") |
361 |
.css("color","white") |
362 |
.text(label); |
363 |
} |
364 |
|
365 |
function checkout_format(el, id, formats, copies_available) { |
366 |
if (formats.length == 0) { |
367 |
alert(_("Item cannot be checked out - no available formats")); |
368 |
return false; |
369 |
} |
370 |
|
371 |
var checkout_format_list = checkout_popup.find("ul.overdrive-format-list").empty(); |
372 |
formats.forEach(function (item) { |
373 |
var li = $('<li>').appendTo(checkout_format_list); |
374 |
$('<input name="checkout-format" type="radio">') |
375 |
.val(item) |
376 |
.appendTo(li); |
377 |
li.append(item); |
378 |
}); |
379 |
checkout_popup.modal("show"); |
380 |
checkout_popup.find(".overdrive-checkout-submit").click(function(e) { |
381 |
e.preventDefault(); |
382 |
var format = checkout_format_list.find("input[type='radio'][name='checkout-format']:checked").val(); |
383 |
item_action({action: "checkout-format", id: id, format: format}, el, copies_available); |
384 |
$(this).unbind( e ); |
385 |
checkout_popup.modal("hide"); |
386 |
}); |
387 |
} |
388 |
|
389 |
this.with_account_details = function( el, callback ) { |
390 |
$(el).append(error_div); |
391 |
load_account_details(function(data) { |
392 |
if (!data.is_logged_in) { |
393 |
$(el).append(login_div); |
394 |
} |
395 |
callback(data); |
396 |
}); |
397 |
} |
398 |
|
399 |
this.display_account_details = function( el ) { |
400 |
$(el).empty().append(error_div); |
401 |
load_account_details(function(data) { |
402 |
display_account(el, data); |
403 |
}); |
404 |
}; |
405 |
|
406 |
this.display_error = function( el, error ) { |
407 |
$(el).empty().append(error_div); |
408 |
display_error(error); |
409 |
}; |
410 |
|
411 |
this.is_logged_in = is_logged_in; |
412 |
|
413 |
this.add_actions = function(el, id, copies_available) { |
414 |
var actions = $('<span class="actions">'); |
415 |
display_actions(actions, id, copies_available); |
416 |
$('<div id="action_'+id+'" class="actions-menu">') |
417 |
.append(actions) |
418 |
.appendTo(el); |
419 |
}; |
420 |
} |